diff options
-rw-r--r-- | src/core/lombok/core/AnnotationProcessor.java | 51 | ||||
-rw-r--r-- | src/core/lombok/javac/apt/LombokProcessor.java | 69 |
2 files changed, 98 insertions, 22 deletions
diff --git a/src/core/lombok/core/AnnotationProcessor.java b/src/core/lombok/core/AnnotationProcessor.java index d4a92408..c72d1bb2 100644 --- a/src/core/lombok/core/AnnotationProcessor.java +++ b/src/core/lombok/core/AnnotationProcessor.java @@ -26,8 +26,9 @@ import static lombok.core.Augments.ClassLoader_lombokAlreadyAddedTo; import java.io.File; import java.io.PrintWriter; import java.io.StringWriter; -import java.lang.reflect.Field; +import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; +import java.lang.reflect.Proxy; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; @@ -83,15 +84,11 @@ public class AnnotationProcessor extends AbstractProcessor { for (Class<?> procEnvClass = procEnv.getClass(); procEnvClass != null; procEnvClass = procEnvClass.getSuperclass()) { try { - Field field; - try { - field = Permit.getField(procEnvClass, "delegate"); - } catch (NoSuchFieldException e) { - field = Permit.getField(procEnvClass, "processingEnv"); - } - Object delegate = field.get(procEnv); - - return tryRecursivelyObtainJavacProcessingEnvironment((ProcessingEnvironment) delegate); + Object delegate = tryGetDelegateField(procEnvClass, procEnv); + if (delegate == null) delegate = tryGetProcessingEnvField(procEnvClass, procEnv); + if (delegate == null) delegate = tryGetProxyDelegateToField(procEnvClass, procEnv); + + if (delegate != null) return tryRecursivelyObtainJavacProcessingEnvironment((ProcessingEnvironment) delegate); } catch (final Exception e) { // no valid delegate, try superclass } @@ -100,6 +97,40 @@ public class AnnotationProcessor extends AbstractProcessor { return null; } + /** + * Gradle incremental processing + */ + private static Object tryGetDelegateField(Class<?> delegateClass, Object instance) { + try { + return Permit.getField(delegateClass, "delegate").get(instance); + } catch (Exception e) { + return null; + } + } + + /** + * Kotlin incremental processing + */ + private static Object tryGetProcessingEnvField(Class<?> delegateClass, Object instance) { + try { + return Permit.getField(delegateClass, "processingEnv").get(instance); + } catch (Exception e) { + return null; + } + } + + /** + * InteliJ >= 2020.3 + */ + private static Object tryGetProxyDelegateToField(Class<?> delegateClass, Object instance) { + try { + InvocationHandler handler = Proxy.getInvocationHandler(instance); + return Permit.getField(handler.getClass(), "val$delegateTo").get(handler); + } catch (Exception e) { + return null; + } + } + static class JavacDescriptor extends ProcessorDescriptor { private Processor processor; diff --git a/src/core/lombok/javac/apt/LombokProcessor.java b/src/core/lombok/javac/apt/LombokProcessor.java index 852e5de6..67df5b20 100644 --- a/src/core/lombok/javac/apt/LombokProcessor.java +++ b/src/core/lombok/javac/apt/LombokProcessor.java @@ -24,8 +24,10 @@ package lombok.javac.apt; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Field; +import java.lang.reflect.InvocationHandler; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.lang.reflect.Proxy; import java.net.URL; import java.util.ArrayList; import java.util.Enumeration; @@ -432,11 +434,12 @@ public class LombokProcessor extends AbstractProcessor { // try to find a "delegate" field in the object, and use this to try to obtain a JavacProcessingEnvironment for (Class<?> procEnvClass = procEnv.getClass(); procEnvClass != null; procEnvClass = procEnvClass.getSuperclass()) { - try { - return getJavacProcessingEnvironment(tryGetDelegateField(procEnvClass, procEnv)); - } catch (final Exception e) { - // delegate field was not found, try on superclass - } + Object delegate = tryGetDelegateField(procEnvClass, procEnv); + if (delegate == null) delegate = tryGetProxyDelegateToField(procEnvClass, procEnv); + if (delegate == null) delegate = tryGetProcessingEnvField(procEnvClass, procEnv); + + if (delegate != null) return getJavacProcessingEnvironment(delegate); + // delegate field was not found, try on superclass } processingEnv.getMessager().printMessage(Kind.WARNING, @@ -454,11 +457,12 @@ public class LombokProcessor extends AbstractProcessor { // try to find a "delegate" field in the object, and use this to check for a JavacFiler for (Class<?> filerClass = filer.getClass(); filerClass != null; filerClass = filerClass.getSuperclass()) { - try { - return getJavacFiler(tryGetDelegateField(filerClass, filer)); - } catch (final Exception e) { - // delegate field was not found, try on superclass - } + Object delegate = tryGetDelegateField(filerClass, filer); + if (delegate == null) delegate = tryGetProxyDelegateToField(filerClass, filer); + if (delegate == null) delegate = tryGetFilerField(filerClass, filer); + + if (delegate != null) return getJavacFiler(delegate); + // delegate field was not found, try on superclass } processingEnv.getMessager().printMessage(Kind.WARNING, @@ -466,7 +470,48 @@ public class LombokProcessor extends AbstractProcessor { return null; } - private Object tryGetDelegateField(Class<?> delegateClass, Object instance) throws Exception { - return Permit.getField(delegateClass, "delegate").get(instance); + /** + * Gradle incremental processing + */ + private Object tryGetDelegateField(Class<?> delegateClass, Object instance) { + try { + return Permit.getField(delegateClass, "delegate").get(instance); + } catch (Exception e) { + return null; + } + } + + /** + * Kotlin incremental processing + */ + private Object tryGetProcessingEnvField(Class<?> delegateClass, Object instance) { + try { + return Permit.getField(delegateClass, "processingEnv").get(instance); + } catch (Exception e) { + return null; + } + } + + /** + * Kotlin incremental processing + */ + private Object tryGetFilerField(Class<?> delegateClass, Object instance) { + try { + return Permit.getField(delegateClass, "filer").get(instance); + } catch (Exception e) { + return null; + } + } + + /** + * InteliJ >= 2020.3 + */ + private Object tryGetProxyDelegateToField(Class<?> delegateClass, Object instance) { + try { + InvocationHandler handler = Proxy.getInvocationHandler(instance); + return Permit.getField(handler.getClass(), "val$delegateTo").get(handler); + } catch (Exception e) { + return null; + } } } |