Skip to content

Commit

Permalink
Add @checkresult to RequestManager methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
sjudd committed Oct 5, 2017
1 parent 739cb35 commit 28e461e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Set;
import javax.annotation.Nullable;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
Expand Down Expand Up @@ -59,6 +60,8 @@ final class RequestManagerGenerator {
"com.bumptech.glide.manager.Lifecycle";
private static final String REQUEST_MANAGER_TREE_NODE_QUALIFIED_NAME =
"com.bumptech.glide.manager.RequestManagerTreeNode";
private static final ClassName CHECK_RESULT_CLASS_NAME =
ClassName.get("android.support.annotation", "CheckResult");

private static final String GENERATED_REQUEST_MANAGER_SIMPLE_NAME =
"GlideRequests";
Expand Down Expand Up @@ -147,6 +150,7 @@ private MethodSpec generateAsMethod(String generatedCodePackageName, TypeSpec re
.addAnnotation(Override.class)
.addTypeVariable(TypeVariableName.get("ResourceType"))
.addParameter(classOfResouceType, "resourceClass")
.addAnnotation(AnnotationSpec.builder(CHECK_RESULT_CLASS_NAME).build())
.returns(requestBuilderOfResourceType)
.addStatement("return new $T<>(glide, this, resourceClass)",
this.generatedRequestBuilderClassName)
Expand All @@ -160,10 +164,6 @@ private List<MethodSpec> generateRequestManagerMethodOverrides() {
TypeMirror rawRequestBuilder = processingEnv.getTypeUtils()
.erasure(requestBuilderType.asType());

final TypeElement classType =
processingEnv.getElementUtils().getTypeElement(Class.class.getCanonicalName());
final TypeMirror rawClassType = processingEnv.getTypeUtils().erasure(classType.asType());

return FluentIterable.from(
processorUtil.findInstanceMethodsReturning(requestManagerType, rawRequestBuilder))
.filter(new Predicate<ExecutableElement>() {
Expand Down Expand Up @@ -195,7 +195,7 @@ private MethodSpec generateRequestManagerMethodOverride(ExecutableElement method
ParameterizedTypeName generatedRequestBuilderOfType =
ParameterizedTypeName.get(generatedRequestBuilderClassName, ClassName.get(typeArgument));

return MethodSpec.overriding(methodToOverride)
MethodSpec.Builder builder = MethodSpec.overriding(methodToOverride)
.returns(generatedRequestBuilderOfType)
.addCode(CodeBlock.builder()
.add("return ($T) super.$N(",
Expand All @@ -209,8 +209,12 @@ public String apply(VariableElement input) {
})
.join(Joiner.on(", ")))
.add(");\n")
.build())
.build();
.build());

for (AnnotationMirror mirror : methodToOverride.getAnnotationMirrors()) {
builder.addAnnotation(AnnotationSpec.get(mirror));
}
return builder.build();
}

private List<MethodSpec> generateAdditionalRequestManagerMethods(
Expand Down
9 changes: 9 additions & 0 deletions library/src/main/java/com/bumptech/glide/RequestManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
Expand Down Expand Up @@ -292,6 +293,7 @@ public void onDestroy() {
*
* @return A new request builder for loading a {@link android.graphics.Bitmap}
*/
@CheckResult
public RequestBuilder<Bitmap> asBitmap() {
return as(Bitmap.class).apply(DECODE_TYPE_BITMAP);
}
Expand All @@ -309,6 +311,7 @@ public RequestBuilder<Bitmap> asBitmap() {
* @return A new request builder for loading a
* {@link com.bumptech.glide.load.resource.gif.GifDrawable}.
*/
@CheckResult
public RequestBuilder<GifDrawable> asGif() {
return as(GifDrawable.class).apply(DECODE_TYPE_GIF);
}
Expand All @@ -323,6 +326,7 @@ public RequestBuilder<GifDrawable> asGif() {
*
* @return A new request builder for loading a {@link Drawable}.
*/
@CheckResult
public RequestBuilder<Drawable> asDrawable() {
return as(Drawable.class);
}
Expand All @@ -333,6 +337,7 @@ public RequestBuilder<Drawable> asDrawable() {
*
* @return A new request builder for loading a {@link Drawable} using the given model.
*/
@CheckResult
public RequestBuilder<Drawable> load(@Nullable Object model) {
return asDrawable().load(model);
}
Expand All @@ -348,6 +353,7 @@ public RequestBuilder<Drawable> load(@Nullable Object model) {
*
* @return A new request builder for downloading content to cache and returning the cache File.
*/
@CheckResult
public RequestBuilder<File> downloadOnly() {
return as(File.class).apply(DOWNLOAD_ONLY_OPTIONS);
}
Expand All @@ -358,6 +364,7 @@ public RequestBuilder<File> downloadOnly() {
*
* @return A new request builder for loading a {@link Drawable} using the given model.
*/
@CheckResult
public RequestBuilder<File> download(@Nullable Object model) {
return downloadOnly().load(model);
}
Expand All @@ -371,6 +378,7 @@ public RequestBuilder<File> download(@Nullable Object model) {
*
* @return A new request builder for obtaining File paths to content.
*/
@CheckResult
public RequestBuilder<File> asFile() {
return as(File.class).apply(skipMemoryCacheOf(true));
}
Expand All @@ -383,6 +391,7 @@ public RequestBuilder<File> asFile() {
* @param resourceClass The resource to decode.
* @return A new request builder for loading the given resource class.
*/
@CheckResult
public <ResourceType> RequestBuilder<ResourceType> as(Class<ResourceType> resourceClass) {
return new RequestBuilder<>(glide, this, resourceClass);
}
Expand Down

0 comments on commit 28e461e

Please sign in to comment.