Search in sources :

Example 76 with ParameterSpec

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

the class ResourceMethodExtensionPlugin method annotateNew.

private ParameterSpec annotateNew(GParameter get, ParameterSpec orgParam) {
    TypeDeclaration typeDeclaration = (TypeDeclaration) get.implementation();
    ParameterSpec.Builder newParam = cloneSingleParamNoAnnotations(orgParam);
    List<AnnotationSpec> newAnnotations = getAnnotationsAsModifiableList(orgParam);
    if (typeDeclaration instanceof StringTypeDeclaration) {
        StringTypeDeclaration n = (StringTypeDeclaration) typeDeclaration;
        if (n.pattern() != null) {
            AnnotationSpec.Builder annoBuilder = AnnotationSpec.builder(javax.validation.constraints.Pattern.class);
            annoBuilder.addMember("regexp", "$S", n.pattern());
            newAnnotations.add(annoBuilder.build());
        }
    }
    if (typeDeclaration instanceof NumberTypeDeclaration) {
        NumberTypeDeclaration n = (NumberTypeDeclaration) typeDeclaration;
        if (n.minimum() != null) {
            AnnotationSpec.Builder annoBuilder = AnnotationSpec.builder(javax.validation.constraints.Min.class);
            annoBuilder.addMember(ANNOTATION_VALUE, "$L", (Long) n.minimum().longValue());
            newAnnotations.add(annoBuilder.build());
        }
        if (n.maximum() != null) {
            AnnotationSpec.Builder annoBuilder = AnnotationSpec.builder(javax.validation.constraints.Max.class);
            annoBuilder.addMember(ANNOTATION_VALUE, "$L", (Long) n.maximum().longValue());
            newAnnotations.add(annoBuilder.build());
        }
    }
    newParam.addAnnotations(newAnnotations);
    return newParam.build();
}
Also used : StringTypeDeclaration(org.raml.v2.api.model.v10.datamodel.StringTypeDeclaration) ParameterSpec(com.squareup.javapoet.ParameterSpec) NumberTypeDeclaration(org.raml.v2.api.model.v10.datamodel.NumberTypeDeclaration) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) TypeDeclaration(org.raml.v2.api.model.v10.datamodel.TypeDeclaration) NumberTypeDeclaration(org.raml.v2.api.model.v10.datamodel.NumberTypeDeclaration) StringTypeDeclaration(org.raml.v2.api.model.v10.datamodel.StringTypeDeclaration)

Example 77 with ParameterSpec

use of com.squareup.javapoet.ParameterSpec in project web3j by web3j.

the class SolidityFunctionWrapper method addParameters.

String addParameters(MethodSpec.Builder methodBuilder, List<AbiDefinition.NamedType> namedTypes, boolean useNativeJavaTypes) throws ClassNotFoundException {
    final List<ParameterSpec> inputParameterTypes = buildParameterTypes(namedTypes, useJavaPrimitiveTypes);
    final List<ParameterSpec> nativeInputParameterTypes = new ArrayList<>(inputParameterTypes.size());
    for (int i = 0; i < inputParameterTypes.size(); ++i) {
        final TypeName typeName = getTypenameForArray(namedTypes.get(i), useJavaPrimitiveTypes);
        nativeInputParameterTypes.add(ParameterSpec.builder(typeName, inputParameterTypes.get(i).name).build());
    }
    methodBuilder.addParameters(nativeInputParameterTypes);
    if (useNativeJavaTypes) {
        return Collection.join(inputParameterTypes, ", \n", // this results in fully qualified names being generated
        this::createMappedParameterTypes);
    } else {
        return Collection.join(inputParameterTypes, ", ", parameterSpec -> parameterSpec.name);
    }
}
Also used : TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) ParameterSpec(com.squareup.javapoet.ParameterSpec) ArrayList(java.util.ArrayList)

Example 78 with ParameterSpec

use of com.squareup.javapoet.ParameterSpec in project conjure-java by palantir.

the class BeanBuilderGenerator method createAddFieldIfMissing.

