Skip to content

Commit

Permalink
Ensure all generated classes are fully nullabilitily annotated (#2789)
Browse files Browse the repository at this point in the history
* Ensure all generated classes are fully nullability annotated

Signed-off-by: Alex Saveau <[email protected]>
  • Loading branch information
SUPERCILEX authored and sjudd committed Jan 9, 2018
1 parent 860f7d0 commit 35f6a0a
Show file tree
Hide file tree
Showing 55 changed files with 682 additions and 167 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
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;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterSpec;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;
Expand Down Expand Up @@ -78,6 +81,7 @@ final class AppModuleGenerator {
private static final String GENERATED_APP_MODULE_IMPL_SIMPLE_NAME =
"GeneratedAppGlideModuleImpl";
private static final String GENERATED_ROOT_MODULE_SIMPLE_NAME = "GeneratedAppGlideModule";

private final ProcessorUtil processorUtil;

AppModuleGenerator(ProcessorUtil processorUtil) {
Expand Down Expand Up @@ -110,8 +114,16 @@ TypeSpec generate(TypeElement appGlideModule, Set<String> libraryGlideModuleClas
MethodSpec.methodBuilder("applyOptions")
.addModifiers(Modifier.PUBLIC)
.addAnnotation(Override.class)
.addParameter(ClassName.get("android.content", "Context"), "context")
.addParameter(ClassName.get("com.bumptech.glide", "GlideBuilder"), "builder")
.addParameter(ParameterSpec.builder(
ClassName.get("android.content", "Context"), "context")
.addAnnotation(nonNull())
.build()
)
.addParameter(ParameterSpec.builder(
ClassName.get("com.bumptech.glide", "GlideBuilder"), "builder")
.addAnnotation(nonNull())
.build()
)
.addStatement("appGlideModule.applyOptions(context, builder)", appGlideModule)
.build();

Expand Down Expand Up @@ -147,6 +159,7 @@ TypeSpec generate(TypeElement appGlideModule, Set<String> libraryGlideModuleClas
builder.addMethod(
MethodSpec.methodBuilder("getRequestManagerFactory")
.addAnnotation(Override.class)
.addAnnotation(nonNull())
.returns(generatedRequestManagerFactoryClassName)
.addStatement("return new $T()", generatedRequestManagerFactoryClassName)
.build());
Expand All @@ -165,7 +178,7 @@ private MethodSpec generateGetExcludedModuleClasses(Collection<String> excludedC
MethodSpec.Builder builder = MethodSpec.methodBuilder("getExcludedModuleClasses")
.addModifiers(Modifier.PUBLIC)
.addAnnotation(Override.class)
.addAnnotation(ClassName.get("android.support.annotation", "NonNull"))
.addAnnotation(nonNull())
.returns(setOfClassOfWildcardOfObject);

if (excludedClassNames.isEmpty()) {
Expand Down Expand Up @@ -193,9 +206,21 @@ private MethodSpec generateRegisterComponents(
MethodSpec.methodBuilder("registerComponents")
.addModifiers(Modifier.PUBLIC)
.addAnnotation(Override.class)
.addParameter(ClassName.get("android.content", "Context"), "context")
.addParameter(ClassName.get("com.bumptech.glide", "Glide"), "glide")
.addParameter(ClassName.get("com.bumptech.glide", "Registry"), "registry");
.addParameter(ParameterSpec.builder(
ClassName.get("android.content", "Context"), "context")
.addAnnotation(nonNull())
.build()
)
.addParameter(ParameterSpec.builder(
ClassName.get("com.bumptech.glide", "Glide"), "glide")
.addAnnotation(nonNull())
.build()
)
.addParameter(ParameterSpec.builder(
ClassName.get("com.bumptech.glide", "Registry"), "registry")
.addAnnotation(nonNull())
.build()
);

for (String glideModule : libraryGlideModuleClassNames) {
if (excludedGlideModuleClassNames.contains(glideModule)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.bumptech.glide.annotation.compiler;

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

import com.bumptech.glide.annotation.GlideOption;
import com.bumptech.glide.annotation.GlideType;
import com.google.common.base.Function;
Expand Down Expand Up @@ -29,9 +31,6 @@
* for an Application.
*/
final class GlideExtensionValidator {
private static final String FULLY_QUALIFIED_NON_NULL_CLASS_NAME =
"android.support.annotation.NonNull";

private final ProcessingEnvironment processingEnvironment;
private final ProcessorUtil processorUtil;

Expand Down Expand Up @@ -254,11 +253,11 @@ public String apply(AnnotationMirror input) {
}
})
.toSet();
if (!annotationNames.contains(FULLY_QUALIFIED_NON_NULL_CLASS_NAME)) {
if (!annotationNames.contains(nonNull().reflectionName())) {
processingEnvironment.getMessager().printMessage(
Kind.WARNING,
executableElement.getEnclosingElement() + "#" + executableElement.getSimpleName()
+ " is missing the " + FULLY_QUALIFIED_NON_NULL_CLASS_NAME + " annotation,"
+ " is missing the " + 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 @@ -63,6 +63,8 @@ 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 =
ClassName.get("android.support.annotation", "NonNull");

private final ProcessingEnvironment processingEnv;
private final TypeElement appGlideModuleType;
Expand Down Expand Up @@ -332,6 +334,10 @@ private static List<AnnotationSpec> getAnnotations(VariableElement element) {
return result;
}

static ClassName nonNull() {
return NONNULL_ANNOTATION;
}

List<ExecutableElement> findInstanceMethodsReturning(TypeElement clazz, TypeMirror returnType) {
return FluentIterable.from(clazz.getEnclosedElements())
.filter(new FilterPublicMethods(returnType, MethodType.INSTANCE))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.bumptech.glide.annotation.compiler;

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 @@ -108,9 +110,7 @@ final class RequestBuilderGenerator {
ImmutableSet.of("clone", "apply", "autoLock", "lock", "autoClone");
private static final ClassName CHECK_RESULT_CLASS_NAME =
ClassName.get("android.support.annotation", "CheckResult");
private static final AnnotationSpec NON_NULL = AnnotationSpec
.builder(ClassName.get("android.support.annotation", "NonNull"))
.build();
private static final AnnotationSpec NON_NULL = AnnotationSpec.builder(nonNull()).build();

private final ProcessingEnvironment processingEnv;
private final ProcessorUtil processorUtil;
Expand Down Expand Up @@ -402,8 +402,14 @@ private List<MethodSpec> generateConstructors() {

MethodSpec firstConstructor =
MethodSpec.constructorBuilder()
.addParameter(classOfTranscodeType, "transcodeClass")
.addParameter(requestBuilderOfWildcardOfObject, "other")
.addParameter(ParameterSpec.builder(classOfTranscodeType, "transcodeClass")
.addAnnotation(nonNull())
.build()
)
.addParameter(ParameterSpec.builder(requestBuilderOfWildcardOfObject, "other")
.addAnnotation(nonNull())
.build()
)
.addStatement("super($N, $N)", "transcodeClass", "other")
.build();

Expand All @@ -412,10 +418,22 @@ private List<MethodSpec> generateConstructors() {
ClassName requestManager = ClassName.get("com.bumptech.glide", "RequestManager");
MethodSpec secondConstructor =
MethodSpec.constructorBuilder()
.addParameter(glide, "glide")
.addParameter(requestManager, "requestManager")
.addParameter(classOfTranscodeType, "transcodeClass")
.addParameter(context, "context")
.addParameter(ParameterSpec.builder(glide, "glide")
.addAnnotation(nonNull())
.build()
)
.addParameter(ParameterSpec.builder(requestManager, "requestManager")
.addAnnotation(nonNull())
.build()
)
.addParameter(ParameterSpec.builder(classOfTranscodeType, "transcodeClass")
.addAnnotation(nonNull())
.build()
)
.addParameter(ParameterSpec.builder(context, "context")
.addAnnotation(nonNull())
.build()
)
.addStatement(
"super($N, $N ,$N, $N)", "glide", "requestManager", "transcodeClass", "context")
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
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;
import com.squareup.javapoet.TypeSpec;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Modifier;
Expand Down Expand Up @@ -77,11 +80,25 @@ TypeSpec generate(String generatedCodePackageName, TypeSpec generatedRequestMana
MethodSpec.methodBuilder("build")
.addModifiers(Modifier.PUBLIC)
.addAnnotation(Override.class)
.addAnnotation(nonNull())
.returns(requestManagerClassName)
.addParameter(ClassName.get(glideType), "glide")
.addParameter(ClassName.get(lifecycleType), "lifecycle")
.addParameter(ClassName.get(requestManagerTreeNodeType), "treeNode")
.addParameter(CONTEXT_CLASS_NAME, "context")
.addParameter(ParameterSpec.builder(ClassName.get(glideType), "glide")
.addAnnotation(nonNull())
.build()
)
.addParameter(ParameterSpec.builder(ClassName.get(lifecycleType), "lifecycle")
.addAnnotation(nonNull())
.build()
)
.addParameter(
ParameterSpec.builder(ClassName.get(requestManagerTreeNodeType), "treeNode")
.addAnnotation(nonNull())
.build()
)
.addParameter(ParameterSpec.builder(CONTEXT_CLASS_NAME, "context")
.addAnnotation(nonNull())
.build()
)
.addStatement(
"return new $T(glide, lifecycle, treeNode, context)",
ClassName.get(generatedCodePackageName, generatedRequestManagerSpec.name))
Expand Down
Loading

0 comments on commit 35f6a0a

Please sign in to comment.