Search in sources :

Example 6 with ClassName

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

the class DeleteResolverGenerator method generateJavaFile.

@NotNull
public JavaFile generateJavaFile(@NotNull StorIOSQLiteTypeMeta storIOSQLiteTypeMeta) {
    final ClassName storIOSQLiteTypeClassName = ClassName.get(storIOSQLiteTypeMeta.packageName, storIOSQLiteTypeMeta.simpleName);
    final TypeSpec deleteResolver = TypeSpec.classBuilder(generateName(storIOSQLiteTypeMeta)).addJavadoc("Generated resolver for Delete Operation.\n").addModifiers(PUBLIC).superclass(ParameterizedTypeName.get(ClassName.get("com.pushtorefresh.storio.sqlite.operations.delete", "DefaultDeleteResolver"), storIOSQLiteTypeClassName)).addMethod(createMapToDeleteQueryMethodSpec(storIOSQLiteTypeMeta, storIOSQLiteTypeClassName)).build();
    return JavaFile.builder(storIOSQLiteTypeMeta.packageName, deleteResolver).indent(INDENT).build();
}
Also used : ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with ClassName

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

the class MappingGenerator method createConstructor.

@NotNull
private MethodSpec createConstructor(StorIOSQLiteTypeMeta storIOSQLiteTypeMeta) {
    final ClassName putResolver = ClassName.get(storIOSQLiteTypeMeta.packageName, PutResolverGenerator.generateName(storIOSQLiteTypeMeta));
    final ClassName getResolver = ClassName.get(storIOSQLiteTypeMeta.packageName, GetResolverGenerator.generateName(storIOSQLiteTypeMeta));
    final ClassName deleteResolver = ClassName.get(storIOSQLiteTypeMeta.packageName, DeleteResolverGenerator.generateName(storIOSQLiteTypeMeta));
    return MethodSpec.constructorBuilder().addModifiers(PUBLIC).addStatement("super(new $T(),\nnew $T(),\nnew $T())", putResolver, getResolver, deleteResolver).build();
}
Also used : ClassName(com.squareup.javapoet.ClassName) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with ClassName

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

the class MappingGenerator method generateJavaFile.

@NotNull
@Override
public JavaFile generateJavaFile(@NotNull StorIOSQLiteTypeMeta storIOSQLiteTypeMeta) {
    final ClassName storIOSQLiteTypeClassName = ClassName.get(storIOSQLiteTypeMeta.packageName, storIOSQLiteTypeMeta.simpleName);
    ClassName superclass = ClassName.get("com.pushtorefresh.storio.sqlite", SUFFIX);
    ParameterizedTypeName superclassParametrized = ParameterizedTypeName.get(superclass, storIOSQLiteTypeClassName);
    final TypeSpec mapping = TypeSpec.classBuilder(storIOSQLiteTypeMeta.simpleName + SUFFIX).addJavadoc("Generated mapping with collection of resolvers.\n").addModifiers(PUBLIC).superclass(superclassParametrized).addMethod(createConstructor(storIOSQLiteTypeMeta)).build();
    return JavaFile.builder(storIOSQLiteTypeMeta.packageName, mapping).indent(INDENT).build();
}
Also used : ClassName(com.squareup.javapoet.ClassName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeSpec(com.squareup.javapoet.TypeSpec) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with ClassName

use of com.squareup.javapoet.ClassName in project tiger by google.

the class Utils method getSourceCodeName.

/**
   * Returns "com_Foo" for com.Foo, "com_Foo_com_Bar_com_Baz" for Foo<Bar, Baz>.
   * upper_bounds_UpperBound_Foo for "? extends Foo" and
   * lower_bounds_LowerBound_Foo for "? super Foo". Assuming raw types are not
   * used, there will be not conflicts.
   */
public static String getSourceCodeName(TypeName typeName) {
    Preconditions.checkNotNull(typeName);
    if (typeName.isPrimitive()) {
        return typeName.toString();
    } else if (typeName instanceof ClassName) {
        return getClassCanonicalName((ClassName) typeName).replace(".", "_");
    } else if (typeName instanceof ParameterizedTypeName) {
        ParameterizedTypeName p = (ParameterizedTypeName) typeName;
        StringBuilder builder = new StringBuilder(getSourceCodeName(p.rawType));
        for (TypeName t : p.typeArguments) {
            builder.append("_").append(getSourceCodeName(t));
        }
        return builder.toString();
    } else if (typeName instanceof WildcardTypeName) {
        WildcardTypeName w = (WildcardTypeName) typeName;
        if (w.upperBounds.size() > 0) {
            return "upper_bounds_" + getSourceCodeName(w.upperBounds.get(0));
        } else {
            Preconditions.checkState(w.lowerBounds.size() > 0);
            return "lower_bounds_" + getSourceCodeName(w.lowerBounds.get(0));
        }
    } else if (typeName instanceof ArrayTypeName) {
        ArrayTypeName arrayTypeName = (ArrayTypeName) typeName;
        return "ArrayOf" + getSourceCodeName(arrayTypeName.componentType);
    }
    throw new RuntimeException("Unexpected type: " + typeName);
}
Also used : TypeName(com.squareup.javapoet.TypeName) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) ClassName(com.squareup.javapoet.ClassName) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 10 with ClassName

