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

Groovy generated transform methods break parser #4254

Closed
gtiwari333 opened this issue Jun 13, 2024 · 2 comments · Fixed by #4848
Closed

Groovy generated transform methods break parser #4254

gtiwari333 opened this issue Jun 13, 2024 · 2 comments · Fixed by #4848
Labels
bug Something isn't working parser-groovy

Comments

@gtiwari333
Copy link

I'm seeing this on groovy class that uses groovy transform (import groovy.transform.*) such as @tostring, @EqualsAndHashCode.

What version of OpenRewrite are you using?

I am using https://github.com/gtiwari333/openrewrite-groovy-bug/blob/main/init.gradle

  • org.openrewrite.recipe:rewrite-recipe-bom - 2.1.20
  • org.openrewrite:rewrite-gradle-plugin - latest.release
  • groovy 4.0.21
  • jdk 17/21
  • gradle 8.8

How are you running OpenRewrite?

%./gradlew clean rewriteRun --init-script init.gradle --info

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

Checkout the following project and run ./gradlew clean rewriteRun --init-script init.gradle --info

Im seeing similar behavior with both jdk17 and jdk21

https://github.com/gtiwari333/openrewrite-groovy-bug/blob/main/README.md

What did you expect to see?

No failures. AutoFormat recipe should run on the code.

What did you see instead?

Error during rewrite run
org.openrewrite.groovy.GroovyParsingException: Failed to parse src/main/groovy/com/example/demo/Test.groovy, cursor position likely inaccurate.
       at org.openrewrite.groovy.GroovyParserVisitor.visit(GroovyParserVisitor.java:170)
       at org.openrewrite.groovy.GroovyParser.lambda$parseInputs$4(GroovyParser.java:154)
       at [email protected]/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
       at [email protected]/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1625)
...
Caused by: java.lang.StringIndexOutOfBoundsException: begin 137, end 137, length 134
       at java.base/java.lang.String.checkBoundsBeginEnd(String.java:4606)
       at java.base/java.lang.String.substring(String.java:2709)
       at org.openrewrite.groovy.GroovyParserVisitor.whitespace(GroovyParserVisitor.java:2171)
       at org.openrewrite.groovy.GroovyParserVisitor.access$000(GroovyParserVisitor.java:66)
       at org.openrewrite.groovy.GroovyParserVisitor$RewriteGroovyVisitor.visitVariableExpressionType(GroovyParserVisitor.java:1955)
       at org.openrewrite.groovy.GroovyParserVisitor$RewriteGroovyVisitor.visitDeclarationExpression(GroovyParserVisitor.java:1262)
       at org.codehaus.groovy.ast.expr.DeclarationExpression.visit(DeclarationExpression.java:89)

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

Run the ./gradlew clean rewriteRun --init-script init.gradle --info command to get full stacktrace. I've provided relevant bits above.

Are you interested in contributing a fix to OpenRewrite?

Not too familiar how AST works. But i can try.

@gtiwari333 gtiwari333 added the bug Something isn't working label Jun 13, 2024
@gtiwari333 gtiwari333 changed the title Groovy transform throws StringIndexOutOfBoundsException Getting StringIndexOutOfBoundsException with Groovy class that has groovy transform. Jun 13, 2024
@timtebeek timtebeek moved this to Backlog in OpenRewrite Jun 13, 2024
@timtebeek
Copy link
Contributor

Thanks for the detailed report and offer to help! We saw an issue with ToString reported before in

Would you agree that's similar? Is there value in keeping both issues open?

@gtiwari333
Copy link
Author

gtiwari333 commented Jun 13, 2024

@timtebeek

Issue reported on #4055 (comment):

  • when a class has multiple fields (AND @tostring) its returning Caused by: java.lang.IllegalStateException: Unexpected constant type java.lang.Boolean exception. They are using Spring Boot 2.7. Which brings Groovy 3 by default.

Issue reported on this page:

  • when the class has single field (AND @tostring), its returning bit different error Caused by: java.lang.StringIndexOutOfBoundsException: begin 137, end 137, length 134. Im using groovy 4.

Im sure that they are different issues.

Stacktrace from the issue reported on this page is suggesting the error is coming from a different place.

Caused by: java.lang.StringIndexOutOfBoundsException: begin 137, end 137, length 134
        at java.base/java.lang.String.checkBoundsBeginEnd(String.java:4606)
        at java.base/java.lang.String.substring(String.java:2709)
        at org.openrewrite.groovy.GroovyParserVisitor.whitespace(GroovyParserVisitor.java:2171)
        at org.openrewrite.groovy.GroovyParserVisitor.access$000(GroovyParserVisitor.java:66)
        at org.openrewrite.groovy.GroovyParserVisitor$RewriteGroovyVisitor.visitVariableExpressionType(GroovyParserVisitor.java:1955)
        at org.openrewrite.groovy.GroovyParserVisitor$RewriteGroovyVisitor.visitDeclarationExpression(GroovyParserVisitor.java:1262)
        at org.codehaus.groovy.ast.expr.DeclarationExpression.visit(DeclarationExpression.java:89)
        at org.codehaus.groovy.ast.CodeVisitorSupport.visitExpressionStatement(CodeVisitorSupport.java:117)
        at org.openrewrite.groovy.GroovyParserVisitor$RewriteGroovyVisitor.lambda$visitExpressionStatement$6(GroovyParserVisitor.java:1310)
        at org.openrewrite.groovy.GroovyParserVisitor$RewriteGroovyVisitor.labeled(GroovyParserVisitor.java:638)
        at org.openrewrite.groovy.GroovyParserVisitor$RewriteGroovyVisitor.visitExpressionStatement(GroovyParserVisitor.java:1309)
        at org.codehaus.groovy.ast.stmt.ExpressionStatement.visit(ExpressionStatement.java:40)
        at org.openrewrite.groovy.GroovyParserVisitor$RewriteGroovyVisitor.visit(GroovyParserVisitor.java:572)
        at org.openrewrite.groovy.GroovyParserVisitor$RewriteGroovyVisitor.visitBlockStatement(GroovyParserVisitor.java:919)
        at org.codehaus.groovy.ast.stmt.BlockStatement.visit(BlockStatement.java:69)
        at org.openrewrite.groovy.GroovyParserVisitor$RewriteGroovyVisitor.visit(GroovyParserVisitor.java:572)
        at org.openrewrite.groovy.GroovyParserVisitor$RewriteGroovyVisitor.visitBlockStatement(GroovyParserVisitor.java:919)
        at org.codehaus.groovy.ast.stmt.BlockStatement.visit(BlockStatement.java:69)
        at org.openrewrite.groovy.GroovyParserVisitor$RewriteGroovyVisitor.visit(GroovyParserVisitor.java:572)
        at org.openrewrite.groovy.GroovyParserVisitor$RewriteGroovyVisitor.access$1200(GroovyParserVisitor.java:560)
        at org.openrewrite.groovy.GroovyParserVisitor$RewriteGroovyClassVisitor.visitMethod(GroovyParserVisitor.java:537)
        at org.openrewrite.groovy.GroovyParserVisitor$RewriteGroovyClassVisitor.lambda$visitClassBlock$3(GroovyParserVisitor.java:334)
        at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)

@jevanlingen jevanlingen changed the title Getting StringIndexOutOfBoundsException with Groovy class that has groovy transform. Groovy generated transform methods break parser Jan 6, 2025
@jevanlingen jevanlingen linked a pull request Jan 6, 2025 that will close this issue
3 tasks
@github-project-automation github-project-automation bot moved this from Backlog to Done in OpenRewrite Jan 6, 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 parser-groovy
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants