use of com.squareup.wire.schema.OneOf 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.wire.schema.OneOf in project wire by square.
the class JavaGenerator method wireFieldAnnotation.
// Example:
//
// @WireField(
// tag = 1,
// type = INT32
// )
//
private AnnotationSpec wireFieldAnnotation(NameAllocator nameAllocator, Field field, MessageType message) {
AnnotationSpec.Builder result = AnnotationSpec.builder(WireField.class);
NameAllocator localNameAllocator = nameAllocator.clone();
int tag = field.getTag();
result.addMember("tag", String.valueOf(tag));
if (field.getType().isMap()) {
result.addMember("keyAdapter", "$S", adapterString(field.getType().getKeyType()));
result.addMember("adapter", "$S", adapterString(field.getType().getValueType()));
} else {
result.addMember("adapter", "$S", adapterString(field.getType()));
}
WireField.Label wireFieldLabel;
// noinspection ConstantConditions
switch(field.getEncodeMode()) {
case REQUIRED:
wireFieldLabel = WireField.Label.REQUIRED;
break;
case OMIT_IDENTITY:
wireFieldLabel = WireField.Label.OMIT_IDENTITY;
break;
case REPEATED:
wireFieldLabel = WireField.Label.REPEATED;
break;
case PACKED:
wireFieldLabel = WireField.Label.PACKED;
break;
case MAP:
case NULL_IF_ABSENT:
default:
wireFieldLabel = null;
}
if (wireFieldLabel != null) {
result.addMember("label", "$T.$L", WireField.Label.class, wireFieldLabel);
}
if (field.isRedacted()) {
result.addMember("redacted", "true");
}
String generatedName = localNameAllocator.get(field);
if (!generatedName.equals(field.getName())) {
result.addMember("declaredName", "$S", field.getName());
}
if (!field.getJsonName().equals(field.getName())) {
result.addMember("jsonName", "$S", field.getJsonName());
}
if (field.isOneOf()) {
String oneofName = null;
for (OneOf oneOf : message.getOneOfs()) {
if (oneOf.getFields().contains(field)) {
oneofName = oneOf.getName();
break;
}
}
if (oneofName == null) {
throw new IllegalArgumentException("No oneof found for field: " + field.getQualifiedName());
}
result.addMember("oneofName", "$S", oneofName);
}
return result.build();
}
use of com.squareup.wire.schema.OneOf in project wire by square.
the class JavaGenerator method builder.
private TypeSpec builder(NameAllocator nameAllocator, MessageType type, ClassName javaType, ClassName builderType) {
TypeSpec.Builder result = TypeSpec.classBuilder("Builder").addModifiers(PUBLIC, STATIC, FINAL);
result.superclass(builderOf(javaType, builderType));
for (Field field : type.getFieldsAndOneOfFields()) {
String fieldName = nameAllocator.get(field);
result.addField(fieldType(field), fieldName, PUBLIC);
}
result.addMethod(builderNoArgsConstructor(nameAllocator, type));
for (Field field : type.fields()) {
result.addMethod(setter(nameAllocator, builderType, null, field));
}
for (OneOf oneOf : type.getOneOfs()) {
for (Field field : oneOf.getFields()) {
result.addMethod(setter(nameAllocator, builderType, oneOf, field));
}
}
result.addMethod(builderBuild(nameAllocator, type, javaType));
return result.build();
}
use of com.squareup.wire.schema.OneOf 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