Search in sources :

Example 81 with MethodSpec

use of com.squareup.javapoet.MethodSpec in project glide by bumptech.

the class RootModuleGenerator method generateRegisterComponents.

private static MethodSpec generateRegisterComponents(Set<String> childGlideModuleClassNames, Set<String> excludedGlideModuleClassNames) {
    MethodSpec.Builder registerComponents = MethodSpec.methodBuilder("registerComponents").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addParameter(ClassName.get("android.content", "Context"), "context").addParameter(ClassName.get("com.bumptech.glide", "Registry"), "registry");
    for (String glideModule : childGlideModuleClassNames) {
        if (excludedGlideModuleClassNames.contains(glideModule)) {
            continue;
        }
        ClassName moduleClassName = ClassName.bestGuess(glideModule);
        registerComponents.addStatement("new $T().registerComponents(context, registry)", moduleClassName);
    }
    // Order matters here. The RootGlideModule must be called last.
    registerComponents.addStatement("rootGlideModule.registerComponents(context, registry)");
    return registerComponents.build();
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ClassName(com.squareup.javapoet.ClassName)

Example 82 with MethodSpec

use of com.squareup.javapoet.MethodSpec in project DeepLinkDispatch by airbnb.

the class DeepLinkProcessor method generateDeepLinkLoader.

private void generateDeepLinkLoader(String packageName, String className, List<DeepLinkAnnotatedElement> elements) throws IOException {
    CodeBlock.Builder initializer = CodeBlock.builder().add("$T.unmodifiableList($T.asList(\n", CLASS_COLLECTIONS, CLASS_ARRAYS).indent();
    int totalElements = elements.size();
    for (int i = 0; i < totalElements; i++) {
        DeepLinkAnnotatedElement element = elements.get(i);
        String type = "DeepLinkEntry.Type." + element.getAnnotationType().toString();
        ClassName activity = ClassName.get(element.getAnnotatedElement());
        Object method = element.getMethod() == null ? null : element.getMethod();
        String uri = element.getUri();
        initializer.add("new DeepLinkEntry($S, $L, $T.class, $S)$L\n", uri, type, activity, method, (i < totalElements - 1) ? "," : "");
    }
    FieldSpec registry = FieldSpec.builder(ParameterizedTypeName.get(List.class, DeepLinkEntry.class), "REGISTRY", Modifier.PUBLIC, Modifier.FINAL, Modifier.STATIC).initializer(initializer.unindent().add("))").build()).build();
    MethodSpec parseMethod = MethodSpec.methodBuilder("parseUri").addModifiers(Modifier.PUBLIC).addAnnotation(AnnotationSpec.builder(Override.class).build()).addParameter(String.class, "uri").returns(DeepLinkEntry.class).beginControlFlow("for (DeepLinkEntry entry : REGISTRY)").beginControlFlow("if (entry.matches(uri))").addStatement("return entry").endControlFlow().endControlFlow().addStatement("return null").build();
    TypeSpec deepLinkLoader = TypeSpec.classBuilder(className + "Loader").addModifiers(Modifier.PUBLIC, Modifier.FINAL).addSuperinterface(ClassName.get(Parser.class)).addField(registry).addMethod(parseMethod).build();
    JavaFile.builder(packageName, deepLinkLoader).build().writeTo(filer);
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) CodeBlock(com.squareup.javapoet.CodeBlock) FieldSpec(com.squareup.javapoet.FieldSpec) ClassName(com.squareup.javapoet.ClassName) List(java.util.List) ArrayList(java.util.ArrayList) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 83 with MethodSpec

use of com.squareup.javapoet.MethodSpec in project epoxy by airbnb.

the class EpoxyProcessor method addDefaultLayoutMethodIfNeeded.

/**
   * If there is no existing implementation of getDefaultLayout we can generate an implementation.
   * This relies on a layout res being set in the @EpoxyModelClass annotation.
   */
private void addDefaultLayoutMethodIfNeeded(TypeElement originalClassElement, List<MethodSpec> methods) {
    MethodSpec getDefaultLayoutMethod = MethodSpec.methodBuilder(GET_DEFAULT_LAYOUT_METHOD_NAME).addAnnotation(Override.class).addAnnotation(LayoutRes.class).addModifiers(Modifier.PROTECTED).returns(TypeName.INT).build();
    if (implementsMethod(originalClassElement, getDefaultLayoutMethod, typeUtils)) {
        return;
    }
    EpoxyModelClass annotation = findClassAnnotationWithLayout(originalClassElement);
    if (annotation == null) {
        logError("Model must use %s annotation if it does not implement %s. (class: %s)", EpoxyModelClass.class, GET_DEFAULT_LAYOUT_METHOD_NAME, originalClassElement.getSimpleName());
        return;
    }
    int layoutRes;
    try {
        layoutRes = annotation.layout();
    } catch (AnnotationTypeMismatchException e) {
        logError("Invalid layout value in %s annotation. (class: %s). %s: %s", EpoxyModelClass.class, originalClassElement.getSimpleName(), e.getClass().getSimpleName(), e.getMessage());
        return;
    }
    if (layoutRes == 0) {
        logError("Model must specify a valid layout resource in the %s annotation. (class: %s)", EpoxyModelClass.class, originalClassElement.getSimpleName());
        return;
    }
    AndroidResource layoutResource = resourceProcessor.getResourceForValue(layoutRes);
    getDefaultLayoutMethod = getDefaultLayoutMethod.toBuilder().addStatement("return $L", layoutResource.code).build();
    methods.add(getDefaultLayoutMethod);
}
Also used : LayoutRes(android.support.annotation.LayoutRes) MethodSpec(com.squareup.javapoet.MethodSpec) AnnotationTypeMismatchException(java.lang.annotation.AnnotationTypeMismatchException)

Example 84 with MethodSpec

use of com.squareup.javapoet.MethodSpec in project storio by pushtorefresh.

the class GetResolverGenerator method createMapFromCursorMethodSpec.

@NotNull
private MethodSpec createMapFromCursorMethodSpec(@NotNull StorIOContentResolverTypeMeta storIOContentResolverTypeMeta, @NotNull ClassName storIOContentResolverTypeClassName) {
    final MethodSpec.Builder builder = MethodSpec.methodBuilder("mapFromCursor").addJavadoc("{@inheritDoc}\n").addAnnotation(Override.class).addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME).addModifiers(PUBLIC).returns(storIOContentResolverTypeClassName).addParameter(ParameterSpec.builder(ClassName.get("android.database", "Cursor"), "cursor").addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME).build()).addStatement("$T object = new $T()", storIOContentResolverTypeClassName, storIOContentResolverTypeClassName).addCode("\n");
    for (final StorIOContentResolverColumnMeta columnMeta : storIOContentResolverTypeMeta.columns.values()) {
        final String columnIndex = "cursor.getColumnIndex(\"" + columnMeta.storIOColumn.name() + "\")";
        final JavaType javaType = columnMeta.javaType;
        final String getFromCursor = getFromCursorString(columnMeta, javaType, columnIndex);
        final boolean isBoxed = javaType.isBoxedType();
        if (isBoxed) {
            // otherwise -> if primitive and value from cursor null -> fail early
            builder.beginControlFlow("if (!cursor.isNull($L))", columnIndex);
        }
        builder.addStatement("object.$L = cursor.$L", columnMeta.elementName, getFromCursor);
        if (isBoxed) {
            builder.endControlFlow();
        }
    }
    return builder.addCode("\n").addStatement("return object").build();
}
Also used : JavaType(com.pushtorefresh.storio.common.annotations.processor.introspection.JavaType) MethodSpec(com.squareup.javapoet.MethodSpec) StorIOContentResolverColumnMeta(com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverColumnMeta) Common.getFromCursorString(com.pushtorefresh.storio.common.annotations.processor.generate.Common.getFromCursorString) NotNull(org.jetbrains.annotations.NotNull)

