use of com.palantir.conjure.java.types.BeanGenerator.EnrichedField in project conjure-java by palantir.
the class BeanBuilderGenerator method createSetters.
private Iterable<MethodSpec> createSetters(Collection<EnrichedField> fields, Map<com.palantir.conjure.spec.TypeName, TypeDefinition> typesMap, boolean override) {
Collection<MethodSpec> setters = Lists.newArrayListWithExpectedSize(fields.size());
for (EnrichedField field : fields) {
setters.add(createSetter(field, typesMap, override));
setters.addAll(createAuxiliarySetters(field, override));
}
return setters;
}
use of com.palantir.conjure.java.types.BeanGenerator.EnrichedField 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.java.types.BeanGenerator.EnrichedField in project conjure-java by palantir.
the class BeanBuilderGenerator method createValidateFieldsMethod.
private static MethodSpec createValidateFieldsMethod(List<EnrichedField> primitives) {
MethodSpec.Builder builder = MethodSpec.methodBuilder("validatePrimitiveFieldsHaveBeenInitialized").addModifiers(Modifier.PRIVATE);
builder.addStatement("$T missingFields = null", ParameterizedTypeName.get(List.class, String.class));
for (EnrichedField field : primitives) {
String name = deriveFieldInitializedName(field);
builder.addStatement("missingFields = addFieldIfMissing(missingFields, $N, $S)", name, field.fieldName().get());
}
builder.beginControlFlow("if (missingFields != null)").addStatement("throw new $T(\"Some required fields have not been set\"," + " $T.of(\"missingFields\", missingFields))", SafeIllegalArgumentException.class, SafeArg.class).endControlFlow();
return builder.build();
}
Aggregations