From 62aa8ce107e4ccec1d54a7e7701bfa83e4e0e48b Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 30 Jun 2020 11:27:32 +0100 Subject: [PATCH] Support incremental annotation processing with Gradle Closes gh-22150 --- .../gradle/incremental.annotation.processors | 1 + .../fieldvalues/javac/Trees.java | 22 ++++++++++++++----- .../gradle/incremental.annotation.processors | 1 + 3 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-autoconfigure-processor/src/main/resources/META-INF/gradle/incremental.annotation.processors create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/resources/META-INF/gradle/incremental.annotation.processors diff --git a/spring-boot-project/spring-boot-tools/spring-boot-autoconfigure-processor/src/main/resources/META-INF/gradle/incremental.annotation.processors b/spring-boot-project/spring-boot-tools/spring-boot-autoconfigure-processor/src/main/resources/META-INF/gradle/incremental.annotation.processors new file mode 100644 index 00000000000..242b07c64b5 --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-autoconfigure-processor/src/main/resources/META-INF/gradle/incremental.annotation.processors @@ -0,0 +1 @@ +org.springframework.boot.autoconfigureprocessor.AutoConfigureAnnotationProcessor,aggregating \ No newline at end of file diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/fieldvalues/javac/Trees.java b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/fieldvalues/javac/Trees.java index c958203d989..f635b8e9701 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/fieldvalues/javac/Trees.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/fieldvalues/javac/Trees.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package org.springframework.boot.configurationprocessor.fieldvalues.javac; +import java.lang.reflect.Field; import java.lang.reflect.Method; import javax.annotation.processing.ProcessingEnvironment; @@ -38,10 +39,21 @@ final class Trees extends ReflectionWrapper { } static Trees instance(ProcessingEnvironment env) throws Exception { - ClassLoader classLoader = env.getClass().getClassLoader(); - Class type = findClass(classLoader, "com.sun.source.util.Trees"); - Method method = findMethod(type, "instance", ProcessingEnvironment.class); - return new Trees(method.invoke(null, env)); + try { + ClassLoader classLoader = env.getClass().getClassLoader(); + Class type = findClass(classLoader, "com.sun.source.util.Trees"); + Method method = findMethod(type, "instance", ProcessingEnvironment.class); + return new Trees(method.invoke(null, env)); + } + catch (Exception ex) { + return instance(unwrap(env)); + } + } + + private static ProcessingEnvironment unwrap(ProcessingEnvironment wrapper) throws Exception { + Field delegateField = wrapper.getClass().getDeclaredField("delegate"); + delegateField.setAccessible(true); + return (ProcessingEnvironment) delegateField.get(wrapper); } } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/resources/META-INF/gradle/incremental.annotation.processors b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/resources/META-INF/gradle/incremental.annotation.processors new file mode 100644 index 00000000000..cd04b210b1d --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/resources/META-INF/gradle/incremental.annotation.processors @@ -0,0 +1 @@ +org.springframework.boot.configurationprocessor.ConfigurationMetadataAnnotationProcessor,aggregating \ No newline at end of file