use of com.squareup.javapoet.MethodSpec in project storio by pushtorefresh.
the class PutResolverGenerator method createMapToContentValuesMethodSpec.
@NotNull
private MethodSpec createMapToContentValuesMethodSpec(@NotNull StorIOSQLiteTypeMeta storIOSQLiteTypeMeta, @NotNull ClassName storIOSQLiteTypeClassName) {
final MethodSpec.Builder builder = MethodSpec.methodBuilder("mapToContentValues").addJavadoc("{@inheritDoc}\n").addAnnotation(Override.class).addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME).addModifiers(PUBLIC).returns(ClassName.get("android.content", "ContentValues")).addParameter(ParameterSpec.builder(storIOSQLiteTypeClassName, "object").addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME).build()).addStatement("ContentValues contentValues = new ContentValues($L)", storIOSQLiteTypeMeta.columns.size()).addCode("\n");
for (StorIOSQLiteColumnMeta columnMeta : storIOSQLiteTypeMeta.columns.values()) {
final boolean ignoreNull = columnMeta.storIOColumn.ignoreNull();
if (ignoreNull) {
builder.beginControlFlow("if (object.$L != null)", columnMeta.elementName + (columnMeta.isMethod() ? "()" : ""));
}
builder.addStatement("contentValues.put($S, object.$L)", columnMeta.storIOColumn.name(), columnMeta.elementName + (columnMeta.isMethod() ? "()" : ""));
if (ignoreNull) {
builder.endControlFlow();
}
}
return builder.addCode("\n").addStatement("return contentValues").build();
}
use of com.squareup.javapoet.MethodSpec in project wire by square.
the class JavaGenerator method messageFieldsAndUnknownFieldsConstructor.
// Example:
//
// public SimpleMessage(int optional_int32, long optional_int64, ByteString unknownFields) {
// super(ADAPTER, unknownFields);
// this.optional_int32 = optional_int32;
// this.optional_int64 = optional_int64;
// }
//
private MethodSpec messageFieldsAndUnknownFieldsConstructor(NameAllocator nameAllocator, MessageType type) {
NameAllocator localNameAllocator = nameAllocator.clone();
String adapterName = localNameAllocator.get("ADAPTER");
String unknownFieldsName = localNameAllocator.newName("unknownFields");
MethodSpec.Builder result = MethodSpec.constructorBuilder().addModifiers(PUBLIC).addStatement("super($N, $N)", adapterName, unknownFieldsName);
for (OneOf oneOf : type.oneOfs()) {
if (oneOf.fields().size() < 2)
continue;
CodeBlock.Builder fieldNamesBuilder = CodeBlock.builder();
boolean first = true;
for (Field field : oneOf.fields()) {
if (!first)
fieldNamesBuilder.add(", ");
fieldNamesBuilder.add("$N", localNameAllocator.get(field));
first = false;
}
CodeBlock fieldNames = fieldNamesBuilder.build();
result.beginControlFlow("if ($T.countNonNull($L) > 1)", Internal.class, fieldNames);
result.addStatement("throw new IllegalArgumentException($S)", "at most one of " + fieldNames + " may be non-null");
result.endControlFlow();
}
for (Field field : type.fieldsAndOneOfFields()) {
TypeName javaType = fieldType(field);
String fieldName = localNameAllocator.get(field);
ParameterSpec.Builder param = ParameterSpec.builder(javaType, fieldName);
if (emitAndroid && field.isOptional()) {
param.addAnnotation(NULLABLE);
}
result.addParameter(param.build());
if (field.isRepeated() || field.type().isMap()) {
result.addStatement("this.$1L = $2T.immutableCopyOf($1S, $1L)", fieldName, Internal.class);
} else {
result.addStatement("this.$1L = $1L", fieldName);
}
}
result.addParameter(BYTE_STRING, unknownFieldsName);
return result.build();
}
use of com.squareup.javapoet.MethodSpec in project react4j by react4j.
the class Generator method buildStaticStepMethodMethod.
@Nonnull
private static MethodSpec buildStaticStepMethodMethod(@Nonnull final ComponentDescriptor descriptor, @Nonnull final Step step, @Nonnull final StepMethod stepMethod) {
final MethodSpec.Builder method = MethodSpec.methodBuilder(stepMethod.getName()).addAnnotation(NONNULL_CLASSNAME);
method.addModifiers(Modifier.STATIC);
if (descriptor.getDeclaredType().asElement().getModifiers().contains(Modifier.PUBLIC)) {
method.addModifiers(Modifier.PUBLIC);
}
final ExecutableType propMethodType = stepMethod.getPropMethodType();
if (null != propMethodType) {
ProcessorUtil.copyTypeParameters(propMethodType, method);
}
ProcessorUtil.copyTypeParameters(descriptor.getElement(), method);
if (!stepMethod.isBuildIntrinsic()) {
final ParameterSpec.Builder parameter = ParameterSpec.builder(stepMethod.getType(), stepMethod.getName(), Modifier.FINAL);
final ExecutableElement propMethod = stepMethod.getPropMethod();
if (null != propMethod) {
ProcessorUtil.copyDocumentedAnnotations(propMethod, parameter);
} else if (stepMethod.isKeyIntrinsic() || stepMethod.isChildrenStreamIntrinsic()) {
parameter.addAnnotation(NONNULL_CLASSNAME);
} else if (stepMethod.isChildOfChildrenIntrinsic()) {
parameter.addAnnotation(NULLABLE_CLASSNAME);
}
method.addParameter(parameter.build());
}
final String infix = asTypeArgumentsInfix(descriptor.getDeclaredType());
if (stepMethod.isBuildIntrinsic()) {
method.addStatement("return new $T" + infix + "().build()", ClassName.bestGuess("Builder"));
} else {
method.addStatement("return new $T" + infix + "().$N( $N )", ClassName.bestGuess("Builder"), stepMethod.getName(), stepMethod.getName());
}
configureStepMethodReturns(method, step, stepMethod.getStepMethodType());
return method.build();
}
use of com.squareup.javapoet.MethodSpec in project react4j by react4j.
the class Generator method buildBuilderStepImpl.
@Nonnull
private static MethodSpec buildBuilderStepImpl(@Nonnull final Step step, @Nonnull final StepMethod stepMethod) {
final MethodSpec.Builder method = MethodSpec.methodBuilder(stepMethod.getName());
method.addModifiers(Modifier.PUBLIC, Modifier.FINAL);
method.addAnnotation(Override.class);
method.addAnnotation(NONNULL_CLASSNAME);
final ExecutableType propMethodType = stepMethod.getPropMethodType();
if (null != propMethodType) {
ProcessorUtil.copyTypeParameters(propMethodType, method);
}
final ParameterSpec.Builder parameter = ParameterSpec.builder(stepMethod.getType(), stepMethod.getName(), Modifier.FINAL);
final ExecutableElement propMethod = stepMethod.getPropMethod();
if (null != propMethod) {
ProcessorUtil.copyDocumentedAnnotations(propMethod, parameter);
} else if (stepMethod.isKeyIntrinsic() || stepMethod.isChildrenStreamIntrinsic()) {
parameter.addAnnotation(NONNULL_CLASSNAME);
} else if (stepMethod.isChildOfChildrenIntrinsic()) {
parameter.addAnnotation(NULLABLE_CLASSNAME);
}
method.addParameter(parameter.build());
if (stepMethod.isChildrenIntrinsic()) {
method.varargs();
final CodeBlock.Builder block = CodeBlock.builder();
block.beginControlFlow("for ( final $T child : $N )", REACT_NODE_CLASSNAME, stepMethod.getName());
block.addStatement("child( child )");
block.endControlFlow();
method.addCode(block.build());
} else if (stepMethod.isChildOfChildrenIntrinsic()) {
final CodeBlock.Builder block = CodeBlock.builder();
block.beginControlFlow("if ( null != $N )", stepMethod.getName());
block.addStatement("_children.push( $N )", stepMethod.getName());
block.endControlFlow();
method.addCode(block.build());
} else if (stepMethod.isChildrenStreamIntrinsic()) {
method.addStatement("children( $N.toArray( $T[]::new ) )", stepMethod.getName(), REACT_NODE_CLASSNAME);
} else if (stepMethod.isChildIntrinsic()) {
assert null != propMethod;
if (null != ProcessorUtil.findAnnotationByType(propMethod, Constants.NONNULL_ANNOTATION_CLASSNAME)) {
method.addStatement("_child = $T.requireNonNull( $N )", Objects.class, stepMethod.getName());
} else {
method.addStatement("_child = $N", stepMethod.getName());
}
} else if (stepMethod.isKeyIntrinsic() || (null != propMethod && null != ProcessorUtil.findAnnotationByType(propMethod, Constants.NONNULL_ANNOTATION_CLASSNAME))) {
method.addStatement("_props.set( $S, $T.requireNonNull( $N ) )", stepMethod.getName(), Objects.class, stepMethod.getName());
} else {
method.addStatement("_props.set( $S, $N )", stepMethod.getName(), stepMethod.getName());
}
if (StepMethodType.TERMINATE == stepMethod.getStepMethodType()) {
method.addStatement("return build()");
} else {
method.addStatement("return this");
}
configureStepMethodReturns(method, step, stepMethod.getStepMethodType());
return method.build();
}
use of com.squareup.javapoet.MethodSpec in project web3sdk by FISCO-BCOS.
the class AbiTypesGenerator method generateFixedTypes.
private <T extends Type> void generateFixedTypes(Class<T> superclass, Path path) throws IOException {
String packageName = createPackageName(superclass);
ClassName className;
for (int mBitSize = 8; mBitSize < Type.MAX_BIT_LENGTH; mBitSize += 8) {
inner: for (int nBitSize = 8; nBitSize < Type.MAX_BIT_LENGTH; nBitSize += 8) {
if (mBitSize + nBitSize > Type.MAX_BIT_LENGTH) {
break inner;
}
MethodSpec constructorSpec1 = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addParameter(BigInteger.class, "value").addStatement("super($L, $L, $N)", mBitSize, nBitSize, "value").build();
MethodSpec constructorSpec2 = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addParameter(int.class, "mBitSize").addParameter(int.class, "nBitSize").addParameter(BigInteger.class, "m").addParameter(BigInteger.class, "n").addStatement("super($L, $L, $N, $N)", mBitSize, nBitSize, "m", "n").build();
className = ClassName.get(packageName, superclass.getSimpleName() + mBitSize + "x" + nBitSize);
FieldSpec defaultFieldSpec = FieldSpec.builder(className, DEFAULT, Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL).initializer("new $T(BigInteger.ZERO)", className).build();
TypeSpec fixedType = TypeSpec.classBuilder(className.simpleName()).addJavadoc(CODEGEN_WARNING).superclass(superclass).addModifiers(Modifier.PUBLIC).addField(defaultFieldSpec).addMethod(constructorSpec1).addMethod(constructorSpec2).build();
write(packageName, fixedType, path);
}
}
}
Aggregations