use of com.squareup.javapoet.MethodSpec 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.javapoet.MethodSpec 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.javapoet.MethodSpec 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();
}
use of com.squareup.javapoet.MethodSpec in project wire by square.
the class JavaGenerator method newBuilder.
// Example:
//
// @Override
// public Message.Builder newBuilder() {
// Builder builder = new Builder();
// builder.optional_int32 = optional_int32;
// ...
// builder.addUnknownFields(unknownFields());
// return builder;
// }
private MethodSpec newBuilder(NameAllocator nameAllocator, MessageType message) {
NameAllocator localNameAllocator = nameAllocator.clone();
String builderName = localNameAllocator.newName("builder");
ClassName javaType = (ClassName) typeName(message.getType());
ClassName builderJavaType = javaType.nestedClass("Builder");
MethodSpec.Builder result = MethodSpec.methodBuilder("newBuilder").addAnnotation(Override.class).addModifiers(PUBLIC).returns(builderJavaType).addStatement("$1T $2L = new $1T()", builderJavaType, builderName);
List<Field> fields = message.getFieldsAndOneOfFields();
for (Field field : fields) {
String fieldName = localNameAllocator.get(field);
if (field.isRepeated() || field.getType().isMap()) {
result.addStatement("$1L.$2L = $3T.copyOf($2L)", builderName, fieldName, Internal.class);
} else {
result.addStatement("$1L.$2L = $2L", builderName, fieldName);
}
}
result.addStatement("$L.addUnknownFields(unknownFields())", builderName);
result.addStatement("return $L", builderName);
return result.build();
}
use of com.squareup.javapoet.MethodSpec in project wire by square.
the class JavaGenerator method messageHashCode.
// Example:
//
// @Override
// public int hashCode() {
// int result = hashCode;
// if (result == 0) {
// result = unknownFields().hashCode();
// result = result * 37 + (f != null ? f.hashCode() : 0);
// hashCode = result;
// }
// return result;
// }
//
// For repeated fields, the final "0" in the example above changes to a "1"
// in order to be the same as the system hash code for an empty list.
//
private MethodSpec messageHashCode(NameAllocator nameAllocator, MessageType type) {
NameAllocator localNameAllocator = nameAllocator.clone();
String resultName = localNameAllocator.newName("result");
MethodSpec.Builder result = MethodSpec.methodBuilder("hashCode").addAnnotation(Override.class).addModifiers(PUBLIC).returns(int.class);
List<Field> fields = type.getFieldsAndOneOfFields();
if (fields.isEmpty()) {
result.addStatement("return unknownFields().hashCode()");
return result.build();
}
result.addStatement("int $N = super.hashCode", resultName);
result.beginControlFlow("if ($N == 0)", resultName);
result.addStatement("$N = unknownFields().hashCode()", resultName);
for (Field field : fields) {
String fieldName = localNameAllocator.get(field);
TypeName typeName = fieldType(field);
result.addCode("$1N = $1N * 37 + ", resultName);
if (typeName == TypeName.BOOLEAN) {
result.addStatement("$T.hashCode($N)", Boolean.class, fieldName);
} else if (typeName == TypeName.INT) {
result.addStatement("$T.hashCode($N)", Integer.class, fieldName);
} else if (typeName == TypeName.LONG) {
result.addStatement("$T.hashCode($N)", Long.class, fieldName);
} else if (typeName == TypeName.FLOAT) {
result.addStatement("$T.hashCode($N)", Float.class, fieldName);
} else if (typeName == TypeName.DOUBLE) {
result.addStatement("$T.hashCode($N)", Double.class, fieldName);
} else if (field.isRequired() || field.isRepeated() || field.getType().isMap()) {
result.addStatement("$L.hashCode()", fieldName);
} else {
result.addStatement("($1L != null ? $1L.hashCode() : 0)", fieldName);
}
}
result.addStatement("super.hashCode = $N", resultName);
result.endControlFlow();
result.addStatement("return $N", resultName);
return result.build();
}
Aggregations