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

Bug in InstanceOfPatternMatch getting the variable name from the name of a nested class #290

Closed
nguyenhoan opened this issue May 9, 2024 · 2 comments · Fixed by #291
Closed
Assignees
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@nguyenhoan
Copy link
Contributor

nguyenhoan commented May 9, 2024

What version of OpenRewrite are you using?

I am using the latest version at the time of this writing at commit 1ed9c44

How are you running OpenRewrite?

I am running the unit tests.

What is the smallest, simplest way to reproduce the problem?

Adding this unit test to org.openrewrite.staticanalysis.InstanceOfPatternMatchTest.If

        @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);
                      }
                  }
                  """
              )
            );
        }

What did you expect to see?

                  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);
                      }
                  }

What did you see instead?

                  import java.util.Map;
                  
                  public class A {
                      void test(Object o) {
                          Map.Entry entry = null;
                          if (o instanceof Map.Entry .Entry) {
                            entry = .Entry;
                          }
                          System.out.println(entry);
                      }
                  }

What is the full stack trace of any errors you encountered?

Are you interested in contributing a fix to OpenRewrite?

There's a bug on this line
where

                    className = className.substring(className.lastIndexOf('.'));

is missing the increment to go the next character after .

                    className = className.substring(className.lastIndexOf('.') + 1);
@nguyenhoan nguyenhoan added the bug Something isn't working label May 9, 2024
@timtebeek
Copy link
Contributor

Hi @nguyenhoan ; thanks a lot for the detailed report, and immediately working out the cause. Would you want to create a PR such that you get credit for the fix as well as a contributor? Just let me know, otherwise I'll make the change myself.

@timtebeek timtebeek moved this to In Progress in OpenRewrite May 9, 2024
@timtebeek timtebeek added the good first issue Good for newcomers label May 9, 2024
@timtebeek timtebeek changed the title A bug in getting the variable name from the name of a nested class Bug in InstanceOfPatternMatch getting the variable name from the name of a nested class May 9, 2024
@nguyenhoan
Copy link
Contributor Author

Thanks for the suggestion!

Here it is
#291

@github-project-automation github-project-automation bot moved this from In Progress to Done in OpenRewrite May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants