Skip to content

Commit

Permalink
Migrate Glide to androidx
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 250772542
  • Loading branch information
sjudd authored and glide-copybara-robot committed May 30, 2019
1 parent 1be3a6c commit d8278f4
Show file tree
Hide file tree
Showing 417 changed files with 1,033 additions and 999 deletions.
1 change: 1 addition & 0 deletions annotation/compiler/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies {
compile project(':annotation')
// This is to support com.sun.tools.javac.util.List, currently used in RootModuleGenerator.
compile files(Jvm.current().getToolsJar())
annotationProcessor "com.google.auto.service:auto-service:${AUTO_SERVICE_VERSION}"
}

// Make sure running `gradlew :annotation:compiler:check` actually does full quality control.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.bumptech.glide.annotation.compiler;

import static com.bumptech.glide.annotation.compiler.ProcessorUtil.nonNull;

import com.bumptech.glide.annotation.Excludes;
import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.ClassName;
Expand Down Expand Up @@ -68,7 +66,7 @@
* }
*
* {@literal @java.lang.Override}
* {@literal @android.support.annotation.NonNull}
* {@literal @androidx.annotation.NonNull}
* public java.util.Set<java.lang.Class<?>> getExcludedModuleClasses() {
* return appGlideModule.getExcludedModuleClasses();
* }
Expand Down Expand Up @@ -115,12 +113,12 @@ TypeSpec generate(TypeElement appGlideModule, Set<String> libraryGlideModuleClas
.addAnnotation(Override.class)
.addParameter(
ParameterSpec.builder(ClassName.get("android.content", "Context"), "context")
.addAnnotation(nonNull())
.addAnnotation(processorUtil.nonNull())
.build())
.addParameter(
ParameterSpec.builder(
ClassName.get("com.bumptech.glide", "GlideBuilder"), "builder")
.addAnnotation(nonNull())
.addAnnotation(processorUtil.nonNull())
.build())
.addStatement("appGlideModule.applyOptions(context, builder)", appGlideModule)
.build();
Expand Down Expand Up @@ -158,7 +156,7 @@ TypeSpec generate(TypeElement appGlideModule, Set<String> libraryGlideModuleClas
builder.addMethod(
MethodSpec.methodBuilder("getRequestManagerFactory")
.addAnnotation(Override.class)
.addAnnotation(nonNull())
.addAnnotation(processorUtil.nonNull())
.returns(generatedRequestManagerFactoryClassName)
.addStatement("return new $T()", generatedRequestManagerFactoryClassName)
.build());
Expand All @@ -178,7 +176,7 @@ private MethodSpec generateGetExcludedModuleClasses(Collection<String> excludedC
MethodSpec.methodBuilder("getExcludedModuleClasses")
.addModifiers(Modifier.PUBLIC)
.addAnnotation(Override.class)
.addAnnotation(nonNull())
.addAnnotation(processorUtil.nonNull())
.returns(setOfClassOfWildcardOfObject);

if (excludedClassNames.isEmpty()) {
Expand Down Expand Up @@ -209,15 +207,15 @@ private MethodSpec generateRegisterComponents(
.addAnnotation(Override.class)
.addParameter(
ParameterSpec.builder(ClassName.get("android.content", "Context"), "context")
.addAnnotation(nonNull())
.addAnnotation(processorUtil.nonNull())
.build())
.addParameter(
ParameterSpec.builder(ClassName.get("com.bumptech.glide", "Glide"), "glide")
.addAnnotation(nonNull())
.addAnnotation(processorUtil.nonNull())
.build())
.addParameter(
ParameterSpec.builder(ClassName.get("com.bumptech.glide", "Registry"), "registry")
.addAnnotation(nonNull())
.addAnnotation(processorUtil.nonNull())
.build());

for (String glideModule : libraryGlideModuleClassNames) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ final class AppModuleProcessor {
appModuleGenerator = new AppModuleGenerator(processorUtil);
requestOptionsGenerator = new RequestOptionsGenerator(processingEnv, processorUtil);
requestManagerGenerator = new RequestManagerGenerator(processingEnv, processorUtil);
requestManagerFactoryGenerator = new RequestManagerFactoryGenerator(processingEnv);
requestManagerFactoryGenerator =
new RequestManagerFactoryGenerator(processingEnv, processorUtil);
glideGenerator = new GlideGenerator(processingEnv, processorUtil);
requestBuilderGenerator = new RequestBuilderGenerator(processingEnv, processorUtil);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,6 @@ public SourceVersion getSupportedSourceVersion() {
*/
@Override
public boolean process(Set<? extends TypeElement> set, RoundEnvironment env) {
// if (set.isEmpty() && !isGeneratedAppGlideModulePending) {
// return false;
// }
processorUtil.process();
boolean newModulesWritten = libraryModuleProcessor.processModules(env);
boolean newExtensionWritten = extensionProcessor.processExtensions(env);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.bumptech.glide.annotation.compiler;

import static com.bumptech.glide.annotation.compiler.ProcessorUtil.nonNull;
import static com.bumptech.glide.annotation.compiler.ProcessorUtil.nonNulls;

import com.bumptech.glide.annotation.GlideOption;
Expand Down Expand Up @@ -284,7 +283,7 @@ public String apply(AnnotationMirror input) {
Kind.WARNING,
getQualifiedMethodName(executableElement)
+ " is missing the "
+ nonNull().reflectionName()
+ processorUtil.nonNull().reflectionName()
+ " annotation,"
+ " please add it to ensure that your extension methods are always returning"
+ " non-null values");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* Generates a Glide look-alike that acts as the entry point to the generated API
* (GlideApp.with(...)).
*
* <p>>Generated {@code com.bumptech.glide.Glide} look-alikes look like this (note that the name is
* <p>Generated {@code com.bumptech.glide.Glide} look-alikes look like this (note that the name is
* configurable in {@link com.bumptech.glide.annotation.GlideModule}):
*
* <pre>
Expand Down Expand Up @@ -64,7 +64,7 @@
* return (GeneratedRequestManager) Glide.with(fragment);
* }
*
* public static GeneratedRequestManager with(android.support.v4.app.Fragment fragment) {
* public static GeneratedRequestManager with(androidx.fragment.app.Fragment fragment) {
* return (GeneratedRequestManager) Glide.with(fragment);
* }
* </code>
Expand Down Expand Up @@ -171,7 +171,7 @@ private MethodSpec overrideGlideStaticMethod(ExecutableElement methodToOverride)
private Builder addReturnAnnotations(Builder builder, ExecutableElement methodToOverride) {
Elements elements = processingEnv.getElementUtils();
TypeElement visibleForTestingTypeElement =
elements.getTypeElement(ProcessorUtil.visibleForTesting().reflectionName());
elements.getTypeElement(processorUtil.visibleForTesting().reflectionName());
String visibleForTestingTypeQualifiedName = visibleForTestingTypeElement.toString();

for (AnnotationMirror mirror : methodToOverride.getAnnotationMirrors()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.ClassName;
Expand All @@ -24,7 +25,6 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
Expand All @@ -48,6 +48,7 @@
import javax.lang.model.type.TypeMirror;
import javax.lang.model.type.TypeVariable;
import javax.lang.model.util.ElementFilter;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
import javax.tools.Diagnostic;

Expand All @@ -62,17 +63,17 @@ final class ProcessorUtil {
GLIDE_MODULE_PACKAGE_NAME + "." + LIBRARY_GLIDE_MODULE_SIMPLE_NAME;
private static final String COMPILER_PACKAGE_NAME =
GlideAnnotationProcessor.class.getPackage().getName();
private static final ClassName NONNULL_ANNOTATION =
private static final ClassName SUPPORT_NONNULL_ANNOTATION =
ClassName.get("android.support.annotation", "NonNull");
private static final ClassName JETBRAINS_NOTNULL_ANNOTATION =
ClassName.get("org.jetbrains.annotations", "NotNull");
private static final ClassName ANDROIDX_NONNULL_ANNOTATION =
ClassName.get("androidx.annotation", "NonNull");
private static final ClassName CHECK_RESULT_ANNOTATION =
private static final ClassName SUPPORT_CHECK_RESULT_ANNOTATION =
ClassName.get("android.support.annotation", "CheckResult");
private static final ClassName ANDROIDX_CHECK_RESULT_ANNOTATION =
ClassName.get("androidx.annotation", "CheckResult");
private static final ClassName VISIBLE_FOR_TESTING =
private static final ClassName SUPPORT_VISIBLE_FOR_TESTING =
ClassName.get("android.support.annotation", "VisibleForTesting");
private static final ClassName ANDROIDX_VISIBLE_FOR_TESTING =
ClassName.get("androidx.annotation", "VisibleForTesting");
Expand Down Expand Up @@ -457,36 +458,33 @@ private static List<AnnotationSpec> getAnnotations(VariableElement element) {
return result;
}

static ClassName visibleForTesting() {
try {
Class.forName(ANDROIDX_VISIBLE_FOR_TESTING.reflectionName());
return ANDROIDX_VISIBLE_FOR_TESTING;
} catch (ClassNotFoundException e) {
return VISIBLE_FOR_TESTING;
}
ClassName visibleForTesting() {
return findAnnotationClassName(ANDROIDX_VISIBLE_FOR_TESTING, SUPPORT_VISIBLE_FOR_TESTING);
}

static ClassName nonNull() {
try {
Class.forName(ANDROIDX_NONNULL_ANNOTATION.reflectionName());
return ANDROIDX_NONNULL_ANNOTATION;
} catch (ClassNotFoundException e) {
return NONNULL_ANNOTATION;
}
ClassName nonNull() {
return findAnnotationClassName(ANDROIDX_NONNULL_ANNOTATION, SUPPORT_NONNULL_ANNOTATION);
}

static ClassName checkResult() {
try {
Class.forName(ANDROIDX_CHECK_RESULT_ANNOTATION.reflectionName());
return ANDROIDX_CHECK_RESULT_ANNOTATION;
} catch (ClassNotFoundException e) {
return CHECK_RESULT_ANNOTATION;
}
ClassName checkResult() {
return findAnnotationClassName(
ANDROIDX_CHECK_RESULT_ANNOTATION, SUPPORT_CHECK_RESULT_ANNOTATION);
}

static List<ClassName> nonNulls() {
return Arrays.asList(
NONNULL_ANNOTATION, JETBRAINS_NOTNULL_ANNOTATION, ANDROIDX_NONNULL_ANNOTATION);
return ImmutableList.of(
SUPPORT_NONNULL_ANNOTATION, JETBRAINS_NOTNULL_ANNOTATION, ANDROIDX_NONNULL_ANNOTATION);
}

private ClassName findAnnotationClassName(ClassName androidxName, ClassName supportName) {
Elements elements = processingEnv.getElementUtils();
TypeElement visibleForTestingTypeElement =
elements.getTypeElement(androidxName.reflectionName());
if (visibleForTestingTypeElement != null) {
return androidxName;
}

return supportName;
}

List<ExecutableElement> findInstanceMethodsReturning(TypeElement clazz, TypeMirror returnType) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.bumptech.glide.annotation.compiler;

import static com.bumptech.glide.annotation.compiler.ProcessorUtil.checkResult;
import static com.bumptech.glide.annotation.compiler.ProcessorUtil.nonNull;

import com.bumptech.glide.annotation.GlideExtension;
import com.bumptech.glide.annotation.GlideOption;
import com.google.common.base.Function;
Expand Down Expand Up @@ -109,9 +106,6 @@ final class RequestBuilderGenerator {
private static final ImmutableSet<String> EXCLUDED_METHODS_FROM_BASE_REQUEST_OPTIONS =
ImmutableSet.of("clone", "apply", "autoLock", "lock", "autoClone");

private static final AnnotationSpec NON_NULL = AnnotationSpec.builder(nonNull()).build();
private static final AnnotationSpec CHECK_RESULT = AnnotationSpec.builder(checkResult()).build();

private final ProcessingEnvironment processingEnv;
private final ProcessorUtil processorUtil;
private final TypeVariableName transcodeTypeName;
Expand Down Expand Up @@ -433,11 +427,11 @@ private List<MethodSpec> generateConstructors() {
MethodSpec.constructorBuilder()
.addParameter(
ParameterSpec.builder(classOfTranscodeType, "transcodeClass")
.addAnnotation(nonNull())
.addAnnotation(processorUtil.nonNull())
.build())
.addParameter(
ParameterSpec.builder(requestBuilderOfWildcardOfObject, "other")
.addAnnotation(nonNull())
.addAnnotation(processorUtil.nonNull())
.build())
.addStatement("super($N, $N)", "transcodeClass", "other")
.build();
Expand All @@ -447,17 +441,22 @@ private List<MethodSpec> generateConstructors() {
ClassName requestManager = ClassName.get("com.bumptech.glide", "RequestManager");
MethodSpec secondConstructor =
MethodSpec.constructorBuilder()
.addParameter(ParameterSpec.builder(glide, "glide").addAnnotation(nonNull()).build())
.addParameter(
ParameterSpec.builder(glide, "glide")
.addAnnotation(processorUtil.nonNull())
.build())
.addParameter(
ParameterSpec.builder(requestManager, "requestManager")
.addAnnotation(nonNull())
.addAnnotation(processorUtil.nonNull())
.build())
.addParameter(
ParameterSpec.builder(classOfTranscodeType, "transcodeClass")
.addAnnotation(nonNull())
.addAnnotation(processorUtil.nonNull())
.build())
.addParameter(
ParameterSpec.builder(context, "context").addAnnotation(nonNull()).build())
ParameterSpec.builder(context, "context")
.addAnnotation(processorUtil.nonNull())
.build())
.addStatement(
"super($N, $N ,$N, $N)", "glide", "requestManager", "transcodeClass", "context")
.build();
Expand All @@ -473,8 +472,8 @@ private MethodSpec generateDownloadOnlyRequestMethod() {
ParameterizedTypeName.get(generatedRequestBuilderClassName, ClassName.get(File.class));
return MethodSpec.methodBuilder("getDownloadOnlyRequest")
.addAnnotation(Override.class)
.addAnnotation(CHECK_RESULT)
.addAnnotation(NON_NULL)
.addAnnotation(processorUtil.checkResult())
.addAnnotation(processorUtil.nonNull())
.returns(generatedRequestBuilderOfFile)
.addModifiers(Modifier.PROTECTED)
.addStatement(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.bumptech.glide.annotation.compiler;

import static com.bumptech.glide.annotation.compiler.ProcessorUtil.nonNull;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterSpec;
Expand Down Expand Up @@ -51,8 +49,10 @@ final class RequestManagerFactoryGenerator {
private final TypeElement requestManagerTreeNodeType;
private final TypeElement requestManagerFactoryInterface;
private final ClassName requestManagerClassName;
private final ProcessorUtil processorUtil;

RequestManagerFactoryGenerator(ProcessingEnvironment processingEnv) {
RequestManagerFactoryGenerator(ProcessingEnvironment processingEnv, ProcessorUtil processorUtil) {
this.processorUtil = processorUtil;
Elements elementUtils = processingEnv.getElementUtils();
glideType = elementUtils.getTypeElement(GLIDE_QUALIFIED_NAME);
lifecycleType = elementUtils.getTypeElement(LIFECYCLE_QUALIFIED_NAME);
Expand All @@ -75,23 +75,23 @@ TypeSpec generate(String generatedCodePackageName, TypeSpec generatedRequestMana
MethodSpec.methodBuilder("build")
.addModifiers(Modifier.PUBLIC)
.addAnnotation(Override.class)
.addAnnotation(nonNull())
.addAnnotation(processorUtil.nonNull())
.returns(requestManagerClassName)
.addParameter(
ParameterSpec.builder(ClassName.get(glideType), "glide")
.addAnnotation(nonNull())
.addAnnotation(processorUtil.nonNull())
.build())
.addParameter(
ParameterSpec.builder(ClassName.get(lifecycleType), "lifecycle")
.addAnnotation(nonNull())
.addAnnotation(processorUtil.nonNull())
.build())
.addParameter(
ParameterSpec.builder(ClassName.get(requestManagerTreeNodeType), "treeNode")
.addAnnotation(nonNull())
.addAnnotation(processorUtil.nonNull())
.build())
.addParameter(
ParameterSpec.builder(CONTEXT_CLASS_NAME, "context")
.addAnnotation(nonNull())
.addAnnotation(processorUtil.nonNull())
.build())
.addStatement(
"return new $T(glide, lifecycle, treeNode, context)",
Expand Down
Loading

0 comments on commit d8278f4

Please sign in to comment.