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 11 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 @@ -1889,8 +1889,8 @@ 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())) {
// Lombok local variables are represented as `final @lombok.val` and `@lombok.var`, which do not appear in source
if ("lombok.val".equals(sym.getQualifiedName().toString()) || "lombok.var".equals(sym.getQualifiedName().toString())) {
return true;
}
if (sym.getMetadata() == null) {
Expand Down Expand Up @@ -2079,7 +2079,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 @@ -1970,8 +1970,8 @@ 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())) {
// Lombok local variables are represented as `final @lombok.val` and `@lombok.var`, which do not appear in source
if ("lombok.val".equals(sym.getQualifiedName().toString()) || "lombok.var".equals(sym.getQualifiedName().toString())) {
return true;
}
if (sym.getMetadata() == null) {
Expand Down Expand Up @@ -2162,7 +2162,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 @@ -1970,8 +1970,8 @@ 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())) {
// Lombok local variables are represented as `final @lombok.val` and `@lombok.var`, which do not appear in source
if ("lombok.val".equals(sym.getQualifiedName().toString()) || "lombok.var".equals(sym.getQualifiedName().toString())) {
return true;
}
if (sym.getMetadata() == null) {
Expand Down Expand Up @@ -2162,7 +2162,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
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,19 @@
import com.sun.tools.javac.comp.*;
import com.sun.tools.javac.file.JavacFileManager;
import com.sun.tools.javac.main.JavaCompiler;
import com.sun.tools.javac.main.Option;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Log;
import com.sun.tools.javac.util.Options;
import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.Timer;
import lombok.Getter;
import org.jspecify.annotations.Nullable;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.Opcodes;
import org.openrewrite.ExecutionContext;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.SourceFile;
import org.openrewrite.internal.MetricsHelper;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.java.internal.JavaTypeCache;
import org.openrewrite.java.tree.J;
Expand All @@ -40,19 +39,25 @@
import org.openrewrite.tree.ParseError;
import org.openrewrite.tree.ParsingEventListener;
import org.openrewrite.tree.ParsingExecutionContextView;
import org.slf4j.LoggerFactory;

import javax.annotation.processing.Processor;
import javax.tools.*;
import java.io.*;
import java.lang.reflect.Constructor;
import java.net.URI;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import static com.sun.tools.javac.util.List.nil;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static java.util.stream.Collectors.toList;

class ReloadableJava8Parser implements JavaParser {
Expand All @@ -70,6 +75,7 @@ class ReloadableJava8Parser implements JavaParser {
private final JavaCompiler compiler;
private final ResettableLog compilerLog;
private final Collection<NamedStyles> styles;
private final List<Processor> annotationProcessors;

ReloadableJava8Parser(@Nullable Collection<Path> classpath,
Collection<byte[]> classBytesClasspath,
Expand Down Expand Up @@ -100,6 +106,70 @@ class ReloadableJava8Parser implements JavaParser {
Options.instance(context).put("-g", "-g");
Options.instance(context).put("-proc", "none");

// Ensure type attribution continues despite errors in individual files or nodes.
// If an error occurs in a single file or node, type attribution should still proceed
// for all other source files and unaffected nodes within the same file.
Options.instance(context).put("should-stop.ifError", "GENERATE");

LOMBOK:
if (System.getenv().getOrDefault("REWRITE_LOMBOK", System.getProperty("rewrite.lombok")) != null &&
classpath != null && classpath.stream().anyMatch(it -> it.toString().contains("lombok"))) {
Processor lombokProcessor = null;
try {
// https://projectlombok.org/contributing/lombok-execution-path
List<String> overrideClasspath = new ArrayList<>();
for (Path part : classpath) {
if (part.toString().contains("lombok")) {
overrideClasspath.add(part.toString());
}
}
// make sure the rewrite-java-lombok dependency comes first
boolean found = false;
for (int i = 0; i < overrideClasspath.size(); i++) {
if (overrideClasspath.get(i).contains("rewrite-java-lombok")) {
overrideClasspath.add(0, overrideClasspath.remove(i));
found = true;
}
}
if (!found) {
// try to find `rewrite-java-lombok` using class loader
URL resource = getClass().getClassLoader().getResource("org/openrewrite/java/lombok/OpenRewriteConfigurationKeysLoader.class");
if (resource != null && resource.getProtocol().equals("jar") && resource.getPath().startsWith("file:")) {
String path = Paths.get(URI.create(resource.getPath().substring(0, resource.getPath().indexOf("!")))).toString();
overrideClasspath.add(0, path);
} else {
break LOMBOK;
}
}
System.setProperty("shadow.override.lombok", String.join(File.pathSeparator, overrideClasspath));

Class<?> shadowLoaderClass = Class.forName("lombok.launch.ShadowClassLoader", true, getClass().getClassLoader());
Constructor<?> shadowLoaderConstructor = shadowLoaderClass.getDeclaredConstructor(
Class.forName("java.lang.ClassLoader"),
Class.forName("java.lang.String"),
Class.forName("java.lang.String"),
Class.forName("java.util.List"),
Class.forName("java.util.List"));
shadowLoaderConstructor.setAccessible(true);

ClassLoader lombokShadowLoader = (ClassLoader) shadowLoaderConstructor.newInstance(
getClass().getClassLoader(),
"lombok",
null,
emptyList(),
singletonList("lombok.patcher.Symbols")
);
lombokProcessor = (Processor) lombokShadowLoader.loadClass("lombok.core.AnnotationProcessor").getDeclaredConstructor().newInstance();
Options.instance(context).put(Option.PROCESSOR, "lombok.launch.AnnotationProcessorHider$AnnotationProcessor");
} catch (ReflectiveOperationException ignore) {
// Lombok was not found or could not be initialized
} finally {
annotationProcessors = lombokProcessor != null ? singletonList(lombokProcessor) : emptyList();
}
} else {
annotationProcessors = emptyList();
}

// MUST be created (registered with the context) after pfm and compilerLog
compiler = new JavaCompiler(context);

Expand All @@ -118,7 +188,7 @@ public void write(char[] cbuf, int off, int len) {
if (logCompilationWarningsAndErrors) {
String log = new String(Arrays.copyOfRange(cbuf, off, len));
if (!StringUtils.isBlank(log)) {
org.slf4j.LoggerFactory.getLogger(ReloadableJava8Parser.class).warn(log);
LoggerFactory.getLogger(ReloadableJava8Parser.class).warn(log);
}
}
}
Expand All @@ -138,45 +208,7 @@ public void close() {
@Override
public Stream<SourceFile> parseInputs(Iterable<Input> sourceFiles, @Nullable Path relativeTo, ExecutionContext ctx) {
ParsingEventListener parsingListener = ParsingExecutionContextView.view(ctx).getParsingListener();
if (classpath != null) { // override classpath
if (context.get(JavaFileManager.class) != pfm) {
throw new IllegalStateException("JavaFileManager has been forked unexpectedly");
}

try {
pfm.setLocation(StandardLocation.CLASS_PATH, classpath.stream().map(Path::toFile).collect(toList()));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

@SuppressWarnings("ConstantConditions") LinkedHashMap<Input, JCTree.JCCompilationUnit> cus = acceptedInputs(sourceFiles)
.collect(Collectors.toMap(
Function.identity(),
input -> {
try {
return compiler.parse(new Java8ParserInputFileObject(input, ctx));
} catch (IllegalStateException e) {
if ("endPosTable already set".equals(e.getMessage())) {
throw new IllegalStateException(
"Call reset() on JavaParser before parsing another set of source files that " +
"have some of the same fully qualified names. Source file [" +
input.getPath() + "]\n[\n" + StringUtils.readFully(input.getSource(ctx), getCharset(ctx)) + "\n]", e);
}
throw e;
}
},
(e2, e1) -> e1, LinkedHashMap::new));

try {
enterAll(cus.values());
compiler.attribute(new TimedTodo(compiler.todo));
} catch (Throwable t) {
// when symbol entering fails on problems like missing types, attribution can often times proceed
// unhindered, but it sometimes cannot (so attribution is always a BEST EFFORT in the presence of errors)
ctx.getOnError().accept(new JavaParsingException("Failed symbol entering or attribution", t));
}

LinkedHashMap<Input, JCTree.JCCompilationUnit> cus = parseInputsToCompilerAst(sourceFiles, ctx);
return cus.entrySet().stream().map(cuByPath -> {
Input input = cuByPath.getKey();
parsingListener.startedParsing(input);
Expand All @@ -190,6 +222,7 @@ public Stream<SourceFile> parseInputs(Iterable<Input> sourceFiles, @Nullable Pat
ctx,
context);
J.CompilationUnit cu = (J.CompilationUnit) parser.scan(cuByPath.getValue(), Space.EMPTY);
//noinspection DataFlowIssue
cuByPath.setValue(null); // allow memory used by this JCCompilationUnit to be released
parsingListener.parsed(input, cu);
return requirePrintEqualsInput(cu, input, relativeTo, ctx);
Expand All @@ -200,6 +233,53 @@ public Stream<SourceFile> parseInputs(Iterable<Input> sourceFiles, @Nullable Pat
});
}

LinkedHashMap<Input, JCTree.JCCompilationUnit> parseInputsToCompilerAst(Iterable<Input> sourceFiles, ExecutionContext ctx) {
if (classpath != null) { // override classpath
if (context.get(JavaFileManager.class) != pfm) {
throw new IllegalStateException("JavaFileManager has been forked unexpectedly");
}

try {
pfm.setLocation(StandardLocation.CLASS_PATH, classpath.stream().map(Path::toFile).collect(toList()));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

LinkedHashMap<Input, JCTree.JCCompilationUnit> cus = new LinkedHashMap<>();
List<Java8ParserInputFileObject> inputFileObjects = acceptedInputs(sourceFiles)
.map(input -> new Java8ParserInputFileObject(input, ctx))
.collect(toList());
if (!annotationProcessors.isEmpty()) {
compiler.initProcessAnnotations(annotationProcessors);
}
try {
//noinspection unchecked
com.sun.tools.javac.util.List<JCTree.JCCompilationUnit> jcCompilationUnits = compiler.parseFiles((List<JavaFileObject>) (List<?>) inputFileObjects);
for (int i = 0; i < inputFileObjects.size(); i++) {
cus.put(inputFileObjects.get(i).getInput(), jcCompilationUnits.get(i));
}
try {
enterAll(cus.values());
JavaCompiler delegate = annotationProcessors.isEmpty() ? compiler : compiler.processAnnotations(jcCompilationUnits, nil());
delegate.attribute(delegate.todo);
} catch (Throwable t) {
// when symbol entering fails on problems like missing types, attribution can often times proceed
// unhindered, but it sometimes cannot (so attribution is always best-effort in the presence of errors)
ctx.getOnError().accept(new JavaParsingException("Failed symbol entering or attribution", t));
}
} catch (IllegalStateException e) {
if ("endPosTable already set".equals(e.getMessage())) {
throw new IllegalStateException(
"Call reset() on JavaParser before parsing another set of source files that " +
"have some of the same fully qualified names.", e);
}
throw e;
}

return cus;
}

@Override
public ReloadableJava8Parser reset() {
typeCache.clear();
Expand Down Expand Up @@ -261,35 +341,6 @@ public void reset(Collection<URI> uris) {
}
}

private static class TimedTodo extends Todo {
private final Todo todo;
private Timer.@Nullable Sample sample;

private TimedTodo(Todo todo) {
super(new Context());
this.todo = todo;
}

@Override
public boolean isEmpty() {
if (sample != null) {
sample.stop(MetricsHelper.successTags(
Timer.builder("rewrite.parse")
.description("The time spent by the JDK in type attributing the source file")
.tag("file.type", "Java")
.tag("step", "(2) Type attribution"))
.register(Metrics.globalRegistry));
}
return todo.isEmpty();
}

@Override
public Env<AttrContext> remove() {
this.sample = Timer.start();
return todo.remove();
}
}

private static class ByteArrayCapableJavacFileManager extends JavacFileManager {
private final List<PackageAwareJavaFileObject> classByteClasspath;

Expand Down Expand Up @@ -332,6 +383,7 @@ public Iterable<JavaFileObject> list(Location location, String packageName, Set<

private static class PackageAwareJavaFileObject extends SimpleJavaFileObject {
private final String pkg;
@Getter
private final String className;
private final byte[] classBytes;

Expand Down Expand Up @@ -365,10 +417,6 @@ public String getPackage() {
return pkg;
}

public String getClassName() {
return className;
}

@Override
public InputStream openInputStream() {
return new ByteArrayInputStream(classBytes);
Expand Down
Loading
Loading