Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
new UsesJavaVersion<>(11),
new UsesMethod<>(optionalIsPresentMatcher));
return Preconditions.check(check, new JavaVisitor<ExecutionContext>() {
@Override
public J visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) {
// A type whose method invocations are misattributed to `java.util.Optional` (e.g. when a
// third-party `Optional` is off the recipe classpath) can fool the `MethodMatcher`, producing
// an uncompilable `isPresent()` call. Skip the file when the simple name `Optional` is bound to
// some other explicitly imported type.
for (J.Import anImport : cu.getImports()) {
if (!anImport.isStatic() && "Optional".equals(anImport.getClassName()) &&
!"java.util.Optional".equals(anImport.getTypeName())) {
return cu;
}
}
return super.visitCompilationUnit(cu, ctx);
}

@Override
public J visitStatement(Statement s, ExecutionContext ctx) {
if (s instanceof J.Unary) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
new UsesJavaVersion<>(11),
new UsesMethod<>(optionalIsPresentMatcher));
return Preconditions.check(check, new JavaVisitor<ExecutionContext>() {
@Override
public J visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) {
// A type whose method invocations are misattributed to `java.util.Optional` (e.g. when a
// third-party `Optional` is off the recipe classpath) can fool the `MethodMatcher`, producing
// an uncompilable `isEmpty()` call. Skip the file when the simple name `Optional` is bound to
// some other explicitly imported type.
for (J.Import anImport : cu.getImports()) {
if (!anImport.isStatic() && "Optional".equals(anImport.getClassName()) &&
!"java.util.Optional".equals(anImport.getTypeName())) {
return cu;
}
}
return super.visitCompilationUnit(cu, ctx);
}

@Override
public J visitStatement(Statement s, ExecutionContext ctx) {
if (s instanceof J.Unary) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.test.TypeValidation;

import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.java.Assertions.version;
Expand Down Expand Up @@ -65,6 +66,32 @@ boolean notEmpty(Optional<String> bar){
);
}

@Test
void doNotChangeNonJdkOptionalMisattributedToJdk() {
// When a third-party `Optional` is off the recipe classpath, `bar.isEmpty()` can be misattributed
// to `java.util.Optional` (here via the `java.util.*` import), which would otherwise fool the
// `MethodMatcher` into emitting an uncompilable `isPresent()` call. See issue #1146.
//language=java
rewriteRun(
spec -> spec.typeValidationOptions(TypeValidation.none()),
version(
java(
"""
package com.example.app;
import java.util.*;
import com.bazaarvoice.jolt.common.Optional;
class App {
boolean notEmpty(Optional<String> bar){
return !bar.isEmpty();
}
}
"""
),
11
)
);
}

@Test
void ifStatement() {
//language=java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.test.TypeValidation;

import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.java.Assertions.version;
Expand All @@ -34,6 +35,64 @@ public void defaults(RecipeSpec spec) {
.parser(JavaParser.fromJavaVersion());
}

@Test
void doNotChangeNonJdkOptionalMisattributedToJdk() {
// When a third-party `Optional` is off the recipe classpath, `bar.isPresent()` can be misattributed
// to `java.util.Optional` (here via the `java.util.*` import), which would otherwise fool the
// `MethodMatcher` into emitting an uncompilable `isEmpty()` call. See issue #1146.
//language=java
rewriteRun(
spec -> spec.typeValidationOptions(TypeValidation.none()),
version(
java(
"""
package com.example.app;
import java.util.*;
import com.bazaarvoice.jolt.common.Optional;
class App {
boolean notPresent(Optional<String> bar){
return !bar.isPresent();
}
}
"""
),
11
)
);
}

@Test
void doNotChangeNonJdkOptional() {
//language=java
rewriteRun(
version(
java(
"""
package com.bazaarvoice.jolt.common;
public class Optional<T> {
public boolean isPresent() { return true; }
}
"""
),
11
),
version(
java(
"""
package com.example.app;
import com.bazaarvoice.jolt.common.Optional;
class App {
boolean notPresent(Optional<String> bar){
return !bar.isPresent();
}
}
"""
),
11
)
);
}

@DocumentExample
@Test
void replaceNotIsPresentWithIsEmpty() {
Expand Down
Loading