Skip to content

Commit

Permalink
Add support for incremental annotation processing
Browse files Browse the repository at this point in the history
  • Loading branch information
technoir42 committed Jan 30, 2019
1 parent a150301 commit 8c98079
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;

Expand Down Expand Up @@ -88,7 +89,8 @@ final class AppModuleGenerator {
this.processorUtil = processorUtil;
}

TypeSpec generate(TypeElement appGlideModule, Set<String> libraryGlideModuleClassNames) {
TypeSpec generate(TypeElement appGlideModule, Set<String> libraryGlideModuleClassNames,
List<Element> indexerElements) {
ClassName appGlideModuleClassName = ClassName.get(appGlideModule);
List<String> excludedGlideModuleClassNames =
getExcludedGlideModuleClassNames(appGlideModule);
Expand Down Expand Up @@ -149,7 +151,12 @@ TypeSpec generate(TypeElement appGlideModule, Set<String> libraryGlideModuleClas
.addMethod(applyOptions)
.addMethod(registerComponents)
.addMethod(isManifestParsingEnabled)
.addMethod(getExcludedModuleClasses);
.addMethod(getExcludedModuleClasses)
.addOriginatingElement(appGlideModule);

for (Element element : indexerElements) {
builder.addOriginatingElement(element);
}

ClassName generatedRequestManagerFactoryClassName =
ClassName.get(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,30 +84,36 @@ boolean maybeWriteAppModule() {
String generatedCodePackageName = appModule.getEnclosingElement().toString();

TypeSpec generatedRequestOptions =
requestOptionsGenerator.generate(generatedCodePackageName, indexedClassNames.extensions);
writeRequestOptions(generatedCodePackageName, generatedRequestOptions);
requestOptionsGenerator.generate(
appModule, generatedCodePackageName, indexedClassNames.extensions,
indexedClassNames.indexerElements);
writeRequestOptions(generatedCodePackageName, generatedRequestOptions);

TypeSpec generatedRequestBuilder =
requestBuilderGenerator.generate(
generatedCodePackageName, indexedClassNames.extensions, generatedRequestOptions);
appModule, generatedCodePackageName, indexedClassNames.extensions,
generatedRequestOptions, indexedClassNames.indexerElements);
writeRequestBuilder(generatedCodePackageName, generatedRequestBuilder);

TypeSpec requestManager =
requestManagerGenerator.generate(
generatedCodePackageName, generatedRequestOptions, generatedRequestBuilder,
indexedClassNames.extensions);
appModule, generatedCodePackageName, generatedRequestOptions, generatedRequestBuilder,
indexedClassNames.extensions, indexedClassNames.indexerElements);
writeRequestManager(generatedCodePackageName, requestManager);

TypeSpec requestManagerFactory =
requestManagerFactoryGenerator.generate(generatedCodePackageName, requestManager);
requestManagerFactoryGenerator.generate(
appModule, generatedCodePackageName, requestManager);
writeRequestManagerFactory(requestManagerFactory);

TypeSpec glide =
glideGenerator.generate(generatedCodePackageName, getGlideName(appModule), requestManager);
glideGenerator.generate(
appModule, generatedCodePackageName, getGlideName(appModule), requestManager);
writeGlide(generatedCodePackageName, glide);

TypeSpec generatedAppGlideModule =
appModuleGenerator.generate(appModule, indexedClassNames.glideModules);
appModuleGenerator.generate(
appModule, indexedClassNames.glideModules, indexedClassNames.indexerElements);
writeAppModule(generatedAppGlideModule);

processorUtil.infoLog("Wrote GeneratedAppGlideModule with: " + indexedClassNames.glideModules);
Expand All @@ -123,6 +129,7 @@ private String getGlideName(TypeElement appModule) {
private FoundIndexedClassNames getIndexedClassNames(PackageElement glideGenPackage) {
Set<String> glideModules = new HashSet<>();
Set<String> extensions = new HashSet<>();
List<Element> indexerElements = new ArrayList<>();
List<? extends Element> glideGeneratedElements = glideGenPackage.getEnclosedElements();
for (Element indexer : glideGeneratedElements) {
Index annotation = indexer.getAnnotation(Index.class);
Expand All @@ -131,11 +138,12 @@ private FoundIndexedClassNames getIndexedClassNames(PackageElement glideGenPacka
if (annotation != null) {
Collections.addAll(glideModules, annotation.modules());
Collections.addAll(extensions, annotation.extensions());
indexerElements.add(indexer);
}
}

processorUtil.debugLog("Found GlideModules: " + glideModules);
return new FoundIndexedClassNames(glideModules, extensions);
return new FoundIndexedClassNames(glideModules, extensions, indexerElements);
}

private void writeGlide(String packageName, TypeSpec glide) {
Expand Down Expand Up @@ -169,10 +177,13 @@ private void writeRequestBuilder(String packageName, TypeSpec requestBuilder) {
private static final class FoundIndexedClassNames {
private final Set<String> glideModules;
private final Set<String> extensions;
private final List<Element> indexerElements;

private FoundIndexedClassNames(Set<String> glideModules, Set<String> extensions) {
private FoundIndexedClassNames(Set<String> glideModules, Set<String> extensions,
List<Element> indexerElements) {
this.glideModules = glideModules;
this.extensions = extensions;
this.indexerElements = indexerElements;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ final class GlideGenerator {
}

TypeSpec generate(
String generatedCodePackageName, String glideName, TypeSpec generatedRequestManager) {
TypeElement appGlideModule, String generatedCodePackageName, String glideName,
TypeSpec generatedRequestManager) {
return TypeSpec.classBuilder(glideName)
.addJavadoc(
"The entry point for interacting with Glide for Applications\n"
Expand All @@ -121,6 +122,7 @@ TypeSpec generate(
.build())
.addMethods(
generateOverridesForGlideMethods(generatedCodePackageName, generatedRequestManager))
.addOriginatingElement(appGlideModule)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ final class IndexerGenerator {
}

TypeSpec generate(List<TypeElement> types) {
List<TypeElement> modules = new ArrayList<>();
List<TypeElement> modules = new ArrayList<>();
List<TypeElement> extensions = new ArrayList<>();
for (TypeElement element : types) {
if (processorUtil.isExtension(element)) {
Expand Down Expand Up @@ -80,19 +80,22 @@ private static TypeSpec generate(List<TypeElement> libraryModules,
AnnotationSpec.builder(Index.class);

String value = getAnnotationValue(annotation);
StringBuilder indexerName = new StringBuilder(
INDEXER_NAME_PREFIX + annotation.getSimpleName() + "_");
for (TypeElement childModule : libraryModules) {
annotationBuilder.addMember(value, "$S", ClassName.get(childModule).toString());
}

StringBuilder indexerName = new StringBuilder(
INDEXER_NAME_PREFIX + annotation.getSimpleName() + "_");
for (TypeElement element : libraryModules) {
indexerName.append(element.getQualifiedName().toString().replace(".", "_"));
indexerName.append(childModule.getQualifiedName().toString().replace(".", "_"));
indexerName.append("_");
}
indexerName = new StringBuilder(indexerName.substring(0, indexerName.length() - 1));

return TypeSpec.classBuilder(indexerName.toString())
TypeSpec.Builder builder = TypeSpec.classBuilder(indexerName.toString());
for (TypeElement element : libraryModules) {
builder.addOriginatingElement(element);
}

return builder
.addAnnotation(annotationBuilder.build())
.addModifiers(Modifier.PUBLIC)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import javax.annotation.Nullable;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
Expand Down Expand Up @@ -134,9 +135,11 @@ final class RequestBuilderGenerator {
}

TypeSpec generate(
TypeElement appGlideModule,
String generatedCodePackageName,
Set<String> glideExtensionClassNames,
@Nullable TypeSpec generatedOptions) {
@Nullable TypeSpec generatedOptions,
List<Element> indexerElements) {
if (generatedOptions != null) {
requestOptionsClassName =
ClassName.get(generatedCodePackageName, generatedOptions.name);
Expand All @@ -163,7 +166,7 @@ TypeSpec generate(
requestOptionsExtensionGenerator.generateInstanceMethodsForExtensions(
glideExtensionClassNames);

return TypeSpec.classBuilder(GENERATED_REQUEST_BUILDER_SIMPLE_NAME)
TypeSpec.Builder builder = TypeSpec.classBuilder(GENERATED_REQUEST_BUILDER_SIMPLE_NAME)
.addJavadoc("Contains all public methods from {@link $T}, all options from\n",
requestBuilderType)
.addJavadoc("{@link $T} and all generated options from\n", requestOptionsType)
Expand All @@ -190,7 +193,12 @@ TypeSpec generate(
requestOptionsExtensionMethods, generatedOptions))
.addMethods(generateRequestBuilderOverrides())
.addMethods(requestOptionsExtensionMethods)
.build();
.addOriginatingElement(appGlideModule);

for (Element element : indexerElements) {
builder.addOriginatingElement(element);
}
return builder.build();
}
/**
* Generates methods with equivalent names and arguments to methods annotated with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ final class RequestManagerFactoryGenerator {
requestManagerClassName = ClassName.get(requestManagerType);
}

TypeSpec generate(String generatedCodePackageName, TypeSpec generatedRequestManagerSpec) {
TypeSpec generate(TypeElement appGlideModule, String generatedCodePackageName,
TypeSpec generatedRequestManagerSpec) {
return TypeSpec.classBuilder(GENERATED_REQUEST_MANAGER_FACTORY_SIMPLE_NAME)
.addModifiers(Modifier.FINAL)
.addSuperinterface(ClassName.get(requestManagerFactoryInterface))
Expand Down Expand Up @@ -103,6 +104,7 @@ TypeSpec generate(String generatedCodePackageName, TypeSpec generatedRequestMana
ClassName.get(generatedCodePackageName, generatedRequestManagerSpec.name))
.build()
)
.addOriginatingElement(appGlideModule)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import javax.annotation.Nullable;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
Expand Down Expand Up @@ -98,10 +99,11 @@ final class RequestManagerGenerator {
}

TypeSpec generate(
TypeElement appGlideModule,
String generatedCodePackageName, @Nullable TypeSpec requestOptions, TypeSpec requestBuilder,
Set<String> glideExtensions) {
Set<String> glideExtensions, List<Element> indexerElements) {
generatedRequestBuilderClassName = ClassName.get(generatedCodePackageName, requestBuilder.name);
return TypeSpec.classBuilder(GENERATED_REQUEST_MANAGER_SIMPLE_NAME)
TypeSpec.Builder builder = TypeSpec.classBuilder(GENERATED_REQUEST_MANAGER_SIMPLE_NAME)
.superclass(requestManagerClassName)
.addJavadoc("Includes all additions from methods in {@link $T}s\n"
+ "annotated with {@link $T}\n"
Expand All @@ -123,7 +125,12 @@ TypeSpec generate(
Collections.singletonList(
generateOverrideSetRequestOptions(generatedCodePackageName, requestOptions)))
.filter(Predicates.<MethodSpec>notNull()))
.build();
.addOriginatingElement(appGlideModule);

for (Element element : indexerElements) {
builder.addOriginatingElement(element);
}
return builder.build();
}

private MethodSpec generateCallSuperConstructor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Set;
import javax.annotation.Nullable;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
Expand Down Expand Up @@ -102,7 +103,8 @@ final class RequestOptionsGenerator {
new RequestOptionsOverrideGenerator(processingEnvironment, processorUtil);
}

TypeSpec generate(String generatedCodePackageName, Set<String> glideExtensionClassNames) {
TypeSpec generate(TypeElement appGlideModule, String generatedCodePackageName,
Set<String> glideExtensionClassNames, List<Element> indexerElements) {
glideOptionsName =
ClassName.get(generatedCodePackageName, GENERATED_REQUEST_OPTIONS_SIMPLE_NAME);

Expand Down Expand Up @@ -181,7 +183,12 @@ public MethodSignature apply(MethodAndStaticVar f) {
.addModifiers(Modifier.FINAL)
.addModifiers(Modifier.PUBLIC)
.addSuperinterface(Cloneable.class)
.superclass(requestOptionsName);
.superclass(requestOptionsName)
.addOriginatingElement(appGlideModule);

for (Element element : indexerElements) {
classBuilder.addOriginatingElement(element);
}

for (MethodAndStaticVar methodAndStaticVar : allMethodsAndStaticVars) {
if (methodAndStaticVar.method != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.bumptech.glide.annotation.compiler.GlideAnnotationProcessor,aggregating
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
* @see GlideOption
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
@Retention(RetentionPolicy.CLASS)
public @interface GlideExtension { }
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* <p>Replaces <meta-data /> tags in AndroidManifest.xml.
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
@Retention(RetentionPolicy.CLASS)
public @interface GlideModule {
/**
* Returns the name of the class that will be used as a replacement for
Expand Down

0 comments on commit 8c98079

Please sign in to comment.