TypeName cataMapperTypeName(DataConstructor dc) {

    TypeName[] argsTypeNames =
        concat(
                dc.arguments().stream().map(DataArgument::type),
                dc.typeRestrictions()
                    .stream()
                    .map(TypeRestriction::idFunction)
                    .map(DataArgument::type))
            .map(t -> Utils.asBoxedType.visit(t, utils.types()))
            .map(this::substituteTypeWithRecursionVar)
            .map(TypeName::get)
            .toArray(TypeName[]::new);

    return adt.dataConstruction().isVisitorDispatch()
        ? argsTypeNames.length == 0
            ? ParameterizedTypeName.get(
                ClassName.get(FlavourImpl.findF0(context.flavour(), utils.elements())),
                TypeName.get(adt.matchMethod().returnTypeVariable()))
            : argsTypeNames.length == 1
                ? ParameterizedTypeName.get(
                    ClassName.get(FlavourImpl.findF(context.flavour(), utils.elements())),
                    argsTypeNames[0],
                    TypeName.get(adt.matchMethod().returnTypeVariable()))
                : ParameterizedTypeName.get(
                    Utils.getClassName(context, MapperDerivator.mapperInterfaceName(dc)),
                    concat(
                            concat(
                                dc.typeVariables().stream().map(TypeVariableName::get),
                                fold(
                                    MapperDerivator.findInductiveArgument(utils, adt, dc),
                                    Stream.<TypeName>of(),
                                    tm ->
                                        Stream.of(
                                            ParameterizedTypeName.get(
                                                ClassName.get(
                                                    FlavourImpl.findF0(
                                                        context.flavour(), utils.elements())),
                                                TypeName.get(
                                                    adt.matchMethod().returnTypeVariable()))))),
                            Stream.of(TypeVariableName.get(adt.matchMethod().returnTypeVariable())))
                        .toArray(TypeName[]::new))
        : TypeName.get(
            utils
                .types()
                .getDeclaredType(
                    Utils.asTypeElement.visit(dc.deconstructor().visitorType().asElement()).get(),
                    dc.deconstructor()
                        .visitorType()
                        .getTypeArguments()
                        .stream()
                        .map(this::substituteTypeWithRecursionVar)
                        .toArray(TypeMirror[]::new)));
  }
 @Test
 public void getParameterizedTypeMirror() {
   DeclaredType setType =
       getTypes().getDeclaredType(getElement(Set.class), getMirror(Object.class));
   assertThat(TypeName.get(setType))
       .isEqualTo(ParameterizedTypeName.get(ClassName.get(Set.class), ClassName.OBJECT));
 }
