use of com.palantir.conjure.spec.FieldDefinition in project conjure-java by palantir.
the class BeanBuilderAuxiliarySettersUtils method createCollectionSetterBuilder.
public static MethodSpec.Builder createCollectionSetterBuilder(String prefix, EnrichedField enriched, TypeMapper typeMapper, ClassName returnClass) {
FieldSpec field = enriched.poetSpec();
FieldDefinition definition = enriched.conjureDef();
Type type = definition.getType();
return MethodSpec.methodBuilder(prefix + StringUtils.capitalize(field.name)).addJavadoc(Javadoc.render(definition.getDocs(), definition.getDeprecated()).map(rendered -> CodeBlock.of("$L", rendered)).orElseGet(() -> CodeBlock.builder().build())).addAnnotations(ConjureAnnotations.deprecation(definition.getDeprecated())).addModifiers(Modifier.PUBLIC).returns(returnClass).addParameter(Parameters.nonnullParameter(widenParameterIfPossible(field.type, type, typeMapper), field.name));
}
use of com.palantir.conjure.spec.FieldDefinition in project conjure-java by palantir.
the class BeanBuilderGenerator method createField.
private EnrichedField createField(FieldName fieldName, FieldDefinition field) {
FieldSpec.Builder spec = FieldSpec.builder(typeMapper.getClassName(field.getType()), JavaNameSanitizer.sanitize(fieldName), Modifier.PRIVATE);
if (field.getType().accept(TypeVisitor.IS_LIST)) {
spec.initializer("new $T<>()", ArrayList.class);
} else if (field.getType().accept(TypeVisitor.IS_SET)) {
spec.initializer("new $T<>()", LinkedHashSet.class);
} else if (field.getType().accept(TypeVisitor.IS_MAP)) {
spec.initializer("new $T<>()", LinkedHashMap.class);
} else if (field.getType().accept(TypeVisitor.IS_OPTIONAL)) {
spec.initializer("$T.empty()", asRawType(typeMapper.getClassName(field.getType())));
} else if (field.getType().accept(MoreVisitors.IS_INTERNAL_REFERENCE)) {
com.palantir.conjure.spec.TypeName name = field.getType().accept(TypeVisitor.REFERENCE);
typeMapper.getType(name).filter(definition -> definition.accept(TypeDefinitionVisitor.IS_ALIAS)).map(definition -> definition.accept(TypeDefinitionVisitor.ALIAS)).ifPresent(aliasDefinition -> {
Type aliasType = aliasDefinition.getAlias();
if (aliasType.accept(MoreVisitors.IS_COLLECTION) || aliasType.accept(TypeVisitor.IS_OPTIONAL)) {
spec.initializer("$T.empty()", typeMapper.getClassName(field.getType()));
}
});
}
return EnrichedField.of(fieldName, field, spec.build());
}
use of com.palantir.conjure.spec.FieldDefinition in project conjure-java by palantir.
the class BeanBuilderGenerator method createCollectionSetter.
private MethodSpec createCollectionSetter(String prefix, EnrichedField enriched, boolean override) {
FieldDefinition definition = enriched.conjureDef();
Type type = definition.getType();
boolean shouldClearFirst = false;
return BeanBuilderAuxiliarySettersUtils.createCollectionSetterBuilder(prefix, enriched, typeMapper, builderClass).addAnnotations(ConjureAnnotations.override(override)).addCode(verifyNotBuilt()).addCode(typeAwareAssignment(enriched, type, shouldClearFirst)).addStatement("return this").build();
}
use of com.palantir.conjure.spec.FieldDefinition in project conjure-java by palantir.
the class BeanGenerator method generateMethodsForFinalStageField.
private static List<MethodSpec> generateMethodsForFinalStageField(EnrichedField enriched, TypeMapper typeMapper, ClassName returnClass) {
List<MethodSpec> methodSpecs = new ArrayList<>();
Type type = enriched.conjureDef().getType();
FieldDefinition definition = enriched.conjureDef();
methodSpecs.add(MethodSpec.methodBuilder(JavaNameSanitizer.sanitize(enriched.fieldName())).addParameter(ParameterSpec.builder(BeanBuilderAuxiliarySettersUtils.widenParameterIfPossible(enriched.poetSpec().type, type, typeMapper), JavaNameSanitizer.sanitize(enriched.fieldName())).addAnnotation(Nonnull.class).build()).addJavadoc(Javadoc.render(definition.getDocs(), definition.getDeprecated()).map(rendered -> CodeBlock.of("$L", rendered)).orElseGet(() -> CodeBlock.builder().build())).addAnnotations(ConjureAnnotations.deprecation(definition.getDeprecated())).addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT).returns(returnClass.box()).build());
if (type.accept(TypeVisitor.IS_LIST)) {
methodSpecs.add(BeanBuilderAuxiliarySettersUtils.createCollectionSetterBuilder("addAll", enriched, typeMapper, returnClass).addModifiers(Modifier.ABSTRACT).build());
methodSpecs.add(BeanBuilderAuxiliarySettersUtils.createItemSetterBuilder(enriched, type.accept(TypeVisitor.LIST).getItemType(), typeMapper, returnClass).addModifiers(Modifier.ABSTRACT).build());
}
if (type.accept(TypeVisitor.IS_SET)) {
methodSpecs.add(BeanBuilderAuxiliarySettersUtils.createCollectionSetterBuilder("addAll", enriched, typeMapper, returnClass).addModifiers(Modifier.ABSTRACT).build());
methodSpecs.add(BeanBuilderAuxiliarySettersUtils.createItemSetterBuilder(enriched, type.accept(TypeVisitor.SET).getItemType(), typeMapper, returnClass).addModifiers(Modifier.ABSTRACT).build());
}
if (type.accept(TypeVisitor.IS_MAP)) {
methodSpecs.add(BeanBuilderAuxiliarySettersUtils.createCollectionSetterBuilder("putAll", enriched, typeMapper, returnClass).addModifiers(Modifier.ABSTRACT).build());
methodSpecs.add(BeanBuilderAuxiliarySettersUtils.createMapSetterBuilder(enriched, typeMapper, returnClass).addModifiers(Modifier.ABSTRACT).build());
}
if (type.accept(TypeVisitor.IS_OPTIONAL)) {
methodSpecs.add(BeanBuilderAuxiliarySettersUtils.createOptionalSetterBuilder(enriched, typeMapper, returnClass).addModifiers(Modifier.ABSTRACT).build());
}
return methodSpecs;
}
Aggregations