Example 85 with MethodSpec

use of com.squareup.javapoet.MethodSpec in project storio by pushtorefresh.

the class PutResolverGenerator method createMapToContentValuesMethodSpec.

@NotNull
private MethodSpec createMapToContentValuesMethodSpec(@NotNull final StorIOContentResolverTypeMeta storIOContentResolverTypeMeta, @NotNull final ClassName storIOContentResolverClassName) {
    MethodSpec.Builder builder = MethodSpec.methodBuilder("mapToContentValues").addJavadoc("{@inheritDoc}\n").addAnnotation(Override.class).addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME).addModifiers(PUBLIC).returns(ClassName.get("android.content", "ContentValues")).addParameter(ParameterSpec.builder(storIOContentResolverClassName, "object").addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME).build()).addStatement("ContentValues contentValues = new ContentValues($L)", storIOContentResolverTypeMeta.columns.size()).addCode("\n");
    for (final StorIOContentResolverColumnMeta columnMeta : storIOContentResolverTypeMeta.columns.values()) {
        final boolean ignoreNull = columnMeta.storIOColumn.ignoreNull();
        if (ignoreNull) {
            builder.beginControlFlow("if (object.$L != null)", columnMeta.elementName + (columnMeta.isMethod() ? "()" : ""));
        }
        builder.addStatement("contentValues.put($S, object.$L)", columnMeta.storIOColumn.name(), columnMeta.elementName + (columnMeta.isMethod() ? "()" : ""));
        if (ignoreNull) {
            builder.endControlFlow();
        }
    }
    return builder.addCode("\n").addStatement("return contentValues").build();
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) StorIOContentResolverColumnMeta(com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverColumnMeta) NotNull(org.jetbrains.annotations.NotNull)

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