diff options
-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>(); |