Search in sources :

Example 6 with MethodSpec

use of com.squareup.javapoet.MethodSpec in project bazel by bazelbuild.

the class JavaCodeGeneratorHelper method parallelDepsMainClassHelper.

/**
   * Writes {@code count-1} class files to the directory {@code projectPath/com/example/deps(index)}
   * and one main class.
   */
static void parallelDepsMainClassHelper(int count, Path projectPath) throws IOException {
    MethodSpec.Builder callDepsBuilder = MethodSpec.methodBuilder("main").addModifiers(Modifier.PUBLIC, Modifier.STATIC).addParameter(String[].class, "args").returns(void.class);
    for (int i = 1; i < count; ++i) {
        ClassName callingClass = ClassName.get("com.example.deps" + i, "Deps" + i);
        callDepsBuilder.addStatement("$T.PrintSth()", callingClass);
    }
    MethodSpec callDeps = callDepsBuilder.build();
    TypeSpec klass = TypeSpec.classBuilder("Main").addModifiers(Modifier.PUBLIC, Modifier.FINAL).addMethod(callDeps).build();
    writeClassToDir(klass, "com.example.generated", projectPath);
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 7 with MethodSpec

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

the class RootModuleGenerator method generate.

static TypeSpec generate(ProcessingEnvironment processingEnv, String rootGlideModuleClassName, Set<String> childGlideModuleClassNames) {
    ClassName rootGlideModule = ClassName.bestGuess(rootGlideModuleClassName);
    Set<String> excludedGlideModuleClassNames = getExcludedGlideModuleClassNames(processingEnv, rootGlideModuleClassName);
    MethodSpec constructor = generateConstructor(rootGlideModule, childGlideModuleClassNames, excludedGlideModuleClassNames);
    MethodSpec registerComponents = generateRegisterComponents(childGlideModuleClassNames, 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("rootGlideModule.applyOptions(context, builder)", rootGlideModule).build();
    MethodSpec isManifestParsingEnabled = MethodSpec.methodBuilder("isManifestParsingEnabled").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).returns(boolean.class).addStatement("return rootGlideModule.isManifestParsingEnabled()", rootGlideModule).build();
    return TypeSpec.classBuilder(GENERATED_ROOT_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(rootGlideModule, "rootGlideModule", Modifier.PRIVATE, Modifier.FINAL).addMethod(constructor).addMethod(applyOptions).addMethod(registerComponents).addMethod(isManifestParsingEnabled).addMethod(getExcludedModuleClasses).build();
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ClassName(com.squareup.javapoet.ClassName)

Example 8 with MethodSpec

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

the class RootModuleGenerator method generateGetExcludedModuleClasses.

// TODO: When we drop support for parsing GlideModules from AndroidManifests, remove this method.
private static 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 9 with MethodSpec

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

the class GetResolverGenerator method createMapFromCursorWithCreatorMethodSpec.

@NotNull
private MethodSpec createMapFromCursorWithCreatorMethodSpec(@NotNull StorIOContentResolverTypeMeta storIOContentResolverTypeMeta, @NotNull ClassName storIOSQLiteTypeClassName) {
    final MethodSpec.Builder builder = MethodSpec.methodBuilder("mapFromCursor").addJavadoc("{@inheritDoc}\n").addAnnotation(Override.class).addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME).addModifiers(PUBLIC).returns(storIOSQLiteTypeClassName).addParameter(ParameterSpec.builder(ClassName.get("android.database", "Cursor"), "cursor").addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME).build()).addCode("\n");
    final StringBuilder paramsBuilder = new StringBuilder();
    paramsBuilder.append("(");
    boolean first = true;
    for (final StorIOContentResolverColumnMeta columnMeta : storIOContentResolverTypeMeta.getOrderedColumns()) {
        final String columnIndex = "cursor.getColumnIndex(\"" + columnMeta.storIOColumn.name() + "\")";
        final JavaType javaType = columnMeta.javaType;
        final String getFromCursor = getFromCursorString(columnMeta, javaType, columnIndex);
        final TypeName name = TypeName.get(((ExecutableElement) columnMeta.element).getReturnType());
        final boolean isBoxed = javaType.isBoxedType();
        if (isBoxed) {
            // otherwise -> if primitive and value from cursor null -> fail early
            builder.addStatement("$T $L = null", name, columnMeta.getRealElementName());
            builder.beginControlFlow("if (!cursor.isNull($L))", columnIndex);
            builder.addStatement("$L = cursor.$L", columnMeta.getRealElementName(), getFromCursor);
            builder.endControlFlow();
        } else {
            builder.addStatement("$T $L = cursor.$L", name, columnMeta.getRealElementName(), getFromCursor);
        }
        if (!first) {
            paramsBuilder.append(", ");
        }
        first = false;
        paramsBuilder.append(columnMeta.getRealElementName());
    }
    paramsBuilder.append(")");
    builder.addCode("\n");
    if (storIOContentResolverTypeMeta.creator.getKind() == ElementKind.CONSTRUCTOR) {
        builder.addStatement("$T object = new $T" + paramsBuilder.toString(), storIOSQLiteTypeClassName, storIOSQLiteTypeClassName);
    } else {
        builder.addStatement("$T object = $T.$L", storIOSQLiteTypeClassName, storIOSQLiteTypeClassName, storIOContentResolverTypeMeta.creator.getSimpleName() + paramsBuilder.toString());
    }
    return builder.addCode("\n").addStatement("return object").build();
}
Also used : JavaType(com.pushtorefresh.storio.common.annotations.processor.introspection.JavaType) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) 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 10 with MethodSpec

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

