Java Code Examples for com.squareup.javapoet.FieldSpec#Builder

The following examples show how to use com.squareup.javapoet.FieldSpec#Builder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpenApiClientGenerator.java    From spring-openapi with MIT License 6 votes vote down vote up
private FieldSpec.Builder getNumberBasedSchemaField(String fieldName, Schema innerSchema, TypeSpec.Builder typeSpecBuilder) {
	FieldSpec.Builder fieldBuilder = createNumberBasedFieldWithFormat(fieldName, innerSchema, typeSpecBuilder);
	if (innerSchema.getMinimum() != null) {
		fieldBuilder.addAnnotation(AnnotationSpec.builder(DecimalMin.class)
				.addMember("value", "$S", innerSchema.getMinimum().toString())
				.build()
		);
	}
	if (innerSchema.getMaximum() != null) {
		fieldBuilder.addAnnotation(AnnotationSpec.builder(DecimalMax.class)
				.addMember("value", "$S", innerSchema.getMaximum().toString())
				.build()
		);
	}
	return fieldBuilder;
}
 
Example 2
Source File: TestingDIComponentProcessor.java    From litho with Apache License 2.0 6 votes vote down vote up
@Override
public TypeSpecDataHolder generateInjectedFields(
    SpecModel specModel, ImmutableList<InjectPropModel> injectPropParams) {
  final TypeSpecDataHolder.Builder builder = TypeSpecDataHolder.newBuilder();

  for (MethodParamModel injectedParam : injectPropParams) {
    final FieldSpec.Builder fieldBuilder =
        FieldSpec.builder(injectedParam.getTypeName(), injectedParam.getName());
    for (AnnotationSpec extAnnotation : injectedParam.getExternalAnnotations()) {
      fieldBuilder.addAnnotation(extAnnotation);
    }

    builder.addField(fieldBuilder.build());
  }

  return builder.build();
}
 
Example 3
Source File: Jsr303ExtensionTest.java    From raml-java-tools with Apache License 2.0 6 votes vote down vote up
@Test
public void forArraysMaxOnly() throws Exception {

    when(array.minItems()).thenReturn(null);
    when(array.maxItems()).thenReturn(5);

    FieldSpec.Builder builder =
            FieldSpec.builder(ParameterizedTypeName.get(List.class, String.class), "champ",
                    Modifier.PUBLIC);
    Jsr303Extension ext = new Jsr303Extension();
    ext.fieldBuilt(objectPluginContext, array, builder, EventType.IMPLEMENTATION);
    assertEquals(1, builder.build().annotations.size());
    assertEquals(Size.class.getName(), builder.build().annotations.get(0).type.toString());
    assertEquals(1, builder.build().annotations.get(0).members.size());
    assertEquals("5", builder.build().annotations.get(0).members.get("max").get(0).toString());
}
 
Example 4
Source File: Jsr303ExtensionTest.java    From raml-java-tools with Apache License 2.0 5 votes vote down vote up
@Test
public void forUnion() throws Exception {

    Jsr303Extension ext = new Jsr303Extension();
    TypeSpec.Builder typeBuilder =
            TypeSpec.classBuilder(ClassName.get("xx.bb", "Foo"));

    FieldSpec.Builder builder =
            FieldSpec.builder(ClassName.get(Double.class), "champ", Modifier.PUBLIC);

    ext.anyFieldCreated(unionPluginContext, union, typeBuilder, builder, EventType.IMPLEMENTATION);

    assertEquals(1, builder.build().annotations.size());
    assertEquals(Valid.class.getName(), builder.build().annotations.get(0).type.toString());
}
 
Example 5
Source File: OpenApiClientGenerator.java    From spring-openapi with MIT License 5 votes vote down vote up
private void parseProperties(TypeSpec.Builder typeSpecBuilder, Map<String, Schema> properties, String targetPackage, List<String> requiredFields,
							 Discriminator discriminator, boolean generateDiscriminatorProperty) {
	String discriminatorPropertyName = discriminator == null ? null : discriminator.getPropertyName();
	for (Map.Entry<String, Schema> propertyEntry : properties.entrySet()) {
		if (!generateDiscriminatorProperty && equalsIgnoreCase(propertyEntry.getKey(), discriminatorPropertyName)) {
			continue;
		}
		// type or ref or oneOf + (discriminator)
		Schema innerSchema = propertyEntry.getValue();
		FieldSpec.Builder fieldSpecBuilder;
		if (innerSchema.getType() != null) {
			fieldSpecBuilder = parseTypeBasedSchema(propertyEntry.getKey(), innerSchema, targetPackage, typeSpecBuilder);
		} else if (innerSchema.get$ref() != null) {
			// simple no inheritance
			fieldSpecBuilder = createSimpleFieldSpec(targetPackage, getNameFromRef(innerSchema.get$ref()), propertyEntry.getKey(), typeSpecBuilder);
		} else if (innerSchema instanceof ComposedSchema && CollectionUtils.isNotEmpty(((ComposedSchema) innerSchema).getAllOf())) {
			fieldSpecBuilder = createSimpleFieldSpec(targetPackage, determineParentClassNameUsingOneOf(innerSchema, propertyEntry.getKey(), allComponents), propertyEntry.getKey(), typeSpecBuilder);
		} else if (innerSchema.getDiscriminator() != null) {
			// complicated inheritance - identify target class
			fieldSpecBuilder = createSimpleFieldSpec(targetPackage, determineParentClassNameUsingDiscriminator(innerSchema, propertyEntry.getKey(), allComponents), propertyEntry.getKey(), typeSpecBuilder);
		} else {
			throw new IllegalArgumentException("Incorrect schema. One of [type, $ref, discriminator+oneOf] has to be defined in property schema");
		}
		if (requiredFields != null && requiredFields.contains(propertyEntry.getKey())) {
			fieldSpecBuilder.addAnnotation(NotNull.class);
		}
		if (fieldSpecBuilder != null) {
			typeSpecBuilder.addField(fieldSpecBuilder.build());
		}
	}
}
 
