public EnumMeta(Field protoField, JavaType javaType) { io.protostuff.compiler.model.Enum enumType = (io.protostuff.compiler.model.Enum) protoField.getType(); for (EnumConstant enumConstant : enumType.getConstants()) { enumNameToValueMap.put(enumConstant.getName(), enumConstant.getValue()); enumValues.put(enumConstant.getValue(), null); } if (!javaType.isEnumType()) { return; } try { Method method = javaType.getRawClass().getMethod("values"); method.setAccessible(true); Object[] values = (Object[]) method.invoke(null); for (Object value : values) { Enum<?> enumValue = (Enum<?>) value; enumValues.put(enumNameToValueMap.get(enumValue.name()), enumValue); } } catch (Throwable e) { throw new IllegalStateException( "Failed to collect enum values, class=" + javaType.getRawClass().getName(), e); } }
protected void enumToString(Enum enumValue, StringBuilder sb) { appendLine(sb, "enum %s {", enumValue.getName()); for (EnumConstant enumConstant : enumValue.getConstants()) { appendLine(sb, " %s = %s;", enumConstant.getName(), enumConstant.getValue()); } sb.append("}\n\n"); }
public EnumSerializerSchema(Field field) { super(field); io.protostuff.compiler.model.Enum enumType = (io.protostuff.compiler.model.Enum) field.getType(); for (EnumConstant enumConstant : enumType.getConstants()) { enumNameToValueMap.put(enumConstant.getName(), enumConstant.getValue()); enumValueSet.add(enumConstant.getValue()); } }
private void checkReservedEnumTags(Enum anEnum, List<EnumConstant> constants) { List<Range> ranges = anEnum.getReservedFieldRanges(); for (EnumConstant constant : constants) { int tag = constant.getValue(); for (Range range : ranges) { if (range.contains(tag)) { throw new ParserException(constant, "Reserved enum tag: %d", tag); } } } }
private void checkDuplicateEnumConstantValues(Enum anEnum, List<EnumConstant> constants) { DynamicMessage.Value allowAlias = anEnum.getOptions().get(ALLOW_ALIAS); if (allowAlias != null && allowAlias.isBooleanType() && allowAlias.getBoolean()) { // skip this check if aliases are allowed return; } Map<Integer, EnumConstant> constantByValue = new HashMap<>(); for (EnumConstant constant : constants) { Integer value = constant.getValue(); if (constantByValue.containsKey(value)) { throw new ParserException(constant, "Duplicate enum constant value: %d", value); } constantByValue.put(value, constant); } }
private void checkReservedEnumTags(Enum anEnum, List<EnumConstant> constants) { List<Range> ranges = anEnum.getReservedFieldRanges(); for (EnumConstant constant : constants) { int tag = constant.getValue(); for (Range range : ranges) { if (range.contains(tag)) { throw new ParserException(constant, "Reserved enum tag: %d", tag); } } } }
private void checkDuplicateEnumConstantValues(Enum anEnum, List<EnumConstant> constants) { DynamicMessage.Value allowAlias = anEnum.getOptions().get(ALLOW_ALIAS); if (allowAlias != null && allowAlias.isBooleanType() && allowAlias.getBoolean()) { // skip this check if aliases are allowed return; } Map<Integer, EnumConstant> constantByValue = new HashMap<>(); for (EnumConstant constant : constants) { Integer value = constant.getValue(); if (constantByValue.containsKey(value)) { throw new ParserException(constant, "Duplicate enum constant value: %d", value); } constantByValue.put(value, constant); } }
protected void enumToString(Enum enumValue, StringBuilder sb) { appendLine(sb, "enum %s {", enumValue.getName()); for (EnumConstant enumConstant : enumValue.getConstants()) { appendLine(sb, " %s = %s;", enumConstant.getName(), enumConstant.getValue()); } sb.append("}\n\n"); }
private void process(Module module, Enum anEnum) { ImmutableEnumDescriptor descriptor = ImmutableEnumDescriptor.builder() .type(NodeType.ENUM) .name(anEnum.getName()) .canonicalName(anEnum.getCanonicalName()) .description(markdownProcessor.toHtml(anEnum.getComments())) .options(anEnum.getOptions().toMap()) .usages(module.usageIndex().getUsages(anEnum).stream() .map(type -> ImmutableUsageItem.builder() .ref(type.getCanonicalName()) .type(UsageType.from(type)) .build()) .collect(Collectors.toList())) .addAllConstants(anEnum.getConstants().stream() .map(enumConstant -> ImmutableEnumConstant.builder() .name(enumConstant.getName()) .value(enumConstant.getValue()) .description(markdownProcessor.toHtml(enumConstant.getComments())) .options(enumConstant.getOptions().toMap()) .build()) .collect(Collectors.toList())) .build(); String output = "data/type/" + anEnum.getCanonicalName() + ".json"; write(module, output, descriptor); } }
private void process(Module module, Enum anEnum) { ImmutableEnumDescriptor descriptor = ImmutableEnumDescriptor.builder() .type(NodeType.ENUM) .name(anEnum.getName()) .canonicalName(anEnum.getCanonicalName()) .description(markdownProcessor.toHtml(anEnum.getComments())) .options(anEnum.getOptions().toMap()) .usages(module.usageIndex().getUsages(anEnum).stream() .map(type -> ImmutableUsageItem.builder() .ref(type.getCanonicalName()) .type(UsageType.from(type)) .build()) .collect(Collectors.toList())) .addAllConstants(anEnum.getConstants().stream() .map(enumConstant -> ImmutableEnumConstant.builder() .name(enumConstant.getName()) .value(enumConstant.getValue()) .description(markdownProcessor.toHtml(enumConstant.getComments())) .options(enumConstant.getOptions().toMap()) .build()) .collect(Collectors.toList())) .build(); String output = "data/type/" + anEnum.getCanonicalName() + ".json"; write(module, output, descriptor); } }