diff options
author | Reinier Zwitserloot <reinier@tipit.to> | 2009-12-08 06:07:23 +0100 |
---|---|---|
committer | Reinier Zwitserloot <reinier@tipit.to> | 2009-12-08 06:07:23 +0100 |
commit | 09b339db4747f889683c96d15267cbe80b64e315 (patch) | |
tree | cb59890b6c70abc08ffeba13abe6de597fe67bdf | |
parent | c3a3c09f4fd00b83814c1949fa5282ac9c595fa3 (diff) | |
parent | d5e7ae61c0d820be49a181a3f1755d84c0908058 (diff) | |
download | lombok-09b339db4747f889683c96d15267cbe80b64e315.tar.gz lombok-09b339db4747f889683c96d15267cbe80b64e315.tar.bz2 lombok-09b339db4747f889683c96d15267cbe80b64e315.zip |
Merge branch 'master' of git@github.com:rzwitserloot/lombok
-rw-r--r-- | src/delombok/lombok/delombok/Delombok.java | 8 | ||||
-rw-r--r-- | src/delombok/lombok/delombok/DelombokApp.java | 167 | ||||
-rw-r--r-- | test/delombok/resource/after/DataIgnore.java | 30 | ||||
-rw-r--r-- | test/delombok/resource/after/GetterWithDollar.java | 15 | ||||
-rw-r--r-- | test/delombok/resource/after/SetterWithDollar.java | 15 | ||||
-rw-r--r-- | test/delombok/resource/before/DataIgnore.java | 4 | ||||
-rw-r--r-- | test/delombok/resource/before/GetterWithDollar.java | 9 | ||||
-rw-r--r-- | test/delombok/resource/before/SetterWithDollar.java | 9 |
8 files changed, 177 insertions, 80 deletions
diff --git a/src/delombok/lombok/delombok/Delombok.java b/src/delombok/lombok/delombok/Delombok.java index f6a62bef..d9d21d95 100644 --- a/src/delombok/lombok/delombok/Delombok.java +++ b/src/delombok/lombok/delombok/Delombok.java @@ -35,6 +35,8 @@ import java.nio.charset.UnsupportedCharsetException; import java.util.ArrayList; import java.util.List; +import javax.tools.JavaFileObject; + import lombok.delombok.CommentPreservingParser.ParseResult; import com.zwitserloot.cmdreader.CmdReader; @@ -248,10 +250,14 @@ public class Delombok { in.close(); } } + + public void delombok(JavaFileObject file, Writer writer) throws IOException { + ParseResult result = parser.parse(file, force); + result.print(writer); + } public void delombok(String file, Writer writer) throws IOException { ParseResult result = parser.parse(file, force); - result.print(writer); } diff --git a/src/delombok/lombok/delombok/DelombokApp.java b/src/delombok/lombok/delombok/DelombokApp.java index 76d5aed3..b48337c8 100644 --- a/src/delombok/lombok/delombok/DelombokApp.java +++ b/src/delombok/lombok/delombok/DelombokApp.java @@ -25,98 +25,107 @@ public class DelombokApp implements LombokApp { 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. - final File toolsJar = findToolsJar(); - if (toolsJar == null) { + Class<?> delombokClass = loadDelombok(); + if (delombokClass == null) { System.err.println("Can't find tools.jar. Rerun delombok with tools.jar on the classpath."); return 1; } - - final JarFile toolsJarFile = new JarFile(toolsJar); - - ClassLoader loader = new ClassLoader() { - private Class<?>loadStreamAsClass(String name, boolean resolve, InputStream in) throws ClassNotFoundException { + try { + loadDelombok().getMethod("main", String[].class).invoke(null, new Object[] {args.toArray(new String[0])}); + } 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; + } + } + + public static Class<?> loadDelombok() throws Exception { + //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. + final File toolsJar = findToolsJar(); + if (toolsJar == null) { + System.err.println("Can't find tools.jar. Rerun delombok with tools.jar on the classpath."); + return null; + } + + final JarFile toolsJarFile = new JarFile(toolsJar); + + ClassLoader loader = new ClassLoader() { + private Class<?>loadStreamAsClass(String name, boolean resolve, InputStream in) throws ClassNotFoundException { + try { try { - try { - byte[] b = new byte[65536]; - ByteArrayOutputStream out = new ByteArrayOutputStream(); - while (true) { - int r = in.read(b); - if (r == -1) break; - out.write(b, 0, r); - } - in.close(); - byte[] data = out.toByteArray(); - Class<?> c = defineClass(name, data, 0, data.length); - if (resolve) resolveClass(c); - return c; - } finally { - in.close(); + byte[] b = new byte[65536]; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + while (true) { + int r = in.read(b); + if (r == -1) break; + out.write(b, 0, r); } - } catch (IOException e2) { - throw new ClassNotFoundException(name, e2); + in.close(); + byte[] data = out.toByteArray(); + Class<?> c = defineClass(name, data, 0, data.length); + if (resolve) resolveClass(c); + return c; + } finally { + in.close(); } + } catch (IOException e2) { + throw new ClassNotFoundException(name, e2); } - - @Override protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { - String rawName = name.replace(".", "/") + ".class"; - JarEntry entry = toolsJarFile.getJarEntry(rawName); - if (entry == null) { - if (name.startsWith("lombok.")) return loadStreamAsClass(name, resolve, super.getResourceAsStream(rawName)); - return super.loadClass(name, resolve); - } - - try { - return loadStreamAsClass(name, resolve, toolsJarFile.getInputStream(entry)); - } catch (IOException e2) { - throw new ClassNotFoundException(name, e2); - } + } + + @Override protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { + String rawName = name.replace(".", "/") + ".class"; + JarEntry entry = toolsJarFile.getJarEntry(rawName); + if (entry == null) { + if (name.startsWith("lombok.")) return loadStreamAsClass(name, resolve, super.getResourceAsStream(rawName)); + return super.loadClass(name, resolve); } - @Override public URL getResource(String name) { - JarEntry entry = toolsJarFile.getJarEntry(name); - if (entry == null) return super.getResource(name); - try { - return new URL("jar:file:" + toolsJar.getAbsolutePath() + "!" + name); - } catch (MalformedURLException ignore) { - return null; - } + try { + return loadStreamAsClass(name, resolve, toolsJarFile.getInputStream(entry)); + } catch (IOException e2) { + throw new ClassNotFoundException(name, e2); } - - @Override public Enumeration<URL> getResources(final String name) throws IOException { - JarEntry entry = toolsJarFile.getJarEntry(name); - final Enumeration<URL> parent = super.getResources(name); - if (entry == null) return super.getResources(name); - return new Enumeration<URL>() { - private boolean first = false; - @Override public boolean hasMoreElements() { - return !first || parent.hasMoreElements(); - } - - @Override public URL nextElement() { - if (!first) { - first = true; - try { - return new URL("jar:file:" + toolsJar.getAbsolutePath() + "!" + name); - } catch (MalformedURLException ignore) { - return parent.nextElement(); - } + } + + @Override public URL getResource(String name) { + JarEntry entry = toolsJarFile.getJarEntry(name); + if (entry == null) return super.getResource(name); + try { + return new URL("jar:file:" + toolsJar.getAbsolutePath() + "!" + name); + } catch (MalformedURLException ignore) { + return null; + } + } + + @Override public Enumeration<URL> getResources(final String name) throws IOException { + JarEntry entry = toolsJarFile.getJarEntry(name); + final Enumeration<URL> parent = super.getResources(name); + if (entry == null) return super.getResources(name); + return new Enumeration<URL>() { + private boolean first = false; + @Override public boolean hasMoreElements() { + return !first || parent.hasMoreElements(); + } + + @Override public URL nextElement() { + if (!first) { + first = true; + try { + return new URL("jar:file:" + toolsJar.getAbsolutePath() + "!" + name); + } catch (MalformedURLException ignore) { + return parent.nextElement(); } - return parent.nextElement(); } - }; - } - }; - try { - loader.loadClass("lombok.delombok.Delombok").getMethod("main", String[].class).invoke(null, new Object[] {args.toArray(new String[0])}); - } catch (InvocationTargetException e1) { - Throwable t = e1.getCause(); - if (t instanceof Error) throw (Error)t; - if (t instanceof Exception) throw (Exception)t; - throw e1; + return parent.nextElement(); + } + }; } - return 0; - } + }; + return loader.loadClass("lombok.delombok.Delombok"); } private void runDirectly(List<String> args) { diff --git a/test/delombok/resource/after/DataIgnore.java b/test/delombok/resource/after/DataIgnore.java new file mode 100644 index 00000000..706d7b59 --- /dev/null +++ b/test/delombok/resource/after/DataIgnore.java @@ -0,0 +1,30 @@ +class DataIgnore { + final int x; + String $name; + public DataIgnore(final int x) { + this.x = x; + } + public int getX() { + return x; + } + @java.lang.Override + public boolean equals(final java.lang.Object o) { + if (o == this) return true; + if (o == null) return false; + if (o.getClass() != this.getClass()) return false; + final DataIgnore other = (DataIgnore)o; + if (this.x != other.x) return false; + return true; + } + @java.lang.Override + public int hashCode() { + final int PRIME = 31; + int result = 1; + result = result * PRIME + this.x; + return result; + } + @java.lang.Override + public java.lang.String toString() { + return "DataIgnore(x=" + x + ")"; + } +} diff --git a/test/delombok/resource/after/GetterWithDollar.java b/test/delombok/resource/after/GetterWithDollar.java new file mode 100644 index 00000000..56be0249 --- /dev/null +++ b/test/delombok/resource/after/GetterWithDollar.java @@ -0,0 +1,15 @@ +class GetterWithDollar1 { + int $i; + + public int getI() { + return $i; + } +} +class GetterWithDollar2 { + int $i; + int i; + + public int getI() { + return i; + } +}
\ No newline at end of file diff --git a/test/delombok/resource/after/SetterWithDollar.java b/test/delombok/resource/after/SetterWithDollar.java new file mode 100644 index 00000000..c26a1ccd --- /dev/null +++ b/test/delombok/resource/after/SetterWithDollar.java @@ -0,0 +1,15 @@ +class SetterWithDollar1 { + int $i; + + public void setI(final int i) { + this.$i = i; + } +} +class SetterWithDollar2 { + int $i; + int i; + + public void setI(final int i) { + this.i = i; + } +}
\ No newline at end of file diff --git a/test/delombok/resource/before/DataIgnore.java b/test/delombok/resource/before/DataIgnore.java new file mode 100644 index 00000000..5f957bdc --- /dev/null +++ b/test/delombok/resource/before/DataIgnore.java @@ -0,0 +1,4 @@ +@lombok.Data class DataIgnore { + final int x; + String $name; +} diff --git a/test/delombok/resource/before/GetterWithDollar.java b/test/delombok/resource/before/GetterWithDollar.java new file mode 100644 index 00000000..e1ef0818 --- /dev/null +++ b/test/delombok/resource/before/GetterWithDollar.java @@ -0,0 +1,9 @@ +//ignore +class GetterWithDollar1 { + @lombok.Getter int $i; +} + +class GetterWithDollar2 { + @lombok.Getter int $i; + @lombok.Getter int i; +} diff --git a/test/delombok/resource/before/SetterWithDollar.java b/test/delombok/resource/before/SetterWithDollar.java new file mode 100644 index 00000000..c09173b0 --- /dev/null +++ b/test/delombok/resource/before/SetterWithDollar.java @@ -0,0 +1,9 @@ +//ignore +class SetterWithDollar1 { + @lombok.Setter int $i; +} + +class SetterWithDollar2 { + @lombok.Setter int $i; + @lombok.Setter int i; +} |