Search in sources :

Example 81 with ParameterSpec

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

the class Retrofit2ServiceGenerator method createCompatibilityBackfillMethod.

private MethodSpec createCompatibilityBackfillMethod(EndpointDefinition endpointDef, TypeMapper returnTypeMapper, TypeMapper argumentTypeMapper, Set<ArgumentName> encodedPathArgs, List<ArgumentDefinition> extraArgs) {
    TypeName returnType = endpointDef.getReturns().map(returnTypeMapper::getClassName).orElse(ClassName.VOID);
    // ensure the correct ordering of parameters by creating the complete sorted parameter list
    List<ParameterSpec> sortedParams = createServiceMethodParameters(endpointDef, argumentTypeMapper, encodedPathArgs);
    List<Optional<ArgumentDefinition>> sortedMaybeExtraArgs = sortedParams.stream().map(param -> extraArgs.stream().filter(arg -> arg.getArgName().get().equals(param.name)).findFirst()).collect(Collectors.toList());
    // omit extraArgs from the back fill method signature
    MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(endpointDef.getEndpointName().get()).addModifiers(Modifier.PUBLIC, Modifier.DEFAULT).addAnnotation(Deprecated.class).addParameters(IntStream.range(0, sortedParams.size()).filter(i -> !sortedMaybeExtraArgs.get(i).isPresent()).mapToObj(sortedParams::get).collect(Collectors.toList())).addAnnotations(ConjureAnnotations.getClientEndpointAnnotations(endpointDef));
    endpointDef.getReturns().ifPresent(type -> methodBuilder.returns(ParameterizedTypeName.get(LISTENABLE_FUTURE_TYPE, returnType.box())));
    // replace extraArgs with default values when invoking the complete method
    StringBuilder sb = new StringBuilder(endpointDef.getReturns().isPresent() ? "return $N(" : "$N(");
    List<Object> values = IntStream.range(0, sortedParams.size()).mapToObj(i -> {
        Optional<ArgumentDefinition> maybeArgDef = sortedMaybeExtraArgs.get(i);
        if (maybeArgDef.isPresent()) {
            sb.append("$L, ");
            return maybeArgDef.get().getType().accept(DefaultTypeValueVisitor.INSTANCE);
        } else {
            sb.append("$N, ");
            return sortedParams.get(i);
        }
    }).collect(Collectors.toList());
    // trim the end
    sb.setLength(sb.length() - 2);
    sb.append(")");
    ImmutableList<Object> methodCallArgs = ImmutableList.builder().add(endpointDef.getEndpointName().get()).addAll(values).build();
    methodBuilder.addStatement(sb.toString(), methodCallArgs.toArray(new Object[0]));
    return methodBuilder.build();
}
Also used : Modifier(javax.lang.model.element.Modifier) Javadoc(com.palantir.conjure.java.util.Javadoc) DefaultableTypeVisitor(com.palantir.conjure.java.visitor.DefaultableTypeVisitor) ClassName(com.squareup.javapoet.ClassName) ConjureDefinition(com.palantir.conjure.spec.ConjureDefinition) ServiceDefinition(com.palantir.conjure.spec.ServiceDefinition) Options(com.palantir.conjure.java.Options) ParameterTypeVisitor(com.palantir.conjure.visitor.ParameterTypeVisitor) MediaType(javax.ws.rs.core.MediaType) ClassNameVisitor(com.palantir.conjure.java.types.ClassNameVisitor) Matcher(java.util.regex.Matcher) Map(java.util.Map) ArgumentName(com.palantir.conjure.spec.ArgumentName) ParameterId(com.palantir.conjure.spec.ParameterId) ImmutableSet(com.google.common.collect.ImmutableSet) ParameterSpec(com.squareup.javapoet.ParameterSpec) SpecializeBinaryClassNameVisitor(com.palantir.conjure.java.types.SpecializeBinaryClassNameVisitor) AuthType(com.palantir.conjure.spec.AuthType) DefaultClassNameVisitor(com.palantir.conjure.java.types.DefaultClassNameVisitor) Collection(java.util.Collection) Set(java.util.Set) TypeMapper(com.palantir.conjure.java.types.TypeMapper) Collectors(java.util.stream.Collectors) ParameterType(com.palantir.conjure.spec.ParameterType) JavaFile(com.squareup.javapoet.JavaFile) List(java.util.List) Stream(java.util.stream.Stream) Paths(com.palantir.util.syntacticpath.Paths) TypeName(com.squareup.javapoet.TypeName) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) EndpointDefinition(com.palantir.conjure.spec.EndpointDefinition) Joiner(com.google.common.base.Joiner) IntStream(java.util.stream.IntStream) SafeLoggerFactory(com.palantir.logsafe.logger.SafeLoggerFactory) TypeFunctions(com.palantir.conjure.java.util.TypeFunctions) AuthTypeVisitor(com.palantir.conjure.visitor.AuthTypeVisitor) HttpPath(com.palantir.conjure.spec.HttpPath) SafeLogger(com.palantir.logsafe.logger.SafeLogger) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Generator(com.palantir.conjure.java.Generator) ParameterOrder(com.palantir.conjure.java.util.ParameterOrder) Path(com.palantir.util.syntacticpath.Path) TypeDefinition(com.palantir.conjure.spec.TypeDefinition) MethodSpec(com.squareup.javapoet.MethodSpec) Packages(com.palantir.conjure.java.util.Packages) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeSpec(com.squareup.javapoet.TypeSpec) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) ConjureAnnotations(com.palantir.conjure.java.ConjureAnnotations) ArgumentDefinition(com.palantir.conjure.spec.ArgumentDefinition) TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) Optional(java.util.Optional) ParameterSpec(com.squareup.javapoet.ParameterSpec) MethodSpec(com.squareup.javapoet.MethodSpec)

