Search in sources :

Example 71 with ParameterSpec

use of com.squareup.javapoet.ParameterSpec in project aws-sdk-java-v2 by aws.

the class ClientClassUtils method consumerBuilderVariant.

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).addAnnotations(spec.annotations).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();
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) Consumer(java.util.function.Consumer) ParameterSpec(com.squareup.javapoet.ParameterSpec) MethodSpec(com.squareup.javapoet.MethodSpec) ClassName(com.squareup.javapoet.ClassName)

Example 72 with ParameterSpec

use of com.squareup.javapoet.ParameterSpec in project aws-sdk-java-v2 by aws.

the class SyncClientInterface method downloadToFileSimpleMethod.

/**
 * @return Simple method for streaming output operations to write response content to a file.
 */
private MethodSpec downloadToFileSimpleMethod(OperationModel opModel, TypeName responseType, ClassName requestType) {
    String methodName = opModel.getMethodName();
    ParameterSpec inputVarParam = ParameterSpec.builder(requestType, opModel.getInput().getVariableName()).build();
    ParameterSpec dstFileParam = ParameterSpec.builder(ClassName.get(Path.class), SYNC_CLIENT_DESTINATION_PATH_PARAM_NAME).build();
    return MethodSpec.methodBuilder(methodName).returns(responseType).addModifiers(Modifier.PUBLIC, Modifier.DEFAULT).addParameter(inputVarParam).addParameter(dstFileParam).addJavadoc(opModel.getDocs(model, ClientType.SYNC, SimpleMethodOverload.FILE)).addExceptions(getExceptionClasses(model, opModel)).addStatement("return $L($N, $T.toFile($N))", methodName, inputVarParam, ClassName.get(ResponseTransformer.class), dstFileParam).build();
}
Also used : ParameterSpec(com.squareup.javapoet.ParameterSpec)

Example 73 with ParameterSpec

use of com.squareup.javapoet.ParameterSpec in project aws-sdk-java-v2 by aws.

the class SyncClientInterface method streamingInputOutputFileSimpleMethod.

/**
 * 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) {
    String methodName = opModel.getMethodName();
    ParameterSpec inputVarParam = ParameterSpec.builder(requestType, opModel.getInput().getVariableName()).build();
    ParameterSpec srcFileParam = ParameterSpec.builder(ClassName.get(Path.class), SYNC_CLIENT_SOURCE_PATH_PARAM_NAME).build();
    ParameterSpec dstFileParam = ParameterSpec.builder(ClassName.get(Path.class), SYNC_CLIENT_DESTINATION_PATH_PARAM_NAME).build();
    return MethodSpec.methodBuilder(methodName).returns(responseType).addModifiers(Modifier.PUBLIC, Modifier.DEFAULT).addParameter(inputVarParam).addParameter(srcFileParam).addParameter(dstFileParam).addJavadoc(opModel.getDocs(model, ClientType.SYNC, SimpleMethodOverload.FILE)).addExceptions(getExceptionClasses(model, opModel)).addStatement("return $L($N, $T.fromFile($N), $T.toFile($N))", methodName, inputVarParam, ClassName.get(RequestBody.class), srcFileParam, ClassName.get(ResponseTransformer.class), dstFileParam).build();
}
Also used : ParameterSpec(com.squareup.javapoet.ParameterSpec)

Example 74 with ParameterSpec

use of com.squareup.javapoet.ParameterSpec in project aws-sdk-java-v2 by aws.

the class SyncClientInterface method uploadFromFileSimpleMethod.

/**
 * @return Simple method for streaming input operations to read data from a file.
 */
private MethodSpec uploadFromFileSimpleMethod(OperationModel opModel, TypeName responseType, ClassName requestType) {
    String methodName = opModel.getMethodName();
    ParameterSpec inputVarParam = ParameterSpec.builder(requestType, opModel.getInput().getVariableName()).build();
    ParameterSpec srcPathParam = ParameterSpec.builder(ClassName.get(Path.class), SYNC_CLIENT_SOURCE_PATH_PARAM_NAME).build();
    return MethodSpec.methodBuilder(methodName).returns(responseType).addModifiers(Modifier.PUBLIC, Modifier.DEFAULT).addParameter(inputVarParam).addParameter(srcPathParam).addJavadoc(opModel.getDocs(model, ClientType.SYNC, SimpleMethodOverload.FILE)).addExceptions(getExceptionClasses(model, opModel)).addStatement("return $L($N, $T.fromFile($N))", methodName, inputVarParam, ClassName.get(RequestBody.class), srcPathParam).build();
}
Also used : ParameterSpec(com.squareup.javapoet.ParameterSpec)

Example 75 with ParameterSpec

use of com.squareup.javapoet.ParameterSpec in project raml-module-builder by folio-org.

the class ResourceMethodExtensionPlugin method addAnnotations.

private Builder addAnnotations(GMethod method, MethodSpec.Builder methodSpec) {
    MethodSpec.Builder ret = cloneMethodWithoutParams(methodSpec);
    MethodSpec spec = methodSpec.build();
    List<ParameterSpec> modifiedParams = new ArrayList<>(spec.parameters);
    Iterator<GParameter> methodParams = method.queryParameters().iterator();
    for (int j = 0; j < modifiedParams.size(); j++) {
        ParameterSpec orgParam = modifiedParams.get(j);
        List<AnnotationSpec> an = orgParam.annotations;
        for (AnnotationSpec a : an) {
            if (a.type.toString().equals("javax.ws.rs.QueryParam")) {
                modifiedParams.set(j, annotateNew(methodParams.next(), orgParam));
            }
            if (a.type.toString().equals("javax.ws.rs.PathParam")) {
                GResource curRes = method.resource();
                while (curRes != null) {
                    for (GParameter u : curRes.uriParameters()) {
                        if (u.name().equals(orgParam.name)) {
                            modifiedParams.set(j, annotateNew(u, orgParam));
                        }
                    }
                    curRes = curRes.parentResource();
                }
            }
        }
    }
    ret.addParameters(modifiedParams);
    return ret;
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ParameterSpec(com.squareup.javapoet.ParameterSpec) GResource(org.raml.jaxrs.generator.ramltypes.GResource) ArrayList(java.util.ArrayList) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) Builder(com.squareup.javapoet.MethodSpec.Builder) GParameter(org.raml.jaxrs.generator.ramltypes.GParameter)

Aggregations

ParameterSpec (com.squareup.javapoet.ParameterSpec)97 MethodSpec (com.squareup.javapoet.MethodSpec)59 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)40 ArrayList (java.util.ArrayList)34 TypeName (com.squareup.javapoet.TypeName)30 ClassName (com.squareup.javapoet.ClassName)27 TypeSpec (com.squareup.javapoet.TypeSpec)26 Map (java.util.Map)26 CodeBlock (com.squareup.javapoet.CodeBlock)23 FieldSpec (com.squareup.javapoet.FieldSpec)22 List (java.util.List)20 HashMap (java.util.HashMap)18 AnnotationSpec (com.squareup.javapoet.AnnotationSpec)17 JavaFile (com.squareup.javapoet.JavaFile)13 Modifier (javax.lang.model.element.Modifier)13 TypeElement (javax.lang.model.element.TypeElement)13 Collectors (java.util.stream.Collectors)12 IOException (java.io.IOException)11 Element (javax.lang.model.element.Element)11 TypeMirror (javax.lang.model.type.TypeMirror)11