Search in sources :

Example 1 with EnumValueDefinition

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();
}
Also used : Iterables(com.google.common.collect.Iterables) Arrays(java.util.Arrays) Modifier(javax.lang.model.element.Modifier) Javadoc(com.palantir.conjure.java.util.Javadoc) FieldSpec(com.squareup.javapoet.FieldSpec) ClassName(com.squareup.javapoet.ClassName) Options(com.palantir.conjure.java.Options) ImmutableList(com.google.common.collect.ImmutableList) JsonValue(com.fasterxml.jackson.annotation.JsonValue) Immutable(com.google.errorprone.annotations.Immutable) Locale(java.util.Locale) Nonnull(javax.annotation.Nonnull) CodeBlock(com.squareup.javapoet.CodeBlock) TypeVariableName(com.squareup.javapoet.TypeVariableName) ParameterSpec(com.squareup.javapoet.ParameterSpec) CaseFormat(com.google.common.base.CaseFormat) MethodSpec(com.squareup.javapoet.MethodSpec) EnumDefinition(com.palantir.conjure.spec.EnumDefinition) Packages(com.palantir.conjure.java.util.Packages) EnumValueDefinition(com.palantir.conjure.spec.EnumValueDefinition) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeSpec(com.squareup.javapoet.TypeSpec) JavaFile(com.squareup.javapoet.JavaFile) List(java.util.List) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) ConjureAnnotations(com.palantir.conjure.java.ConjureAnnotations) TypeName(com.squareup.javapoet.TypeName) Collections(java.util.Collections) CodeBlock(com.squareup.javapoet.CodeBlock) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Collections(java.util.Collections) Arrays(java.util.Arrays)

Example 2 with EnumValueDefinition

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();
}
Also used : EnumValueDefinition(com.palantir.conjure.spec.EnumValueDefinition) CodeBlock(com.squareup.javapoet.CodeBlock) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 3 with EnumValueDefinition

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();
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ImmutableList(com.google.common.collect.ImmutableList) EnumValueDefinition(com.palantir.conjure.spec.EnumValueDefinition)

Example 4 with EnumValueDefinition

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();
}
Also used : Locale(java.util.Locale) EnumValueDefinition(com.palantir.conjure.spec.EnumValueDefinition) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 5 with EnumValueDefinition

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;
}
Also used : EnumValueDefinition(com.palantir.conjure.spec.EnumValueDefinition) Documentation(com.palantir.conjure.spec.Documentation)

Aggregations

EnumValueDefinition (com.palantir.conjure.spec.EnumValueDefinition)7 CodeBlock (com.squareup.javapoet.CodeBlock)3 ImmutableList (com.google.common.collect.ImmutableList)2 EnumDefinition (com.palantir.conjure.spec.EnumDefinition)2 MethodSpec (com.squareup.javapoet.MethodSpec)2 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)2 TypeSpec (com.squareup.javapoet.TypeSpec)2 List (java.util.List)2 Locale (java.util.Locale)2 JsonValue (com.fasterxml.jackson.annotation.JsonValue)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SerializationFeature (com.fasterxml.jackson.databind.SerializationFeature)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 TextNode (com.fasterxml.jackson.databind.node.TextNode)1 Jdk8Module (com.fasterxml.jackson.datatype.jdk8.Jdk8Module)1 CaseFormat (com.google.common.base.CaseFormat)1 Preconditions (com.google.common.base.Preconditions)1 Iterables (com.google.common.collect.Iterables)1 Immutable (com.google.errorprone.annotations.Immutable)1