Example 82 with ParameterSpec

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

the class UnionGenerator method generateUnknownWrapper.

private static TypeSpec generateUnknownWrapper(ClassName baseClass, ClassName visitorClass, Options options) {
    ParameterizedTypeName genericMapType = ParameterizedTypeName.get(Map.class, String.class, Object.class);
    ParameterizedTypeName genericHashMapType = ParameterizedTypeName.get(HashMap.class, String.class, Object.class);
    ParameterSpec typeParameter = ParameterSpec.builder(String.class, "type").addAnnotation(Nonnull.class).build();
    ParameterSpec annotatedTypeParameter = ParameterSpec.builder(UNKNOWN_MEMBER_TYPE, "type").addAnnotation(AnnotationSpec.builder(JsonProperty.class).addMember("value", "\"type\"").build()).build();
    ClassName wrapperClass = baseClass.peerClass(UNKNOWN_WRAPPER_CLASS_NAME);
    List<FieldSpec> fields = ImmutableList.of(FieldSpec.builder(UNKNOWN_MEMBER_TYPE, "type", Modifier.PRIVATE, Modifier.FINAL).build(), FieldSpec.builder(genericMapType, VALUE_FIELD_NAME, Modifier.PRIVATE, Modifier.FINAL).build());
    TypeSpec.Builder typeBuilder = TypeSpec.classBuilder(wrapperClass).addModifiers(Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL).addSuperinterface(baseClass).addFields(fields).addMethod(MethodSpec.constructorBuilder().addModifiers(Modifier.PRIVATE).addAnnotation(ConjureAnnotations.propertiesJsonCreator()).addParameter(annotatedTypeParameter).addStatement("this($N, new $T())", typeParameter, genericHashMapType).build()).addMethod(MethodSpec.constructorBuilder().addModifiers(Modifier.PRIVATE).addParameter(typeParameter).addParameter(ParameterSpec.builder(genericMapType, VALUE_FIELD_NAME).addAnnotation(Nonnull.class).build()).addStatement("$L", Expressions.requireNonNull(typeParameter.name, "type cannot be null")).addStatement("$L", Expressions.requireNonNull(VALUE_FIELD_NAME, String.format("%s cannot be null", VALUE_FIELD_NAME))).addStatement("this.$1N = $1N", typeParameter).addStatement("this.$1L = $1L", VALUE_FIELD_NAME).build()).addMethod(MethodSpec.methodBuilder("getType").addModifiers(Modifier.PRIVATE).addAnnotation(AnnotationSpec.builder(JsonProperty.class).build()).addStatement("return type").returns(UNKNOWN_MEMBER_TYPE).build()).addMethod(MethodSpec.methodBuilder("getValue").addModifiers(Modifier.PRIVATE).addAnnotation(AnnotationSpec.builder(JsonAnyGetter.class).build()).addStatement("return $L", VALUE_FIELD_NAME).returns(genericMapType).build()).addMethod(MethodSpec.methodBuilder("put").addModifiers(Modifier.PRIVATE).addParameter(String.class, "key").addParameter(Object.class, "val").addAnnotation(AnnotationSpec.builder(JsonAnySetter.class).build()).addStatement("$L.put(key, val)", VALUE_FIELD_NAME).build()).addMethod(createWrapperAcceptMethod(visitorClass, VISIT_UNKNOWN_METHOD_NAME, typeParameter.name, false, options)).addMethod(MethodSpecs.createEquals(wrapperClass)).addMethod(MethodSpecs.createEqualTo(wrapperClass, fields)).addMethod(MethodSpecs.createHashCode(fields)).addMethod(MethodSpecs.createToString(wrapperClass.simpleName(), fields.stream().map(fieldSpec -> FieldName.of(fieldSpec.name)).collect(Collectors.toList())));
    return typeBuilder.build();
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) Modifier(javax.lang.model.element.Modifier) Javadoc(com.palantir.conjure.java.util.Javadoc) DefaultableTypeVisitor(com.palantir.conjure.java.visitor.DefaultableTypeVisitor) BiFunction(java.util.function.BiFunction) ClassName(com.squareup.javapoet.ClassName) PeekingIterator(com.google.common.collect.PeekingIterator) StringUtils(org.apache.commons.lang3.StringUtils) Options(com.palantir.conjure.java.Options) JsonValue(com.fasterxml.jackson.annotation.JsonValue) Map(java.util.Map) JsonAnyGetter(com.fasterxml.jackson.annotation.JsonAnyGetter) StableCollectors(com.palantir.conjure.java.util.StableCollectors) ParameterSpec(com.squareup.javapoet.ParameterSpec) SafeIllegalArgumentException(com.palantir.logsafe.exceptions.SafeIllegalArgumentException) FieldDefinition(com.palantir.conjure.spec.FieldDefinition) Collectors(java.util.stream.Collectors) JavaFile(com.squareup.javapoet.JavaFile) JsonAnySetter(com.fasterxml.jackson.annotation.JsonAnySetter) List(java.util.List) Stream(java.util.stream.Stream) JsonTypeInfo(com.fasterxml.jackson.annotation.JsonTypeInfo) TypeName(com.squareup.javapoet.TypeName) JsonIgnoreProperties(com.fasterxml.jackson.annotation.JsonIgnoreProperties) Nulls(com.fasterxml.jackson.annotation.Nulls) TypeFunctions(com.palantir.conjure.java.util.TypeFunctions) FieldSpec(com.squareup.javapoet.FieldSpec) Type(com.palantir.conjure.spec.Type) DoubleFunction(java.util.function.DoubleFunction) HashMap(java.util.HashMap) Function(java.util.function.Function) Iterators(com.google.common.collect.Iterators) ArrayList(java.util.ArrayList) UnionDefinition(com.palantir.conjure.spec.UnionDefinition) JsonTypeName(com.fasterxml.jackson.annotation.JsonTypeName) SafeArg(com.palantir.logsafe.SafeArg) ImmutableList(com.google.common.collect.ImmutableList) Nonnull(javax.annotation.Nonnull) CodeBlock(com.squareup.javapoet.CodeBlock) IntFunction(java.util.function.IntFunction) TypeVariableName(com.squareup.javapoet.TypeVariableName) TypeDefinition(com.palantir.conjure.spec.TypeDefinition) JsonSubTypes(com.fasterxml.jackson.annotation.JsonSubTypes) MethodSpec(com.squareup.javapoet.MethodSpec) Packages(com.palantir.conjure.java.util.Packages) JavaNameSanitizer(com.palantir.conjure.java.util.JavaNameSanitizer) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeSpec(com.squareup.javapoet.TypeSpec) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) ConjureAnnotations(com.palantir.conjure.java.ConjureAnnotations) FieldName(com.palantir.conjure.spec.FieldName) Comparator(java.util.Comparator) Collections(java.util.Collections) JsonSetter(com.fasterxml.jackson.annotation.JsonSetter) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) ParameterSpec(com.squareup.javapoet.ParameterSpec) Nonnull(javax.annotation.Nonnull) ClassName(com.squareup.javapoet.ClassName) FieldSpec(com.squareup.javapoet.FieldSpec) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 83 with ParameterSpec

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

