Skip to content

fix: handle comment at the end of a method declaration - #4996

Merged
timtebeek merged 3 commits into
openrewrite:mainfrom
auke-:feat-native-methods
Feb 17, 2025
Merged

timtebeek merged 3 commits into
openrewrite:mainfrom
auke-:feat-native-methods

Conversation

@auke-

@auke- auke- commented Feb 7, 2025 •

Copy link
Copy Markdown
Contributor

Multi line comment is allowed at almost any part of the Java source code
where whitespace is allowed. The reloadable Java parser visitor however
swallows multi line comment at the end of a method declaration, which
happens to be a common pattern in GWT in order to embed JavaScript in a
Java class.

@timtebeek timtebeek added bug Something isn't working java labels Feb 7, 2025
@timtebeek
timtebeek marked this pull request as draft February 7, 2025 09:30
@timtebeek timtebeek changed the title chore: reproduce issue with GWT JNSI methods chore: reproduce issue with trailing comments used by GWT JNSI methods Feb 7, 2025
Multi line comment is allowed at almost any part of the Java source code
where whitespace is allowed. The reloadable Java parser visitor however
swallows multi line comment at the end of a method declaration, which
happens to be a common pattern in GWT in order to embed JavaScript in a
Java class.

Note that this effectively is a partial revert of openrewrite#4412
(commit ac32851).
@auke-
auke- force-pushed the feat-native-methods branch from 0ecf34a to f1f6a63 Compare February 12, 2025 18:49
@auke- auke- changed the title chore: reproduce issue with trailing comments used by GWT JNSI methods fix: handle comment at the end of a method declaration Feb 12, 2025
@timtebeek
timtebeek marked this pull request as ready for review February 12, 2025 19:09
@timtebeek
timtebeek self-requested a review February 12, 2025 19:09

@timtebeek timtebeek left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Appreciate the help and patience here @auke- ! This is looking good on our end. We'll wait for the tests to clear and then we can merge.

@timtebeek

Copy link
Copy Markdown
Member

Turns out this was indeed needed for those erroneous nodes you had helpfully linked above, thanks!

JavaParserTest > erroneousExpressionStatements(String) > [3] source=package com.example.demo;
class FooBar {
    public void test(int num string s, int b) {
      String a; this.ownerR
      System.out.println();
    }
}
 FAILED
    org.opentest4j.AssertionFailedError: [When parsing and printing the source code back to text without modifications, the printed source didn't match the original source code. This means there is a bug in the parser implementation itself. Please open an issue to report this, providing a sample of the code that generated this error for "com/example/demo/FooBar.java":
diff --git a/com/example/demo/FooBar.java b/com/example/demo/FooBar.java
index 5028cf3..7e634ec 100644
--- a/com/example/demo/FooBar.java
+++ b/com/example/demo/FooBar.java
@@ -1,7 +1,7 @@ 
 package com.example.demo;
 class FooBar {
     public void test(int num string s, int b) {
-      String a; this.ownerR
+      String a this.ownerR
       System.out.println();
     }
 }
\ No newline at end of file
    ] 
    expected: 
      "package com.example.demo;
      class FooBar {
          public void test(int num string s, int b) {
            String a; this.ownerR
            System.out.println();
          }
      }"
     but was: 
      "package com.example.demo;
      class FooBar {
          public void test(int num string s, int b) {
            String a this.ownerR
            System.out.println();
          }
      }"
        at app//org.openrewrite.test.RewriteTest.assertContentEquals(RewriteTest.java:618)
        at app//org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:308)
        at app//org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:129)
        at app//org.openrewrite.java.JavaParserTest.erroneousExpressionStatements(JavaParserTest.java:242)

/cc @knutwannheden

@timtebeek
timtebeek marked this pull request as draft February 12, 2025 19:25
@timtebeek
timtebeek self-requested a review February 12, 2025 19:25
@knutwannheden

knutwannheden commented Feb 17, 2025 •

Copy link
Copy Markdown
Contributor

I wonder if we shouldn't remove that test case for now, which attempts to parse erroneous code like this (see first method parameter):

public void test(int num string s, int b) {
  String a; this.ownerR
  System.out.println();
}

While it would be nice to not fail to parse code like this, it seems like it would be higher priority to parse correct code correctly.

@vudayani, @BoykoAlex : Maybe you can comment on which erroneous input we need to be able to successfully parse into an LST?

@timtebeek
timtebeek marked this pull request as ready for review February 17, 2025 11:31
@timtebeek

Copy link
Copy Markdown
Member

Had another look and adjusted the tests: turns out it's a parameterized test that tests a couple of different cases, but the failing case had two separate compilation issues: one in the arguments, and then another with a missing field reference and semicolon. I've separated those cases as I believe the goal here was to support completions at the point where the user has their cursor, thus most closely matching a single erroneous node at that position. With that I think we can merge this already, and revise if needed asynchronously without indeed breaking valid usage. Thanks all!

@timtebeek
timtebeek merged commit 99e1666 into openrewrite:main Feb 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working java

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

ChangePackage doesn't handle classes with GWT JNSI methods

3 participants