Java Code Examples for com.squareup.javapoet.TypeSpec#interfaceBuilder()
The following examples show how to use
com.squareup.javapoet.TypeSpec#interfaceBuilder() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GeneratedCodeGenerator.java From jackdaw with Apache License 2.0 | 5 votes |
private TypeSpec.Builder createTypeSpecBuilder(final ClassType classType, final String className) { switch (classType) { case ANNOTATION: return TypeSpec.annotationBuilder(className); case INTERFACE: return TypeSpec.interfaceBuilder(className); default: return TypeSpec.classBuilder(className); } }
Example 2
Source File: GenerateYamlParserSupportClasses.java From camel-k-runtime with Apache License 2.0 | 4 votes |
public final TypeSpec generateHasExpression() { TypeSpec.Builder type = TypeSpec.interfaceBuilder("HasExpression"); type.addModifiers(Modifier.PUBLIC); type.addMethod( MethodSpec.methodBuilder("setExpression") .addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT) .addParameter(ExpressionDefinition.class, "expressionDefinition") .addAnnotation( AnnotationSpec.builder(JsonTypeInfo.class) .addMember("use", "$L", "JsonTypeInfo.Id.NAME") .addMember("include", "$L", "JsonTypeInfo.As.WRAPPER_OBJECT") .build()) .build() ); type.addMethod( MethodSpec.methodBuilder("getExpression") .addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT) .returns(ExpressionDefinition.class) .build() ); definitions(EXPRESSION_DEFINITION_CLASS).forEach( (k, v) -> { String name = k; name = WordUtils.capitalize(name, '_', '-'); name = StringUtils.remove(name, "_"); name = StringUtils.remove(name, "-"); type.addMethod(MethodSpec.methodBuilder("set" + name) .addAnnotation( AnnotationSpec.builder(JsonAlias.class).addMember("value", "$S", k).build()) .addModifiers(Modifier.PUBLIC, Modifier.DEFAULT) .addParameter(v, "definition") .addCode( CodeBlock.builder() .beginControlFlow("if (getExpression() != null)") .addStatement("throw new IllegalArgumentException(\"And expression has already been set\")") .endControlFlow() .addStatement("setExpression(definition);").build()) .build() ); } ); return type.build(); }
Example 3
Source File: GenerateYamlParserSupportClasses.java From camel-k-runtime with Apache License 2.0 | 4 votes |
public final TypeSpec generateHasDataFormat() { TypeSpec.Builder type = TypeSpec.interfaceBuilder("HasDataFormat"); type.addModifiers(Modifier.PUBLIC); type.addMethod( MethodSpec.methodBuilder("setDataFormatType") .addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT) .addParameter(DataFormatDefinition.class, "dataFormatType") .addAnnotation( AnnotationSpec.builder(JsonAlias.class). addMember("value", "{$S, $S}", "data-format-type", "data-format") .build()) .addAnnotation( AnnotationSpec.builder(JsonTypeInfo.class) .addMember("use", "$L", "JsonTypeInfo.Id.NAME") .addMember("include", "$L", "JsonTypeInfo.As.WRAPPER_OBJECT") .build()) .build() ); type.addMethod( MethodSpec.methodBuilder("getDataFormatType") .addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT) .returns(DataFormatDefinition.class) .build() ); definitions(DATAFORMAT_DEFINITION_CLASS).forEach( (k, v) -> { String name = k; name = WordUtils.capitalize(name, '_', '-'); name = StringUtils.remove(name, "_"); name = StringUtils.remove(name, "-"); type.addMethod(MethodSpec.methodBuilder("set" + name) .addAnnotation( AnnotationSpec.builder(JsonAlias.class).addMember("value", "$S", k).build()) .addModifiers(Modifier.PUBLIC, Modifier.DEFAULT) .addParameter(v, "definition") .addCode( CodeBlock.builder() .beginControlFlow("if (getDataFormatType() != null)") .addStatement("throw new IllegalArgumentException(\"A data format has already been set\")") .endControlFlow() .addStatement("setDataFormatType(definition);") .build()) .build() ); } ); return type.build(); }
Example 4
Source File: GenerateYamlParserSupportClasses.java From camel-k-runtime with Apache License 2.0 | 4 votes |
public final TypeSpec generateHasLoadBalancerType() { TypeSpec.Builder type = TypeSpec.interfaceBuilder("HasLoadBalancerType"); type.addModifiers(Modifier.PUBLIC); type.addMethod( MethodSpec.methodBuilder("setLoadBalancerType") .addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT) .addParameter(LoadBalancerDefinition.class, "loadbalancer") .addAnnotation( AnnotationSpec.builder(JsonTypeInfo.class) .addMember("use", "$L", "JsonTypeInfo.Id.NAME") .addMember("include", "$L", "JsonTypeInfo.As.WRAPPER_OBJECT") .build()) .build() ); type.addMethod( MethodSpec.methodBuilder("getLoadBalancerType") .addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT) .returns(LoadBalancerDefinition.class) .build() ); definitions(LOAD_BALANCE_DEFINITION_CLASS).forEach( (k, v) -> { String name = k; name = WordUtils.capitalize(name, '_', '-'); name = StringUtils.remove(name, "_"); name = StringUtils.remove(name, "-"); type.addMethod(MethodSpec.methodBuilder("set" + name) .addAnnotation( AnnotationSpec.builder(JsonAlias.class).addMember("value", "$S", k).build()) .addModifiers(Modifier.PUBLIC, Modifier.DEFAULT) .addParameter(v, "definition") .addCode( CodeBlock.builder() .beginControlFlow("if (getLoadBalancerType() != null)") .addStatement("throw new IllegalArgumentException(\"A load-balancer has already been set\")") .endControlFlow() .addStatement("setLoadBalancerType(definition);").build()) .build() ); } ); return type.build(); }
Example 5
Source File: OpenApi2JaxRs.java From apicurio-studio with Apache License 2.0 | 4 votes |
/** * Generates a Jax-rs interface from the given codegen information. * @param _interface */ protected String generateJavaInterface(CodegenJavaInterface _interface) { // Create the JAX-RS interface spec itself. Builder interfaceBuilder = TypeSpec .interfaceBuilder(ClassName.get(_interface.getPackage(), _interface.getName())); interfaceBuilder.addModifiers(Modifier.PUBLIC) .addAnnotation(AnnotationSpec.builder(ClassName.get("javax.ws.rs", "Path")) .addMember("value", "$S", _interface.getPath()).build()) .addJavadoc("A JAX-RS interface. An implementation of this interface must be provided.\n"); // Add specs for all the methods. for (CodegenJavaMethod cgMethod : _interface.getMethods()) { com.squareup.javapoet.MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(cgMethod.getName()); methodBuilder.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT); // The @Path annotation. if (cgMethod.getPath() != null) { methodBuilder.addAnnotation(AnnotationSpec.builder(ClassName.get("javax.ws.rs", "Path")) .addMember("value", "$S", cgMethod.getPath()).build()); } // The @GET, @PUT, @POST, etc annotation methodBuilder.addAnnotation(AnnotationSpec.builder(ClassName.get("javax.ws.rs", cgMethod.getMethod().toUpperCase())).build()); // The @Produces annotation if (cgMethod.getProduces() != null && !cgMethod.getProduces().isEmpty()) { methodBuilder.addAnnotation(AnnotationSpec.builder(ClassName.get("javax.ws.rs", "Produces")) .addMember("value", "$L", toStringArrayLiteral(cgMethod.getProduces())).build()); } // The @Consumes annotation if (cgMethod.getConsumes() != null && !cgMethod.getConsumes().isEmpty()) { methodBuilder.addAnnotation(AnnotationSpec.builder(ClassName.get("javax.ws.rs", "Consumes")) .addMember("value", "$L", toStringArrayLiteral(cgMethod.getConsumes())).build()); } // The method return type. if (cgMethod.getReturn() != null) { TypeName returnType = generateTypeName(cgMethod.getReturn().getCollection(), cgMethod.getReturn().getType(), cgMethod.getReturn().getFormat(), true, ClassName.get("javax.ws.rs.core", "Response")); if (getSettings().reactive || cgMethod.isAsync()) { returnType = generateReactiveTypeName(returnType); } methodBuilder.returns(returnType); } // The method arguments. if (cgMethod.getArguments() != null && !cgMethod.getArguments().isEmpty()) { for (CodegenJavaArgument cgArgument : cgMethod.getArguments()) { TypeName defaultParamType = ClassName.OBJECT; if (cgArgument.getIn().equals("body")) { defaultParamType = ClassName.get("java.io", "InputStream"); } TypeName paramType = generateTypeName(cgArgument.getCollection(), cgArgument.getType(), cgArgument.getFormat(), cgArgument.getRequired(), defaultParamType); if (cgArgument.getTypeSignature() != null) { // TODO try to find a re-usable data type that matches the type signature } com.squareup.javapoet.ParameterSpec.Builder paramBuilder = ParameterSpec.builder(paramType, paramNameToJavaArgName(cgArgument.getName())); if (cgArgument.getIn().equals("path")) { paramBuilder.addAnnotation(AnnotationSpec.builder(ClassName.get("javax.ws.rs", "PathParam")) .addMember("value", "$S", cgArgument.getName()).build()); } if (cgArgument.getIn().equals("query")) { paramBuilder.addAnnotation(AnnotationSpec.builder(ClassName.get("javax.ws.rs", "QueryParam")) .addMember("value", "$S", cgArgument.getName()).build()); } if (cgArgument.getIn().equals("header")) { paramBuilder.addAnnotation(AnnotationSpec.builder(ClassName.get("javax.ws.rs", "HeaderParam")) .addMember("value", "$S", cgArgument.getName()).build()); } methodBuilder.addParameter(paramBuilder.build()); } } // TODO:: error responses (4xx and 5xx) // Should errors be modeled in some way? JAX-RS has a few ways to handle them. I'm inclined to // not generate anything in the interface for error responses. // Javadoc if (cgMethod.getDescription() != null) { methodBuilder.addJavadoc(cgMethod.getDescription()); methodBuilder.addJavadoc("\n"); } interfaceBuilder.addMethod(methodBuilder.build()); } TypeSpec jaxRsInterface = interfaceBuilder.build(); JavaFile javaFile = JavaFile.builder(_interface.getPackage(), jaxRsInterface).build(); return javaFile.toString(); }