Example 6
Source File: ObjectTypeHandlerPlugin.java    From raml-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public FieldSpec.Builder additionalPropertiesFieldBuilt(ObjectPluginContext objectPluginContext, FieldSpec.Builder incoming, EventType eventType) {
    for (ObjectTypeHandlerPlugin plugin : plugins) {
        if ( incoming == null ) {
            break;
        }
        incoming = plugin.additionalPropertiesFieldBuilt(objectPluginContext, incoming, eventType);
    }

    return incoming;
}
 
Example 7
Source File: Jsr303ExtensionTest.java    From raml-java-tools with Apache License 2.0 5 votes vote down vote up
@Test
public void forArraysOfStrings() throws Exception {

    when(array.minItems()).thenReturn(null);
    when(array.maxItems()).thenReturn(null);

    FieldSpec.Builder builder =
            FieldSpec.builder(ParameterizedTypeName.get(List.class, String.class), "champ",
                    Modifier.PUBLIC);
    Jsr303Extension ext = new Jsr303Extension();
    ext.fieldBuilt(objectPluginContext, array, builder, EventType.IMPLEMENTATION);
    assertEquals(0, builder.build().annotations.size());
}
 
Example 8
Source File: Jsr303ExtensionTest.java    From raml-java-tools with Apache License 2.0 5 votes vote down vote up
@Test
public void forBigInt() throws Exception {

    setupNumberFacets();
    Jsr303Extension ext = new Jsr303Extension();
    FieldSpec.Builder builder =
            FieldSpec.builder(ClassName.get(BigInteger.class), "champ", Modifier.PUBLIC);

    ext.fieldBuilt(objectPluginContext, number, builder, EventType.IMPLEMENTATION);

    assertForIntegerNumber(builder);
}
 
Example 9
Source File: JacksonBasicExtension.java    From raml-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public FieldSpec.Builder fieldBuilt(ObjectPluginContext objectPluginContext, TypeDeclaration declaration, FieldSpec.Builder fieldSpec, EventType eventType) {
    AnnotationSpec.Builder annotation = AnnotationSpec.builder(JsonProperty.class)
            .addMember("value", "$S", declaration.name());
    if (declaration.defaultValue() != null) {
        annotation.addMember("defaultValue", "$S", declaration.defaultValue());

    }
    return fieldSpec.addAnnotation(
            annotation.build());
}
 
Example 10
Source File: TypeSelectableFiller.java    From data-mediator with Apache License 2.0 5 votes vote down vote up
@Override
public FieldSpec.Builder[] createFieldBuilder(String pkgName, String interName, String classname,
                                              List<FieldData> datas, int superInterfaceFlagsForParent) {
    //if parent handled.
    if(Util.hasFlag(superInterfaceFlagsForParent, FieldData.FLAG_SELECTABLE)){
        return null;
    }
    return new FieldSpec.Builder[]{
            FieldSpec.builder(TypeName.BOOLEAN, FD_SELECTABLE.getPropertyName(), Modifier.PRIVATE),
    };
}
 
Example 11
Source File: JniProcessor.java    From cronet with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public JniProcessor() {
    // If non-debug we use shorter names to save space.
    if (ProcessorArgs.HASH_JNI_NAMES) {
        // J.N
        mNativeClassPackage = "J";
        mNativeClassStr = "N";
    }
    mNativeClassName = ClassName.get(mNativeClassPackage, mNativeClassStr);

    FieldSpec.Builder testingFlagBuilder =
            FieldSpec.builder(TypeName.BOOLEAN, NATIVE_TEST_FIELD_NAME)
                    .addModifiers(Modifier.STATIC, Modifier.PUBLIC);
    FieldSpec.Builder throwFlagBuilder =
            FieldSpec.builder(TypeName.BOOLEAN, NATIVE_REQUIRE_MOCK_FIELD_NAME)
                    .addModifiers(Modifier.STATIC, Modifier.PUBLIC);

    // State of mNativesBuilder needs to be preserved between processing rounds.
    mNativesBuilder = TypeSpec.classBuilder(mNativeClassName)
                              .addAnnotation(createGeneratedAnnotation())
                              .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
                              .addField(testingFlagBuilder.build())
                              .addField(throwFlagBuilder.build());

    try {
        sNativeMethodHashFunction = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        // MD5 support is required for all Java platforms so this should never happen.
        printError(e.getMessage());
    }
}
 
