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

Partial support Lombok for java 8 (mostly done) #4855

Merged
merged 29 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
761c2ea
Support Lombok for java 8
jevanlingen Jan 7, 2025
7cb644f
Support Lombok for java 8
jevanlingen Jan 7, 2025
69984ba
Support Lombok for java 8
jevanlingen Jan 7, 2025
12986eb
Add rewrite-java-lombok to classpath
jevanlingen Jan 7, 2025
e284bf5
Improve message
jevanlingen Jan 7, 2025
80e9ead
Remove `TimedTodo` to be more like the other java parsers
jevanlingen Jan 7, 2025
7a9fb18
cleanup
jevanlingen Jan 7, 2025
1531ca4
Add JavaCompiler `delegate` fix
jevanlingen Jan 7, 2025
b15c51d
Support Lomboks `var` and `val` for Java 8
jevanlingen Jan 7, 2025
b7fbd6e
Cleanup
jevanlingen Jan 7, 2025
da9de0c
Cleanup
jevanlingen Jan 7, 2025
71a2e13
Cleanup of `isLombokGenerated` methods
jevanlingen Jan 8, 2025
6a8c5c6
Cleanup of `isLombokGenerated` methods
jevanlingen Jan 8, 2025
1959b5c
Cleanup of `isLombokGenerated` methods
jevanlingen Jan 8, 2025
a21fbbd
Cleanup of `isLombokGenerated` methods
jevanlingen Jan 8, 2025
5dc7963
Cleanup of `isLombokGenerated` methods
jevanlingen Jan 8, 2025
baf98bb
Cleanup
jevanlingen Jan 8, 2025
fcb70a3
Merge branch 'main' into lombok-java-8
jevanlingen Jan 8, 2025
1b6173e
Improve `onConstructor` and `onConstructorNoArgs` args
jevanlingen Jan 8, 2025
ddde9b0
Fix missing `onConstructor_` check
jevanlingen Jan 9, 2025
6b3d764
Cleanup
jevanlingen Jan 9, 2025
75f636b
Apply suggestions from code review
jevanlingen Jan 9, 2025
05fda97
Merge branch 'main' into lombok-java-8
jevanlingen Jan 9, 2025
3bc94df
Improve tests
jevanlingen Jan 9, 2025
b5deb67
Merge branch 'main' into lombok-java-8
jevanlingen Jan 9, 2025
e0c917d
Merge branch 'main' into lombok-java-8
jevanlingen Jan 9, 2025
b59aecc
Rename JavacAnnotationHandler with no action to XNoOpHandler
jevanlingen Jan 10, 2025
fb52094
Rename JavacAnnotationHandler with no action to XNoOpHandler
jevanlingen Jan 10, 2025
3f2f60b
Improve lomboks `ExampleException` test
jevanlingen Jan 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import com.sun.source.tree.*;
import com.sun.source.util.TreePathScanner;
import com.sun.tools.javac.code.Attribute;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.tree.DCTree;
Expand All @@ -27,6 +26,7 @@
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.*;
import com.sun.tools.javac.util.Context;
import lombok.Generated;
import org.jspecify.annotations.Nullable;
import org.openrewrite.Cursor;
import org.openrewrite.ExecutionContext;
Expand Down Expand Up @@ -1493,9 +1493,11 @@ private J.VariableDeclarations visitVariables(List<VariableTree> nodes, Space fm
// this is a lambda parameter with an inferred type expression
typeExpr = null;
} else {
boolean lombokVal = isLombokVal(node);
Space space = whitespace();
boolean lombokVal = source.substring(cursor).startsWith("val");
jevanlingen marked this conversation as resolved.
Show resolved Hide resolved
cursor += 3; // skip `val` or `var`
typeExpr = new J.Identifier(randomId(),
sourceBefore(lombokVal ? "val" : "var"),
space,
Markers.build(singletonList(JavaVarKeyword.build())),
emptyList(),
lombokVal ? "val" : "var",
Expand Down Expand Up @@ -1659,8 +1661,8 @@ public J visitWildcard(WildcardTree node, Space fmt) {
}

private static int getActualStartPosition(JCTree t) {
// not sure if this is a bug in Lombok, but the variable's start position is after the `val` annotation
if (t instanceof JCVariableDecl && isLombokVal((JCVariableDecl) t)) {
// The variable's start position in the source is wrongly after lombok's `@val` annotation
if (t instanceof JCVariableDecl && isLombokGenerated(t)) {
return ((JCVariableDecl) t).mods.annotations.get(0).getStartPosition();
}
return t.getStartPosition();
Expand Down Expand Up @@ -1825,7 +1827,7 @@ private List<JRightPadded<Statement>> convertStatements(@Nullable List<? extends

Map<Integer, List<Tree>> treesGroupedByStartPosition = new LinkedHashMap<>();
for (Tree t : trees) {
if (isLombokGenerated(t)) {
if (!(t instanceof JCVariableDecl) && isLombokGenerated(t)) {
continue;
}
treesGroupedByStartPosition.computeIfAbsent(((JCTree) t).getStartPosition(), k -> new ArrayList<>(1)).add(t);
Expand Down Expand Up @@ -1857,51 +1859,23 @@ private List<JRightPadded<Statement>> convertStatements(@Nullable List<? extends
return converted;
}

private static boolean isLombokVal(JCTree.JCVariableDecl t) {
if (t.sym != null && t.sym.getMetadata() != null) {
for (Attribute.Compound a : t.sym.getDeclarationAttributes()) {
if ("lombok.val".equals(a.type.toString())) {
return true;
}
}
}
return false;
}

private static boolean isLombokGenerated(Tree t) {
Tree tree = (t instanceof JCAnnotation) ? ((JCAnnotation) t).getAnnotationType() : t;

Symbol sym = null;
if (t instanceof JCAnnotation) {
t = ((JCAnnotation) t).getAnnotationType();
}
if (t instanceof JCIdent) {
sym = ((JCIdent) t).sym;
} else if (t instanceof JCTree.JCMethodDecl) {
sym = ((JCMethodDecl) t).sym;
} else if (t instanceof JCTree.JCClassDecl) {
sym = ((JCClassDecl) t).sym;
} else if (t instanceof JCTree.JCVariableDecl) {
sym = ((JCVariableDecl) t).sym;
if (tree instanceof JCIdent) {
sym = ((JCIdent) tree).sym;
} else if (tree instanceof JCTree.JCMethodDecl) {
sym = ((JCMethodDecl) tree).sym;
} else if (tree instanceof JCTree.JCClassDecl) {
sym = ((JCClassDecl) tree).sym;
} else if (tree instanceof JCTree.JCVariableDecl) {
sym = ((JCVariableDecl) tree).sym;
return sym != null && sym.getDeclarationAttributes().stream().anyMatch(a -> "lombok.val".equals(a.type.toString()));
}
return isLombokGenerated(sym);
}

private static boolean isLombokGenerated(@Nullable Symbol sym) {
if (sym == null) {
return false;
}
// Lombok val is represented as a @lombok.val on a "final" modifier, neither which appear in source
if ("lombok.val".equals(sym.getQualifiedName().toString())) {
return true;
}
if (sym.getMetadata() == null) {
return false;
}
for (Attribute.Compound a : sym.getDeclarationAttributes()) {
if ("lombok.Generated".equals(a.type.toString())) {
return true;
}
}
return false;
//noinspection ConstantConditions
return sym != null && ("lombok.val".equals(sym.getQualifiedName().toString()) || sym.getAnnotation(Generated.class) != null);
}

/**
Expand Down Expand Up @@ -2079,7 +2053,6 @@ private ReloadableJava11ModifierResults sortedModifiersAndAnnotations(ModifiersT
for (int i = cursor; i < source.length(); i++) {
if (annotationPosTable.containsKey(i)) {
JCAnnotation jcAnnotation = annotationPosTable.get(i);
// Skip over lombok's "@val" annotation which does not actually appear in source
if (isLombokGenerated(jcAnnotation.getAnnotationType())) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
import com.sun.source.doctree.DocCommentTree;
import com.sun.source.tree.*;
import com.sun.source.util.TreePathScanner;
import com.sun.tools.javac.code.Attribute;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.tree.DocCommentTable;
import com.sun.tools.javac.tree.EndPosTable;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.*;
import com.sun.tools.javac.util.Context;
import lombok.Generated;
import org.jspecify.annotations.Nullable;
import org.openrewrite.Cursor;
import org.openrewrite.ExecutionContext;
Expand Down Expand Up @@ -1572,9 +1572,11 @@ private J.VariableDeclarations visitVariables(List<VariableTree> nodes, Space fm
// this is a lambda parameter with an inferred type expression
typeExpr = null;
} else {
boolean lombokVal = isLombokVal(node);
Space space = whitespace();
boolean lombokVal = source.substring(cursor).startsWith("val");
jevanlingen marked this conversation as resolved.
Show resolved Hide resolved
cursor += 3; // skip `val` or `var`
typeExpr = new J.Identifier(randomId(),
sourceBefore(lombokVal ? "val" : "var"),
space,
Markers.build(singletonList(JavaVarKeyword.build())),
emptyList(),
lombokVal ? "val" : "var",
Expand Down Expand Up @@ -1723,8 +1725,8 @@ public J visitWildcard(WildcardTree node, Space fmt) {
}

private static int getActualStartPosition(JCTree t) {
// not sure if this is a bug in Lombok, but the variable's start position is after the `val` annotation
if (t instanceof JCVariableDecl && isLombokVal((JCVariableDecl) t)) {
// The variable's start position in the source is wrongly after lombok's `@val` annotation
if (t instanceof JCVariableDecl && isLombokGenerated(t)) {
return ((JCVariableDecl) t).mods.annotations.get(0).getStartPosition();
}
return t.getStartPosition();
Expand Down Expand Up @@ -1906,7 +1908,7 @@ private List<JRightPadded<Statement>> convertStatements(@Nullable List<? extends

Map<Integer, List<Tree>> treesGroupedByStartPosition = new LinkedHashMap<>();
for (Tree t : trees) {
if (isLombokGenerated(t)) {
if (!(t instanceof JCVariableDecl) && isLombokGenerated(t)) {
continue;
}
treesGroupedByStartPosition.computeIfAbsent(((JCTree) t).getStartPosition(), k -> new ArrayList<>(1)).add(t);
Expand Down Expand Up @@ -1938,51 +1940,23 @@ private List<JRightPadded<Statement>> convertStatements(@Nullable List<? extends
return converted;
}

private static boolean isLombokVal(JCTree.JCVariableDecl t) {
if (t.sym != null && t.sym.getMetadata() != null) {
for (Attribute.Compound a : t.sym.getDeclarationAttributes()) {
if ("lombok.val".equals(a.type.toString())) {
return true;
}
}
}
return false;
}

private static boolean isLombokGenerated(Tree t) {
Tree tree = (t instanceof JCAnnotation) ? ((JCAnnotation) t).getAnnotationType() : t;

Symbol sym = null;
if (t instanceof JCAnnotation) {
t = ((JCAnnotation) t).getAnnotationType();
}
if (t instanceof JCIdent) {
sym = ((JCIdent) t).sym;
} else if (t instanceof JCTree.JCMethodDecl) {
sym = ((JCMethodDecl) t).sym;
} else if (t instanceof JCTree.JCClassDecl) {
sym = ((JCClassDecl) t).sym;
} else if (t instanceof JCTree.JCVariableDecl) {
sym = ((JCVariableDecl) t).sym;
if (tree instanceof JCIdent) {
sym = ((JCIdent) tree).sym;
} else if (tree instanceof JCTree.JCMethodDecl) {
sym = ((JCMethodDecl) tree).sym;
} else if (tree instanceof JCTree.JCClassDecl) {
sym = ((JCClassDecl) tree).sym;
} else if (tree instanceof JCTree.JCVariableDecl) {
sym = ((JCVariableDecl) tree).sym;
return sym != null && sym.getDeclarationAttributes().stream().anyMatch(a -> "lombok.val".equals(a.type.toString()));
}
return isLombokGenerated(sym);
}

private static boolean isLombokGenerated(@Nullable Symbol sym) {
if (sym == null) {
return false;
}
// Lombok val is represented as a @lombok.val on a "final" modifier, neither which appear in source
if ("lombok.val".equals(sym.getQualifiedName().toString())) {
return true;
}
if (sym.getMetadata() == null) {
return false;
}
for (Attribute.Compound a : sym.getDeclarationAttributes()) {
if ("lombok.Generated".equals(a.type.toString())) {
return true;
}
}
return false;
//noinspection ConstantConditions
return sym != null && ("lombok.val".equals(sym.getQualifiedName().toString()) || sym.getAnnotation(Generated.class) != null);
}

/**
Expand Down Expand Up @@ -2162,7 +2136,6 @@ private ReloadableJava17ModifierResults sortedModifiersAndAnnotations(ModifiersT
for (int i = cursor; i < source.length(); i++) {
if (annotationPosTable.containsKey(i)) {
JCAnnotation jcAnnotation = annotationPosTable.get(i);
// Skip over lombok's "@val" annotation which does not actually appear in source
if (isLombokGenerated(jcAnnotation.getAnnotationType())) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
import com.sun.source.doctree.DocCommentTree;
import com.sun.source.tree.*;
import com.sun.source.util.TreePathScanner;
import com.sun.tools.javac.code.Attribute;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.tree.DocCommentTable;
import com.sun.tools.javac.tree.EndPosTable;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.*;
import com.sun.tools.javac.util.Context;
import lombok.Generated;
import org.jspecify.annotations.Nullable;
import org.openrewrite.Cursor;
import org.openrewrite.ExecutionContext;
Expand Down Expand Up @@ -1572,9 +1572,11 @@ private J.VariableDeclarations visitVariables(List<VariableTree> nodes, Space fm
// this is a lambda parameter with an inferred type expression
typeExpr = null;
} else {
boolean lombokVal = isLombokVal(node);
Space space = whitespace();
boolean lombokVal = source.substring(cursor).startsWith("val");
jevanlingen marked this conversation as resolved.
Show resolved Hide resolved
cursor += 3; // skip `val` or `var`
typeExpr = new J.Identifier(randomId(),
sourceBefore(lombokVal ? "val" : "var"),
space,
Markers.build(singletonList(JavaVarKeyword.build())),
emptyList(),
lombokVal ? "val" : "var",
Expand Down Expand Up @@ -1723,8 +1725,8 @@ public J visitWildcard(WildcardTree node, Space fmt) {
}

private static int getActualStartPosition(JCTree t) {
// not sure if this is a bug in Lombok, but the variable's start position is after the `val` annotation
if (t instanceof JCVariableDecl && isLombokVal((JCVariableDecl) t)) {
// The variable's start position in the source is wrongly after lombok's `@val` annotation
if (t instanceof JCVariableDecl && isLombokGenerated(t)) {
return ((JCVariableDecl) t).mods.annotations.get(0).getStartPosition();
}
return t.getStartPosition();
Expand Down Expand Up @@ -1906,7 +1908,7 @@ private List<JRightPadded<Statement>> convertStatements(@Nullable List<? extends

Map<Integer, List<Tree>> treesGroupedByStartPosition = new LinkedHashMap<>();
for (Tree t : trees) {
if (isLombokGenerated(t)) {
if (!(t instanceof JCVariableDecl) && isLombokGenerated(t)) {
continue;
}
treesGroupedByStartPosition.computeIfAbsent(((JCTree) t).getStartPosition(), k -> new ArrayList<>(1)).add(t);
Expand Down Expand Up @@ -1938,51 +1940,23 @@ private List<JRightPadded<Statement>> convertStatements(@Nullable List<? extends
return converted;
}

private static boolean isLombokVal(JCTree.JCVariableDecl t) {
if (t.sym != null && t.sym.getMetadata() != null) {
for (Attribute.Compound a : t.sym.getDeclarationAttributes()) {
if ("lombok.val".equals(a.type.toString())) {
return true;
}
}
}
return false;
}

private static boolean isLombokGenerated(Tree t) {
Tree tree = (t instanceof JCAnnotation) ? ((JCAnnotation) t).getAnnotationType() : t;

Symbol sym = null;
if (t instanceof JCAnnotation) {
t = ((JCAnnotation) t).getAnnotationType();
}
if (t instanceof JCIdent) {
sym = ((JCIdent) t).sym;
} else if (t instanceof JCTree.JCMethodDecl) {
sym = ((JCMethodDecl) t).sym;
} else if (t instanceof JCTree.JCClassDecl) {
sym = ((JCClassDecl) t).sym;
} else if (t instanceof JCTree.JCVariableDecl) {
sym = ((JCVariableDecl) t).sym;
if (tree instanceof JCIdent) {
sym = ((JCIdent) tree).sym;
} else if (tree instanceof JCTree.JCMethodDecl) {
sym = ((JCMethodDecl) tree).sym;
} else if (tree instanceof JCTree.JCClassDecl) {
sym = ((JCClassDecl) tree).sym;
} else if (tree instanceof JCTree.JCVariableDecl) {
sym = ((JCVariableDecl) tree).sym;
return sym != null && sym.getDeclarationAttributes().stream().anyMatch(a -> "lombok.val".equals(a.type.toString()));
}
return isLombokGenerated(sym);
}

private static boolean isLombokGenerated(@Nullable Symbol sym) {
if (sym == null) {
return false;
}
// Lombok val is represented as a @lombok.val on a "final" modifier, neither which appear in source
if ("lombok.val".equals(sym.getQualifiedName().toString())) {
return true;
}
if (sym.getMetadata() == null) {
return false;
}
for (Attribute.Compound a : sym.getDeclarationAttributes()) {
if ("lombok.Generated".equals(a.type.toString())) {
return true;
}
}
return false;
//noinspection ConstantConditions
return sym != null && ("lombok.val".equals(sym.getQualifiedName().toString()) || sym.getAnnotation(Generated.class) != null);
}

/**
Expand Down Expand Up @@ -2162,7 +2136,6 @@ private ReloadableJava21ModifierResults sortedModifiersAndAnnotations(ModifiersT
for (int i = cursor; i < source.length(); i++) {
if (annotationPosTable.containsKey(i)) {
JCAnnotation jcAnnotation = annotationPosTable.get(i);
// Skip over lombok's "@val" annotation which does not actually appear in source
if (isLombokGenerated(jcAnnotation.getAnnotationType())) {
continue;
}
Expand Down
1 change: 1 addition & 0 deletions rewrite-java-8/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies {
compileOnly("org.slf4j:slf4j-api:1.7.+")

implementation(project(":rewrite-java"))
runtimeOnly(project(":rewrite-java-lombok"))
implementation("org.ow2.asm:asm:latest.release")

implementation("io.micrometer:micrometer-core:1.9.+")
Expand Down
Loading
Loading