Skip to content

Commit

Permalink
Fix bug in InstanceOfPatternMatch getting the variable name from th…
Browse files Browse the repository at this point in the history
…e name of a nested class (#291)

Co-authored-by: Hoan Nguyen <hoanamzn@amazon.com>
  • Loading branch information
nguyenhoan and Hoan Nguyen committed May 10, 2024
1 parent a7ed18f commit 989faff
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,7 @@ public String variableName(@Nullable JavaType type) {
return name;
} else if (type instanceof JavaType.FullyQualified) {
String className = ((JavaType.FullyQualified) type).getClassName();
if (className.indexOf('.') > 0) {
className = className.substring(className.lastIndexOf('.'));
}
className = className.substring(className.lastIndexOf('.') + 1);
String baseName = null;
switch (style) {
case SHORT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,41 @@ void test(Object o) {
);
}

@Test
void conflictingVariableOfNestedType() {
rewriteRun(
//language=java
java(
"""
import java.util.Map;
public class A {
void test(Object o) {
Map.Entry entry = null;
if (o instanceof Map.Entry) {
entry = (Map.Entry) o;
}
System.out.println(entry);
}
}
""",
"""
import java.util.Map;
public class A {
void test(Object o) {
Map.Entry entry = null;
if (o instanceof Map.Entry entry1) {
entry = entry1;
}
System.out.println(entry);
}
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/2787")
@Disabled
@Test
Expand Down

0 comments on commit 989faff

Please sign in to comment.