rewrite icon indicating copy to clipboard operation
rewrite copied to clipboard

Getting StringIndexOutOfBoundsException with Groovy class that has groovy transform.

Open gtiwari333 opened this issue 1 year ago • 2 comments

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 avatar Jun 13 '24 18:06 gtiwari333

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

  • https://github.com/openrewrite/rewrite/issues/4055#issue-2156591037

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

timtebeek avatar Jun 13 '24 18:06 timtebeek

@timtebeek

Issue reported on https://github.com/openrewrite/rewrite/issues/4055#issue-2156591037:

  • 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)

gtiwari333 avatar Jun 13 '24 18:06 gtiwari333