the class EnumGenerator method createValueOf.

private static MethodSpec createValueOf(ClassName thisClass, List<EnumValueDefinition> values) {
    ParameterSpec param = ParameterSpec.builder(ClassName.get(String.class), "value").addAnnotation(Nonnull.class).build();
    CodeBlock.Builder parser = CodeBlock.builder().beginControlFlow("switch (upperCasedValue)");
    for (EnumValueDefinition value : values) {
        parser.add("case $S:\n", value.getValue()).indent().addStatement("return $L", value.getValue()).unindent();
    }
    parser.add("default:\n").indent().addStatement("return new $T(Value.UNKNOWN, upperCasedValue)", thisClass).unindent().endControlFlow();
    boolean anyDeprecatedValues = values.stream().anyMatch(definition -> definition.getDeprecated().isPresent());
    return MethodSpec.methodBuilder("valueOf").addModifiers(Modifier.PUBLIC, Modifier.STATIC).returns(thisClass).addAnnotation(ConjureAnnotations.delegatingJsonCreator()).addAnnotations(anyDeprecatedValues ? ImmutableList.of(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "deprecation").build()) : ImmutableList.of()).addParameter(param).addStatement("$L", Expressions.requireNonNull(param.name, param.name + " cannot be null")).addStatement("String upperCasedValue = $N.toUpperCase($T.ROOT)", param, Locale.class).addCode(parser.build()).build();
}
Also used : ParameterSpec(com.squareup.javapoet.ParameterSpec) Nonnull(javax.annotation.Nonnull) EnumValueDefinition(com.palantir.conjure.spec.EnumValueDefinition) CodeBlock(com.squareup.javapoet.CodeBlock)

