diff options
author | Roel Spilker <r.spilker@gmail.com> | 2011-02-15 00:03:50 +0100 |
---|---|---|
committer | Roel Spilker <r.spilker@gmail.com> | 2011-02-15 00:07:34 +0100 |
commit | 2bacfa62a330e1274ee89c76fa22a36116f8fa3d (patch) | |
tree | ae91080908815ff8b92f9573ac1cfe8c3428c484 | |
parent | ed4068d84289390c74ca2e45149c9a22f96ce764 (diff) | |
download | lombok-2bacfa62a330e1274ee89c76fa22a36116f8fa3d.tar.gz lombok-2bacfa62a330e1274ee89c76fa22a36116f8fa3d.tar.bz2 lombok-2bacfa62a330e1274ee89c76fa22a36116f8fa3d.zip |
Should be a fix for issue #177: NoClassDefFoundErrors for JavacProcessingEnvironment would occur when using the m2eclipse plugin, for example when removing an entry from the build path.
The actual change is small but this took quite some searching. m2eclipse uses plexus, and plexus uses a custom classloader, which means lombok can't find the JavacProcessingEnvironment loaded by that classloader.
We fix it here by adding lombok to that custom classloader. Perhaps more die-hard m2eclipse users find a problem with this approach, but assuming these plexus compile runs are stand-alone, this should work great.
fixed by Roel and Reinier.
-rw-r--r-- | src/core/lombok/core/AnnotationProcessor.java | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/src/core/lombok/core/AnnotationProcessor.java b/src/core/lombok/core/AnnotationProcessor.java index ae47545c..ad7c58b9 100644 --- a/src/core/lombok/core/AnnotationProcessor.java +++ b/src/core/lombok/core/AnnotationProcessor.java @@ -21,12 +21,17 @@ */ package lombok.core; +import java.io.File; import java.io.PrintWriter; import java.io.StringWriter; +import java.lang.reflect.Method; +import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Map; import java.util.Set; +import java.util.WeakHashMap; import javax.annotation.processing.AbstractProcessor; import javax.annotation.processing.ProcessingEnvironment; @@ -39,6 +44,8 @@ import javax.lang.model.element.Element; import javax.lang.model.element.TypeElement; import javax.tools.Diagnostic.Kind; +import lombok.patcher.inject.LiveInjector; + @SupportedAnnotationTypes("*") @SupportedSourceVersion(SourceVersion.RELEASE_6) public class AnnotationProcessor extends AbstractProcessor { @@ -58,6 +65,8 @@ public class AnnotationProcessor extends AbstractProcessor { private final List<ProcessorDescriptor> active = new ArrayList<ProcessorDescriptor>(); private final List<String> delayedWarnings = new ArrayList<String>(); + private static final Map<ClassLoader, Boolean> lombokAlreadyAddedTo = new WeakHashMap<ClassLoader, Boolean>(); + static class JavacDescriptor extends ProcessorDescriptor { private Processor processor; @@ -69,7 +78,15 @@ public class AnnotationProcessor extends AbstractProcessor { if (!procEnv.getClass().getName().equals("com.sun.tools.javac.processing.JavacProcessingEnvironment")) return false; try { - processor = (Processor)Class.forName("lombok.javac.apt.Processor").newInstance(); + ClassLoader toFix = procEnv.getClass().getClassLoader(); + if (toFix.getClass().getCanonicalName().equals("org.codehaus.plexus.compiler.javac.IsolatedClassLoader")) { + if (lombokAlreadyAddedTo.put(toFix, true) == null) { + Method m = toFix.getClass().getDeclaredMethod("addURL", URL.class); + URL selfUrl = new File(LiveInjector.findPathJar(AnnotationProcessor.class)).toURI().toURL(); + m.invoke(toFix, selfUrl); + } + } + processor = (Processor)Class.forName("lombok.javac.apt.Processor", false, procEnv.getClass().getClassLoader()).newInstance(); } catch (Exception e) { delayedWarnings.add("You found a bug in lombok; lombok.javac.apt.Processor is not available. Lombok will not run during this compilation: " + trace(e)); return false; @@ -77,8 +94,15 @@ public class AnnotationProcessor extends AbstractProcessor { delayedWarnings.add("Can't load javac processor due to (most likely) a class loader problem: " + trace(e)); return false; } - - processor.init(procEnv); + try { + processor.init(procEnv); + } catch (Exception e) { + delayedWarnings.add("lombok.javac.apt.Processor could not be initialized. Lombok will not run during this compilation: " + trace(e)); + return false; + } catch (NoClassDefFoundError e) { + delayedWarnings.add("Can't initialize javac processor due to (most likely) a class loader problem: " + trace(e)); + return false; + } return true; } |