Search in sources :

Example 21 with MethodSpec

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();
}
Also used : WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) Set(java.util.Set) HashSet(java.util.HashSet) MethodSpec(com.squareup.javapoet.MethodSpec) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) HashSet(java.util.HashSet)

Example 22 with MethodSpec

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();
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) Builder(com.squareup.javapoet.TypeSpec.Builder) ClassName(com.squareup.javapoet.ClassName)

Example 23 with MethodSpec

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();
}
Also used : Function(com.google.common.base.Function) AnnotationMirror(javax.lang.model.element.AnnotationMirror) MethodSpec(com.squareup.javapoet.MethodSpec) TypeElement(javax.lang.model.element.TypeElement) ArrayList(java.util.ArrayList) VariableElement(javax.lang.model.element.VariableElement)

Example 24 with MethodSpec

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();
}
Also used : Function(com.google.common.base.Function) AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeMirror(javax.lang.model.type.TypeMirror) MethodSpec(com.squareup.javapoet.MethodSpec) VariableElement(javax.lang.model.element.VariableElement) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 25 with MethodSpec

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();
}
Also used : Function(com.google.common.base.Function) MethodSpec(com.squareup.javapoet.MethodSpec) ArrayList(java.util.ArrayList) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

MethodSpec (com.squareup.javapoet.MethodSpec)155 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)43 TypeName (com.squareup.javapoet.TypeName)42 ArrayList (java.util.ArrayList)38 ClassName (com.squareup.javapoet.ClassName)34 TypeSpec (com.squareup.javapoet.TypeSpec)31 ParameterSpec (com.squareup.javapoet.ParameterSpec)24 CodeBlock (com.squareup.javapoet.CodeBlock)23 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)20 Nonnull (javax.annotation.Nonnull)17 TypeMirror (javax.lang.model.type.TypeMirror)15 WireField (com.squareup.wire.WireField)14 Field (com.squareup.wire.schema.Field)14 TypeElement (javax.lang.model.element.TypeElement)14 ByteString (okio.ByteString)13 JvmLanguages.builtInAdapterString (com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString)12 List (java.util.List)11 VariableElement (javax.lang.model.element.VariableElement)11 Utf8String (org.fisco.bcos.web3j.abi.datatypes.Utf8String)11 FieldSpec (com.squareup.javapoet.FieldSpec)10