use of com.squareup.wire.schema.EnumConstant in project wire by square.
the class JavaGenerator method generateEnum.
/** @deprecated Use {@link #generateType(Type)} */
@Deprecated
public TypeSpec generateEnum(EnumType type) {
ClassName javaType = (ClassName) typeName(type.type());
TypeSpec.Builder builder = TypeSpec.enumBuilder(javaType.simpleName()).addModifiers(PUBLIC).addSuperinterface(WireEnum.class);
if (!type.documentation().isEmpty()) {
builder.addJavadoc("$L\n", sanitizeJavadoc(type.documentation()));
}
// Output Private tag field
builder.addField(TypeName.INT, "value", PRIVATE, FINAL);
MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder();
constructorBuilder.addStatement("this.value = value");
constructorBuilder.addParameter(TypeName.INT, "value");
// Enum constant options, each of which requires a constructor parameter and a field.
Set<ProtoMember> allOptionFieldsBuilder = new LinkedHashSet<>();
for (EnumConstant constant : type.constants()) {
for (ProtoMember protoMember : constant.options().map().keySet()) {
Field optionField = schema.getField(protoMember);
if (allOptionFieldsBuilder.add(protoMember)) {
TypeName optionJavaType = typeName(optionField.type());
builder.addField(optionJavaType, optionField.name(), PUBLIC, FINAL);
constructorBuilder.addParameter(optionJavaType, optionField.name());
constructorBuilder.addStatement("this.$L = $L", optionField.name(), optionField.name());
}
}
}
ImmutableList<ProtoMember> allOptionMembers = ImmutableList.copyOf(allOptionFieldsBuilder);
String enumArgsFormat = "$L" + Strings.repeat(", $L", allOptionMembers.size());
builder.addMethod(constructorBuilder.build());
MethodSpec.Builder fromValueBuilder = MethodSpec.methodBuilder("fromValue").addJavadoc("Return the constant for {@code value} or null.\n").addModifiers(PUBLIC, STATIC).returns(javaType).addParameter(int.class, "value").beginControlFlow("switch (value)");
Set<Integer> seenTags = new LinkedHashSet<>();
for (EnumConstant constant : type.constants()) {
Object[] enumArgs = new Object[allOptionMembers.size() + 1];
enumArgs[0] = constant.tag();
for (int i = 0; i < allOptionMembers.size(); i++) {
ProtoMember protoMember = allOptionMembers.get(i);
Field field = schema.getField(protoMember);
Object value = constant.options().map().get(protoMember);
enumArgs[i + 1] = value != null ? fieldInitializer(field.type(), value) : null;
}
TypeSpec.Builder constantBuilder = TypeSpec.anonymousClassBuilder(enumArgsFormat, enumArgs);
if (!constant.documentation().isEmpty()) {
constantBuilder.addJavadoc("$L\n", sanitizeJavadoc(constant.documentation()));
}
if ("true".equals(constant.options().get(ENUM_DEPRECATED))) {
constantBuilder.addAnnotation(Deprecated.class);
}
builder.addEnumConstant(constant.name(), constantBuilder.build());
// Ensure constant case tags are unique, which might not be the case if allow_alias is true.
if (seenTags.add(constant.tag())) {
fromValueBuilder.addStatement("case $L: return $L", constant.tag(), constant.name());
}
}
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());
// Enum type options.
FieldSpec options = optionsField(ENUM_OPTIONS, "ENUM_OPTIONS", type.options());
if (options != null) {
builder.addField(options);
}
// Public Getter
builder.addMethod(MethodSpec.methodBuilder("getValue").addAnnotation(Override.class).addModifiers(PUBLIC).returns(TypeName.INT).addStatement("return value").build());
if (!emitCompact) {
// Adds the ProtoAdapter implementation at the bottom.
builder.addType(enumAdapter(javaType, adapterJavaType));
}
return builder.build();
}
Aggregations