Search in sources :

Example 36 with MethodSpec

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

Example 37 with MethodSpec

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

Example 38 with MethodSpec

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();
}
Also used : ParameterSpec(com.squareup.javapoet.ParameterSpec) MethodSpec(com.squareup.javapoet.MethodSpec) TypeElement(javax.lang.model.element.TypeElement) ArrayList(java.util.ArrayList) Builder(com.squareup.javapoet.MethodSpec.Builder)

Example 39 with MethodSpec

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

Example 40 with MethodSpec

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();
}
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) ParameterSpec(com.squareup.javapoet.ParameterSpec) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

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