use of com.squareup.javapoet.CodeBlock in project wire by square.
the class JavaGenerator method optionAnnotation.
@Nullable
private AnnotationSpec optionAnnotation(ProtoMember protoMember, Object value) {
if (!emitAppliedOptions)
return null;
Field field = schema.getField(protoMember);
if (field == null)
return null;
if (!eligibleAsAnnotationMember(schema, field))
return null;
ProtoFile protoFile = schema.protoFile(field.getLocation().getPath());
String simpleName = camelCase(field.getName(), true) + "Option";
ClassName type = ClassName.get(javaPackage(protoFile), simpleName);
CodeBlock fieldValue = fieldInitializer(field.getType(), value);
return AnnotationSpec.builder(type).addMember("value", fieldValue).build();
}
use of com.squareup.javapoet.CodeBlock in project wire by square.
the class JavaGenerator method messageConstructor.
// Example:
//
// public SimpleMessage(int optional_int32, long optional_int64, ByteString unknownFields) {
// super(ADAPTER, unknownFields);
// this.optional_int32 = optional_int32;
// this.optional_int64 = optional_int64;
// }
//
// Alternate example, where the constructor takes in a builder, would be the case when there are
// too many fields:
//
// public SimpleMessage(Builder builder, ByteString unknownFields) {
// super(ADAPTER, unknownFields);
// this.optional_int32 = builder.optional_int32;
// this.optional_int64 = builder.optional_int64;
// }
//
private MethodSpec messageConstructor(NameAllocator nameAllocator, MessageType type, ClassName builderJavaType) {
boolean constructorTakesAllFields = constructorTakesAllFields(type);
NameAllocator localNameAllocator = nameAllocator.clone();
String adapterName = localNameAllocator.get("ADAPTER");
String unknownFieldsName = localNameAllocator.newName("unknownFields");
String builderName = localNameAllocator.newName("builder");
MethodSpec.Builder result = MethodSpec.constructorBuilder().addModifiers(PUBLIC).addStatement("super($N, $N)", adapterName, unknownFieldsName);
for (OneOf oneOf : type.getOneOfs()) {
if (oneOf.getFields().size() < 2)
continue;
CodeBlock.Builder fieldNamesBuilder = CodeBlock.builder();
boolean first = true;
for (Field field : oneOf.getFields()) {
if (!first)
fieldNamesBuilder.add(", ");
if (constructorTakesAllFields) {
fieldNamesBuilder.add("$N", localNameAllocator.get(field));
} else {
fieldNamesBuilder.add("$N.$N", builderName, 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.getFieldsAndOneOfFields()) {
TypeName javaType = fieldType(field);
String fieldName = localNameAllocator.get(field);
String fieldAccessName = constructorTakesAllFields ? fieldName : builderName + "." + fieldName;
if (constructorTakesAllFields) {
ParameterSpec.Builder param = ParameterSpec.builder(javaType, fieldName);
if (emitAndroidAnnotations && field.getEncodeMode() == Field.EncodeMode.NULL_IF_ABSENT) {
param.addAnnotation(NULLABLE);
}
result.addParameter(param.build());
}
if (field.getEncodeMode() == Field.EncodeMode.OMIT_IDENTITY) {
// Other scalars use not-boxed types to guarantee a value.
if (field.getType().isScalar() && (field.getType() == ProtoType.STRING || field.getType() == ProtoType.BYTES) || (isEnum(field.getType()) && !field.getType().equals(ProtoType.STRUCT_NULL))) {
result.beginControlFlow("if ($L == null)", fieldAccessName);
result.addStatement("throw new IllegalArgumentException($S)", fieldAccessName + " == null");
result.endControlFlow();
}
}
if (field.getType().isMap() && isStruct(field.getType().getValueType())) {
result.addStatement("this.$1L = $2T.immutableCopyOfMapWithStructValues($1S, $3L)", fieldName, Internal.class, fieldAccessName);
} else if (isStruct(field.getType())) {
result.addStatement("this.$1L = $2T.immutableCopyOfStruct($1S, $3L)", fieldName, Internal.class, fieldAccessName);
} else if (field.isRepeated() || field.getType().isMap()) {
result.addStatement("this.$1L = $2T.immutableCopyOf($1S, $3L)", fieldName, Internal.class, fieldAccessName);
} else {
result.addStatement("this.$1L = $2L", fieldName, fieldAccessName);
}
}
if (!constructorTakesAllFields) {
result.addParameter(builderJavaType, builderName);
}
result.addParameter(BYTE_STRING, unknownFieldsName);
return result.build();
}
Aggregations