From 1007940bd993a18b12f444d0867264f41a5fff11 Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Wed, 6 Nov 2024 19:31:37 +0100 Subject: [PATCH] Fix Lombok initialization in Java parser (#4647) * Fix Lombok initialization in Java parser * Fix path calculation to work on Windows --------- Co-authored-by: Sam Snyder --- .../java/isolated/ReloadableJava17Parser.java | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17Parser.java b/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17Parser.java index c7d9bf60a13..a5301b05e19 100644 --- a/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17Parser.java +++ b/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17Parser.java @@ -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; @@ -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 overrideClasspath = new ArrayList<>(); - for (String part : systemClasspath.split("\\s*" + (File.pathSeparatorChar == ';' ? ";" : ":") + "\\s*")) { - if (part.contains("lombok")) { - overrideClasspath.add(part); - } + List 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(