use of com.squareup.javapoet.CodeBlock in project wire by square.
the class JavaGenerator method identityValue.
private CodeBlock identityValue(Field field) {
switch(field.getEncodeMode()) {
case MAP:
return CodeBlock.of("$T.emptyMap()", Collections.class);
case REPEATED:
case PACKED:
return CodeBlock.of("$T.emptyList()", Collections.class);
case NULL_IF_ABSENT:
return CodeBlock.of("null");
case OMIT_IDENTITY:
ProtoType protoType = field.getType();
Type type = schema.getType(protoType);
if (protoType.equals(ProtoType.STRUCT_NULL)) {
return CodeBlock.of("null");
} else if (field.isOneOf()) {
return CodeBlock.of("null");
} else if (protoType.isScalar()) {
CodeBlock value = PROTOTYPE_TO_IDENTITY_VALUES.get(protoType);
if (value == null) {
throw new IllegalArgumentException("Unexpected scalar proto type: " + protoType);
}
return value;
} else if (type instanceof MessageType) {
return CodeBlock.of("null");
} else if (type instanceof EnumType) {
return identity((EnumType) type);
}
case REQUIRED:
default:
throw new IllegalArgumentException("No identity value for field: " + field + "(" + field.getEncodeMode() + ")");
}
}
use of com.squareup.javapoet.CodeBlock in project wire by square.
the class JavaGenerator method messageAdapterEncodedSize.
private MethodSpec messageAdapterEncodedSize(NameAllocator nameAllocator, MessageType type, TypeName javaType, boolean useBuilder) {
MethodSpec.Builder result = MethodSpec.methodBuilder("encodedSize").addAnnotation(Override.class).addModifiers(PUBLIC).returns(int.class).addParameter(javaType, "value");
String resultName = nameAllocator.clone().newName("result");
result.addStatement("int $L = 0", resultName);
for (Field field : type.getFieldsAndOneOfFields()) {
int fieldTag = field.getTag();
String fieldName = nameAllocator.get(field);
CodeBlock adapter = adapterFor(field, nameAllocator);
boolean omitIdentity = field.getEncodeMode().equals(Field.EncodeMode.OMIT_IDENTITY);
if (omitIdentity) {
result.beginControlFlow("if (!$T.equals(value.$L, $L))", ClassName.get(Objects.class), fieldName, identityValue(field));
}
result.addCode("$L += ", resultName).addCode("$L.encodedSizeWithTag($L, ", adapter, fieldTag).addCode((useBuilder ? "value.$L" : "$L(value)"), fieldName).addCode(");\n");
if (omitIdentity) {
result.endControlFlow();
}
}
if (useBuilder) {
result.addStatement("$L += value.unknownFields().size()", resultName);
}
result.addStatement("return $L", resultName);
return result.build();
}
use of com.squareup.javapoet.CodeBlock in project wire by square.
the class JavaGenerator method decodeAndAssign.
private CodeBlock decodeAndAssign(Field field, NameAllocator nameAllocator, boolean useBuilder) {
String fieldName = nameAllocator.get(field);
CodeBlock decode = CodeBlock.of("$L.decode(reader)", singleAdapterFor(field, nameAllocator));
if (field.isRepeated()) {
return useBuilder ? field.getType().equals(ProtoType.STRUCT_NULL) ? CodeBlock.of("builder.$L.add(($T) $L)", fieldName, Void.class, decode) : CodeBlock.of("builder.$L.add($L)", fieldName, decode) : CodeBlock.of("$L.add($L)", fieldName, decode);
} else if (field.getType().isMap()) {
return useBuilder ? CodeBlock.of("builder.$L.putAll($L)", fieldName, decode) : CodeBlock.of("$L.putAll($L)", fieldName, decode);
} else {
return useBuilder ? field.getType().equals(ProtoType.STRUCT_NULL) ? CodeBlock.of("builder.$L(($T) $L)", fieldName, Void.class, decode) : CodeBlock.of("builder.$L($L)", fieldName, decode) : CodeBlock.of("$L = $L", fieldName, decode);
}
}
use of com.squareup.javapoet.CodeBlock in project wire by square.
the class JavaGenerator method builderNoArgsConstructor.
// Example:
//
// public Builder() {
// names = newMutableList();
// }
//
private MethodSpec builderNoArgsConstructor(NameAllocator nameAllocator, MessageType type) {
MethodSpec.Builder result = MethodSpec.constructorBuilder().addModifiers(PUBLIC);
for (Field field : type.getFieldsAndOneOfFields()) {
String fieldName = nameAllocator.get(field);
CodeBlock initialValue = initialValue(field);
if (initialValue != null) {
result.addStatement("$L = $L", fieldName, initialValue);
}
}
return result.build();
}
use of com.squareup.javapoet.CodeBlock in project wire by square.
the class JavaGenerator method messageAdapterEncode.
private MethodSpec messageAdapterEncode(NameAllocator nameAllocator, MessageType type, TypeName javaType, boolean useBuilder, boolean reverse) {
MethodSpec.Builder result = MethodSpec.methodBuilder("encode").addAnnotation(Override.class).addModifiers(PUBLIC).addParameter(reverse ? ReverseProtoWriter.class : ProtoWriter.class, "writer").addParameter(javaType, "value").addException(IOException.class);
List<CodeBlock> encodeCalls = new ArrayList<>();
for (Field field : type.getFieldsAndOneOfFields()) {
int fieldTag = field.getTag();
CodeBlock adapter = adapterFor(field, nameAllocator);
String fieldName = nameAllocator.get(field);
CodeBlock.Builder encodeCall = CodeBlock.builder();
if (field.getEncodeMode().equals(Field.EncodeMode.OMIT_IDENTITY)) {
encodeCall.add("if (!$T.equals(value.$L, $L)) ", ClassName.get(Objects.class), fieldName, identityValue(field));
}
encodeCall.add("$L.encodeWithTag(writer, $L, ", adapter, fieldTag).add((useBuilder ? "value.$L" : "$L(value)"), fieldName).add(");\n");
encodeCalls.add(encodeCall.build());
}
if (useBuilder) {
encodeCalls.add(CodeBlock.builder().addStatement("writer.writeBytes(value.unknownFields())").build());
}
if (reverse) {
Collections.reverse(encodeCalls);
}
for (CodeBlock encodeCall : encodeCalls) {
result.addCode(encodeCall);
}
return result.build();
}
Aggregations