use of com.squareup.javapoet.MethodSpec in project Rocket by mozilla-tw.
the class AppModuleGenerator method generateGetExcludedModuleClasses.
// TODO: When we drop support for parsing GlideModules from AndroidManifests, remove this method.
private MethodSpec generateGetExcludedModuleClasses(Set<String> excludedClassNames) {
TypeName wildCardOfObject = WildcardTypeName.subtypeOf(Object.class);
ParameterizedTypeName classOfWildcardOfObjet = ParameterizedTypeName.get(ClassName.get(Class.class), wildCardOfObject);
ParameterizedTypeName setOfClassOfWildcardOfObject = ParameterizedTypeName.get(ClassName.get(Set.class), classOfWildcardOfObjet);
ParameterizedTypeName hashSetOfClassOfWildcardOfObject = ParameterizedTypeName.get(ClassName.get(HashSet.class), classOfWildcardOfObjet);
MethodSpec.Builder builder = MethodSpec.methodBuilder("getExcludedModuleClasses").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).returns(setOfClassOfWildcardOfObject);
if (excludedClassNames.isEmpty()) {
builder.addStatement("return $T.emptySet()", Collections.class);
} else {
builder.addStatement("$T excludedClasses = new $T()", setOfClassOfWildcardOfObject, hashSetOfClassOfWildcardOfObject);
for (String excludedClassName : excludedClassNames) {
// TODO: Remove this when we no longer support manifest parsing.
// Using a Literal ($L) instead of a type ($T) to get a fully qualified import that allows
// us to suppress deprecation warnings. Aimed at deprecated GlideModules.
builder.addStatement("excludedClasses.add($L.class)", excludedClassName);
}
builder.addStatement("return excludedClasses");
}
return builder.build();
}
use of com.squareup.javapoet.MethodSpec in project Rocket by mozilla-tw.
the class AppModuleGenerator method generate.
TypeSpec generate(TypeElement appGlideModule, Set<String> libraryGlideModuleClassNames) {
ClassName appGlideModuleClassName = ClassName.get(appGlideModule);
Set<String> excludedGlideModuleClassNames = getExcludedGlideModuleClassNames(appGlideModule);
MethodSpec constructor = generateConstructor(appGlideModuleClassName, libraryGlideModuleClassNames, excludedGlideModuleClassNames);
MethodSpec registerComponents = generateRegisterComponents(libraryGlideModuleClassNames, excludedGlideModuleClassNames);
MethodSpec getExcludedModuleClasses = generateGetExcludedModuleClasses(excludedGlideModuleClassNames);
MethodSpec applyOptions = MethodSpec.methodBuilder("applyOptions").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addParameter(ClassName.get("android.content", "Context"), "context").addParameter(ClassName.get("com.bumptech.glide", "GlideBuilder"), "builder").addStatement("appGlideModule.applyOptions(context, builder)", appGlideModule).build();
MethodSpec isManifestParsingEnabled = MethodSpec.methodBuilder("isManifestParsingEnabled").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).returns(boolean.class).addStatement("return appGlideModule.isManifestParsingEnabled()", appGlideModule).build();
Builder builder = TypeSpec.classBuilder(GENERATED_APP_MODULE_IMPL_SIMPLE_NAME).addModifiers(Modifier.FINAL).addAnnotation(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "deprecation").build()).superclass(ClassName.get(GENERATED_ROOT_MODULE_PACKAGE_NAME, GENERATED_ROOT_MODULE_SIMPLE_NAME)).addField(appGlideModuleClassName, "appGlideModule", Modifier.PRIVATE, Modifier.FINAL).addMethod(constructor).addMethod(applyOptions).addMethod(registerComponents).addMethod(isManifestParsingEnabled).addMethod(getExcludedModuleClasses);
ClassName generatedRequestManagerFactoryClassName = ClassName.get(RequestManagerFactoryGenerator.GENERATED_REQUEST_MANAGER_FACTORY_PACKAGE_NAME, RequestManagerFactoryGenerator.GENERATED_REQUEST_MANAGER_FACTORY_SIMPLE_NAME);
builder.addMethod(MethodSpec.methodBuilder("getRequestManagerFactory").addAnnotation(Override.class).returns(generatedRequestManagerFactoryClassName).addStatement("return new $T()", generatedRequestManagerFactoryClassName).build());
return builder.build();
}
use of com.squareup.javapoet.MethodSpec in project Rocket by mozilla-tw.
the class GlideGenerator method overrideGlideStaticMethod.
private MethodSpec overrideGlideStaticMethod(ExecutableElement methodToOverride) {
List<? extends VariableElement> parameters = methodToOverride.getParameters();
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(Lists.transform(parameters, new Function<VariableElement, ParameterSpec>() {
@Override
public ParameterSpec apply(VariableElement input) {
return ParameterSpec.get(input);
}
}));
TypeElement visibleForTestingType = processingEnv.getElementUtils().getTypeElement(VISIBLE_FOR_TESTING_QUALIFIED_NAME);
for (AnnotationMirror mirror : methodToOverride.getAnnotationMirrors()) {
builder.addAnnotation(AnnotationSpec.get(mirror));
// See #1977.
if (mirror.getAnnotationType().asElement().equals(visibleForTestingType)) {
builder.addAnnotation(AnnotationSpec.builder(ClassName.get(SUPPRESS_LINT_PACKAGE_NAME, SUPPRESS_LINT_CLASS_NAME)).addMember("value", "$S", "VisibleForTests").build());
}
}
boolean returnsValue = element != null;
if (returnsValue) {
builder.returns(ClassName.get(element));
}
String code = returnsValue ? "return " : "";
code += "$T.$N(";
List<Object> args = new ArrayList<>();
args.add(ClassName.get(glideType));
args.add(methodToOverride.getSimpleName());
if (!parameters.isEmpty()) {
for (VariableElement param : parameters) {
code += "$L, ";
args.add(param.getSimpleName());
}
code = code.substring(0, code.length() - 2);
}
code += ")";
builder.addStatement(code, args.toArray(new Object[0]));
return builder.build();
}
use of com.squareup.javapoet.MethodSpec in project Rocket by mozilla-tw.
the class RequestBuilderGenerator method generateRequestBuilderOverride.
/**
* Generates an override of a particular method in {@link com.bumptech.glide.RequestBuilder} that
* returns {@link 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 = MethodSpec.overriding(methodToOverride).returns(generatedRequestBuilderOfType).addCode(CodeBlock.builder().add("return ($T) super.$N(", generatedRequestBuilderOfType, methodToOverride.getSimpleName()).add(FluentIterable.from(methodToOverride.getParameters()).transform(new Function<VariableElement, String>() {
@Override
public String apply(VariableElement input) {
return input.getSimpleName().toString();
}
}).join(Joiner.on(", "))).add(");\n").build());
for (AnnotationMirror mirror : methodToOverride.getAnnotationMirrors()) {
builder.addAnnotation(AnnotationSpec.get(mirror));
}
return builder.build();
}
use of com.squareup.javapoet.MethodSpec in project Rocket by mozilla-tw.
the class RequestOptionsGenerator method generate.
TypeSpec generate(String generatedCodePackageName, Set<String> glideExtensionClassNames) {
glideOptionsName = ClassName.get(generatedCodePackageName, GENERATED_REQUEST_OPTIONS_SIMPLE_NAME);
List<MethodAndStaticVar> methodsForExtensions = generateMethodsForExtensions(glideExtensionClassNames);
Set<MethodSignature> extensionMethodSignatures = ImmutableSet.copyOf(Iterables.transform(methodsForExtensions, new Function<MethodAndStaticVar, MethodSignature>() {
@Nullable
@Override
public MethodSignature apply(MethodAndStaticVar f) {
return new MethodSignature(f.method);
}
}));
List<MethodAndStaticVar> staticOverrides = generateStaticMethodOverridesForRequestOptions();
List<MethodSpec> instanceOverrides = generateInstanceMethodOverridesForRequestOptions();
List<MethodAndStaticVar> allMethodsAndStaticVars = new ArrayList<>();
for (MethodAndStaticVar item : staticOverrides) {
if (extensionMethodSignatures.contains(new MethodSignature(item.method))) {
continue;
}
allMethodsAndStaticVars.add(item);
}
for (MethodSpec methodSpec : instanceOverrides) {
if (extensionMethodSignatures.contains(new MethodSignature(methodSpec))) {
continue;
}
allMethodsAndStaticVars.add(new MethodAndStaticVar(methodSpec));
}
allMethodsAndStaticVars.addAll(methodsForExtensions);
TypeSpec.Builder classBuilder = TypeSpec.classBuilder(GENERATED_REQUEST_OPTIONS_SIMPLE_NAME).addAnnotation(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "deprecation").build()).addJavadoc(generateClassJavadoc(glideExtensionClassNames)).addModifiers(Modifier.FINAL).addModifiers(Modifier.PUBLIC).addSuperinterface(Cloneable.class).superclass(requestOptionsName);
for (MethodAndStaticVar methodAndStaticVar : allMethodsAndStaticVars) {
if (methodAndStaticVar.method != null) {
classBuilder.addMethod(methodAndStaticVar.method);
}
if (methodAndStaticVar.staticField != null) {
classBuilder.addField(methodAndStaticVar.staticField);
}
}
return classBuilder.build();
}
Aggregations