use of com.squareup.javapoet.NameAllocator in project wire by square.
the class JavaGenerator method generateEnum.
private TypeSpec generateEnum(EnumType type) {
NameAllocator nameAllocator = nameAllocators.getUnchecked(type);
String value = nameAllocator.get("value");
ClassName javaType = (ClassName) typeName(type.getType());
TypeSpec.Builder builder = TypeSpec.enumBuilder(javaType.simpleName()).addModifiers(PUBLIC).addSuperinterface(WireEnum.class);
if (!type.getDocumentation().isEmpty()) {
builder.addJavadoc("$L\n", sanitizeJavadoc(type.getDocumentation()));
}
for (AnnotationSpec annotation : optionAnnotations(type.getOptions())) {
builder.addAnnotation(annotation);
}
if (type.isDeprecated()) {
builder.addAnnotation(Deprecated.class);
}
// Output Private tag field
builder.addField(TypeName.INT, value, PRIVATE, FINAL);
// Enum constructor takes the constant tag.
builder.addMethod(MethodSpec.constructorBuilder().addStatement("this.$1N = $1N", value).addParameter(TypeName.INT, value).build());
MethodSpec.Builder fromValueBuilder = MethodSpec.methodBuilder("fromValue").addJavadoc("Return the constant for {@code $N} or null.\n", value).addModifiers(PUBLIC, STATIC).returns(javaType).addParameter(int.class, value).beginControlFlow("switch ($N)", value);
Set<Integer> seenTags = new LinkedHashSet<>();
for (EnumConstant constant : type.getConstants()) {
TypeSpec.Builder constantBuilder = TypeSpec.anonymousClassBuilder("$L", constant.getTag());
if (!constant.getDocumentation().isEmpty()) {
constantBuilder.addJavadoc("$L\n", sanitizeJavadoc(constant.getDocumentation()));
}
for (AnnotationSpec annotation : optionAnnotations(constant.getOptions())) {
constantBuilder.addAnnotation(annotation);
}
AnnotationSpec wireEnumConstantAnnotation = wireEnumConstantAnnotation(nameAllocator, constant);
if (wireEnumConstantAnnotation != null) {
constantBuilder.addAnnotation(wireEnumConstantAnnotation);
}
if (constant.isDeprecated()) {
constantBuilder.addAnnotation(Deprecated.class);
}
builder.addEnumConstant(nameAllocator.get(constant), constantBuilder.build());
// Ensure constant case tags are unique, which might not be the case if allow_alias is true.
if (seenTags.add(constant.getTag())) {
fromValueBuilder.addStatement("case $L: return $L", constant.getTag(), nameAllocator.get(constant));
}
}
builder.addMethod(fromValueBuilder.addStatement("default: return null").endControlFlow().build());
// ADAPTER
FieldSpec.Builder adapterBuilder = FieldSpec.builder(adapterOf(javaType), "ADAPTER").addModifiers(PUBLIC, STATIC, FINAL);
ClassName adapterJavaType = javaType.nestedClass("ProtoAdapter_" + javaType.simpleName());
if (!emitCompact) {
adapterBuilder.initializer("new $T()", adapterJavaType);
} else {
adapterBuilder.initializer("$T.newEnumAdapter($T.class)", ProtoAdapter.class, javaType);
}
builder.addField(adapterBuilder.build());
// Public Getter
builder.addMethod(MethodSpec.methodBuilder("getValue").addAnnotation(Override.class).addModifiers(PUBLIC).returns(TypeName.INT).addStatement("return $N", value).build());
if (!emitCompact) {
// Adds the ProtoAdapter implementation at the bottom.
builder.addType(enumAdapter(javaType, adapterJavaType, type));
}
return builder.build();
}
use of com.squareup.javapoet.NameAllocator 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.javapoet.NameAllocator in project wire by square.
the class JavaGenerator method generateAbstractAdapter.
/** Returns an abstract adapter for {@code type}. */
public TypeSpec generateAbstractAdapter(MessageType type) {
NameAllocator nameAllocator = nameAllocators.getUnchecked(type);
ClassName adapterTypeName = abstractAdapterName(type.type());
ClassName typeName = (ClassName) typeName(type.type());
TypeSpec.Builder adapter = messageAdapter(nameAllocator, type, typeName, adapterTypeName, null).toBuilder();
if (adapterTypeName.enclosingClassName() != null) {
adapter.addModifiers(STATIC);
}
for (Type nestedType : type.nestedTypes()) {
if (profile.getAdapter(nestedType.type()) == null) {
throw new IllegalArgumentException("Missing custom proto adapter for " + nestedType.type().enclosingTypeOrPackage() + "." + nestedType.type().simpleName() + " when enclosing proto has custom proto adapter.");
}
if (nestedType instanceof MessageType) {
adapter.addType(generateAbstractAdapter((MessageType) nestedType));
}
}
return adapter.build();
}
use of com.squareup.javapoet.NameAllocator 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.javapoet.NameAllocator 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();
}
Aggregations