Skip to content

Commit

Permalink
Fixes issue 422: VerifyError when running delombok in a javac7 enviro…
Browse files Browse the repository at this point in the history
…nment.
  • Loading branch information
rzwitserloot committed Oct 22, 2012
1 parent a295c4f commit bde859c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/changelog.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Lombok Changelog

### v0.11.5 (Edgy Guinea Pig)
* FEATURE: Lombok can be disabled entirely for any given compile run by using JVM switch `-Dlombok.disable`. This might be useful for code style checkers and such.
* BUGFIX: {Delombok} Running delombok has been causing VerifyError errors when used with javac 1.7 since 0.11.0.

### v0.11.4 (August 13th, 2012)
* FEATURE: {Experimental} `@Value`, `@Wither` and `@FieldDefaults` are now available. These are a lot like `@Data` but geared towards immutable classes. [Documentation on @Value](http://projectlombok.org/features/experimental/Value.html), [Documentation on @Wither](http://projectlombok.org/features/experimental/Wither.html) and [Documentation on @FieldDefaults](http://projectlombok.org/features/experimental/FieldDefaults.html).
Expand Down
51 changes: 49 additions & 2 deletions src/delombok/lombok/delombok/Delombok.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.Writer;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
Expand All @@ -44,9 +46,11 @@
import javax.tools.DiagnosticListener;
import javax.tools.JavaFileObject;

import lombok.Lombok;
import lombok.javac.CommentCatcher;
import lombok.javac.LombokOptions;

import com.sun.tools.javac.comp.Todo;
import com.sun.tools.javac.main.JavaCompiler;
import com.sun.tools.javac.main.OptionName;
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
Expand Down Expand Up @@ -378,14 +382,13 @@ public boolean delombok() throws IOException {
baseMap.put(unit, fileToBase.get(fileToParse));
roots.add(unit);
}

if (compiler.errorCount() > 0) {
// At least one parse error. No point continuing (a real javac run doesn't either).
return false;
}

JavaCompiler delegate = compiler.processAnnotations(compiler.enterTrees(toJavacList(roots)));
delegate.flow(delegate.attribute(delegate.todo));
callFlowMethodOnJavaCompiler(delegate, callAttributeMethodOnJavaCompiler(delegate, delegate.todo));
for (JCCompilationUnit unit : roots) {
DelombokResult result = new DelombokResult(catcher.getComments(unit), unit, force || options.isChanged(unit));
if (verbose) feedback.printf("File: %s [%s]\n", unit.sourcefile.getName(), result.isChanged() ? "delomboked" : "unchanged");
Expand All @@ -409,6 +412,50 @@ public boolean delombok() throws IOException {
return true;
}

private static Method attributeMethod;
/** Method is needed because the call signature has changed between javac6 and javac7; no matter what we compile against, using delombok in the other means VerifyErrors. */
private static Object callAttributeMethodOnJavaCompiler(JavaCompiler compiler, Todo arg) {
if (attributeMethod == null) {
try {
attributeMethod = JavaCompiler.class.getDeclaredMethod("attribute", java.util.Queue.class);
} catch (NoSuchMethodException e) {
try {
attributeMethod = JavaCompiler.class.getDeclaredMethod("attribute", com.sun.tools.javac.util.ListBuffer.class);
} catch (NoSuchMethodException e2) {
throw Lombok.sneakyThrow(e2);
}
}
}
try {
return attributeMethod.invoke(compiler, arg);
} catch (Exception e) {
if (e instanceof InvocationTargetException) throw Lombok.sneakyThrow(e.getCause());
throw Lombok.sneakyThrow(e);
}
}

private static Method flowMethod;
/** Method is needed because the call signature has changed between javac6 and javac7; no matter what we compile against, using delombok in the other means VerifyErrors. */
private static void callFlowMethodOnJavaCompiler(JavaCompiler compiler, Object arg) {
if (flowMethod == null) {
try {
flowMethod = JavaCompiler.class.getDeclaredMethod("flow", java.util.Queue.class);
} catch (NoSuchMethodException e) {
try {
flowMethod = JavaCompiler.class.getDeclaredMethod("flow", com.sun.tools.javac.util.List.class);
} catch (NoSuchMethodException e2) {
throw Lombok.sneakyThrow(e2);
}
}
}
try {
flowMethod.invoke(compiler, arg);
} catch (Exception e) {
if (e instanceof InvocationTargetException) throw Lombok.sneakyThrow(e.getCause());
throw Lombok.sneakyThrow(e);
}
}

private static String canonical(File dir) {
try {
return dir.getCanonicalPath();
Expand Down

0 comments on commit bde859c

Please sign in to comment.