use of com.squareup.javapoet.MethodSpec in project glide by bumptech.
the class AppModuleGenerator method generateConstructor.
private MethodSpec generateConstructor(ClassName appGlideModule, Collection<String> libraryGlideModuleClassNames, Collection<String> excludedGlideModuleClassNames) {
MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder();
constructorBuilder.addStatement("appGlideModule = new $T()", appGlideModule);
ClassName androidLogName = ClassName.get("android.util", "Log");
// Add some log lines to indicate to developers which modules where discovered.
constructorBuilder.beginControlFlow("if ($T.isLoggable($S, $T.DEBUG))", androidLogName, GLIDE_LOG_TAG, androidLogName);
constructorBuilder.addStatement("$T.d($S, $S)", androidLogName, GLIDE_LOG_TAG, "Discovered AppGlideModule from annotation: " + appGlideModule);
// Excluded GlideModule classes from the manifest are logged in Glide's singleton.
for (String glideModule : libraryGlideModuleClassNames) {
if (excludedGlideModuleClassNames.contains(glideModule)) {
constructorBuilder.addStatement("$T.d($S, $S)", androidLogName, GLIDE_LOG_TAG, "AppGlideModule excludes LibraryGlideModule from annotation: " + glideModule);
} else {
constructorBuilder.addStatement("$T.d($S, $S)", androidLogName, GLIDE_LOG_TAG, "Discovered LibraryGlideModule from annotation: " + glideModule);
}
}
constructorBuilder.endControlFlow();
return constructorBuilder.build();
}
use of com.squareup.javapoet.MethodSpec in project glide by bumptech.
the class AppModuleGenerator method generateRegisterComponents.
private MethodSpec generateRegisterComponents(Collection<String> libraryGlideModuleClassNames, Collection<String> excludedGlideModuleClassNames) {
MethodSpec.Builder registerComponents = MethodSpec.methodBuilder("registerComponents").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).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)) {
continue;
}
ClassName moduleClassName = ClassName.bestGuess(glideModule);
registerComponents.addStatement("new $T().registerComponents(context, glide, registry)", moduleClassName);
}
// Order matters here. The AppGlideModule must be called last.
registerComponents.addStatement("appGlideModule.registerComponents(context, glide, registry)");
return registerComponents.build();
}
use of com.squareup.javapoet.MethodSpec in project glide by bumptech.
the class GlideGenerator method overrideGlideStaticMethod.
private MethodSpec overrideGlideStaticMethod(ExecutableElement methodToOverride) {
List<ParameterSpec> parameters = ProcessorUtil.getParameters(methodToOverride);
TypeElement element = (TypeElement) processingEnv.getTypeUtils().asElement(methodToOverride.getReturnType());
MethodSpec.Builder builder = MethodSpec.methodBuilder(methodToOverride.getSimpleName().toString()).addModifiers(Modifier.PUBLIC, Modifier.STATIC).addJavadoc(processorUtil.generateSeeMethodJavadoc(methodToOverride)).addParameters(parameters);
addReturnAnnotations(builder, methodToOverride);
boolean returnsValue = element != null;
if (returnsValue) {
builder.returns(ClassName.get(element));
}
StringBuilder code = new StringBuilder(returnsValue ? "return " : "");
code.append("$T.$N(");
List<Object> args = new ArrayList<>();
args.add(ClassName.get(glideType));
args.add(methodToOverride.getSimpleName());
if (!parameters.isEmpty()) {
for (ParameterSpec param : parameters) {
code.append("$L, ");
args.add(param.name);
}
code = new StringBuilder(code.substring(0, code.length() - 2));
}
code.append(")");
builder.addStatement(code.toString(), args.toArray(new Object[0]));
return builder.build();
}
use of com.squareup.javapoet.MethodSpec in project glide by bumptech.
the class RequestBuilderGenerator method generateConstructors.
private List<MethodSpec> generateConstructors() {
ParameterizedTypeName classOfTranscodeType = ParameterizedTypeName.get(ClassName.get(Class.class), transcodeTypeName);
TypeName wildcardOfObject = WildcardTypeName.subtypeOf(Object.class);
ParameterizedTypeName requestBuilderOfWildcardOfObject = ParameterizedTypeName.get(ClassName.get(requestBuilderType), wildcardOfObject);
MethodSpec firstConstructor = MethodSpec.constructorBuilder().addParameter(ParameterSpec.builder(classOfTranscodeType, "transcodeClass").addAnnotation(nonNull()).build()).addParameter(ParameterSpec.builder(requestBuilderOfWildcardOfObject, "other").addAnnotation(nonNull()).build()).addStatement("super($N, $N)", "transcodeClass", "other").build();
ClassName context = ClassName.get("android.content", "Context");
ClassName glide = ClassName.get("com.bumptech.glide", "Glide");
ClassName requestManager = ClassName.get("com.bumptech.glide", "RequestManager");
MethodSpec secondConstructor = MethodSpec.constructorBuilder().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();
return ImmutableList.of(firstConstructor, secondConstructor);
}
use of com.squareup.javapoet.MethodSpec in project glide by bumptech.
the class RequestBuilderGenerator method generateRequestBuilderOverride.
/**
* Generates an override of a particular method in {@code com.bumptech.glide.RequestBuilder} that
* returns {@code com.bumptech.glide.RequestBuilder} so that it returns our generated subclass
* instead.
*/
private MethodSpec generateRequestBuilderOverride(ExecutableElement methodToOverride) {
// We've already verified that this method returns a RequestBuilder and RequestBuilders have
// exactly one type argument, so this is safe unless those assumptions change.
TypeMirror typeArgument = ((DeclaredType) methodToOverride.getReturnType()).getTypeArguments().get(0);
ParameterizedTypeName generatedRequestBuilderOfType = ParameterizedTypeName.get(generatedRequestBuilderClassName, ClassName.get(typeArgument));
MethodSpec.Builder builder = ProcessorUtil.overriding(methodToOverride).returns(generatedRequestBuilderOfType);
builder.addCode(CodeBlock.builder().add("return ($T) super.$N(", generatedRequestBuilderOfType, methodToOverride.getSimpleName()).add(FluentIterable.from(builder.build().parameters).transform(new Function<ParameterSpec, String>() {
@Override
public String apply(ParameterSpec input) {
return input.name;
}
}).join(Joiner.on(", "))).add(");\n").build());
for (AnnotationMirror mirror : methodToOverride.getAnnotationMirrors()) {
builder = builder.addAnnotation(AnnotationSpec.get(mirror));
}
if (methodToOverride.isVarArgs()) {
builder = builder.addModifiers(Modifier.FINAL).addAnnotation(SafeVarargs.class).addAnnotation(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "varargs").build());
}
return builder.build();
}
Aggregations