use of com.squareup.javapoet.ClassName in project tiger by google.

the class TigerDaggerGeneratorProcessor method generateComponentBuilder.

private void generateComponentBuilder(ClassName componentClassName, @Nullable TypeElement dependencyComponent, List<TypeElement> sortedPassedModules, TypeSpec.Builder componentBuilder, ComponentInfo coreInjector) {
    TypeSpec.Builder builderBuilder = TypeSpec.classBuilder("Builder").addModifiers(Modifier.PUBLIC, Modifier.STATIC);
    // set parent inject methods.
    ClassName dependencyClassName = null;
    if (dependencyComponent != null) {
        dependencyClassName = (ClassName) ClassName.get(dependencyComponent.asType());
        Utils.addSetMethod(componentClassName, builderBuilder, dependencyClassName);
    }
    /**
     * Set module methods.
     */
    for (TypeElement m : sortedPassedModules) {
        Utils.addSetMethod(componentClassName, builderBuilder, (ClassName) ClassName.get(m.asType()));
    }
    // build() method.
    MethodSpec.Builder buildMethodBuilder = MethodSpec.methodBuilder("build").addModifiers(Modifier.PUBLIC).returns(componentClassName);
    StringBuilder returnCodeBuilder = new StringBuilder("return new $T(");
    if (dependencyClassName != null) {
        returnCodeBuilder.append(Utils.getSourceCodeName(dependencyComponent));
        if (!sortedPassedModules.isEmpty()) {
            returnCodeBuilder.append(", ");
        }
    }
    if (!sortedPassedModules.isEmpty()) {
        for (TypeElement module : sortedPassedModules) {
            String moduleFiledName = Utils.getSourceCodeName(module);
            returnCodeBuilder.append(moduleFiledName).append(", ");
        }
        returnCodeBuilder.delete(returnCodeBuilder.length() - 2, returnCodeBuilder.length());
    }
    returnCodeBuilder.append(");");
    buildMethodBuilder.addCode(returnCodeBuilder.toString(), componentClassName);
    builderBuilder.addMethod(buildMethodBuilder.build());
    componentBuilder.addType(builderBuilder.build());
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) TypeElement(javax.lang.model.element.TypeElement) ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

ClassName (com.squareup.javapoet.ClassName)99 TypeSpec (com.squareup.javapoet.TypeSpec)60 MethodSpec (com.squareup.javapoet.MethodSpec)41 TypeElement (javax.lang.model.element.TypeElement)37 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)35 TypeName (com.squareup.javapoet.TypeName)30 Element (javax.lang.model.element.Element)19 TypeMirror (javax.lang.model.type.TypeMirror)17 Builder (com.squareup.javapoet.TypeSpec.Builder)16 FieldSpec (com.squareup.javapoet.FieldSpec)15 ArrayList (java.util.ArrayList)15 CodeBlock (com.squareup.javapoet.CodeBlock)11 ExecutableElement (javax.lang.model.element.ExecutableElement)11 NotNull (org.jetbrains.annotations.NotNull)10 VariableElement (javax.lang.model.element.VariableElement)9 JavaFile (com.squareup.javapoet.JavaFile)8 Map (java.util.Map)8 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)7 IOException (java.io.IOException)7 HashMap (java.util.HashMap)7