/** Returns the constant named {@code name}, or null if this enum has no such constant. */ public EnumConstant constant(String name) { for (EnumConstant constant : constants()) { if (constant.name().equals(name)) { return constant; } } return null; }
/** Returns the constant tagged {@code tag}, or null if this enum has no such constant. */ public EnumConstant constant(int tag) { for (EnumConstant constant : constants()) { if (constant.tag() == tag) { return constant; } } return null; }
EnumConstant enumDefault(ProtoType type) { EnumType wireEnum = (EnumType) schema.getType(type); return wireEnum.constants().get(0); }
void validateEnumConstantNameUniqueness(Iterable<Type> nestedTypes) { Multimap<String, EnumType> nameToType = LinkedHashMultimap.create(); for (Type type : nestedTypes) { if (type instanceof EnumType) { EnumType enumType = (EnumType) type; for (EnumConstant enumConstant : enumType.constants()) { nameToType.put(enumConstant.name(), enumType); } } } for (Map.Entry<String, Collection<EnumType>> entry : nameToType.asMap().entrySet()) { if (entry.getValue().size() > 1) { StringBuilder error = new StringBuilder(); String constant = entry.getKey(); int index = 1; error.append(String.format("multiple enums share constant %s:", constant)); for (EnumType enumType : entry.getValue()) { error.append(String.format("\n %s. %s.%s (%s)", index++, enumType.type(), constant, enumType.constant(constant).location())); } addError("%s", error); } } }
constructorBuilder.addModifiers(PUBLIC); constructorBuilder.addStatement("super($T.VARINT, $T.class)", FieldEncoding.class, javaType); for (EnumConstant constant : type.constants()) { String name = nameAllocator.get(constant); FieldSpec.Builder fieldBuilder = FieldSpec.builder(javaType, name) .returns(int.class) .addParameter(javaType, value); for (EnumConstant constant : type.constants()) { String name = nameAllocator.get(constant); toValueBuilder.addStatement("if ($N.equals($N)) return $L", value, name, constant.tag()); .addParameter(int.class, value); fromValueBuilder.beginControlFlow("switch ($N)", value); for (EnumConstant constant : type.constants()) { String name = nameAllocator.get(constant); fromValueBuilder.addStatement("case $L: return $N", constant.tag(), name);
for (EnumConstant constant : type.constants()) { for (ProtoMember protoMember : constant.options().map().keySet()) { Field optionField = schema.getField(protoMember); for (EnumConstant constant : type.constants()) { Object[] enumArgs = new Object[allOptionMembers.size() + 1]; enumArgs[0] = constant.tag();
private void markRoots(Type type) { ProtoType protoType = type.type(); if (identifierSet.includes(protoType)) { marks.root(protoType); queue.add(protoType); } else { if (type instanceof MessageType) { for (Field field : ((MessageType) type).fieldsAndOneOfFields()) { markRoots(ProtoMember.get(protoType, field.name())); } } else if (type instanceof EnumType) { for (EnumConstant enumConstant : ((EnumType) type).constants()) { markRoots(ProtoMember.get(protoType, enumConstant.name())); } } else { throw new AssertionError(); } } for (Type nested : type.nestedTypes()) { markRoots(nested); } }
private void markEnum(EnumType wireEnum) { markOptions(wireEnum.options()); if (marks.containsAllMembers(wireEnum.type())) { for (EnumConstant constant : wireEnum.constants()) { if (marks.contains(ProtoMember.get(wireEnum.type(), constant.name()))) { markOptions(constant.options()); } } } }
@Override public NameAllocator load(Type type) throws Exception { NameAllocator nameAllocator = new NameAllocator(); if (type instanceof MessageType) { nameAllocator.newName("serialVersionUID", "serialVersionUID"); nameAllocator.newName("ADAPTER", "ADAPTER"); nameAllocator.newName("MESSAGE_OPTIONS", "MESSAGE_OPTIONS"); if (emitAndroid) { nameAllocator.newName("CREATOR", "CREATOR"); } ImmutableList<Field> fieldsAndOneOfFields = ((MessageType) type).fieldsAndOneOfFields(); Set<String> collidingNames = collidingFieldNames(fieldsAndOneOfFields); for (Field field : fieldsAndOneOfFields) { String suggestion = collidingNames.contains(field.name()) ? field.qualifiedName() : field.name(); nameAllocator.newName(suggestion, field); } } else if (type instanceof EnumType) { nameAllocator.newName("value", "value"); nameAllocator.newName("i", "i"); nameAllocator.newName("reader", "reader"); nameAllocator.newName("writer", "writer"); for (EnumConstant constant : ((EnumType) type).constants()) { nameAllocator.newName(constant.name(), constant); } } return nameAllocator; } });
/** Returns the constant named {@code name}, or null if this enum has no such constant. */ public EnumConstant constant(String name) { for (EnumConstant constant : constants()) { if (constant.name().equals(name)) { return constant; } } return null; }
/** Returns the constant named {@code name}, or null if this enum has no such constant. */ public EnumConstant constant(String name) { for (EnumConstant constant : constants()) { if (constant.name().equals(name)) { return constant; } } return null; }
/** Returns the constant tagged {@code tag}, or null if this enum has no such constant. */ public EnumConstant constant(int tag) { for (EnumConstant constant : constants()) { if (constant.tag() == tag) { return constant; } } return null; }
/** Returns the constant tagged {@code tag}, or null if this enum has no such constant. */ public EnumConstant constant(int tag) { for (EnumConstant constant : constants()) { if (constant.tag() == tag) { return constant; } } return null; }
EnumConstant enumDefault(ProtoType type) { EnumType wireEnum = (EnumType) schema.getType(type); return wireEnum.constants().get(0); }
private String getDescriptionFromEnumType(EnumType type) { String format = "* %d - %s %s\n"; StringBuffer sb = new StringBuffer(); sb.append(type.documentation()); sb.append("\n"); for (EnumConstant enumConstant : type.constants()) { sb.append(String.format(format, enumConstant.tag(), enumConstant.name(),enumConstant.documentation())); } return sb.toString(); }
void validateEnumConstantNameUniqueness(Iterable<Type> nestedTypes) { Multimap<String, EnumType> nameToType = LinkedHashMultimap.create(); for (Type type : nestedTypes) { if (type instanceof EnumType) { EnumType enumType = (EnumType) type; for (EnumConstant enumConstant : enumType.constants()) { nameToType.put(enumConstant.name(), enumType); } } } for (Map.Entry<String, Collection<EnumType>> entry : nameToType.asMap().entrySet()) { if (entry.getValue().size() > 1) { StringBuilder error = new StringBuilder(); String constant = entry.getKey(); int index = 1; error.append(String.format("multiple enums share constant %s:", constant)); for (EnumType enumType : entry.getValue()) { error.append(String.format("\n %s. %s.%s (%s)", index++, enumType.type(), constant, enumType.constant(constant).location())); } addError("%s", error); } } }
private Schema buildSchema(ProtoType protoType) { Schema schema = new Schema(); Type type = this.schema.getType(protoType); if (type instanceof EnumType) { schema.type("integer"); // TODO: 2018/5/22 追加特别的描述 schema.description(getDescriptionFromEnumType((EnumType) type)); schema.setEnum(((EnumType) type).constants().stream().map(EnumConstant::tag).collect(Collectors.toList())); } else if (type instanceof MessageType) { schema.properties(buildProperties((MessageType) type)); } return schema; }
private void markRoots(Type type) { ProtoType protoType = type.type(); if (identifierSet.includes(protoType)) { marks.root(protoType); queue.add(protoType); } else { if (type instanceof MessageType) { for (Field field : ((MessageType) type).fieldsAndOneOfFields()) { markRoots(ProtoMember.get(protoType, field.name())); } } else if (type instanceof EnumType) { for (EnumConstant enumConstant : ((EnumType) type).constants()) { markRoots(ProtoMember.get(protoType, enumConstant.name())); } } else { throw new AssertionError(); } } for (Type nested : type.nestedTypes()) { markRoots(nested); } }
private void markEnum(EnumType wireEnum) { markOptions(wireEnum.options()); if (marks.containsAllMembers(wireEnum.type())) { for (EnumConstant constant : wireEnum.constants()) { if (marks.contains(ProtoMember.get(wireEnum.type(), constant.name()))) { markOptions(constant.options()); } } } }
private void markEnum(EnumType wireEnum) { markOptions(wireEnum.options()); if (marks.containsAllMembers(wireEnum.type())) { for (EnumConstant constant : wireEnum.constants()) { if (marks.contains(ProtoMember.get(wireEnum.type(), constant.name()))) { markOptions(constant.options()); } } } }