use of com.squareup.javapoet.ParameterSpec in project so by onap.
the class FluentGenerator method createInnerInfoClass.
protected TypeSpec createInnerInfoClass(ObjectType oType) {
List<FieldSpec> classFields = new ArrayList<>();
List<MethodSpec> methods = new ArrayList<>();
classFields.add(FieldSpec.builder(String.class, "partialUri").addModifiers(Modifier.PRIVATE, Modifier.FINAL, Modifier.STATIC).initializer("$S", oType.getPartialUri()).build());
classFields.add(FieldSpec.builder(ParameterizedTypeName.get(List.class, String.class), "paths").addModifiers(Modifier.PRIVATE, Modifier.FINAL, Modifier.STATIC).initializer("$T.asList($L)", ClassName.get(Arrays.class), "\"" + oType.getPaths().stream().collect(Collectors.joining("\", \"")) + "\"").build());
if (oType.getType().equals("plural")) {
Pair<String, String> path = splitClasspath(this.pluralFragmentClass);
ClassName fragmentClass = ClassName.get(path.getLeft(), path.getRight());
path = splitClasspath(this.baseBuilderClass);
ClassName baseClass = ClassName.get(path.getLeft(), path.getRight());
classFields.add(FieldSpec.builder(fragmentClass, "fragment").addModifiers(Modifier.PRIVATE, Modifier.FINAL, Modifier.STATIC).initializer("new $T(new $L(new $T(){}))", fragmentClass, upperCamel(oType.getName()), baseClass).build());
}
ClassName superInterface;
String name;
if (oType.getType().equals("plural")) {
Pair<String, String> path = splitClasspath(this.pluralBuilderClass);
superInterface = ClassName.get(path.getLeft(), path.getRight());
name = oType.getAdditionalName();
} else if (oType.getType().equals("singular")) {
Pair<String, String> path = splitClasspath(this.singularBuilderClass);
superInterface = ClassName.get(path.getLeft(), path.getRight());
name = oType.getName();
} else {
Pair<String, String> path = splitClasspath(this.topLevelBuilderClass);
superInterface = ClassName.get(path.getLeft(), path.getRight());
name = oType.getName();
}
superInterface = superInterface.nestedClass("Info");
methods.add(MethodSpec.methodBuilder("getPaths").returns(ParameterizedTypeName.get(List.class, String.class)).addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addStatement("return Info.paths").build());
methods.add(MethodSpec.methodBuilder("getPartialUri").returns(String.class).addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addStatement("return Info.partialUri").build());
if (oType.getType().equals("plural")) {
Pair<String, String> path = splitClasspath(this.pluralFragmentClass);
ClassName fragmentClass = ClassName.get(path.getLeft(), path.getRight());
methods.add(MethodSpec.methodBuilder("getFragment").returns(fragmentClass).addModifiers(Modifier.PUBLIC).addStatement("return fragment").build());
} else if (oType.getType().equals("singular")) {
Pair<String, String> path = splitClasspath(this.singleFragmentClass);
ClassName fragmentClass = ClassName.get(path.getLeft(), path.getRight());
path = splitClasspath(this.baseBuilderClass);
ClassName baseClass = ClassName.get(path.getLeft(), path.getRight());
List<ParameterSpec> typeParams = new ArrayList<>();
for (ObjectField oF : oType.getFields()) {
if (oF.getType().equals("string")) {
typeParams.add(ParameterSpec.builder(String.class, lowerCamel(makeValidJavaVariable(oF.getName()))).build());
} else if (oF.getType().equals("integer")) {
typeParams.add(ParameterSpec.builder(int.class, lowerCamel(makeValidJavaVariable(oF.getName()))).build());
}
}
methods.add(MethodSpec.methodBuilder("getFragment").returns(fragmentClass).addParameters(typeParams).addModifiers(Modifier.PUBLIC).addStatement("return new $T(new $L(new $T(){}, $L))", fragmentClass, upperCamel(oType.getName()), baseClass, typeParams.stream().map(item -> item.name).collect(Collectors.joining(", "))).build());
}
if (!oType.getType().equals("top level")) {
classFields.add(FieldSpec.builder(String.class, "name").addModifiers(Modifier.PRIVATE, Modifier.FINAL, Modifier.STATIC).initializer("$S", name).build());
classFields.add(FieldSpec.builder(ClassName.get("", "UriParams"), "uriParams").addModifiers(Modifier.PRIVATE, Modifier.FINAL, Modifier.STATIC).initializer("new $T()", ClassName.get("", "UriParams")).build());
methods.add(MethodSpec.methodBuilder("getName").returns(String.class).addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addStatement("return Info.name").build());
methods.add(MethodSpec.methodBuilder("getUriParams").returns(ClassName.get("", "UriParams")).addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addStatement("return Info.uriParams").build());
}
TypeSpec.Builder returnTypeSpec = TypeSpec.classBuilder("Info").addModifiers(Modifier.PUBLIC, Modifier.STATIC).addSuperinterface(superInterface).addFields(classFields).addMethods(methods);
if (!oType.getType().equals("top level")) {
returnTypeSpec.addType(createUriParamsClass(superInterface, oType));
}
return returnTypeSpec.build();
}
use of com.squareup.javapoet.ParameterSpec in project so by onap.
the class FluentGenerator method createAccessMethod.
protected MethodSpec createAccessMethod(ObjectType oType, boolean isChild, boolean isStatic) {
ClassName childClass = ClassName.get(CLASSPATH, upperCamel(oType.getName()));
MethodSpec.Builder b = MethodSpec.methodBuilder(lowerCamel(oType.getName())).returns(childClass);
List<Modifier> modifiers = new ArrayList<>();
if (isStatic) {
modifiers.add(Modifier.STATIC);
}
modifiers.add(Modifier.PUBLIC);
b.addModifiers(modifiers);
List<ParameterSpec> params = new ArrayList<>();
for (ObjectField oF : doc.get(oType.getName()).getFields()) {
if (oF.getType().equals("string")) {
params.add(ParameterSpec.builder(String.class, lowerCamel(makeValidJavaVariable(oF.getName()))).build());
} else if (oF.getType().equals("integer")) {
params.add(ParameterSpec.builder(int.class, lowerCamel(makeValidJavaVariable(oF.getName()))).build());
}
}
List<String> paramNames = params.stream().map(item -> item.name).collect(Collectors.toList());
if (isChild) {
paramNames.add(0, "this");
}
b.addParameters(params).addStatement("return new $T($L)", childClass, String.join(", ", paramNames));
return b.build();
}
use of com.squareup.javapoet.ParameterSpec in project so by onap.
the class FluentGenerator method createInterfaceMethods.
protected List<MethodSpec> createInterfaceMethods(ObjectType oType, List<ParameterSpec> typeParams) {
List<MethodSpec> methods = new ArrayList<>();
CodeBlock.Builder uriTemplateCodeBlock = CodeBlock.builder();
if (!oType.getType().equals("top level")) {
uriTemplateCodeBlock.add("return this.parentObj.uriTemplate() + Info.partialUri");
} else {
uriTemplateCodeBlock.add("return Info.partialUri");
}
methods.add(MethodSpec.methodBuilder("uriTemplate").returns(String.class).addModifiers(Modifier.PUBLIC).addStatement(uriTemplateCodeBlock.build()).addAnnotation(Override.class).build());
ClassName arrayUtils = ClassName.get(ArrayUtils.class);
CodeBlock.Builder valuesReturn = CodeBlock.builder();
if (oType.getType().equals("top level")) {
valuesReturn.add("return new Object[0]");
} else {
if (!typeParams.isEmpty()) {
valuesReturn.add("return $T.addAll(this.parentObj.values(), $L)", arrayUtils, String.join(", ", typeParams.stream().map(item -> "this." + item.name).collect(Collectors.toList())));
} else {
valuesReturn.add("return this.parentObj.values()");
}
}
methods.add(MethodSpec.methodBuilder("values").returns(Object[].class).addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addStatement(valuesReturn.build()).build());
if (!oType.getType().equals("top level")) {
ClassName returnType = null;
CodeBlock.Builder block = CodeBlock.builder();
if (oType.getType().equals("singular")) {
Pair<String, String> path = splitClasspath(this.singularClass);
returnType = ClassName.get(path.getLeft(), path.getRight());
block.add("return new $T(this.parentObj.uriTemplate(), Info.partialUri, Info.name, false)", returnType);
} else if (oType.getType().equals("plural")) {
Pair<String, String> path = splitClasspath(this.pluralClass);
returnType = ClassName.get(path.getLeft(), path.getRight());
block.add("return new $T(Info.name, this.parentObj.uriTemplate(), Info.partialUri)", returnType);
}
methods.add(MethodSpec.methodBuilder("build").returns(returnType).addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addStatement(block.build()).build());
}
return methods;
}
use of com.squareup.javapoet.ParameterSpec in project celesta by CourseOrchestra.
the class CursorGenerator method buildConstructors.
private static List<MethodSpec> buildConstructors(GrainElement ge, TypeName columnsClassType) {
List<MethodSpec> results = new ArrayList<>();
ParameterSpec contextParam = ParameterSpec.builder(CallContext.class, "context").build();
ParameterSpec fieldsParam = ParameterSpec.builder(ParameterizedTypeName.get(Set.class, String.class), "fields").build();
ParameterSpec columnsParam = ParameterSpec.builder(ArrayTypeName.of(ParameterizedTypeName.get(ClassName.get(ColumnMeta.class), WildcardTypeName.subtypeOf(Object.class))), "columns").build();
ParameterSpec parametersParam = ParameterSpec.builder(ParameterizedTypeName.get(Map.class, String.class, Object.class), "parameters").build();
Supplier<MethodSpec.Builder> msp = () -> MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addParameter(contextParam);
// Common constructor
MethodSpec.Builder builder = msp.get();
if (ge instanceof ParameterizedView) {
builder.addParameter(parametersParam);
builder.addStatement("super(context, parameters)");
} else {
builder.addStatement("super(context)");
}
results.add(builder.build());
if (ge instanceof SequenceElement) {
return results;
}
// Constructor with columns limitation
builder = msp.get();
if (ge instanceof ParameterizedView) {
builder.addParameter(parametersParam);
builder.addParameter(columnsParam).varargs();
builder.addStatement("super(context, parameters, columns)");
} else {
builder.addParameter(columnsParam).varargs();
builder.addStatement("super(context, columns)");
}
results.add(builder.build());
// Deprecated constructor with fields limitation
builder = msp.get();
if (ge instanceof ParameterizedView) {
builder.addParameter(fieldsParam);
builder.addParameter(parametersParam);
builder.addStatement("super(context, fields, parameters)");
} else {
builder.addParameter(fieldsParam);
builder.addStatement("super(context, fields)");
}
results.add(builder.addAnnotation(Deprecated.class).build());
// ParameterizedView constructors
if (ge instanceof ParameterizedView) {
ParameterizedView pv = (ParameterizedView) ge;
builder = msp.get();
for (Parameter parameter : pv.getParameters().values()) {
builder.addParameter(ParameterSpec.builder(parameter.getJavaClass(), parameter.getName()).build());
}
String spec = "super (context, paramsMap(" + pv.getParameters().values().stream().map(c -> "$N").collect(Collectors.joining(", ")) + "))";
builder.addStatement(spec, pv.getParameters().keySet().toArray());
results.add(builder.build());
builder = msp.get();
for (Parameter parameter : pv.getParameters().values()) {
builder.addParameter(ParameterSpec.builder(parameter.getJavaClass(), parameter.getName()).build());
}
builder.addParameter(columnsParam).varargs();
spec = "super (context, paramsMap(" + pv.getParameters().values().stream().map(c -> "$N").collect(Collectors.joining(", ")) + "), columns)";
builder.addStatement(spec, pv.getParameters().keySet().toArray());
results.add(builder.build());
results.add(getParameterizedViewTypedConstructorHelper(pv));
}
return results;
}
use of com.squareup.javapoet.ParameterSpec in project celesta by CourseOrchestra.
the class CursorGenerator method buildClearBuffer.
private MethodSpec buildClearBuffer(Map<String, ? extends ColumnMeta<?>> columns, Set<Column<?>> pk) {
ParameterSpec param = ParameterSpec.builder(boolean.class, "withKeys").build();
MethodSpec.Builder builder = MethodSpec.methodBuilder("_clearBuffer").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addParameter(param);
if (!pk.isEmpty()) {
builder.beginControlFlow("if ($N)", param.name);
pk.stream().forEach(c -> builder.addStatement("this.$N = null", camelize(c.getName())));
builder.endControlFlow();
}
columns.entrySet().stream().filter(e -> !pk.contains(e.getValue())).forEach(e -> builder.addStatement("this.$N = null", camelize(e.getKey())));
return builder.build();
}
Aggregations