Beispiel #3
0
  private TypeName typeName0(
      final Function<TypeParameterInfo, TypeName> typeParameterToTypeName,
      TypeVariableInfoToTypeName typeVariableInfoToTypeName) {
    final Optional<TypeName> maybeTypeName =
        type()
            .map(
                nameInfo -> {
                  Optional<PackageInfo> maybePackageInfo = packageInfo();
                  PackageInfo packageInfo = maybePackageInfo.get();
                  return nameInfo.toClassName(packageInfo);
                })
            .map(
                className -> {
                  TypeName[] genericTypeNameArray =
                      typeParameterInfoList()
                          .stream()
                          .map(typeParameterToTypeName)
                          .collect(Collectors.toList())
                          .toArray(new TypeName[] {});

                  return genericTypeNameArray.length == 0
                      ? className
                      : ParameterizedTypeName.get(className, genericTypeNameArray);
                });
    return typeVariableInfo()
        .map(typeVariableInfoToTypeName.get(maybeTypeName))
        .orElseGet(() -> maybeTypeName.get());
  }
  public FieldSpec buildRowIdColumnFieldSpec() {
    String name = "_rowid_";
    TypeName columnDefType = ParameterizedTypeName.get(Types.ColumnDef, TypeName.LONG.box());

    CodeBlock initializer;
    initializer =
        CodeBlock.builder()
            .add(
                "new $T($S, $T.class, $L, $L, $L, $L, $L, $L)",
                columnDefType,
                name,
                TypeName.LONG,
                false /* nullable */,
                true /* primary key */,
                false /* autoincrement */,
                true /* autoId */,
                false,
                false)
            .build();

    return FieldSpec.builder(columnDefType, name)
        .addModifiers(publicStaticFinal)
        .initializer(initializer)
        .build();
  }
  @Test
  public void getTypeVariableTypeMirrorRecursive() {
    TypeMirror typeMirror = getElement(Recursive.class).asType();
    ParameterizedTypeName typeName = (ParameterizedTypeName) TypeName.get(typeMirror);
    String className = Recursive.class.getCanonicalName();
    assertThat(typeName.toString()).isEqualTo(className + "<T>");

    TypeVariableName typeVariableName = (TypeVariableName) typeName.typeArguments.get(0);

    try {
      typeVariableName.bounds.set(0, null);
      fail("Expected UnsupportedOperationException");
    } catch (UnsupportedOperationException expected) {
    }

    assertThat(typeVariableName.toString()).isEqualTo("T");
    assertThat(typeVariableName.bounds.toString())
        .isEqualTo("[java.util.Map<java.util.List<T>, java.util.Set<T[]>>]");
  }
  private void generate(RestActionClass actionClass) {
    TypeSpec.Builder classBuilder =
        TypeSpec.classBuilder(actionClass.getHelperName())
            .addModifiers(Modifier.PUBLIC)
            .addJavadoc("SantaRest compile time, autogenerated class, which fills actions")
            .addSuperinterface(
                ParameterizedTypeName.get(
                    ClassName.get(SantaRest.ActionHelper.class), actionClass.getTypeName()));

    classBuilder.addMethod(createRequestMethod(actionClass));
    classBuilder.addMethod(createFillResponseMethod(actionClass));
    classBuilder.addMethod(createFillErrorMethod(actionClass));
    saveClass(actionClass.getPackageName(), classBuilder.build());
  }
 public TypeSpec createTerminalType() {
   final TypeName terminal =
       ParameterizedTypeName.get(
           ClassName.get(Terminal.class), ClassName.get(packageName, terminalName));
   return TypeSpec.classBuilder(terminalName)
       .addJavadoc("The &lt;$L&gt; element.\n", tag)
       .addAnnotation(AnnotationSpec.builder(Root.class).build())
       .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
       .addSuperinterface(terminal)
       .addField(createFieldMissing())
       .addField(createFieldValue())
       .addMethod(createPublicConstructor())
       .addMethod(createPrivateConstructor())
       .addMethod(createValueGetter())
       .addMethod(createCompareTo())
       .addMethod(createHashCode())
       .addMethod(createEquals())
       .addMethod(createShow())
       .build();
 }
  private void addCreatorField(HelperClassBuilder builder) {
    FieldSpec.Builder field =
        FieldSpec.builder(
            ParameterizedTypeName.get(
                ClassName.get(Parcelable.Creator.class), builder.getArgClassName()),
            FIELD_NAME_CREATOR,
            Modifier.PUBLIC,
            Modifier.STATIC,
            Modifier.FINAL);

    field.addAnnotation(
        WeaveBuilder.weave()
            .field()
            .withStatement("%s.%s", fullName(builder.getClassName()), FIELD_NAME_CREATOR)
            .build());
    field.initializer(
        "new $T<$T>() {\n"
            + "\t@$T\n"
            + "\tpublic $T createFromParcel($T in) {\n"
            + "\t\treturn new $T(in);\n"
            + "\t}\n"
            + "\t@$T\n"
            + "\tpublic $T[] newArray(int size) {\n"
            + "\t\treturn new $T[size];\n"
            + "\t}\n"
            + "}",
        Parcelable.Creator.class,
        builder.getArgClassName(),
        Override.class,
        builder.getArgClassName(),
        Parcel.class,
        builder.getArgClassName(),
        Override.class,
        builder.getArgClassName(),
        builder.getArgClassName());

    builder.getBuilder().addField(field.build());
  }
 @Test
 public void parameterizedType() throws Exception {
   ParameterizedTypeName type = ParameterizedTypeName.get(Map.class, String.class, Long.class);
   assertThat(type.toString()).isEqualTo("java.util.Map<java.lang.String, java.lang.Long>");
 }
