use of com.squareup.wire.schema.Field in project wire by square.
the class JavaGenerator method messageEquals.
// Example:
//
// @Override
// public boolean equals(Object other) {
// if (other == this) return true;
// if (!(other instanceof SimpleMessage)) return false;
// SimpleMessage o = (SimpleMessage) other;
// return equals(unknownFields(), o.unknownFields())
// && equals(optional_int32, o.optional_int32);
//
private MethodSpec messageEquals(NameAllocator nameAllocator, MessageType type) {
NameAllocator localNameAllocator = nameAllocator.clone();
String otherName = localNameAllocator.newName("other");
String oName = localNameAllocator.newName("o");
TypeName javaType = typeName(type.getType());
MethodSpec.Builder result = MethodSpec.methodBuilder("equals").addAnnotation(Override.class).addModifiers(PUBLIC).returns(boolean.class).addParameter(Object.class, otherName);
result.addStatement("if ($N == this) return true", otherName);
result.addStatement("if (!($N instanceof $T)) return false", otherName, javaType);
result.addStatement("$T $N = ($T) $N", javaType, oName, javaType, otherName);
result.addCode("$[return unknownFields().equals($N.unknownFields())", oName);
List<Field> fields = type.getFieldsAndOneOfFields();
for (Field field : fields) {
String fieldName = localNameAllocator.get(field);
if (field.isRequired() || field.isRepeated() || field.getType().isMap()) {
result.addCode("\n&& $1L.equals($2N.$1L)", fieldName, oName);
} else {
result.addCode("\n&& $1T.equals($2L, $3N.$2L)", Internal.class, fieldName, oName);
}
}
result.addCode(";\n$]");
return result.build();
}
use of com.squareup.wire.schema.Field in project wire by square.
the class JavaGenerator method messageToString.
private MethodSpec messageToString(NameAllocator nameAllocator, MessageType type) {
NameAllocator localNameAllocator = nameAllocator.clone();
MethodSpec.Builder result = MethodSpec.methodBuilder("toString").addAnnotation(Override.class).addModifiers(PUBLIC).returns(String.class);
String builderName = localNameAllocator.newName("builder");
result.addStatement("$1T $2N = new $1T()", StringBuilder.class, builderName);
for (Field field : type.getFieldsAndOneOfFields()) {
String fieldName = nameAllocator.get(field);
TypeName fieldType = fieldType(field);
if (field.isRepeated() || field.getType().isMap()) {
result.addCode("if (!$N.isEmpty()) ", fieldName);
} else if (!field.isRequired() && !fieldType.isPrimitive()) {
result.addCode("if ($N != null) ", fieldName);
}
if (field.isRedacted()) {
result.addStatement("$N.append(\", $N=$L\")", builderName, field.getName(), DOUBLE_FULL_BLOCK);
} else if (field.getType().equals(ProtoType.STRING)) {
result.addStatement("$N.append(\", $N=\").append($T.sanitize($L))", builderName, field.getName(), Internal.class, fieldName);
} else {
result.addStatement("$N.append(\", $N=\").append($L)", builderName, field.getName(), fieldName);
}
}
result.addStatement("return builder.replace(0, 2, \"$L{\").append('}').toString()", type.getType().getSimpleName());
return result.build();
}
use of com.squareup.wire.schema.Field in project wire by square.
the class JavaGenerator method messageFieldsConstructor.
// Example:
//
// public SimpleMessage(int optional_int32, long optional_int64) {
// this(builder.optional_int32, builder.optional_int64, null);
// }
//
private MethodSpec messageFieldsConstructor(NameAllocator nameAllocator, MessageType type) {
MethodSpec.Builder result = MethodSpec.constructorBuilder();
result.addModifiers(PUBLIC);
result.addCode("this(");
for (Field field : type.getFieldsAndOneOfFields()) {
TypeName javaType = fieldType(field);
String fieldName = nameAllocator.get(field);
ParameterSpec.Builder param = ParameterSpec.builder(javaType, fieldName);
if (emitAndroidAnnotations && field.getEncodeMode() == Field.EncodeMode.NULL_IF_ABSENT) {
param.addAnnotation(NULLABLE);
}
result.addParameter(param.build());
result.addCode("$L, ", fieldName);
}
result.addCode("$T.EMPTY);\n", BYTE_STRING);
return result.build();
}
use of com.squareup.wire.schema.Field 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.wire.schema.Field 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();
}
Aggregations