Skip to content

Commit a5673d0

Browse files
tbroyereamonnmcmanus
authored andcommitted
Make AutoValue and AutoService support Gradle incremental build. This work was done by Thomas Broyer.
Closes #657 RELNOTES=Make AutoValue and AutoService support Gradle incremental build. Thanks to Thomas Broyer for this work. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=214442018
1 parent 73e848a commit a5673d0

9 files changed

Lines changed: 115 additions & 6 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.google.auto.service.processor.AutoServiceProcessor,AGGREGATING

‎value/processor/pom.xml‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@
5656
<artifactId>escapevelocity</artifactId>
5757
<version>0.9</version>
5858
</dependency>
59+
<dependency>
60+
<groupId>net.ltgt.gradle.incap</groupId>
61+
<artifactId>incap</artifactId>
62+
<version>0.1</version>
63+
</dependency>
64+
<dependency>
65+
<groupId>net.ltgt.gradle.incap</groupId>
66+
<artifactId>incap-processor</artifactId>
67+
<version>0.1</version>
68+
<scope>provided</scope>
69+
</dependency>
5970
<dependency>
6071
<groupId>com.google.guava</groupId>
6172
<artifactId>guava</artifactId>

‎value/src/main/java/com/google/auto/value/extension/AutoValueExtension.java‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,62 @@ public interface Context {
108108
Set<ExecutableElement> abstractMethods();
109109
}
110110

