Skip to content

Commit

Permalink
Fix Lombok initialization in Java parser (#4647)
Browse files Browse the repository at this point in the history
* Fix Lombok initialization in Java parser

* Fix path calculation to work on Windows

---------

Co-authored-by: Sam Snyder <[email protected]>
  • Loading branch information
knutwannheden and sambsnyd authored Nov 6, 2024
1 parent 420f56d commit 1007940
Showing 1 changed file with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@
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.stream.Stream;
Expand Down Expand Up @@ -119,27 +121,31 @@ private ReloadableJava17Parser(
Processor lombokProcessor = null;
try {
// https://projectlombok.org/contributing/lombok-execution-path
String systemClasspath = System.getProperty("java.class.path");
if (systemClasspath != null && !systemClasspath.isEmpty()) {
List<String> overrideClasspath = new ArrayList<>();
for (String part : systemClasspath.split("\\s*" + (File.pathSeparatorChar == ';' ? ";" : ":") + "\\s*")) {
if (part.contains("lombok")) {
overrideClasspath.add(part);
}
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;
}
}
// 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) {
}
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));
}
System.setProperty("shadow.override.lombok", String.join(File.pathSeparator, overrideClasspath));

Class<?> shadowLoaderClass = Class.forName("lombok.launch.ShadowClassLoader", true, getClass().getClassLoader());
Constructor<?> shadowLoaderConstructor = shadowLoaderClass.getDeclaredConstructor(
Expand Down

0 comments on commit 1007940

Please sign in to comment.