use of com.squareup.wire.schema.Field 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.Field in project wire by square.
the class JavaGenerator method messageAdapterRedact.
private MethodSpec messageAdapterRedact(NameAllocator nameAllocator, MessageType type, ClassName javaType, boolean useBuilder, ClassName builderJavaType) {
MethodSpec.Builder result = MethodSpec.methodBuilder("redact").addAnnotation(Override.class).addModifiers(PUBLIC).returns(javaType).addParameter(javaType, "value");
int redactedFieldCount = 0;
List<String> requiredRedacted = new ArrayList<>();
for (Field field : type.getFieldsAndOneOfFields()) {
if (field.isRedacted()) {
redactedFieldCount++;
if (field.isRequired()) {
requiredRedacted.add(nameAllocator.get(field));
}
}
}
if (!useBuilder) {
result.addStatement((redactedFieldCount == 0) ? "return value" : "return null");
return result.build();
}
if (!requiredRedacted.isEmpty()) {
boolean isPlural = requiredRedacted.size() != 1;
result.addStatement("throw new $T($S)", UnsupportedOperationException.class, (isPlural ? "Fields" : "Field") + " '" + Joiner.on("', '").join(requiredRedacted) + "' " + (isPlural ? "are" : "is") + " required and cannot be redacted.");
return result.build();
}
result.addStatement("$1T builder = value.newBuilder()", builderJavaType);
for (Field field : type.getFieldsAndOneOfFields()) {
String fieldName = nameAllocator.get(field);
if (field.isRedacted()) {
if (field.isRepeated()) {
result.addStatement("builder.$N = $T.emptyList()", fieldName, Collections.class);
} else if (field.getType().isMap()) {
result.addStatement("builder.$N = $T.emptyMap()", fieldName, Collections.class);
} else {
result.addStatement("builder.$N = null", fieldName);
}
} else if (!field.getType().isScalar() && !isEnum(field.getType())) {
if (field.isRepeated()) {
CodeBlock adapter = singleAdapterFor(field, nameAllocator);
result.addStatement("$T.redactElements(builder.$N, $L)", Internal.class, fieldName, adapter);
} else if (field.getType().isMap()) {
// We only need to ask the values to redact themselves if the type is a message.
if (!field.getType().getValueType().isScalar() && !isEnum(field.getType().getValueType())) {
CodeBlock adapter = singleAdapterFor(field.getType().getValueType());
result.addStatement("$T.redactElements(builder.$N, $L)", Internal.class, fieldName, adapter);
}
} else {
CodeBlock adapter = adapterFor(field, nameAllocator);
if (!field.isRequired()) {
result.addCode("if (builder.$N != null) ", fieldName);
}
result.addStatement("builder.$1N = $2L.redact(builder.$1N)", fieldName, adapter);
}
}
}
result.addStatement("builder.clearUnknownFields()");
result.addStatement("return builder.build()");
return result.build();
}
use of com.squareup.wire.schema.Field in project wire by square.
the class JavaGenerator method collidingFieldNames.
/**
* Returns the set of names that are not unique within {@code fields}.
*/
private Set<String> collidingFieldNames(List<Field> fields) {
Set<String> fieldNames = new LinkedHashSet<>();
Set<String> collidingNames = new LinkedHashSet<>();
for (Field field : fields) {
if (!fieldNames.add(field.getName())) {
collidingNames.add(field.getName());
}
}
return collidingNames;
}
use of com.squareup.wire.schema.Field in project wire by square.
the class JavaGenerator method setter.
private MethodSpec setter(NameAllocator nameAllocator, TypeName builderType, OneOf oneOf, Field field) {
TypeName javaType = fieldType(field);
String fieldName = nameAllocator.get(field);
MethodSpec.Builder result = MethodSpec.methodBuilder(fieldName).addModifiers(PUBLIC).addParameter(javaType, fieldName).returns(builderType);
if (!field.getDocumentation().isEmpty()) {
result.addJavadoc("$L\n", sanitizeJavadoc(field.getDocumentation()));
}
if (field.isDeprecated()) {
result.addAnnotation(Deprecated.class);
}
if (field.isRepeated() || field.getType().isMap()) {
result.addStatement("$T.checkElementsNotNull($L)", Internal.class, fieldName);
}
result.addStatement("this.$L = $L", fieldName, fieldName);
if (oneOf != null) {
for (Field other : oneOf.getFields()) {
if (field != other) {
result.addStatement("this.$L = null", nameAllocator.get(other));
}
}
}
result.addStatement("return this");
return result.build();
}
use of com.squareup.wire.schema.Field in project wire by square.
the class JavaGenerator method messageAdapterDecode.
private MethodSpec messageAdapterDecode(NameAllocator nameAllocator, MessageType type, TypeName javaType, boolean useBuilder, ClassName builderJavaType) {
MethodSpec.Builder result = MethodSpec.methodBuilder("decode").addAnnotation(Override.class).addModifiers(PUBLIC).returns(javaType).addParameter(ProtoReader.class, "reader").addException(IOException.class);
List<Field> fields = TAG_ORDERING.sortedCopy(type.getFieldsAndOneOfFields());
if (useBuilder) {
result.addStatement("$1T builder = new $1T()", builderJavaType);
} else {
for (Field field : fields) {
result.addStatement("$T $N = $L", fieldType(field), nameAllocator.get(field), initialValue(field));
}
}
result.addStatement("long token = reader.beginMessage()");
result.beginControlFlow("for (int tag; (tag = reader.nextTag()) != -1;)");
result.beginControlFlow("switch (tag)");
for (Field field : fields) {
int fieldTag = field.getTag();
if (isEnum(field.getType()) && !field.getType().equals(ProtoType.STRUCT_NULL)) {
result.beginControlFlow("case $L:", fieldTag);
result.beginControlFlow("try");
result.addCode(decodeAndAssign(field, nameAllocator, useBuilder));
result.addCode(";\n");
if (useBuilder) {
result.nextControlFlow("catch ($T e)", EnumConstantNotFoundException.class);
result.addStatement("builder.addUnknownField(tag, $T.VARINT, (long) e.value)", FieldEncoding.class);
// try/catch
result.endControlFlow();
} else {
result.nextControlFlow("catch ($T ignored)", EnumConstantNotFoundException.class);
// try/catch
result.endControlFlow();
}
result.addStatement("break");
// case
result.endControlFlow();
} else {
result.addCode("case $L: $L; break;\n", fieldTag, decodeAndAssign(field, nameAllocator, useBuilder));
}
}
result.beginControlFlow("default:");
if (useBuilder) {
result.addStatement("reader.readUnknownField(tag)");
} else {
result.addStatement("reader.skip()");
}
// default
result.endControlFlow();
// switch
result.endControlFlow();
// for
result.endControlFlow();
if (useBuilder) {
result.addStatement("builder.addUnknownFields(reader.endMessageAndGetUnknownFields(token))");
} else {
result.addStatement("reader.endMessageAndGetUnknownFields(token)");
}
if (useBuilder) {
result.addStatement("return builder.build()");
} else {
result.addCode("return fromProto(");
boolean first = true;
for (Field field : type.getFieldsAndOneOfFields()) {
if (!first)
result.addCode(", ");
result.addCode("$N", nameAllocator.get(field));
first = false;
}
result.addCode(");\n");
}
return result.build();
}
Aggregations