private static MethodSpec createAddFieldIfMissing(int fieldCount) {
    ParameterizedTypeName listOfStringType = ParameterizedTypeName.get(List.class, String.class);
    ParameterSpec listParam = ParameterSpec.builder(listOfStringType, "prev").build();
    ParameterSpec fieldValueParam = ParameterSpec.builder(TypeName.BOOLEAN, "initialized").build();
    ParameterSpec fieldNameParam = ParameterSpec.builder(ClassName.get(String.class), "fieldName").build();
    return MethodSpec.methodBuilder("addFieldIfMissing").addModifiers(Modifier.PRIVATE, Modifier.STATIC).returns(listOfStringType).addParameter(listParam).addParameter(fieldValueParam).addParameter(fieldNameParam).addStatement("$T missingFields = $N", listOfStringType, listParam).beginControlFlow("if (!$N)", fieldValueParam).beginControlFlow("if (missingFields == null)").addStatement("missingFields = new $T<>($L)", ArrayList.class, fieldCount).endControlFlow().addStatement("missingFields.add($N)", fieldNameParam).endControlFlow().addStatement("return missingFields").build();
}
Also used : ParameterSpec(com.squareup.javapoet.ParameterSpec) ArrayList(java.util.ArrayList) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 79 with ParameterSpec

use of com.squareup.javapoet.ParameterSpec in project conjure-java by palantir.

the class BeanGenerator method createAddFieldIfMissing.

private static MethodSpec createAddFieldIfMissing(int fieldCount) {
    ParameterizedTypeName listOfStringType = ParameterizedTypeName.get(List.class, String.class);
    ParameterSpec listParam = ParameterSpec.builder(listOfStringType, "prev").build();
    ParameterSpec fieldValueParam = ParameterSpec.builder(ClassName.OBJECT, "fieldValue").build();
    ParameterSpec fieldNameParam = ParameterSpec.builder(ClassName.get(String.class), "fieldName").build();
    return MethodSpec.methodBuilder("addFieldIfMissing").addModifiers(Modifier.PRIVATE, Modifier.STATIC).returns(listOfStringType).addParameter(listParam).addParameter(fieldValueParam).addParameter(fieldNameParam).addStatement("$T missingFields = $N", listOfStringType, listParam).beginControlFlow("if ($N == null)", fieldValueParam).beginControlFlow("if (missingFields == null)").addStatement("missingFields = new $T<>($L)", ArrayList.class, fieldCount).endControlFlow().addStatement("missingFields.add($N)", fieldNameParam).endControlFlow().addStatement("return missingFields").build();
}
Also used : ParameterSpec(com.squareup.javapoet.ParameterSpec) ArrayList(java.util.ArrayList) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 80 with ParameterSpec

use of com.squareup.javapoet.ParameterSpec in project conjure-java by palantir.

the class JerseyServiceGenerator method createAuthParameter.

private Optional<ParameterSpec> createAuthParameter(Optional<AuthType> auth, boolean withAnnotations) {
    if (!auth.isPresent()) {
        return Optional.empty();
    }
    ClassName annotationClassName;
    ClassName tokenClassName;
    String paramName;
    String tokenName;
    if (auth.get().accept(AuthTypeVisitor.IS_HEADER)) {
        annotationClassName = ClassName.get("javax.ws.rs", "HeaderParam");
        tokenClassName = ClassName.get("com.palantir.tokens.auth", "AuthHeader");
        paramName = Auth.AUTH_HEADER_PARAM_NAME;
        tokenName = "Authorization";
    } else if (auth.get().accept(AuthTypeVisitor.IS_COOKIE)) {
        annotationClassName = ClassName.get("javax.ws.rs", "CookieParam");
        tokenClassName = ClassName.get("com.palantir.tokens.auth", "BearerToken");
        paramName = Auth.COOKIE_AUTH_PARAM_NAME;
        tokenName = auth.get().accept(AuthTypeVisitor.COOKIE).getCookieName();
    } else {
        throw new IllegalStateException("Unrecognized auth type: " + auth.get());
    }
    ParameterSpec.Builder paramSpec = ParameterSpec.builder(tokenClassName, paramName);
    if (withAnnotations) {
        paramSpec.addAnnotation(AnnotationSpec.builder(annotationClassName).addMember("value", "$S", tokenName).build());
        if (options.requireNotNullAuthAndBodyParams()) {
            paramSpec.addAnnotation(AnnotationSpec.builder(NOT_NULL).build());
        }
    }
    return Optional.of(paramSpec.build());
}
Also used : ParameterSpec(com.squareup.javapoet.ParameterSpec) ClassName(com.squareup.javapoet.ClassName)

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