Skip to content

Commit

Permalink
Add @nonnull to generated options and warn if @nonnull is missing.
Browse files Browse the repository at this point in the history
Progress towards #2774.
  • Loading branch information
sjudd committed Dec 31, 2017
1 parent ebff466 commit 62d7464
Show file tree
Hide file tree
Showing 37 changed files with 105 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import com.bumptech.glide.annotation.GlideOption;
import com.bumptech.glide.annotation.GlideType;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
Expand All @@ -14,6 +18,7 @@
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.tools.Diagnostic.Kind;

/**
* Validates that classes annotated with {@link com.bumptech.glide.annotation.GlideExtension}
Expand Down Expand Up @@ -73,6 +78,7 @@ private void validateGlideOption(ExecutableElement executableElement) {
}

private void validateNewGlideOption(ExecutableElement executableElement) {
validateNewGlideOptionAnnotations(executableElement);
validateGlideOptionParameters(executableElement);
TypeMirror returnType = executableElement.getReturnType();
if (!isRequestOptions(returnType)) {
Expand All @@ -84,6 +90,26 @@ private void validateNewGlideOption(ExecutableElement executableElement) {
validateGlideOptionOverride(executableElement);
}

private void validateNewGlideOptionAnnotations(ExecutableElement executableElement) {
Set<String> annotationNames =
FluentIterable.from(executableElement.getAnnotationMirrors())
.transform(new Function<AnnotationMirror, String>() {
@Override
public String apply(AnnotationMirror input) {
return input.getAnnotationType().asElement().toString();
}
})
.toSet();
if (!annotationNames.contains("android.support.annotation.NonNull")) {
processingEnvironment.getMessager().printMessage(
Kind.WARNING,
executableElement.getEnclosingElement() + "#" + executableElement.getSimpleName()
+ " is missing the @NonNull annotation,"
+ " please add it to ensure that your extension methods are always returning non-null"
+ " values");
}
}

private void validateDeprecatedGlideOption(ExecutableElement executableElement) {
validateStaticVoid(executableElement, GlideOption.class);
validateGlideOptionParameters(executableElement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ final class RequestOptionsGenerator {
REQUEST_OPTIONS_PACKAGE_NAME + "." + REQUEST_OPTIONS_SIMPLE_NAME;
private static final ClassName CHECK_RESULT_CLASS_NAME =
ClassName.get("android.support.annotation", "CheckResult");
private static final ClassName NON_NULL_CLASS_NAME =
ClassName.get("android.support.annotation", "NonNull");

private final ProcessingEnvironment processingEnvironment;
private final ClassName requestOptionsName;
Expand Down Expand Up @@ -298,6 +300,8 @@ private List<MethodAndStaticVar> generateMethodsForRequestOptionsExtensionNew(
code = new StringBuilder(code.substring(0, code.length() - 2));
code.append(")");
builder.addStatement(code.toString(), args.toArray(new Object[0]));

builder.addAnnotation(AnnotationSpec.builder(NON_NULL_CLASS_NAME).build());
builder.addAnnotation(AnnotationSpec.builder(CHECK_RESULT_CLASS_NAME).build());

List<MethodAndStaticVar> result = new ArrayList<>();
Expand Down Expand Up @@ -370,6 +374,7 @@ private List<MethodAndStaticVar> generateMethodsForRequestOptionsExtensionDeprec
builder.addStatement(code.toString(), args.toArray(new Object[0]));

builder.addStatement("return this");
builder.addAnnotation(AnnotationSpec.builder(NON_NULL_CLASS_NAME).build());
builder.addAnnotation(AnnotationSpec.builder(CHECK_RESULT_CLASS_NAME).build());

List<MethodAndStaticVar> result = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,14 @@ public void compilation_withRequestOptionsReturnValue_succeeds() {
JavaFileObjects.forSourceLines(
"Extension",
"package com.bumptech.glide.test;",
"import android.support.annotation.NonNull;",
"import com.bumptech.glide.annotation.GlideExtension;",
"import com.bumptech.glide.annotation.GlideOption;",
"import com.bumptech.glide.request.RequestOptions;",
"@GlideExtension",
"public class Extension {",
" private Extension() {}",
" @NonNull",
" @GlideOption",
" public static RequestOptions doSomething(RequestOptions options) {",
" return options;",
Expand All @@ -268,16 +270,44 @@ public void compilation_withNonRequestOptionsReturnValue_fails() {
JavaFileObjects.forSourceLines(
"Extension",
"package com.bumptech.glide.test;",
"import android.support.annotation.NonNull;",
"import com.bumptech.glide.annotation.GlideExtension;",
"import com.bumptech.glide.annotation.GlideOption;",
"import com.bumptech.glide.request.RequestOptions;",
"@GlideExtension",
"public class Extension {",
" private Extension() {}",
" @NonNull",
" @GlideOption",
" public static Object doSomething(RequestOptions options) {",
" return options;",
" }",
"}"));
}

@Test
public void compilation_withMissingNonNullAnnotation_warns() {
Compilation compilation = javac()
.withProcessors(new GlideAnnotationProcessor())
.compile(
emptyAppModule(),
JavaFileObjects.forSourceLines(
"Extension",
"package com.bumptech.glide.test;",
"import com.bumptech.glide.annotation.GlideExtension;",
"import com.bumptech.glide.annotation.GlideOption;",
"import com.bumptech.glide.request.RequestOptions;",
"@GlideExtension",
"public class Extension {",
" private Extension() {}",
" @GlideOption",
" public static RequestOptions doSomething(RequestOptions options) {",
" return options;",
" }",
"}"));
assertThat(compilation).succeeded();
assertThat(compilation).hadWarningCount(1);
assertThat(compilation).hadWarningContaining("@NonNull");
assertThat(compilation).hadWarningContaining("com.bumptech.glide.test.Extension#doSomething");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bumptech.glide.test;

import android.support.annotation.NonNull;
import com.bumptech.glide.annotation.GlideExtension;
import com.bumptech.glide.annotation.GlideOption;
import com.bumptech.glide.request.RequestOptions;
Expand All @@ -11,6 +12,7 @@ private Extension() {
// Utility class.
}

@NonNull
@GlideOption(memoizeStaticMethod = true)
public static RequestOptions test(RequestOptions requestOptions) {
return requestOptions.centerCrop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ public final GlideOptions autoClone() {
/**
* @see Extension#test(RequestOptions)
*/
@NonNull
@CheckResult
public GlideOptions test() {
return (GlideOptions) Extension.test(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,7 @@ public GlideRequest<TranscodeType> dontAnimate() {
/**
* @see GlideOptions#test()
*/
@NonNull
@CheckResult
public GlideRequest<TranscodeType> test() {
if (getMutableOptions() instanceof GlideOptions) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bumptech.glide.test;

import android.support.annotation.NonNull;
import com.bumptech.glide.annotation.GlideExtension;
import com.bumptech.glide.annotation.GlideOption;
import com.bumptech.glide.request.RequestOptions;
Expand All @@ -11,6 +12,7 @@ private Extension() {
// Utility class.
}

@NonNull
@GlideOption(override = GlideOption.OVERRIDE_EXTEND)
public static RequestOptions centerCrop(RequestOptions requestOptions) {
return requestOptions.centerCrop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ public final GlideOptions autoClone() {
* @see RequestOptions#centerCrop()
*/
@Override
@NonNull
@CheckResult
public GlideOptions centerCrop() {
return (GlideOptions) Extension.centerCrop(super.centerCrop());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,7 @@ public GlideRequest<TranscodeType> dontAnimate() {
/**
* @see GlideOptions#centerCrop()
*/
@NonNull
@CheckResult
public GlideRequest<TranscodeType> centerCrop() {
if (getMutableOptions() instanceof GlideOptions) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bumptech.glide.test;

import android.support.annotation.NonNull;
import com.bumptech.glide.annotation.GlideExtension;
import com.bumptech.glide.annotation.GlideOption;
import com.bumptech.glide.request.RequestOptions;
Expand All @@ -11,6 +12,7 @@ private Extension() {
// Utility class.
}

@NonNull
@GlideOption(override = GlideOption.OVERRIDE_EXTEND)
public static RequestOptions override(RequestOptions requestOptions, int width, int height) {
return requestOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,7 @@ public final GlideOptions autoClone() {
* @see RequestOptions#override(int, int)
*/
@Override
@NonNull
@CheckResult
public GlideOptions override(int width, int height) {
return (GlideOptions) Extension.override(super.override(width, height), width, height);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,7 @@ public GlideRequest<TranscodeType> dontAnimate() {
/**
* @see GlideOptions#override(int, int)
*/
@NonNull
@CheckResult
public GlideRequest<TranscodeType> override(int width, int height) {
if (getMutableOptions() instanceof GlideOptions) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bumptech.glide.test;

import android.support.annotation.NonNull;
import com.bumptech.glide.annotation.GlideExtension;
import com.bumptech.glide.annotation.GlideOption;
import com.bumptech.glide.request.RequestOptions;
Expand All @@ -11,6 +12,7 @@ private Extension() {
// Utility class.
}

@NonNull
@GlideOption(override = GlideOption.OVERRIDE_REPLACE)
public static RequestOptions centerCrop(RequestOptions requestOptions) {
return requestOptions.centerCrop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@ public final GlideOptions autoClone() {
/**
* @see Extension#centerCrop(RequestOptions)
*/
@NonNull
@CheckResult
public GlideOptions centerCrop() {
return (GlideOptions) Extension.centerCrop(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,7 @@ public GlideRequest<TranscodeType> dontAnimate() {
/**
* @see GlideOptions#centerCrop()
*/
@NonNull
@CheckResult
public GlideRequest<TranscodeType> centerCrop() {
if (getMutableOptions() instanceof GlideOptions) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bumptech.glide.test;

import android.support.annotation.NonNull;
import com.bumptech.glide.annotation.GlideExtension;
import com.bumptech.glide.annotation.GlideOption;
import com.bumptech.glide.request.RequestOptions;
Expand All @@ -11,6 +12,7 @@ private Extension() {
// Utility class.
}

@NonNull
@GlideOption(skipStaticMethod = true)
public static RequestOptions test(RequestOptions requestOptions) {
return requestOptions.centerCrop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ public final GlideOptions autoClone() {
/**
* @see Extension#test(RequestOptions)
*/
@NonNull
@CheckResult
public GlideOptions test() {
return (GlideOptions) Extension.test(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,7 @@ public GlideRequest<TranscodeType> dontAnimate() {
/**
* @see GlideOptions#test()
*/
@NonNull
@CheckResult
public GlideRequest<TranscodeType> test() {
if (getMutableOptions() instanceof GlideOptions) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bumptech.glide.test;

import android.support.annotation.NonNull;
import com.bumptech.glide.annotation.GlideExtension;
import com.bumptech.glide.annotation.GlideOption;
import com.bumptech.glide.request.RequestOptions;
Expand All @@ -11,6 +12,7 @@ private Extension() {
// Utility class.
}

@NonNull
@GlideOption(staticMethodName = "testSomething")
public static RequestOptions test(RequestOptions requestOptions) {
return requestOptions.centerCrop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ public final GlideOptions autoClone() {
/**
* @see Extension#test(RequestOptions)
*/
@NonNull
@CheckResult
public GlideOptions test() {
return (GlideOptions) Extension.test(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,7 @@ public GlideRequest<TranscodeType> dontAnimate() {
/**
* @see GlideOptions#test()
*/
@NonNull
@CheckResult
public GlideRequest<TranscodeType> test() {
if (getMutableOptions() instanceof GlideOptions) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bumptech.glide.test;

import android.support.annotation.NonNull;
import com.bumptech.glide.annotation.GlideExtension;
import com.bumptech.glide.annotation.GlideOption;
import com.bumptech.glide.request.RequestOptions;
Expand All @@ -11,6 +12,7 @@ private ExtensionWithOption() {
// Utility class.
}

@NonNull
@GlideOption
public static RequestOptions squareThumb(RequestOptions requestOptions) {
return requestOptions.centerCrop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ public final GlideOptions autoClone() {
/**
* @see ExtensionWithOption#squareThumb(RequestOptions)
*/
@NonNull
@CheckResult
public GlideOptions squareThumb() {
return (GlideOptions) ExtensionWithOption.squareThumb(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,7 @@ public GlideRequest<TranscodeType> dontAnimate() {
/**
* @see GlideOptions#squareThumb()
*/
@NonNull
@CheckResult
public GlideRequest<TranscodeType> squareThumb() {
if (getMutableOptions() instanceof GlideOptions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ public final GlideOptions autoClone() {
/**
* @see Extension#test(RequestOptions)
*/
@NonNull
@CheckResult
public GlideOptions test() {
if (isAutoCloneEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,7 @@ public GlideRequest<TranscodeType> dontAnimate() {
/**
* @see GlideOptions#test()
*/
@NonNull
@CheckResult
public GlideRequest<TranscodeType> test() {
if (getMutableOptions() instanceof GlideOptions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ public final GlideOptions autoClone() {
* @see RequestOptions#centerCrop()
*/
@Override
@NonNull
@CheckResult
public GlideOptions centerCrop() {
if (isAutoCloneEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,7 @@ public GlideRequest<TranscodeType> dontAnimate() {
/**
* @see GlideOptions#centerCrop()
*/
@NonNull
@CheckResult
public GlideRequest<TranscodeType> centerCrop() {
if (getMutableOptions() instanceof GlideOptions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@ public final GlideOptions autoClone() {
/**
* @see Extension#centerCrop(RequestOptions)
*/
@NonNull
@CheckResult
public GlideOptions centerCrop() {
if (isAutoCloneEnabled()) {
Expand Down
Loading

0 comments on commit 62d7464

Please sign in to comment.