Skip to content

Commit

Permalink
Fix an ordering issue in Glide's annotation processor.
Browse files Browse the repository at this point in the history
Internal and external compilers use different iteration orders when
iterating over sets. For our processor to have consistent output, we
have to ensure we process annotations in a defined order.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=177824343
  • Loading branch information
sjudd committed Dec 7, 2017
1 parent 3dc1d18 commit 11b5fd7
Showing 1 changed file with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
import com.squareup.javapoet.TypeSpec;
import com.squareup.javapoet.TypeSpec.Builder;
import com.squareup.javapoet.WildcardTypeName;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
Expand Down Expand Up @@ -82,15 +85,22 @@ final class AppModuleGenerator {

TypeSpec generate(TypeElement appGlideModule, Set<String> libraryGlideModuleClassNames) {
ClassName appGlideModuleClassName = ClassName.get(appGlideModule);
Set<String> excludedGlideModuleClassNames =
List<String> excludedGlideModuleClassNames =
getExcludedGlideModuleClassNames(appGlideModule);

List<String> orderedLibraryGlideModuleClassNames =
new ArrayList<>(libraryGlideModuleClassNames);
Collections.sort(orderedLibraryGlideModuleClassNames);

MethodSpec constructor =
generateConstructor(
appGlideModuleClassName, libraryGlideModuleClassNames, excludedGlideModuleClassNames);
appGlideModuleClassName,
orderedLibraryGlideModuleClassNames,
excludedGlideModuleClassNames);

MethodSpec registerComponents =
generateRegisterComponents(libraryGlideModuleClassNames, excludedGlideModuleClassNames);
generateRegisterComponents(
orderedLibraryGlideModuleClassNames, excludedGlideModuleClassNames);

MethodSpec getExcludedModuleClasses =
generateGetExcludedModuleClasses(excludedGlideModuleClassNames);
Expand Down Expand Up @@ -143,7 +153,7 @@ TypeSpec generate(TypeElement appGlideModule, Set<String> libraryGlideModuleClas
}

// TODO: When we drop support for parsing GlideModules from AndroidManifests, remove this method.
private MethodSpec generateGetExcludedModuleClasses(Set<String> excludedClassNames) {
private MethodSpec generateGetExcludedModuleClasses(Collection<String> excludedClassNames) {
TypeName wildCardOfObject = WildcardTypeName.subtypeOf(Object.class);
ParameterizedTypeName classOfWildcardOfObjet =
ParameterizedTypeName.get(ClassName.get(Class.class), wildCardOfObject);
Expand Down Expand Up @@ -174,8 +184,9 @@ private MethodSpec generateGetExcludedModuleClasses(Set<String> excludedClassNam
return builder.build();
}

private MethodSpec generateRegisterComponents(Set<String> libraryGlideModuleClassNames,
Set<String> excludedGlideModuleClassNames) {
private MethodSpec generateRegisterComponents(
Collection<String> libraryGlideModuleClassNames,
Collection<String> excludedGlideModuleClassNames) {
MethodSpec.Builder registerComponents =
MethodSpec.methodBuilder("registerComponents")
.addModifiers(Modifier.PUBLIC)
Expand All @@ -198,7 +209,8 @@ private MethodSpec generateRegisterComponents(Set<String> libraryGlideModuleClas
}

private MethodSpec generateConstructor(ClassName appGlideModule,
Set<String> libraryGlideModuleClassNames, Set<String> excludedGlideModuleClassNames) {
Collection<String> libraryGlideModuleClassNames,
Collection<String> excludedGlideModuleClassNames) {
MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder();
constructorBuilder.addStatement("appGlideModule = new $T()", appGlideModule);

Expand All @@ -224,8 +236,11 @@ private MethodSpec generateConstructor(ClassName appGlideModule,
return constructorBuilder.build();
}

private Set<String> getExcludedGlideModuleClassNames(TypeElement appGlideModule) {
return processorUtil.findClassValuesFromAnnotationOnClassAsNames(
private List<String> getExcludedGlideModuleClassNames(TypeElement appGlideModule) {
Set<String> names = processorUtil.findClassValuesFromAnnotationOnClassAsNames(
appGlideModule, Excludes.class);
List<String> result = new ArrayList<>(names);
Collections.sort(result);
return result;
}
}

0 comments on commit 11b5fd7

Please sign in to comment.