Example 12
Source File: JacksonBasicExtension.java    From raml-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public FieldSpec.Builder fieldBuilt(ObjectPluginContext objectPluginContext, TypeDeclaration declaration, FieldSpec.Builder fieldSpec, EventType eventType) {
    AnnotationSpec.Builder annotation = AnnotationSpec.builder(JsonProperty.class)
            .addMember("value", "$S", declaration.name());
    if (declaration.defaultValue() != null) {
        annotation.addMember("defaultValue", "$S", declaration.defaultValue());

    }
    return fieldSpec.addAnnotation(
            annotation.build());
}
 
Example 13
Source File: OpenApiClientGenerator.java    From spring-openapi with MIT License 5 votes vote down vote up
private FieldSpec.Builder createNumberBasedFieldWithFormat(String fieldName, Schema innerSchema, TypeSpec.Builder typeSpecBuilder) {
	if (innerSchema.getFormat() == null || equalsIgnoreCase(innerSchema.getFormat(), "int32")) {
		return createSimpleFieldSpec(JAVA_LANG_PKG, "Integer", fieldName, typeSpecBuilder);
	} else if (equalsIgnoreCase(innerSchema.getFormat(), "int64")) {
		return createSimpleFieldSpec(JAVA_LANG_PKG, "Long", fieldName, typeSpecBuilder);
	} else if (equalsIgnoreCase(innerSchema.getFormat(), "float")) {
		return createSimpleFieldSpec(JAVA_LANG_PKG, "Float", fieldName, typeSpecBuilder);
	} else if (equalsIgnoreCase(innerSchema.getFormat(), "double")) {
		return createSimpleFieldSpec(JAVA_LANG_PKG, "Double", fieldName, typeSpecBuilder);
	} else {
		return createSimpleFieldSpec(JAVA_LANG_PKG, "Integer", fieldName, typeSpecBuilder);
	}
}
 
Example 14
Source File: Jsr303ExtensionTest.java    From raml-java-tools with Apache License 2.0 5 votes vote down vote up
@Test
public void forDouble() throws Exception {

    setupNumberFacets();
    Jsr303Extension ext = new Jsr303Extension();
    FieldSpec.Builder builder =
            FieldSpec.builder(ClassName.get(Double.class), "champ", Modifier.PUBLIC);

    ext.fieldBuilt(objectPluginContext, object, builder, EventType.IMPLEMENTATION);

    assertEquals(1, builder.build().annotations.size());
    assertEquals(Valid.class.getName(), builder.build().annotations.get(0).type.toString());
}
 
Example 15
Source File: UnionTypeHandlerPlugin.java    From raml-java-tools with Apache License 2.0 4 votes vote down vote up
@Override
public FieldSpec.Builder fieldBuilt(UnionPluginContext context, TypeDeclaration ramlType, FieldSpec.Builder fieldSpec, EventType eventType) {
    return fieldSpec;
}
 
Example 16
Source File: AllTypesPluginHelper.java    From raml-java-tools with Apache License 2.0 4 votes vote down vote up
@Override
public FieldSpec.Builder fieldBuilt(ObjectPluginContext objectPluginContext, TypeDeclaration declaration, FieldSpec.Builder incoming, EventType eventType) {
    return objectTypeHandlerPlugin.fieldBuilt(objectPluginContext, declaration, incoming, eventType);
}
 
Example 17
Source File: GenericJacksonAdditionalProperties.java    From raml-java-tools with Apache License 2.0 4 votes vote down vote up
@Override
public FieldSpec.Builder additionalPropertiesFieldBuilt(ObjectPluginContext objectPluginContext, FieldSpec.Builder incoming, EventType eventType) {

    return incoming.addAnnotation(jsonIgnore);
}
 
Example 18
Source File: PluginOne.java    From raml-java-tools with Apache License 2.0 4 votes vote down vote up
@Override
public FieldSpec.Builder fieldBuilt(ObjectPluginContext objectPluginContext, TypeDeclaration declaration, FieldSpec.Builder incoming, EventType eventType) {
    return null;
}
 
Example 19
Source File: TypeInterfaceFiller.java    From data-mediator with Apache License 2.0 2 votes vote down vote up
/**
 *  create field builder. if you want. eg: android.os.Parcelable
 * @param pkgName the package name.
 * @param interName the simple interface name.
 * @param classname the simple class name of generate java file
 * @param datas the field datas.
 * @param superInterfaceFlagsForParent the super interface flags
 * @return the field data array.
 */
public FieldSpec.Builder[] createFieldBuilder(String pkgName, String interName,
                                              String classname, List<FieldData> datas, int superInterfaceFlagsForParent) {
    return null;
}
 
Example 20
Source File: ObjectTypeHandlerPlugin.java    From raml-java-tools with Apache License 2.0 votes vote down vote up
FieldSpec.Builder additionalPropertiesFieldBuilt(ObjectPluginContext objectPluginContext, FieldSpec.Builder incoming, EventType eventType);