diff options
author | mszabo-wikia <2721291+mszabo-wikia@users.noreply.github.com> | 2018-06-07 17:45:43 +0200 |
---|---|---|
committer | mszabo-wikia <2721291+mszabo-wikia@users.noreply.github.com> | 2018-06-07 17:45:43 +0200 |
commit | 6311a7efe394bf19cd2a874d29d7d97b26a25a8d (patch) | |
tree | ed7b8cdbbb8b9e4a102b16046887446ce79cdbe9 /src | |
parent | 4256d7daeda177ed3e361eb972fce62d9e1050c5 (diff) | |
download | lombok-6311a7efe394bf19cd2a874d29d7d97b26a25a8d.tar.gz lombok-6311a7efe394bf19cd2a874d29d7d97b26a25a8d.tar.bz2 lombok-6311a7efe394bf19cd2a874d29d7d97b26a25a8d.zip |
Also generify AnnotationProcessor logic
Diffstat (limited to 'src')
-rw-r--r-- | src/core/lombok/core/AnnotationProcessor.java | 28 | ||||
-rw-r--r-- | src/core/lombok/javac/apt/LombokProcessor.java | 1 |
2 files changed, 23 insertions, 6 deletions
diff --git a/src/core/lombok/core/AnnotationProcessor.java b/src/core/lombok/core/AnnotationProcessor.java index d05597b6..04448ecb 100644 --- a/src/core/lombok/core/AnnotationProcessor.java +++ b/src/core/lombok/core/AnnotationProcessor.java @@ -49,6 +49,7 @@ import lombok.patcher.ClassRootFinder; @SupportedAnnotationTypes("*") public class AnnotationProcessor extends AbstractProcessor { + private static String trace(Throwable t) { StringWriter w = new StringWriter(); t.printStackTrace(new PrintWriter(w, true)); @@ -71,18 +72,33 @@ public class AnnotationProcessor extends AbstractProcessor { * the delegate ProcessingEnvironment of the gradle wrapper is returned. */ public static ProcessingEnvironment getJavacProcessingEnvironment(ProcessingEnvironment procEnv, List<String> delayedWarnings) { - final Class<? extends ProcessingEnvironment> procEnvClass = procEnv.getClass(); - if (procEnvClass.getName().equals("org.gradle.api.internal.tasks.compile.processing.IncrementalProcessingEnvironment")) { + ProcessingEnvironment javacProcEnv = tryRecursivelyObtainJavacProcessingEnvironment(procEnv); + + if (javacProcEnv == null) { + delayedWarnings.add("Can't get the delegate of the gradle IncrementalProcessingEnvironment."); + } + + return javacProcEnv; + } + + private static ProcessingEnvironment tryRecursivelyObtainJavacProcessingEnvironment(ProcessingEnvironment procEnv) { + if (procEnv.getClass().getName().equals("com.sun.tools.javac.processing.JavacProcessingEnvironment")) { + return procEnv; + } + + for (Class<?> procEnvClass = procEnv.getClass(); procEnvClass != null; procEnvClass = procEnvClass.getSuperclass()) { try { Field field = procEnvClass.getDeclaredField("delegate"); field.setAccessible(true); Object delegate = field.get(procEnv); - return (ProcessingEnvironment) delegate; + + return tryRecursivelyObtainJavacProcessingEnvironment((ProcessingEnvironment) delegate); } catch (final Exception e) { - delayedWarnings.add("Can't get the delegate of the gradle IncrementalProcessingEnvironment: " + trace(e)); + // no valid delegate, try superclass } } - return procEnv; + + return null; } @@ -96,7 +112,7 @@ public class AnnotationProcessor extends AbstractProcessor { @Override boolean want(ProcessingEnvironment procEnv, List<String> delayedWarnings) { ProcessingEnvironment javacProcEnv = getJavacProcessingEnvironment(procEnv, delayedWarnings); - if (!javacProcEnv.getClass().getName().equals("com.sun.tools.javac.processing.JavacProcessingEnvironment")) return false; + if (javacProcEnv == null) return false; try { ClassLoader classLoader = findAndPatchClassLoader(javacProcEnv); diff --git a/src/core/lombok/javac/apt/LombokProcessor.java b/src/core/lombok/javac/apt/LombokProcessor.java index 124d5b6c..04b494bf 100644 --- a/src/core/lombok/javac/apt/LombokProcessor.java +++ b/src/core/lombok/javac/apt/LombokProcessor.java @@ -451,6 +451,7 @@ public class LombokProcessor extends AbstractProcessor { "Can't get a JavacFiler from " + filer.getClass().getName() + ". Lombok won't work."); return null; } + private Object tryGetDelegateField(Class<?> delegateClass, Object instance) throws Exception { Field field = delegateClass.getDeclaredField("delegate"); field.setAccessible(true); |