aboutsummaryrefslogtreecommitdiff
path: root/src/delombok
diff options
context:
space:
mode:
authorReinier Zwitserloot <reinier@tipit.to>2009-11-27 14:44:51 +0100
committerReinier Zwitserloot <reinier@tipit.to>2009-11-27 14:44:51 +0100
commit3aa55579ccb47c0cd4fb7f2737fea8f3154a39a1 (patch)
treee6317a1bf6e61018e62fe5fe9d2c696fb2e3f0e8 /src/delombok
parent5a9dff08393e7767a7dbe5ca483764614aa1ac54 (diff)
downloadlombok-3aa55579ccb47c0cd4fb7f2737fea8f3154a39a1.tar.gz
lombok-3aa55579ccb47c0cd4fb7f2737fea8f3154a39a1.tar.bz2
lombok-3aa55579ccb47c0cd4fb7f2737fea8f3154a39a1.zip
Delombok now auto-discovers and uses tools.jar from your java installation.
Diffstat (limited to 'src/delombok')
-rw-r--r--src/delombok/lombok/delombok/Delombok.java14
-rw-r--r--src/delombok/lombok/delombok/DelombokApp.java92
2 files changed, 92 insertions, 14 deletions
diff --git a/src/delombok/lombok/delombok/Delombok.java b/src/delombok/lombok/delombok/Delombok.java
index 2daa9af7..6e7fccaf 100644
--- a/src/delombok/lombok/delombok/Delombok.java
+++ b/src/delombok/lombok/delombok/Delombok.java
@@ -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.")
diff --git a/src/delombok/lombok/delombok/DelombokApp.java b/src/delombok/lombok/delombok/DelombokApp.java
new file mode 100644
index 00000000..e1b7604f
--- /dev/null
+++ b/src/delombok/lombok/delombok/DelombokApp.java
@@ -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";
+ }
+}
+