the class GetResolverGenerator method createMapFromCursorWithCreatorMethodSpec.

@NotNull
private MethodSpec createMapFromCursorWithCreatorMethodSpec(@NotNull StorIOSQLiteTypeMeta storIOSQLiteTypeMeta, @NotNull ClassName storIOSQLiteTypeClassName) {
    final MethodSpec.Builder builder = MethodSpec.methodBuilder("mapFromCursor").addJavadoc("{@inheritDoc}\n").addAnnotation(Override.class).addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME).addModifiers(PUBLIC).returns(storIOSQLiteTypeClassName).addParameter(ParameterSpec.builder(ClassName.get("android.database", "Cursor"), "cursor").addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME).build()).addCode("\n");
    final StringBuilder paramsBuilder = new StringBuilder();
    paramsBuilder.append("(");
    boolean first = true;
    for (final StorIOSQLiteColumnMeta columnMeta : storIOSQLiteTypeMeta.getOrderedColumns()) {
        final String columnIndex = "cursor.getColumnIndex(\"" + columnMeta.storIOColumn.name() + "\")";
        final JavaType javaType = columnMeta.javaType;
        final String getFromCursor = getFromCursorString(columnMeta, javaType, columnIndex);
        final TypeName name = TypeName.get(((ExecutableElement) columnMeta.element).getReturnType());
        final boolean isBoxed = javaType.isBoxedType();
        if (isBoxed) {
            // otherwise -> if primitive and value from cursor null -> fail early
            builder.addStatement("$T $L = null", name, columnMeta.getRealElementName());
            builder.beginControlFlow("if (!cursor.isNull($L))", columnIndex);
            builder.addStatement("$L = cursor.$L", columnMeta.getRealElementName(), getFromCursor);
            builder.endControlFlow();
        } else {
            builder.addStatement("$T $L = cursor.$L", name, columnMeta.getRealElementName(), getFromCursor);
        }
        if (!first) {
            paramsBuilder.append(", ");
        }
        first = false;
        paramsBuilder.append(columnMeta.getRealElementName());
    }
    paramsBuilder.append(")");
    builder.addCode("\n");
    if (storIOSQLiteTypeMeta.creator.getKind() == ElementKind.CONSTRUCTOR) {
        builder.addStatement("$T object = new $T" + paramsBuilder.toString(), storIOSQLiteTypeClassName, storIOSQLiteTypeClassName);
    } else {
        builder.addStatement("$T object = $T.$L", storIOSQLiteTypeClassName, storIOSQLiteTypeClassName, storIOSQLiteTypeMeta.creator.getSimpleName() + paramsBuilder.toString());
    }
    return builder.addCode("\n").addStatement("return object").build();
}
Also used : StorIOSQLiteColumnMeta(com.pushtorefresh.storio.sqlite.annotations.processor.introspection.StorIOSQLiteColumnMeta) JavaType(com.pushtorefresh.storio.common.annotations.processor.introspection.JavaType) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) MethodSpec(com.squareup.javapoet.MethodSpec) Common.getFromCursorString(com.pushtorefresh.storio.common.annotations.processor.generate.Common.getFromCursorString) 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