rewrite: Lombok: JavaParser fails with StringIndexOutOfBoundsException when lombok processor is enabled
What version of OpenRewrite are you using?
- OpenRewrite 8.41.2
What is the smallest, simplest way to reproduce the problem?
I haven’t debugged the exact root cause yet. the issue seems to occur when annotations are added or removed in the source code. This appears to result in incorrect character positions for the cursor. (Will add more detail about the issue after debugging)
@Getter
@Builder
public static class Request {
@Nonnull private final List<TransportJobId> transportJobIds;
@Nullable private final UnixTimeMillis estimatedReadyAt;
@Nonnull private final RegionId regionId;
@Nonnull private final boolean wasTriggeredFromFulfillmentOrder;
}
What did you expect to see?
A valid compilation unit should be generated for the source file.
What did you see instead?
A ParseError instance is generated for the source file instead.
What is the full stack trace of any errors you encountered?
java.lang.StringIndexOutOfBoundsException: begin 5327, end 5315, length 5434
java.base/java.lang.String.checkBoundsBeginEnd(String.java:3319)
java.base/java.lang.String.substring(String.java:1874)
org.openrewrite.java.isolated.ReloadableJava17ParserVisitor.visitAnnotation(ReloadableJava17ParserVisitor.java:143)
org.openrewrite.java.isolated.ReloadableJava17ParserVisitor.visitAnnotation(ReloadableJava17ParserVisitor.java:72)
com.sun.tools.javac.tree.JCTree$JCAnnotation.accept(JCTree.java:2601)
com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:82)
org.openrewrite.java.isolated.ReloadableJava17ParserVisitor.convert(ReloadableJava17ParserVisitor.java:1604)
...
Are you interested in [contributing a fix to OpenRewrite]
Yes.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 20 (20 by maintainers)
Hi @timtebeek,I verified the fix with our Uber monorepo and discovered a couple more issues, which I’ve addressed with fixes in OpenRewrite (PR to be published soon). After running the FindMissingTypes recipe, I didn’t find any missing types. Finally we got Lombok working! 🚀
Massive thanks again for the help here @amishra-u ! We ended up packaging an edge build of lombok to bring this to Java 11, 17 and 21 by default already.
Once there’s an official release we can drop that local jar again; there’s no further changes beyond pulling forward a newer build.
There’s some work to be done still for Java 8 (fix missing type information & enable by default), but we can pick that up separately.
Able to reproduce this bug with below unit test, @Nonnull to field to cause the error.
This is basically the reason we haven’t enabled the Lombok processor yet. It appears like it doesn’t always adjust the offsets in the AST correctly. I suspect that us a bug in Lombok. So either we need to get it fixed there or we need to adjust our parser so that it tracks the cursor in the file without relying on the AST.