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();
}
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();
}
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();
}
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();
}
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();
}
Aggregations