Example 84 with ParameterSpec

use of com.squareup.javapoet.ParameterSpec in project cypher-dsl by neo4j-contrib.

the class NodeImplBuilder method buildCopyConstructor.

private MethodSpec buildCopyConstructor() {
    ParameterSpec symbolicName = ParameterSpec.builder(TYPE_NAME_SYMBOLIC_NAME, "symbolicName").build();
    ParameterSpec labelsParameter = ParameterSpec.builder(ParameterizedTypeName.get(TYPE_NAME_LIST, TYPE_NAME_NODE_LABEL), "labels").build();
    ParameterSpec properties = ParameterSpec.builder(ClassName.get(Properties.class), "properties").build();
    return MethodSpec.constructorBuilder().addModifiers(Modifier.PRIVATE).addParameter(symbolicName).addParameter(labelsParameter).addParameter(properties).addStatement("super($N, $N, $N)", symbolicName, labelsParameter, properties).build();
}
Also used : ParameterSpec(com.squareup.javapoet.ParameterSpec)

Example 85 with ParameterSpec

use of com.squareup.javapoet.ParameterSpec in project DaggerCodeParse-2.38.1base by hellogaod.

the class AssistedInjectionAnnotations method assistedParameterSpecs.

private static ImmutableList<ParameterSpec> assistedParameterSpecs(List<? extends VariableElement> paramElements, List<? extends TypeMirror> paramTypes, ComponentImplementation.ShardImplementation shardImplementation) {
    ImmutableList.Builder<ParameterSpec> assistedParameterSpecs = ImmutableList.builder();
    for (int i = 0; i < paramElements.size(); i++) {
        VariableElement paramElement = paramElements.get(i);
        TypeMirror paramType = paramTypes.get(i);
        if (AssistedInjectionAnnotations.isAssistedParameter(paramElement)) {
            assistedParameterSpecs.add(ParameterSpec.builder(TypeName.get(paramType), shardImplementation.getUniqueFieldNameForAssistedParam(paramElement)).build());
        }
    }
    return assistedParameterSpecs.build();
}
Also used : ParameterSpec(com.squareup.javapoet.ParameterSpec) TypeMirror(javax.lang.model.type.TypeMirror) ImmutableList(com.google.common.collect.ImmutableList) DaggerStreams.toImmutableList(dagger.internal.codegen.extension.DaggerStreams.toImmutableList) VariableElement(javax.lang.model.element.VariableElement)

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