Skip to content
Merged
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 @@ -77,7 +77,11 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
J result = super.postVisit(tree, ctx);
InstanceOfPatternReplacements original = getCursor().getMessage("flowTypeScope");
if (original != null && !original.isEmpty()) {
return UseInstanceOfPatternMatching.refactor(result, original, getCursor().getParentOrThrow());
Cursor methodCursor = getCursor().dropParentUntil(
v -> v instanceof J.MethodDeclaration || v instanceof J.ClassDeclaration || v == Cursor.ROOT_VALUE);
Set<String> introducedNames = methodCursor.computeMessageIfAbsent(
"introducedPatternVarNames", k -> new HashSet<>());
return UseInstanceOfPatternMatching.refactor(result, original, getCursor().getParentOrThrow(), introducedNames);
}
return result;
}
Expand Down Expand Up @@ -476,9 +480,10 @@ private boolean isUnboundedWildcardParameterized(TypeTree typeTree) {
private static class UseInstanceOfPatternMatching extends JavaVisitor<Integer> {

private final InstanceOfPatternReplacements replacements;
private final Set<String> introducedNames;

static @Nullable J refactor(@Nullable J tree, InstanceOfPatternReplacements replacements, Cursor cursor) {
return new UseInstanceOfPatternMatching(replacements).visit(tree, 0, cursor);
static @Nullable J refactor(@Nullable J tree, InstanceOfPatternReplacements replacements, Cursor cursor, Set<String> introducedNames) {
return new UseInstanceOfPatternMatching(replacements, introducedNames).visit(tree, 0, cursor);
}

@Override
Expand All @@ -489,7 +494,7 @@ public J visitBinary(J.Binary binary, Integer p) {
Cursor widenedCursor = updateCursor(b);

// Collect names from the left side if it's an instanceof with a pattern
Set<String> usedNames = new HashSet<>();
Set<String> usedNames = new HashSet<>(introducedNames);
if (b.getLeft() instanceof J.InstanceOf) {
J.InstanceOf leftInstanceOf = (J.InstanceOf) b.getLeft();
if (leftInstanceOf.getPattern() != null) {
Expand All @@ -499,11 +504,15 @@ public J visitBinary(J.Binary binary, Integer p) {

Expression newRight;
if (binary.getRight() instanceof J.InstanceOf) {
newRight = replacements.processInstanceOf((J.InstanceOf) binary.getRight(), widenedCursor, usedNames);
J.InstanceOf rightResult = replacements.processInstanceOf((J.InstanceOf) binary.getRight(), widenedCursor, usedNames);
trackIntroducedName((J.InstanceOf) binary.getRight(), rightResult);
newRight = rightResult;
} else if (binary.getRight() instanceof J.Parentheses &&
((J.Parentheses<?>) binary.getRight()).getTree() instanceof J.InstanceOf) {
@SuppressWarnings("unchecked") J.Parentheses<J.InstanceOf> originalRight = (J.Parentheses<J.InstanceOf>) binary.getRight();
newRight = originalRight.withTree(replacements.processInstanceOf(originalRight.getTree(), widenedCursor, usedNames));
J.InstanceOf rightResult = replacements.processInstanceOf(originalRight.getTree(), widenedCursor, usedNames);
trackIntroducedName(originalRight.getTree(), rightResult);
newRight = originalRight.withTree(rightResult);
} else {
newRight = (Expression) visitNonNull(binary.getRight(), p, widenedCursor);
}
Expand All @@ -516,7 +525,15 @@ public J visitBinary(J.Binary binary, Integer p) {
@Override
public J.InstanceOf visitInstanceOf(J.InstanceOf instanceOf, Integer p) {
instanceOf = (J.InstanceOf) super.visitInstanceOf(instanceOf, p);
return replacements.processInstanceOf(instanceOf, getCursor(), new HashSet<>());
J.InstanceOf result = replacements.processInstanceOf(instanceOf, getCursor(), new HashSet<>(introducedNames));
trackIntroducedName(instanceOf, result);
return result;
}

private void trackIntroducedName(J.InstanceOf original, J.InstanceOf result) {
if (result.getPattern() != null && original.getPattern() == null) {
introducedNames.add(((J.Identifier) result.getPattern()).getSimpleName());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1989,4 +1989,48 @@ void foo(Object object) {
)
);
}

@Issue("https://github.com/moderneinc/customer-requests/issues/2202")
@Test
void flowScopedPatternVariableConflictWithElseReturn() {
rewriteRun(
//language=java
java(
"""
class A {
void test(Object objNotOnOrAfter, Object objNotBefore) {
String strNotOnOrAfter = "";
if (objNotOnOrAfter != null && objNotOnOrAfter instanceof String) {
strNotOnOrAfter = (String) objNotOnOrAfter;
} else {
return;
}

String strNotBefore = "";
if (objNotBefore != null && objNotBefore instanceof String) {
strNotBefore = (String) objNotBefore;
}
}
}
""",
"""
class A {
void test(Object objNotOnOrAfter, Object objNotBefore) {
String strNotOnOrAfter = "";
if (objNotOnOrAfter != null && objNotOnOrAfter instanceof String string) {
strNotOnOrAfter = string;
} else {
return;
}

String strNotBefore = "";
if (objNotBefore != null && objNotBefore instanceof String string1) {
strNotBefore = string1;
}
}
}
"""
)
);
}
}
Loading