Beispiel #10
0
  public List<MethodSpec> buildMethodSpecs() {
    List<MethodSpec> methodSpecs = new ArrayList<>();

    List<AnnotationSpec> overrideAndNonNull =
        Arrays.asList(Specs.buildNonNullAnnotationSpec(), Specs.buildOverrideAnnotationSpec());

    List<AnnotationSpec> overrideAndNullable =
        Arrays.asList(Specs.buildNullableAnnotationSpec(), Specs.buildOverrideAnnotationSpec());

    methodSpecs.add(
        MethodSpec.methodBuilder("getModelClass")
            .addAnnotations(overrideAndNonNull)
            .addModifiers(Modifier.PUBLIC)
            .returns(
                ParameterizedTypeName.get(ClassName.get(Class.class), schema.getModelClassName()))
            .addStatement("return $T.class", schema.getModelClassName())
            .build());

    methodSpecs.add(
        MethodSpec.methodBuilder("getTableName")
            .addAnnotations(overrideAndNonNull)
            .addModifiers(Modifier.PUBLIC)
            .returns(Types.String)
            .addStatement("return $L", TABLE_NAME)
            .build());

    methodSpecs.add(
        MethodSpec.methodBuilder("getPrimaryKey")
            .addAnnotations(overrideAndNonNull)
            .addModifiers(Modifier.PUBLIC)
            .returns(Types.WildcardColumnDef)
            .addStatement("return $N", primaryKey)
            .build());

    methodSpecs.add(
        MethodSpec.methodBuilder("getColumns")
            .addAnnotations(overrideAndNonNull)
            .addModifiers(Modifier.PUBLIC)
            .returns(Types.ColumnList)
            .addStatement("return $L", COLUMNS)
            .build());

    methodSpecs.add(
        MethodSpec.methodBuilder("getEscapedColumnNames")
            .addAnnotations(overrideAndNonNull)
            .addModifiers(Modifier.PUBLIC)
            .returns(Types.StringArray)
            .addStatement("return $L", ESCAPED_COLUMN_NAMES)
            .build());

    methodSpecs.add(
        MethodSpec.methodBuilder("getCreateTableStatement")
            .addAnnotations(overrideAndNonNull)
            .addModifiers(Modifier.PUBLIC)
            .returns(Types.String)
            .addStatement("return $S", sql.buildCreateTableStatement(schema))
            .build());

    methodSpecs.add(
        MethodSpec.methodBuilder("getCreateIndexStatements")
            .addAnnotations(overrideAndNonNull)
            .addModifiers(Modifier.PUBLIC)
            .returns(Types.getList(Types.String))
            .addCode(sql.buildCreateIndexStatements(schema))
            .build());

    methodSpecs.add(
        MethodSpec.methodBuilder("getDropTableStatement")
            .addAnnotations(overrideAndNonNull)
            .addModifiers(Modifier.PUBLIC)
            .returns(Types.String)
            .addStatement("return $S", sql.buildDropTableStatement(schema))
            .build());

    methodSpecs.add(
        MethodSpec.methodBuilder("getInsertStatement")
            .addAnnotations(overrideAndNonNull)
            .addModifiers(Modifier.PUBLIC)
            .returns(Types.String)
            .addStatement("return $S", sql.buildInsertStatement(schema))
            .build());

    methodSpecs.add(
        MethodSpec.methodBuilder("bindArgs")
            .addModifiers(Modifier.PUBLIC)
            .returns(TypeName.VOID)
            .addParameter(
                ParameterSpec.builder(Types.OrmaConnection, "conn")
                    .addAnnotation(Specs.buildNonNullAnnotationSpec())
                    .build())
            .addParameter(
                ParameterSpec.builder(Types.SQLiteStatement, "statement")
                    .addAnnotation(Specs.buildNonNullAnnotationSpec())
                    .build())
            .addParameter(
                ParameterSpec.builder(schema.getModelClassName(), "model")
                    .addAnnotation(Specs.buildNonNullAnnotationSpec())
                    .build())
            .addCode(buildBindArgs())
            .build());

    methodSpecs.add(
        MethodSpec.methodBuilder("createModelFromCursor")
            .addAnnotations(overrideAndNonNull)
            .addModifiers(Modifier.PUBLIC)
            .returns(schema.getModelClassName())
            .addParameter(
                ParameterSpec.builder(Types.OrmaConnection, "conn")
                    .addAnnotation(Specs.buildNonNullAnnotationSpec())
                    .build())
            .addParameter(
                ParameterSpec.builder(Types.Cursor, "cursor")
                    .addAnnotation(Specs.buildNonNullAnnotationSpec())
                    .build())
            .addCode(buildCreateModelFromCursor())
            .build());

    return methodSpecs;
  }