mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-14 08:18:29 +00:00
Delombok now auto-discovers and uses tools.jar from your java installation.
This commit is contained in:
1 parent
5a9dff0839
commit
3aa55579cc
3 files changed
+99
-15
No files matched your search
@@ -35,9 +35,6 @@ import java.nio.charset.UnsupportedCharsetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.mangosdk.spi.ProviderFor;
|
||||
|
||||
import lombok.core.LombokApp;
|
||||
import lombok.delombok.CommentPreservingParser.ParseResult;
|
||||
|
||||
import com.zwitserloot.cmdreader.CmdReader;
|
||||
@@ -59,17 +56,6 @@ public class Delombok {
|
||||
/** If null, output to standard out. */
|
||||
private File output = null;
|
||||
|
||||
@ProviderFor(LombokApp.class)
|
||||
public static class DelombokApp implements LombokApp {
|
||||
@Override public void runApp(String[] args) {
|
||||
Delombok.main(args);
|
||||
}
|
||||
|
||||
@Override public String getAppName() {
|
||||
return "delombok";
|
||||
}
|
||||
}
|
||||
|
||||
private static class CmdArgs {
|
||||
@Shorthand("v")
|
||||
@Description("Print the name of each file as it is being delombok-ed.")
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
package lombok.delombok;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
|
||||
import lombok.core.LombokApp;
|
||||
|
||||
import org.mangosdk.spi.ProviderFor;
|
||||
|
||||
@ProviderFor(LombokApp.class)
|
||||
public class DelombokApp implements LombokApp {
|
||||
@Override public int runApp(String[] args) throws Exception {
|
||||
try {
|
||||
Class.forName("com.sun.tools.javac.main.JavaCompiler");
|
||||
runDirectly(args);
|
||||
return 0;
|
||||
} catch (ClassNotFoundException e) {
|
||||
//tools.jar is probably not on the classpath. We're going to try and find it, and then load the rest via a ClassLoader that includes tools.jar.
|
||||
File toolsJar = findToolsJar();
|
||||
if (toolsJar == null) {
|
||||
System.err.println("Can't find tools.jar. Rerun delombok with tools.jar on the classpath.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
URLClassLoader loader = new URLClassLoader(new URL[] {toolsJar.toURI().toURL()});
|
||||
try {
|
||||
loader.loadClass("lombok.delombok.Delombok").getMethod("main", String[].class).invoke(null, new Object[] {args});
|
||||
} catch (InvocationTargetException e1) {
|
||||
Throwable t = e1.getCause();
|
||||
if (t instanceof Error) throw (Error)t;
|
||||
if (t instanceof Exception) throw (Exception)t;
|
||||
throw e1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
private void runDirectly(String[] args) {
|
||||
Delombok.main(args);
|
||||
}
|
||||
|
||||
private static File findToolsJar() {
|
||||
try {
|
||||
File toolsJar = findToolsJarViaRT();
|
||||
if (toolsJar != null) return toolsJar;
|
||||
} catch (Throwable ignore) {}
|
||||
|
||||
return findToolsJarViaProperties();
|
||||
}
|
||||
|
||||
private static File findToolsJarViaProperties() {
|
||||
File home = new File(System.getProperty("java.home", "."));
|
||||
File toolsJar = checkToolsJar(home);
|
||||
if (toolsJar != null) return toolsJar;
|
||||
toolsJar = checkToolsJar(new File(home, "lib"));
|
||||
if (toolsJar != null) return toolsJar;
|
||||
toolsJar = checkToolsJar(new File(home.getParentFile(), "lib"));
|
||||
if (toolsJar != null) return toolsJar;
|
||||
toolsJar = checkToolsJar(new File(new File(home, "jdk"), "lib"));
|
||||
if (toolsJar != null) return toolsJar;
|
||||
return null;
|
||||
}
|
||||
|
||||
private static File findToolsJarViaRT() {
|
||||
String url = ClassLoader.getSystemClassLoader().getResource("java/lang/String.class").toString();
|
||||
if (!url.startsWith("jar:file:")) return null;
|
||||
url = url.substring("jar:file:".length());
|
||||
int idx = url.indexOf('!');
|
||||
if (idx == -1) return null;
|
||||
url = url.substring(0, idx);
|
||||
|
||||
File toolsJar = checkToolsJar(new File(url).getParentFile());
|
||||
if (toolsJar != null) return toolsJar;
|
||||
toolsJar = checkToolsJar(new File(new File(url).getParentFile().getParentFile().getParentFile(), "lib"));
|
||||
if (toolsJar != null) return toolsJar;
|
||||
return null;
|
||||
}
|
||||
|
||||
private static File checkToolsJar(File d) {
|
||||
if (d.getName().equals("tools.jar") && d.isFile() && d.canRead()) return d;
|
||||
d = new File(d, "tools.jar");
|
||||
if (d.getName().equals("tools.jar") && d.isFile() && d.canRead()) return d;
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override public String getAppName() {
|
||||
return "delombok";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,7 +113,13 @@ public class Installer {
|
||||
}
|
||||
for (LombokApp app : services) {
|
||||
if (appName.equals(app.getAppName())) {
|
||||
app.runApp(newArgs);
|
||||
try {
|
||||
int returnCode = app.runApp(newArgs);
|
||||
System.exit(returnCode);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.exit(10);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user