diff options
author | Reinier Zwitserloot <reinier@zwitserloot.com> | 2010-10-01 03:19:48 +0200 |
---|---|---|
committer | Reinier Zwitserloot <reinier@zwitserloot.com> | 2010-10-01 03:19:48 +0200 |
commit | 5a24e6f9f2caaa154ce862ec51ffc1b56ab4d9d3 (patch) | |
tree | 594868a54899ac173c31e33d93edefc2188c85d4 | |
parent | 05c97e8bfa25dd577a21b680760b2736fc541849 (diff) | |
download | lombok-5a24e6f9f2caaa154ce862ec51ffc1b56ab4d9d3.tar.gz lombok-5a24e6f9f2caaa154ce862ec51ffc1b56ab4d9d3.tar.bz2 lombok-5a24e6f9f2caaa154ce862ec51ffc1b56ab4d9d3.zip |
Fix for "ZIP file closed" errors caused by the postprocessor trying to load more lombok classes to do its job, after javac has already closed the classloader via a hack. We hack the hack to stop javac from closing only _our_ (that is, lombok.jar's) classloader.
Co-Author: Roel
-rw-r--r-- | src/core/lombok/javac/apt/Processor.java | 64 |
1 files changed, 63 insertions, 1 deletions
diff --git a/src/core/lombok/javac/apt/Processor.java b/src/core/lombok/javac/apt/Processor.java index ab445309..1d3d0c34 100644 --- a/src/core/lombok/javac/apt/Processor.java +++ b/src/core/lombok/javac/apt/Processor.java @@ -21,8 +21,12 @@ */ package lombok.javac.apt; +import java.io.IOException; +import java.io.InputStream; import java.lang.reflect.Field; import java.lang.reflect.Method; +import java.net.URL; +import java.util.Enumeration; import java.util.IdentityHashMap; import java.util.Map; import java.util.Set; @@ -74,8 +78,10 @@ public class Processor extends AbstractProcessor { transformer = new JavacTransformer(procEnv.getMessager()); trees = Trees.instance(procEnv); } - + private void placePostCompileHook() { + stopJavacProcessingEnvironmentFromClosingOurClassloader(); + Context context = processingEnv.getContext(); try { @@ -100,6 +106,62 @@ public class Processor extends AbstractProcessor { } } + private static class WrappingClassLoader extends ClassLoader { + private final ClassLoader parent; + + public WrappingClassLoader(ClassLoader parent) { + this.parent = parent; + } + + public Class<?> loadClass(String name) throws ClassNotFoundException { + return parent.loadClass(name); + } + + public String toString() { + return parent.toString(); + } + + public URL getResource(String name) { + return parent.getResource(name); + } + + public Enumeration<URL> getResources(String name) throws IOException { + return parent.getResources(name); + } + + public InputStream getResourceAsStream(String name) { + return parent.getResourceAsStream(name); + } + + public void setDefaultAssertionStatus(boolean enabled) { + parent.setDefaultAssertionStatus(enabled); + } + + public void setPackageAssertionStatus(String packageName, boolean enabled) { + parent.setPackageAssertionStatus(packageName, enabled); + } + + public void setClassAssertionStatus(String className, boolean enabled) { + parent.setClassAssertionStatus(className, enabled); + } + + public void clearAssertionStatus() { + parent.clearAssertionStatus(); + } + } + + private void stopJavacProcessingEnvironmentFromClosingOurClassloader() { + try { + Field f = JavacProcessingEnvironment.class.getDeclaredField("processorClassLoader"); + f.setAccessible(true); + ClassLoader unwrapped = (ClassLoader) f.get(processingEnv); + ClassLoader wrapped = new WrappingClassLoader(unwrapped); + f.set(processingEnv, wrapped); + } catch (Throwable t) { + throw Lombok.sneakyThrow(t); + } + } + /** {@inheritDoc} */ @Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { IdentityHashMap<JCCompilationUnit, Void> units = new IdentityHashMap<JCCompilationUnit, Void>(); |