public static MethodSpec.Builder overriding(ExecutableElement method) { return MethodSpec.methodBuilder(method.getSimpleName().toString()) .addAnnotation(Override.class) .addModifiers(method.getModifiers().stream().filter(modifier -> modifier != Modifier.ABSTRACT).collect(Collectors.toList())) .returns(TypeName.get(method.getReturnType())) .varargs(method.isVarArgs()) .addExceptions(method.getThrownTypes().stream().map(TypeName::get).collect(Collectors.toList())) .addTypeVariables(method.getTypeParameters().stream().map(TypeVariableName::get).collect(Collectors.toList())) .addParameters(method.getParameters().stream().map(element -> ParameterSpec.get(element).toBuilder() .addAnnotations(element.getAnnotationMirrors().stream().map(AnnotationSpec::get).collect(Collectors.toList())).build()).collect(Collectors.toList())); }
private MethodSpec constructMethod(final Element element, final ClassName returnClazz, final String parent, final Modifier... modifiers) { final ExecutableElement templateMethod = (ExecutableElement) element; final String methodName = templateMethod.getSimpleName().toString(); final TypeName returnType = getReturnTypeDefinition(returnClazz, templateMethod); final MethodSpec.Builder methodToAdd = MethodSpec.methodBuilder(methodName) .addModifiers(modifiers) .addAnnotation(Override.class) .addExceptions(templateMethod.getThrownTypes().stream().map(TypeName::get).collect(Collectors.toList())) .returns(returnType); templateMethod.getTypeParameters().forEach(tp -> methodToAdd.addTypeVariable(TypeVariableName.get(tp))); final String parentCall = parent.isEmpty() ? "" : parent + "."; final String body = "return ($T) " + parentCall + "super.$L("; addMethodBody(methodToAdd, templateMethod, body, ")", returnClazz, methodName); return methodToAdd.build(); }
.addExceptions(templateMethod.getThrownTypes().stream().map(TypeName::get).collect(Collectors.toList())) .returns(returnType); final MethodSpec.Builder methodToAdd = MethodSpec.methodBuilder(methodName) .addModifiers(Modifier.STATIC, Modifier.PUBLIC) .addExceptions(templateMethod.getThrownTypes().stream().map(TypeName::get).collect(Collectors.toList())) .returns(returnType);
/** * Generate a simple method for operations with streaming input and output members. * Streaming input member that reads data from a file and a streaming output member that write response content to a file. */ private MethodSpec streamingInputOutputFileSimpleMethod(OperationModel opModel, TypeName responseType, ClassName requestType) { return MethodSpec.methodBuilder(opModel.getMethodName()) .returns(responseType) .addModifiers(Modifier.PUBLIC, Modifier.DEFAULT) .addParameter(requestType, opModel.getInput().getVariableName()) .addParameter(ClassName.get(Path.class), "sourcePath") .addParameter(ClassName.get(Path.class), "destinationPath") .addJavadoc(opModel.getDocs(model, ClientType.SYNC, SimpleMethodOverload.FILE)) .addExceptions(getExceptionClasses(model, opModel)) .addStatement("return $L($L, $T.fromFile(sourcePath), $T.toFile(destinationPath))", opModel.getMethodName(), opModel.getInput().getVariableName(), ClassName.get(RequestBody.class), ClassName.get(ResponseTransformer.class)) .build(); }
static MethodSpec consumerBuilderVariant(MethodSpec spec, String javadoc) { Validate.validState(spec.parameters.size() > 0, "A first parameter is required to generate a consumer-builder method."); Validate.validState(spec.parameters.get(0).type instanceof ClassName, "The first parameter must be a class."); ParameterSpec firstParameter = spec.parameters.get(0); ClassName firstParameterClass = (ClassName) firstParameter.type; TypeName consumer = ParameterizedTypeName.get(ClassName.get(Consumer.class), firstParameterClass.nestedClass("Builder")); MethodSpec.Builder result = MethodSpec.methodBuilder(spec.name) .returns(spec.returnType) .addExceptions(spec.exceptions) .addJavadoc(javadoc) .addModifiers(Modifier.PUBLIC, Modifier.DEFAULT) .addTypeVariables(spec.typeVariables) .addParameter(ParameterSpec.builder(consumer, firstParameter.name).build()); // Parameters StringBuilder methodBody = new StringBuilder("return $L($T.builder().applyMutation($L).build()"); for (int i = 1; i < spec.parameters.size(); i++) { ParameterSpec parameter = spec.parameters.get(i); methodBody.append(", ").append(parameter.name); result.addParameter(parameter); } methodBody.append(")"); result.addStatement(methodBody.toString(), spec.name, firstParameterClass, firstParameter.name); return result.build(); }
private MethodSpec skeletonMethod(MethodSpec completeMethod) { MethodSpec.Builder skeleton = completeMethod.isConstructor() ? constructorBuilder() : methodBuilder(completeMethod.name).returns(completeMethod.returnType); if (completeMethod.isConstructor()) { // Code in Turbine must (for technical reasons in javac) have a valid super() call for // constructors, otherwise javac will bark, and Turbine has no way to avoid this. So we retain // constructor method bodies if they do exist skeleton.addCode(completeMethod.code); } return skeleton .addModifiers(completeMethod.modifiers) .addTypeVariables(completeMethod.typeVariables) .addParameters(completeMethod.parameters) .addExceptions(completeMethod.exceptions) .varargs(completeMethod.varargs) .addAnnotations(completeMethod.annotations) .build(); }
/** * Generate a simple method for operations with streaming input and output members. * Streaming input member that reads data from a file and a streaming output member that write response content to a file. */ private MethodSpec streamingInputOutputFileSimpleMethod(OperationModel opModel, TypeName responseType, ClassName requestType) { return MethodSpec.methodBuilder(opModel.getMethodName()) .returns(responseType) .addModifiers(Modifier.PUBLIC, Modifier.DEFAULT) .addParameter(requestType, opModel.getInput().getVariableName()) .addParameter(ClassName.get(Path.class), "sourcePath") .addParameter(ClassName.get(Path.class), "destinationPath") .addJavadoc(opModel.getDocs(model, ClientType.SYNC, SimpleMethodOverload.FILE)) .addExceptions(getExceptionClasses(model, opModel)) .addStatement("return $L($L, $T.fromFile(sourcePath), $T.toFile(destinationPath))", opModel.getMethodName(), opModel.getInput().getVariableName(), ClassName.get(RequestBody.class), ClassName.get(ResponseTransformer.class)) .build(); }
private static MethodSpec.Builder operationBaseSignature(IntermediateModel model, OperationModel opModel, Consumer<MethodSpec.Builder> addFirstParameter, SimpleMethodOverload simpleMethodOverload, String methodName) { TypeName responseType = ClassName.get(model.getMetadata().getFullModelPackageName(), opModel.getReturnType().getReturnType()); TypeName returnType = opModel.hasStreamingOutput() ? STREAMING_TYPE_VARIABLE : responseType; MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(methodName) .returns(returnType) .addModifiers(Modifier.PUBLIC) .addJavadoc(opModel.getDocs(model, ClientType.SYNC, simpleMethodOverload)) .addExceptions(getExceptionClasses(model, opModel)); addFirstParameter.accept(methodBuilder); streamingMethod(methodBuilder, opModel, responseType); return methodBuilder; }
/** * @return Simple method for streaming input operations to read data from a file. */ private MethodSpec uploadFromFileSimpleMethod(OperationModel opModel, TypeName responseType, ClassName requestType) { return MethodSpec.methodBuilder(opModel.getMethodName()) .returns(responseType) .addModifiers(Modifier.PUBLIC, Modifier.DEFAULT) .addParameter(requestType, opModel.getInput().getVariableName()) .addParameter(ClassName.get(Path.class), "filePath") .addJavadoc(opModel.getDocs(model, ClientType.SYNC, SimpleMethodOverload.FILE)) .addExceptions(getExceptionClasses(model, opModel)) .addStatement("return $L($L, $T.fromFile($L))", opModel.getMethodName(), opModel.getInput().getVariableName(), ClassName.get(RequestBody.class), "filePath") .build(); }
/** * @return Simple method for streaming output operations to write response content to a file. */ private MethodSpec downloadToFileSimpleMethod(OperationModel opModel, TypeName responseType, ClassName requestType) { return MethodSpec.methodBuilder(opModel.getMethodName()) .returns(responseType) .addModifiers(Modifier.PUBLIC, Modifier.DEFAULT) .addParameter(requestType, opModel.getInput().getVariableName()) .addParameter(ClassName.get(Path.class), "filePath") .addJavadoc(opModel.getDocs(model, ClientType.SYNC, SimpleMethodOverload.FILE)) .addExceptions(getExceptionClasses(model, opModel)) .addStatement("return $L($L, $T.toFile($L))", opModel.getMethodName(), opModel.getInput().getVariableName(), ClassName.get(ResponseTransformer.class), "filePath") .build(); }
private static MethodSpec.Builder operationBaseSignature(IntermediateModel model, OperationModel opModel, Consumer<MethodSpec.Builder> addFirstParameter, SimpleMethodOverload simpleMethodOverload, String methodName) { TypeName responseType = ClassName.get(model.getMetadata().getFullModelPackageName(), opModel.getReturnType().getReturnType()); TypeName returnType = opModel.hasStreamingOutput() ? STREAMING_TYPE_VARIABLE : responseType; MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(methodName) .returns(returnType) .addModifiers(Modifier.PUBLIC) .addJavadoc(opModel.getDocs(model, ClientType.SYNC, simpleMethodOverload)) .addExceptions(getExceptionClasses(model, opModel)); addFirstParameter.accept(methodBuilder); streamingMethod(methodBuilder, opModel, responseType); return methodBuilder; }
/** * @return Simple method for streaming output operations to write response content to a file. */ private MethodSpec downloadToFileSimpleMethod(OperationModel opModel, TypeName responseType, ClassName requestType) { return MethodSpec.methodBuilder(opModel.getMethodName()) .returns(responseType) .addModifiers(Modifier.PUBLIC, Modifier.DEFAULT) .addParameter(requestType, opModel.getInput().getVariableName()) .addParameter(ClassName.get(Path.class), "filePath") .addJavadoc(opModel.getDocs(model, ClientType.SYNC, SimpleMethodOverload.FILE)) .addExceptions(getExceptionClasses(model, opModel)) .addStatement("return $L($L, $T.toFile($L))", opModel.getMethodName(), opModel.getInput().getVariableName(), ClassName.get(ResponseTransformer.class), "filePath") .build(); }
/** * @return Simple method for streaming input operations to read data from a file. */ private MethodSpec uploadFromFileSimpleMethod(OperationModel opModel, TypeName responseType, ClassName requestType) { return MethodSpec.methodBuilder(opModel.getMethodName()) .returns(responseType) .addModifiers(Modifier.PUBLIC, Modifier.DEFAULT) .addParameter(requestType, opModel.getInput().getVariableName()) .addParameter(ClassName.get(Path.class), "filePath") .addJavadoc(opModel.getDocs(model, ClientType.SYNC, SimpleMethodOverload.FILE)) .addExceptions(getExceptionClasses(model, opModel)) .addStatement("return $L($L, $T.fromFile($L))", opModel.getMethodName(), opModel.getInput().getVariableName(), ClassName.get(RequestBody.class), "filePath") .build(); }
/** * @return Simple method for streaming output operations to get content as an input stream. */ private MethodSpec inputStreamSimpleMethod(OperationModel opModel, TypeName responseType, ClassName requestType) { TypeName returnType = ParameterizedTypeName.get(ClassName.get(ResponseInputStream.class), responseType); return MethodSpec.methodBuilder(opModel.getMethodName()) .returns(returnType) .addModifiers(Modifier.PUBLIC, Modifier.DEFAULT) .addParameter(requestType, opModel.getInput().getVariableName()) .addJavadoc(opModel.getDocs(model, ClientType.SYNC, SimpleMethodOverload.INPUT_STREAM)) .addExceptions(getExceptionClasses(model, opModel)) .addStatement("return $L($L, $T.toInputStream())", opModel.getMethodName(), opModel.getInput().getVariableName(), ClassName.get(ResponseTransformer.class)) .build(); }
private MethodSpec constructMethod(final Element element, final ClassName returnClazz, final String parent, final Modifier... modifiers) { final ExecutableElement templateMethod = (ExecutableElement) element; final String methodName = templateMethod.getSimpleName().toString(); final TypeName returnType = getReturnTypeDefinition(returnClazz, templateMethod); final MethodSpec.Builder methodToAdd = MethodSpec.methodBuilder(methodName) .addModifiers(modifiers) .addAnnotation(Override.class) .addExceptions(templateMethod.getThrownTypes().stream().map(TypeName::get).collect(Collectors.toList())) .returns(returnType); templateMethod.getTypeParameters().forEach(tp -> methodToAdd.addTypeVariable(TypeVariableName.get(tp))); final String parentCall = parent.isEmpty() ? "" : parent + "."; final String body = "return ($T) " + parentCall + "super.$L("; addMethodBody(methodToAdd, templateMethod, body, ")", returnClazz, methodName); return methodToAdd.build(); }
/** * @return Simple method for streaming output operations to get the content as a byte buffer or other in-memory types. */ private MethodSpec bytesSimpleMethod(OperationModel opModel, TypeName responseType, ClassName requestType) { TypeName returnType = ParameterizedTypeName.get(ClassName.get(ResponseBytes.class), responseType); return MethodSpec.methodBuilder(opModel.getMethodName() + "AsBytes") .returns(returnType) .addModifiers(Modifier.PUBLIC, Modifier.DEFAULT) .addParameter(requestType, opModel.getInput().getVariableName()) .addJavadoc(opModel.getDocs(model, ClientType.SYNC, SimpleMethodOverload.BYTES)) .addExceptions(getExceptionClasses(model, opModel)) .addStatement("return $L($L, $T.toBytes())", opModel.getMethodName(), opModel.getInput().getVariableName(), ClassName.get(ResponseTransformer.class)) .build(); }
/** * @return Simple method for streaming output operations to get content as an input stream. */ private MethodSpec inputStreamSimpleMethod(OperationModel opModel, TypeName responseType, ClassName requestType) { TypeName returnType = ParameterizedTypeName.get(ClassName.get(ResponseInputStream.class), responseType); return MethodSpec.methodBuilder(opModel.getMethodName()) .returns(returnType) .addModifiers(Modifier.PUBLIC, Modifier.DEFAULT) .addParameter(requestType, opModel.getInput().getVariableName()) .addJavadoc(opModel.getDocs(model, ClientType.SYNC, SimpleMethodOverload.INPUT_STREAM)) .addExceptions(getExceptionClasses(model, opModel)) .addStatement("return $L($L, $T.toInputStream())", opModel.getMethodName(), opModel.getInput().getVariableName(), ClassName.get(ResponseTransformer.class)) .build(); }
/** * @return Simple method for streaming output operations to get the content as a byte buffer or other in-memory types. */ private MethodSpec bytesSimpleMethod(OperationModel opModel, TypeName responseType, ClassName requestType) { TypeName returnType = ParameterizedTypeName.get(ClassName.get(ResponseBytes.class), responseType); return MethodSpec.methodBuilder(opModel.getMethodName() + "AsBytes") .returns(returnType) .addModifiers(Modifier.PUBLIC, Modifier.DEFAULT) .addParameter(requestType, opModel.getInput().getVariableName()) .addJavadoc(opModel.getDocs(model, ClientType.SYNC, SimpleMethodOverload.BYTES)) .addExceptions(getExceptionClasses(model, opModel)) .addStatement("return $L($L, $T.toBytes())", opModel.getMethodName(), opModel.getInput().getVariableName(), ClassName.get(ResponseTransformer.class)) .build(); }
private MethodSpec.Builder operationSimpleMethodSignature(IntermediateModel model, OperationModel opModel, String methodName) { TypeName returnType = ClassName.get(model.getMetadata().getFullModelPackageName(), opModel.getReturnType().getReturnType()); return MethodSpec.methodBuilder(methodName) .returns(returnType) .addModifiers(Modifier.PUBLIC) .addModifiers(Modifier.DEFAULT) .addExceptions(getExceptionClasses(model, opModel)); }
private MethodSpec.Builder operationSimpleMethodSignature(IntermediateModel model, OperationModel opModel, String methodName) { TypeName returnType = ClassName.get(model.getMetadata().getFullModelPackageName(), opModel.getReturnType().getReturnType()); return MethodSpec.methodBuilder(methodName) .returns(returnType) .addModifiers(Modifier.PUBLIC) .addModifiers(Modifier.DEFAULT) .addExceptions(getExceptionClasses(model, opModel)); }