Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in InstanceOfPatternMatch getting the variable name from the name of a nested class #291

Merged
merged 1 commit into from
May 10, 2024
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 @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Subtle using last index of being -1 here and adding 1; unless it's found in which case you skip. Smart. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

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