use of com.palantir.conjure.spec.EnumValueDefinition in project conjure-java by palantir.
the class EnumGenerator method createValuesList.
private static FieldSpec createValuesList(ClassName thisClass, List<EnumValueDefinition> values) {
CodeBlock arrayValues = values.stream().map(value -> CodeBlock.of("$L", value.getValue())).collect(CodeBlock.joining(", "));
boolean anyDeprecatedValues = values.stream().anyMatch(definition -> definition.getDeprecated().isPresent());
return FieldSpec.builder(ParameterizedTypeName.get(ClassName.get(List.class), thisClass), "values", Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL).initializer(CodeBlock.of("$T.unmodifiableList($T.asList($L))", Collections.class, Arrays.class, arrayValues)).addAnnotations(anyDeprecatedValues ? ImmutableList.of(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "deprecation").build()) : ImmutableList.of()).build();
}
use of com.palantir.conjure.spec.EnumValueDefinition in project conjure-java by palantir.
the class EnumGenerator method generateAcceptVisitMethod.
private static MethodSpec generateAcceptVisitMethod(ClassName visitorClass, List<EnumValueDefinition> values) {
CodeBlock.Builder switchBlock = CodeBlock.builder();
switchBlock.beginControlFlow("switch (value)");
for (EnumValueDefinition value : values) {
switchBlock.add("case $N:\n", value.getValue()).addStatement("return visitor.$L()", getVisitorMethodName(value));
}
switchBlock.add("default:\n").addStatement("return visitor.$1L(string)", VISIT_UNKNOWN_METHOD_NAME).endControlFlow();
ParameterizedTypeName parameterizedVisitorClass = ParameterizedTypeName.get(visitorClass, TYPE_VARIABLE);
boolean anyDeprecatedValues = values.stream().anyMatch(definition -> definition.getDeprecated().isPresent());
return MethodSpec.methodBuilder("accept").addModifiers(Modifier.PUBLIC).addParameter(ParameterSpec.builder(parameterizedVisitorClass, "visitor").build()).addTypeVariable(TYPE_VARIABLE).returns(TYPE_VARIABLE).addCode(switchBlock.build()).addAnnotations(anyDeprecatedValues ? ImmutableList.of(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "deprecation").build()) : ImmutableList.of()).build();
}
use of com.palantir.conjure.spec.EnumValueDefinition in project conjure-java by palantir.
the class EnumGenerator method generateMemberVisitMethods.
private static List<MethodSpec> generateMemberVisitMethods(Iterable<EnumValueDefinition> values) {
ImmutableList.Builder<MethodSpec> methods = ImmutableList.builder();
for (EnumValueDefinition value : values) {
MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder(getVisitorMethodName(value)).addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT).returns(TYPE_VARIABLE);
Javadoc.render(value.getDocs(), value.getDeprecated()).ifPresent(docs -> methodSpecBuilder.addJavadoc("$L", docs));
value.getDeprecated().ifPresent(_deprecated -> methodSpecBuilder.addAnnotation(Deprecated.class));
methods.add(methodSpecBuilder.build());
}
return methods.build();
}
use of com.palantir.conjure.spec.EnumValueDefinition in project conjure-java by palantir.
the class EnumGenerator method createEnum.
private static TypeSpec createEnum(ClassName enumClass, Iterable<EnumValueDefinition> values, boolean withUnknown) {
TypeSpec.Builder enumBuilder = TypeSpec.enumBuilder(enumClass.simpleName()).addAnnotation(ConjureAnnotations.getConjureGeneratedAnnotation(EnumGenerator.class)).addModifiers(Modifier.PUBLIC);
for (EnumValueDefinition value : values) {
TypeSpec.Builder anonymousClassBuilder = TypeSpec.anonymousClassBuilder("");
Javadoc.render(value.getDocs(), value.getDeprecated()).ifPresent(docs -> anonymousClassBuilder.addJavadoc("$L", docs));
value.getDeprecated().ifPresent(_deprecation -> anonymousClassBuilder.addAnnotation(Deprecated.class));
enumBuilder.addEnumConstant(value.getValue(), anonymousClassBuilder.build());
}
if (withUnknown) {
enumBuilder.addEnumConstant("UNKNOWN");
} else {
enumBuilder.addMethod(MethodSpec.methodBuilder("fromString").addJavadoc("$L", "Preferred, case-insensitive constructor for string-to-enum conversion.\n").addAnnotation(ConjureAnnotations.delegatingJsonCreator()).addModifiers(Modifier.PUBLIC, Modifier.STATIC).addParameter(ClassName.get(String.class), "value").addStatement("return $T.valueOf(value.toUpperCase($T.ROOT))", enumClass, Locale.class).returns(enumClass).build());
}
return enumBuilder.build();
}
use of com.palantir.conjure.spec.EnumValueDefinition in project conjure by palantir.
the class ConjureParserUtils method parseEnumValue.
private static EnumValueDefinition parseEnumValue(com.palantir.conjure.parser.types.complex.EnumValueDefinition def) {
EnumValueDefinition enumValue = EnumValueDefinition.builder().value(def.value()).docs(def.docs().map(Documentation::of)).deprecated(def.deprecated().map(Documentation::of)).build();
EnumValueDefinitionValidator.validateAll(enumValue);
return enumValue;
}
Aggregations