111+
/**
112+
* Indicates to an annotation processor environment supporting incremental annotation processing
113+
* (currently a feature specific to Gradle starting with version 4.8) the incremental type of an
114+
* Extension.
115+
*
116+
* <p>The constants for this enum are ordered by increasing performance (but also constraints).
117+
*
118+
* @see <a
119+
* href="https://docs.gradle.org/current/userguide/java_plugin.html#sec:incremental_annotation_processing">Gradle
120+
* documentation of its incremental annotation processing</a>
121+
*/
122+
public enum IncrementalExtensionType {
123+
/**
124+
* The incrementality of this extension is unknown, or it is neither aggregating nor isolating.
125+
*/
126+
UNKNOWN,
127+
128+
/**
129+
* This extension is <i>aggregating</i>, meaning that it may generate outputs based on several
130+
* annotated input classes and it respects the constraints imposed on aggregating processors.
131+
* It is unusual for AutoValue extensions to be aggregating.
132+
*
133+
* @see <a
134+
* href="https://docs.gradle.org/current/userguide/java_plugin.html#aggregating_annotation_processors">Gradle
135+
* definition of aggregating processors</a>
136+
*/
137+
AGGREGATING,
138+
139+
/**
140+
* This extension is <i>isolating</i>, meaning roughly that its output depends on the
141+
* {@code @AutoValue} class and its dependencies, but not on other {@code @AutoValue} classes
142+
* that might be compiled at the same time. The constraints that an isolating extension must
143+
* respect are the same as those that Gradle imposes on an isolating annotation processor.
144+
*
145+
* @see <a
146+
* href="https://docs.gradle.org/current/userguide/java_plugin.html#isolating_annotation_processors">Gradle
147+
* definition of isolating processors</a>
148+
*/
149+
ISOLATING
150+
}
151+
152+
/**
153+
* Determines the incremental type of this Extension.
154+
*
155+
* <p>The {@link ProcessingEnvironment} can be used, among other things, to obtain the processor
156+
* options, using {@link ProcessingEnvironment#getOptions()}.
157+
*
158+
* <p>The actual incremental type of the AutoValue processor as a whole will be the loosest
159+
* incremental types of the Extensions present in the annotation processor path. The default
160+
* returned value is {@link IncrementalExtensionType#UNKNOWN}, which will disable incremental
161+
* annotation processing entirely.
162+
*/
163+
public IncrementalExtensionType incrementalType(ProcessingEnvironment processingEnvironment) {
164+
return IncrementalExtensionType.UNKNOWN;
165+
}
166+
111167
/**
112168
* Determines whether this Extension applies to the given context.
113169
*

‎value/src/main/java/com/google/auto/value/extension/memoized/processor/MemoizeExtension.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import java.util.Map;
5656
import java.util.Optional;
5757
import javax.annotation.processing.Messager;
58+
import javax.annotation.processing.ProcessingEnvironment;
5859
import javax.lang.model.SourceVersion;
5960
import javax.lang.model.element.AnnotationMirror;
6061
import javax.lang.model.element.ExecutableElement;
@@ -76,6 +77,11 @@ public final class MemoizeExtension extends AutoValueExtension {
7677
private static final AnnotationSpec SUPPRESS_WARNINGS =
7778
AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "Immutable").build();
7879

80+
@Override
81+
public IncrementalExtensionType incrementalType(ProcessingEnvironment processingEnvironment) {
82+
return IncrementalExtensionType.ISOLATING;
83+
}
84+
7985
@Override
8086
public boolean applicable(Context context) {
8187
return !memoizedMethods(context).isEmpty();

‎value/src/main/java/com/google/auto/value/extension/memoized/processor/MemoizedValidator.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@
3333
import javax.lang.model.element.Element;
3434
import javax.lang.model.element.ExecutableElement;
3535
import javax.lang.model.element.TypeElement;
36+
import net.ltgt.gradle.incap.IncrementalAnnotationProcessor;
37+
import net.ltgt.gradle.incap.IncrementalAnnotationProcessorType;
3638

3739
/**
3840
* An annotation {@link Processor} that reports errors for {@link Memoized @Memoized} methods that
3941
* are not inside {@code AutoValue}-annotated classes.
4042
*/
4143
@AutoService(Processor.class)
44+
@IncrementalAnnotationProcessor(IncrementalAnnotationProcessorType.ISOLATING)
4245
@SupportedAnnotationTypes(MEMOIZED_NAME)
4346
public final class MemoizedValidator extends AbstractProcessor {
4447
@Override

‎value/src/main/java/com/google/auto/value/processor/AutoAnnotationProcessor.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
import javax.lang.model.util.Types;
5959
import javax.tools.Diagnostic;
6060
import javax.tools.JavaFileObject;
61+
import net.ltgt.gradle.incap.IncrementalAnnotationProcessor;
62+
import net.ltgt.gradle.incap.IncrementalAnnotationProcessorType;
6163

6264
/**
6365
* Javac annotation processor (compiler plugin) to generate annotation implementations. User code
@@ -66,6 +68,7 @@
6668
* @author emcmanus@google.com (Éamonn McManus)
6769
*/
6870
@AutoService(Processor.class)
71+
@IncrementalAnnotationProcessor(IncrementalAnnotationProcessorType.ISOLATING)
6972
@SupportedAnnotationTypes(AUTO_ANNOTATION_NAME)
7073
public class AutoAnnotationProcessor extends AbstractProcessor {
7174
public AutoAnnotationProcessor() {}

‎value/src/main/java/com/google/auto/value/processor/AutoOneOfProcessor.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import java.util.Set;
3737
import javax.annotation.processing.Processor;
3838
import javax.annotation.processing.SupportedAnnotationTypes;
39-
import javax.annotation.processing.SupportedOptions;
4039
import javax.lang.model.element.AnnotationMirror;
4140
import javax.lang.model.element.AnnotationValue;
4241
import javax.lang.model.element.Element;
@@ -46,6 +45,8 @@
4645
import javax.lang.model.type.DeclaredType;
4746
import javax.lang.model.type.TypeKind;
4847
import javax.lang.model.type.TypeMirror;
48+
import net.ltgt.gradle.incap.IncrementalAnnotationProcessor;
49+
import net.ltgt.gradle.incap.IncrementalAnnotationProcessorType;
4950

5051
/**
5152
* Javac annotation processor (compiler plugin) for {@linkplain com.google.auto.value.AutoOneOf
@@ -56,7 +57,7 @@
5657
*/
5758
@AutoService(Processor.class)
5859
@SupportedAnnotationTypes(AUTO_ONE_OF_NAME)
59-
@SupportedOptions("com.google.auto.value.OmitIdentifiers")
60+
@IncrementalAnnotationProcessor(IncrementalAnnotationProcessorType.ISOLATING)
6061
public class AutoOneOfProcessor extends AutoValueOrOneOfProcessor {
6162
public AutoOneOfProcessor() {
6263
super(AUTO_ONE_OF_NAME);

‎value/src/main/java/com/google/auto/value/processor/AutoValueBuilderProcessor.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import javax.lang.model.element.Element;
3131
import javax.lang.model.element.TypeElement;
3232
import javax.tools.Diagnostic;
33+
import net.ltgt.gradle.incap.IncrementalAnnotationProcessor;
34+
import net.ltgt.gradle.incap.IncrementalAnnotationProcessorType;
3335

3436
/**
3537
* Annotation processor that checks that the type that {@code AutoValue.Builder} is applied to is
@@ -39,6 +41,7 @@
3941
* @author Éamonn McManus
4042
*/
4143
@AutoService(Processor.class)
44+
@IncrementalAnnotationProcessor(IncrementalAnnotationProcessorType.ISOLATING)
4245
@SupportedAnnotationTypes(AUTO_VALUE_BUILDER_NAME)
4346
public class AutoValueBuilderProcessor extends AbstractProcessor {
4447
@Override

‎value/src/main/java/com/google/auto/value/processor/AutoValueProcessor.java‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.lang.annotation.Annotation;
3434
import java.util.ArrayList;
3535
import java.util.Collections;
36+
import java.util.Comparator;
3637
import java.util.HashSet;
3738
import java.util.List;
3839
import java.util.Optional;
@@ -43,14 +44,15 @@
4344
import javax.annotation.processing.ProcessingEnvironment;
4445
import javax.annotation.processing.Processor;
4546
import javax.annotation.processing.SupportedAnnotationTypes;
46-
import javax.annotation.processing.SupportedOptions;
4747
import javax.lang.model.element.AnnotationMirror;
4848
import javax.lang.model.element.Element;
4949
import javax.lang.model.element.ElementKind;
5050
import javax.lang.model.element.ExecutableElement;
5151
import javax.lang.model.element.TypeElement;
5252
import javax.lang.model.type.TypeKind;
5353
import javax.lang.model.type.TypeMirror;
54+
import net.ltgt.gradle.incap.IncrementalAnnotationProcessor;
55+
import net.ltgt.gradle.incap.IncrementalAnnotationProcessorType;
5456

5557
/**
5658
* Javac annotation processor (compiler plugin) for value types; user code never references this
@@ -61,8 +63,10 @@
6163
*/
6264
@AutoService(Processor.class)
6365
@SupportedAnnotationTypes(AUTO_VALUE_NAME)
64-
@SupportedOptions("com.google.auto.value.OmitIdentifiers")
66+
@IncrementalAnnotationProcessor(IncrementalAnnotationProcessorType.DYNAMIC)
6567
public class AutoValueProcessor extends AutoValueOrOneOfProcessor {
68+
private static final String OMIT_IDENTIFIERS_OPTION = "com.google.auto.value.OmitIdentifiers";
69+
6670
public AutoValueProcessor() {
6771
this(AutoValueProcessor.class.getClassLoader());
6872
}
@@ -112,6 +116,28 @@ public synchronized void init(ProcessingEnvironment processingEnv) {
112116
}
113117
}
114118

119+
@Override
120+
public Set<String> getSupportedOptions() {
121+
AutoValueExtension.IncrementalExtensionType incrementalType =
122+
extensions.stream()
123+
.map(e -> e.incrementalType(processingEnv))
124+
.min(Comparator.naturalOrder())
125+
.orElse(AutoValueExtension.IncrementalExtensionType.ISOLATING);
126+
switch (incrementalType) {
127+
case ISOLATING:
128+
return ImmutableSet.of(
129+
OMIT_IDENTIFIERS_OPTION,
130+
IncrementalAnnotationProcessorType.ISOLATING.getProcessorOption());
131+
case AGGREGATING:
132+
return ImmutableSet.of(
133+
OMIT_IDENTIFIERS_OPTION,
134+
IncrementalAnnotationProcessorType.AGGREGATING.getProcessorOption());
135+
case UNKNOWN:
136+
return ImmutableSet.of(OMIT_IDENTIFIERS_OPTION);
137+
}
138+
throw new AssertionError(incrementalType);
139+
}
140+
115141
private static String generatedSubclassName(TypeElement type, int depth) {
116142
return generatedClassName(type, Strings.repeat("$", depth) + "AutoValue_");
117143
}
@@ -201,8 +227,7 @@ void processType(TypeElement type) {
201227
AutoValueTemplateVars vars = new AutoValueTemplateVars();
202228
vars.finalSubclass = TypeSimplifier.simpleNameOf(finalSubclass);
203229
vars.types = processingEnv.getTypeUtils();
204-
vars.identifiers =
205-
!processingEnv.getOptions().containsKey("com.google.auto.value.OmitIdentifiers");
230+
vars.identifiers = !processingEnv.getOptions().containsKey(OMIT_IDENTIFIERS_OPTION);
206231
defineSharedVarsForType(type, methods, vars);
207232
defineVarsForType(type, vars, toBuilderMethods, propertyMethods, builder);
208233

0 commit comments

Comments
 (0)