use of com.palantir.conjure.spec.TypeDefinition in project conjure-java by palantir.
the class JerseyServiceGenerator method generate.
@Override
public Stream<JavaFile> generate(ConjureDefinition conjureDefinition) {
ClassName binaryReturnType = options.jerseyBinaryAsResponse() ? BINARY_RETURN_TYPE_RESPONSE : BINARY_RETURN_TYPE_OUTPUT;
TypeName optionalBinaryReturnType = options.jerseyBinaryAsResponse() ? BINARY_RETURN_TYPE_RESPONSE : OPTIONAL_BINARY_RETURN_TYPE;
Map<com.palantir.conjure.spec.TypeName, TypeDefinition> types = TypeFunctions.toTypesMap(conjureDefinition);
ClassNameVisitor defaultVisitor = new DefaultClassNameVisitor(types.keySet(), options);
TypeMapper returnTypeMapper = new TypeMapper(types, new SpecializeBinaryClassNameVisitor(defaultVisitor, types, binaryReturnType, optionalBinaryReturnType));
TypeMapper argumentTypeMapper = new TypeMapper(types, new SpecializeBinaryClassNameVisitor(defaultVisitor, types, BINARY_ARGUMENT_TYPE));
return conjureDefinition.getServices().stream().map(serviceDef -> generateService(serviceDef, returnTypeMapper, argumentTypeMapper));
}
use of com.palantir.conjure.spec.TypeDefinition in project conjure-java by palantir.
the class BeanGenerator method generateBeanType.
@SuppressWarnings("CyclomaticComplexity")
public static JavaFile generateBeanType(TypeMapper typeMapper, ObjectDefinition typeDef, Map<com.palantir.conjure.spec.TypeName, TypeDefinition> typesMap, Options options) {
ImmutableList<EnrichedField> fields = createFields(typeMapper, typeDef.getFields());
ImmutableList<FieldSpec> poetFields = EnrichedField.toPoetSpecs(fields);
ImmutableList<EnrichedField> nonPrimitiveEnrichedFields = fields.stream().filter(field -> !field.isPrimitive()).collect(ImmutableList.toImmutableList());
com.palantir.conjure.spec.TypeName prefixedName = Packages.getPrefixedName(typeDef.getTypeName(), options.packagePrefix());
ClassName objectClass = ClassName.get(prefixedName.getPackage(), prefixedName.getName());
ClassName builderClass = ClassName.get(objectClass.packageName(), objectClass.simpleName(), "Builder");
ClassName builderImplementation = options.useStagedBuilders() && fields.stream().anyMatch(field -> !fieldShouldBeInFinalStage(field)) ? ClassName.get(objectClass.packageName(), objectClass.simpleName(), "DefaultBuilder") : builderClass;
TypeSpec.Builder typeBuilder = TypeSpec.classBuilder(prefixedName.getName()).addModifiers(Modifier.PUBLIC, Modifier.FINAL).addFields(poetFields).addMethod(createConstructor(fields, poetFields)).addMethods(createGetters(fields, typesMap, options));
if (!poetFields.isEmpty()) {
typeBuilder.addMethod(MethodSpecs.createEquals(objectClass)).addMethod(MethodSpecs.createEqualTo(objectClass, poetFields));
if (useCachedHashCode(fields)) {
MethodSpecs.addCachedHashCode(typeBuilder, poetFields);
} else {
typeBuilder.addMethod(MethodSpecs.createHashCode(poetFields));
}
}
typeBuilder.addMethod(MethodSpecs.createToString(prefixedName.getName(), fields.stream().map(EnrichedField::fieldName).collect(Collectors.toList())));
if (poetFields.size() <= MAX_NUM_PARAMS_FOR_FACTORY) {
typeBuilder.addMethod(createStaticFactoryMethod(fields, objectClass));
}
if (!nonPrimitiveEnrichedFields.isEmpty()) {
typeBuilder.addMethod(createValidateFields(nonPrimitiveEnrichedFields)).addMethod(createAddFieldIfMissing(nonPrimitiveEnrichedFields.size()));
}
if (poetFields.isEmpty()) {
// Need to add JsonSerialize annotation which indicates that the empty bean serializer should be used to
// serialize this class. Without this annotation no serializer will be set for this class, thus preventing
// serialization.
typeBuilder.addAnnotation(JsonSerialize.class).addField(createSingletonField(objectClass));
if (!options.strictObjects()) {
typeBuilder.addAnnotation(AnnotationSpec.builder(JsonIgnoreProperties.class).addMember("ignoreUnknown", "$L", true).build());
}
} else {
ImmutableList<EnrichedField> fieldsNeedingBuilderStage = fields.stream().filter(field -> !fieldShouldBeInFinalStage(field)).collect(ImmutableList.toImmutableList());
typeBuilder.addAnnotation(AnnotationSpec.builder(JsonDeserialize.class).addMember("builder", "$T.class", builderImplementation).build());
if (!options.useStagedBuilders() || fieldsNeedingBuilderStage.isEmpty()) {
typeBuilder.addMethod(createBuilder(builderClass)).addType(BeanBuilderGenerator.generate(typeMapper, objectClass, builderClass, typeDef, typesMap, options, Optional.empty()));
} else {
List<TypeSpec> interfaces = generateStageInterfaces(objectClass, builderClass, typeMapper, fieldsNeedingBuilderStage, fields.stream().filter(BeanGenerator::fieldShouldBeInFinalStage).collect(ImmutableList.toImmutableList()));
List<ClassName> interfacesAsClasses = interfaces.stream().map(stageInterface -> ClassName.get(objectClass.packageName(), objectClass.simpleName(), stageInterface.name)).collect(Collectors.toList());
TypeSpec builderInterface = TypeSpec.interfaceBuilder("Builder").addModifiers(Modifier.PUBLIC).addMethods(interfaces.stream().map(stageInterface -> stageInterface.methodSpecs).flatMap(List::stream).map(method -> method.toBuilder().addAnnotation(Override.class).returns(method.name.equals("build") ? method.returnType : ClassName.get(objectClass.packageName(), objectClass.simpleName(), "Builder")).build()).collect(Collectors.toSet())).addSuperinterfaces(interfacesAsClasses.stream().map(ClassName::box).collect(Collectors.toList())).build();
typeBuilder.addTypes(interfaces).addType(builderInterface).addMethod(MethodSpec.methodBuilder("builder").addModifiers(Modifier.PUBLIC, Modifier.STATIC).returns(interfacesAsClasses.get(0)).addStatement("return new DefaultBuilder()").build()).addType(BeanBuilderGenerator.generate(typeMapper, objectClass, builderClass, typeDef, typesMap, options, Optional.of(ClassName.get(objectClass.packageName(), objectClass.simpleName(), builderInterface.name))));
}
}
typeBuilder.addAnnotation(ConjureAnnotations.getConjureGeneratedAnnotation(BeanGenerator.class));
typeDef.getDocs().ifPresent(docs -> typeBuilder.addJavadoc("$L", Javadoc.render(docs)));
return JavaFile.builder(prefixedName.getPackage(), typeBuilder.build()).skipJavaLangImports(true).indent(" ").build();
}
use of com.palantir.conjure.spec.TypeDefinition in project conjure-java by palantir.
the class DialogueServiceGenerator method generate.
@Override
public Stream<JavaFile> generate(ConjureDefinition conjureDefinition) {
Map<TypeName, TypeDefinition> types = TypeFunctions.toTypesMap(conjureDefinition);
DialogueEndpointsGenerator endpoints = new DialogueEndpointsGenerator(options);
TypeMapper parameterTypes = new TypeMapper(types, new SpecializeBinaryClassNameVisitor(new DefaultClassNameVisitor(types.keySet(), options), types, ClassName.get(BinaryRequestBody.class)));
TypeMapper returnTypes = new TypeMapper(types, new SpecializeBinaryClassNameVisitor(new DefaultClassNameVisitor(types.keySet(), options), types, ClassName.get(InputStream.class)));
Map<TypeName, TypeDefinition> typeDefinitionsByName = conjureDefinition.getTypes().stream().collect(Collectors.toMap(type -> type.accept(TypeDefinitionVisitor.TYPE_NAME), Function.identity()));
DialogueInterfaceGenerator interfaceGenerator = new DialogueInterfaceGenerator(options, new ParameterTypeMapper(parameterTypes), new ReturnTypeMapper(returnTypes));
TypeNameResolver typeNameResolver = typeName -> Preconditions.checkNotNull(typeDefinitionsByName.get(typeName), "Referenced unknown TypeName", SafeArg.of("typeName", typeName));
StaticFactoryMethodGenerator asyncGenerator = new DefaultStaticFactoryMethodGenerator(options, typeNameResolver, new ParameterTypeMapper(parameterTypes), new ReturnTypeMapper(returnTypes), StaticFactoryMethodType.ASYNC);
StaticFactoryMethodGenerator blockingGenerator = new DefaultStaticFactoryMethodGenerator(options, typeNameResolver, new ParameterTypeMapper(parameterTypes), new ReturnTypeMapper(returnTypes), StaticFactoryMethodType.BLOCKING);
return conjureDefinition.getServices().stream().flatMap(serviceDef -> Stream.of(endpoints.endpointsClass(serviceDef), interfaceGenerator.generateBlocking(serviceDef, blockingGenerator), interfaceGenerator.generateAsync(serviceDef, asyncGenerator)));
}
use of com.palantir.conjure.spec.TypeDefinition in project conjure-java by palantir.
the class UnionGenerator method generateUnionType.
public static JavaFile generateUnionType(TypeMapper typeMapper, Map<com.palantir.conjure.spec.TypeName, TypeDefinition> typesMap, UnionDefinition typeDef, Options options) {
com.palantir.conjure.spec.TypeName prefixedTypeName = Packages.getPrefixedName(typeDef.getTypeName(), options.packagePrefix());
ClassName unionClass = ClassName.get(prefixedTypeName.getPackage(), prefixedTypeName.getName());
ClassName baseClass = unionClass.nestedClass("Base");
ClassName visitorClass = unionClass.nestedClass("Visitor");
ClassName visitorBuilderClass = unionClass.nestedClass("VisitorBuilder");
Map<FieldDefinition, TypeName> memberTypes = typeDef.getUnion().stream().collect(StableCollectors.toLinkedMap(Function.identity(), entry -> typeMapper.getClassName(entry.getType())));
List<FieldSpec> fields = ImmutableList.of(FieldSpec.builder(baseClass, VALUE_FIELD_NAME, Modifier.PRIVATE, Modifier.FINAL).build());
TypeSpec.Builder typeBuilder = TypeSpec.classBuilder(typeDef.getTypeName().getName()).addAnnotation(ConjureAnnotations.getConjureGeneratedAnnotation(UnionGenerator.class)).addModifiers(Modifier.PUBLIC, Modifier.FINAL).addFields(fields).addMethod(generateConstructor(baseClass)).addMethod(generateGetValue(baseClass)).addMethods(generateStaticFactories(typeMapper, unionClass, typeDef.getUnion())).addMethod(generateAcceptVisitMethod(visitorClass)).addType(generateVisitor(unionClass, visitorClass, memberTypes, visitorBuilderClass, options)).addType(generateVisitorBuilder(unionClass, visitorClass, visitorBuilderClass, memberTypes, options)).addTypes(generateVisitorBuilderStageInterfaces(unionClass, visitorClass, memberTypes, options)).addType(generateBase(baseClass, visitorClass, memberTypes)).addTypes(generateWrapperClasses(typeMapper, typesMap, baseClass, visitorClass, typeDef.getUnion(), options)).addType(generateUnknownWrapper(baseClass, visitorClass, options)).addMethod(generateEquals(unionClass)).addMethod(MethodSpecs.createEqualTo(unionClass, fields)).addMethod(MethodSpecs.createHashCode(fields)).addMethod(MethodSpecs.createToString(unionClass.simpleName(), fields.stream().map(fieldSpec -> FieldName.of(fieldSpec.name)).collect(Collectors.toList())));
typeDef.getDocs().ifPresent(docs -> typeBuilder.addJavadoc("$L", Javadoc.render(docs)));
return JavaFile.builder(prefixedTypeName.getPackage(), typeBuilder.build()).skipJavaLangImports(true).indent(" ").build();
}
use of com.palantir.conjure.spec.TypeDefinition in project conjure by palantir.
the class ConjureParserUtils method parseConjureDef.
static ConjureDefinition parseConjureDef(Map<String, AnnotatedConjureSourceFile> annotatedParsedDefs) {
ImmutableList.Builder<ServiceDefinition> servicesBuilder = ImmutableList.builder();
ImmutableList.Builder<ErrorDefinition> errorsBuilder = ImmutableList.builder();
ImmutableList.Builder<TypeDefinition> typesBuilder = ImmutableList.builder();
annotatedParsedDefs.values().forEach(annotatedParsed -> {
ConjureSourceFile parsed = annotatedParsed.conjureSourceFile();
try {
ConjureTypeParserVisitor.ReferenceTypeResolver typeResolver = new ConjureTypeParserVisitor.ByParsedRepresentationTypeNameResolver(parsed.types(), annotatedParsed.importProviders(), annotatedParsedDefs);
// Resolve objects first, so we can use them in service validations
Map<TypeName, TypeDefinition> objects = parseObjects(parsed.types(), typeResolver);
Map<TypeName, TypeDefinition> importedObjects = parseImportObjects(parsed.types().conjureImports(), annotatedParsedDefs);
Map<TypeName, TypeDefinition> allObjects = new HashMap<>();
allObjects.putAll(objects);
allObjects.putAll(importedObjects);
DealiasingTypeVisitor dealiasingVisitor = new DealiasingTypeVisitor(allObjects);
parsed.services().forEach((serviceName, service) -> {
servicesBuilder.add(parseService(service, TypeName.of(serviceName.name(), parseConjurePackage(service.conjurePackage())), typeResolver, dealiasingVisitor));
});
typesBuilder.addAll(objects.values());
errorsBuilder.addAll(parseErrors(parsed.types().definitions(), typeResolver));
} catch (RuntimeException e) {
throw new ConjureRuntimeException(String.format("Encountered error trying to parse file '%s'", annotatedParsed.sourceFile()), e);
}
});
ConjureDefinition definition = ConjureDefinition.builder().version(Conjure.SUPPORTED_IR_VERSION).types(typesBuilder.build()).errors(errorsBuilder.build()).services(servicesBuilder.build()).build();
ConjureDefinitionValidator.validateAll(definition);
return definition;
}
Aggregations