/** * TODO (guptasu): Only needed to create NullValue enum. Check if this can be removed. * Create the Protobuf.Enum instance from enumDescriptorProto. */ private static Enum createEnum(String enumName, EnumDescriptorProto enumDescriptorProto, String fileName) { com.google.protobuf.Enum.Builder coreEnumBuilder = com.google.protobuf.Enum.newBuilder().setName(enumName); coreEnumBuilder.setSyntax(Syntax.SYNTAX_PROTO3); for (EnumValueDescriptorProto value : enumDescriptorProto.getValueList()) { com.google.protobuf.EnumValue.Builder coreEnumValueBuilder = com.google.protobuf.EnumValue.newBuilder(); coreEnumValueBuilder.setName(value.getName()).setNumber(value.getNumber()); coreEnumBuilder.addEnumvalue(coreEnumValueBuilder.build()); } coreEnumBuilder.setSourceContext(SourceContext.newBuilder().setFileName(fileName)); return coreEnumBuilder.build(); }
private EnumDescriptor(final EnumDescriptorProto proto, final FileDescriptor file, final Descriptor parent, final int index) throws DescriptorValidationException { this.index = index; this.proto = proto; fullName = computeFullName(file, parent, proto.getName()); this.file = file; containingType = parent; if (proto.getValueCount() == 0) { // We cannot allow enums with no values because this would mean there // would be no valid default value for fields of this type. throw new DescriptorValidationException(this, "Enums must contain at least one value."); } values = new EnumValueDescriptor[proto.getValueCount()]; for (int i = 0; i < proto.getValueCount(); i++) { values[i] = new EnumValueDescriptor( proto.getValue(i), file, this, i); } file.pool.addSymbol(this); }
public void testPackedEnumField() throws Exception { FileDescriptorProto fileDescriptorProto = FileDescriptorProto.newBuilder() .setName("foo.proto") .addEnumType(EnumDescriptorProto.newBuilder() .setName("Enum") .addValue(EnumValueDescriptorProto.newBuilder() .setName("FOO") .setNumber(1) .build()) .build()) .addMessageType(DescriptorProto.newBuilder() .setName("Message") .addField(FieldDescriptorProto.newBuilder() .setName("foo") .setTypeName("Enum") .setNumber(1) .setLabel(FieldDescriptorProto.Label.LABEL_REPEATED) .setOptions(DescriptorProtos.FieldOptions.newBuilder() .setPacked(true) .build()) .build()) .build()) .build(); Descriptors.FileDescriptor.buildFrom( fileDescriptorProto, new FileDescriptor[0]); }
private static Optional<String> getFullName(FileDescriptorProto descriptor, List<Integer> path) { String fullNameSoFar = descriptor.getPackage(); switch (path.get(0)) { case FileDescriptorProto.MESSAGE_TYPE_FIELD_NUMBER: DescriptorProto message = descriptor.getMessageType(path.get(1)); return appendMessageToFullName(message, path, fullNameSoFar); case FileDescriptorProto.ENUM_TYPE_FIELD_NUMBER: EnumDescriptorProto enumDescriptor = descriptor.getEnumType(path.get(1)); return Optional.of(appendEnumToFullName(enumDescriptor, path, fullNameSoFar)); case FileDescriptorProto.SERVICE_FIELD_NUMBER: ServiceDescriptorProto serviceDescriptor = descriptor.getService(path.get(1)); fullNameSoFar = appendNameComponent(fullNameSoFar, serviceDescriptor.getName()); if (path.size() > 2) { fullNameSoFar = appendFieldComponent( fullNameSoFar, serviceDescriptor.getMethod(path.get(3)).getName()); } return Optional.of(fullNameSoFar); default: return Optional.empty(); } }
private static Optional<String> appendToFullName( DescriptorProto messageDescriptor, List<Integer> path, String fullNameSoFar) { switch (path.get(0)) { case DescriptorProto.NESTED_TYPE_FIELD_NUMBER: DescriptorProto nestedMessage = messageDescriptor.getNestedType(path.get(1)); return appendMessageToFullName(nestedMessage, path, fullNameSoFar); case DescriptorProto.ENUM_TYPE_FIELD_NUMBER: EnumDescriptorProto enumDescriptor = messageDescriptor.getEnumType(path.get(1)); return Optional.of(appendEnumToFullName(enumDescriptor, path, fullNameSoFar)); case DescriptorProto.FIELD_FIELD_NUMBER: FieldDescriptorProto fieldDescriptor = messageDescriptor.getField(path.get(1)); return Optional.of(appendFieldComponent(fullNameSoFar, fieldDescriptor.getName())); default: return Optional.empty(); } }
private EnumDescriptor (final EnumDescriptorProto proto, final FileDescriptor file, final Descriptor parent, final int index) throws DescriptorValidationException { this.index = index; this.proto = proto; fullName = computeFullName (file, parent, proto.getName ()); this.file = file; containingType = parent; if (proto.getValueCount () == 0) { // We cannot allow enums with no values because this would mean there // would be no valid default value for fields of this type. throw new DescriptorValidationException (this, "Enums must contain at least one value."); } values = new EnumValueDescriptor[proto.getValueCount ()]; for (int i = 0; i < proto.getValueCount (); i++) { values[i] = new EnumValueDescriptor ( proto.getValue (i), file, this, i); } file.pool.addSymbol (this); }
private void buildAllOptions(final FileDescriptorProto.Builder proto) { if (!buildOptions(proto.getOptionsBuilder())) { proto.clearOptions(); } for (final FieldDescriptorProto.Builder extensionProto : proto.getExtensionBuilderList()) { if (!buildOptions(extensionProto.getOptionsBuilder())) { extensionProto.clearOptions(); } } for (final EnumDescriptorProto.Builder enumProto : proto.getEnumTypeBuilderList()) { buildAllOptions(enumProto); } for (final ServiceDescriptorProto.Builder serviceProto : proto.getServiceBuilderList()) { buildAllOptions(serviceProto); } for (final DescriptorProto.Builder messageProto : proto.getMessageTypeBuilderList()) { buildAllOptions(messageProto); } }
private void buildAllOptions(final DescriptorProto.Builder proto) { if (!buildOptions(proto.getOptionsBuilder())) { proto.clearOptions(); } for (final FieldDescriptorProto.Builder fieldProto : proto.getFieldBuilderList()) { if (!buildOptions(fieldProto.getOptionsBuilder())) { fieldProto.clearOptions(); } } for (final FieldDescriptorProto.Builder extensionProto : proto.getExtensionBuilderList()) { if (!buildOptions(extensionProto.getOptionsBuilder())) { extensionProto.clearOptions(); } } for (final EnumDescriptorProto.Builder enumProto : proto.getEnumTypeBuilderList()) { buildAllOptions(enumProto); } for (final DescriptorProto.Builder messageProto : proto.getNestedTypeBuilderList()) { buildAllOptions(messageProto); } }
/** Used by the other overload, descends recursively into messages. */ private static boolean hasConflictingClassName(DescriptorProto messageDesc, String name) { if (name.equals(messageDesc.getName())) { return true; } for (EnumDescriptorProto enumDesc : messageDesc.getEnumTypeList()) { if (name.equals(enumDesc.getName())) { return true; } } for (DescriptorProto nestedMessageDesc : messageDesc.getNestedTypeList()) { if (hasConflictingClassName(nestedMessageDesc, name)) { return true; } } return false; }
/** Checks whether any generated classes conflict with the given name. */ private static boolean hasConflictingClassName(FileDescriptorProto file, String name) { for (EnumDescriptorProto enumDesc : file.getEnumTypeList()) { if (name.equals(enumDesc.getName())) { return true; } } for (ServiceDescriptorProto serviceDesc : file.getServiceList()) { if (name.equals(serviceDesc.getName())) { return true; } } for (DescriptorProto messageDesc : file.getMessageTypeList()) { if (hasConflictingClassName(messageDesc, name)) { return true; } } return false; }
private void doPrint(FileDescriptorProto fdp, String javaPackage, String outerClassName) { List<DescriptorProto> messageDescList = Lists.newArrayList(fdp.getMessageTypeList()); List<ServiceDescriptorProto> serviceDescList = Lists.newArrayList(fdp.getServiceList()); List<EnumDescriptorProto> enumDescList = Lists.newArrayList(fdp.getEnumTypeList()); messageDescList.stream().filter(temp -> temp.getEnumTypeList() != null) .forEach(temp -> enumDescList.addAll(temp.getEnumTypeList())); printEnum(enumDescList, javaPackage, outerClassName); printMessage(messageDescList, javaPackage, outerClassName); printService(serviceDescList, javaPackage); }
private void printEnum(List<EnumDescriptorProto> enumDescList, String javaPackage, String outerClassName) { for (EnumDescriptorProto enumDesc : enumDescList) { String enumClassType = enumDesc.getName(); String enumPackageName = javaPackage + "." + outerClassName; String fullpojoType = enumPackageName.toLowerCase() + "." + enumClassType; pojoTypes.put(enumClassType, fullpojoType); PrintEnumFile enumFile = new PrintEnumFile(generatePath, enumPackageName, enumClassType); try { enumFile.setEnumFields(enumDesc.getValueList()); } finally { enumFile.print(); } } }
private EnumDescriptorProto generateEnum(Enum e) { EnumDescriptorProto.Builder builder = EnumDescriptorProto.newBuilder(); builder.setName(getSimpleName(e.getName())); for (EnumValue value : e.getEnumvalueList()) { EnumValueDescriptorProto.Builder valueBuilder = EnumValueDescriptorProto.newBuilder(); valueBuilder.setName(value.getName()); valueBuilder.setNumber(value.getNumber()); valueBuilder.setOptions(generateEnumValueOptions(value)); builder.addValue(valueBuilder.build()); } builder.setOptions(generateEnumOptions(e)); return builder.build(); }
private EnumType(ProtoContainerElement parent, EnumDescriptorProto proto, String path) { super(parent, proto.getName(), path); this.proto = proto; // Build values. ImmutableList.Builder<EnumValue> valuesBuilder = ImmutableList.builder(); List<EnumValueDescriptorProto> valueProtos = proto.getValueList(); for (int i = 0; i < valueProtos.size(); i++) { EnumValueDescriptorProto value = valueProtos.get(i); String childPath = buildPath(path, EnumDescriptorProto.VALUE_FIELD_NUMBER, i); valuesBuilder.add(EnumValue.create(this, value, childPath)); } values = valuesBuilder.build(); }
@Accepts protected void accept(EnumDescriptorProto.Builder enumType) { pushParent(BuilderVisitorNodeInfo.create(enumType, currentFile)); visitRepeated(EnumDescriptorProto.VALUE_FIELD_NUMBER); visit(enumType.getOptionsBuilder()); popExpectedParent(enumType); }
/** See {@link FileDescriptor#setProto}. */ private void setProto(final EnumDescriptorProto proto) { this.proto = proto; for (int i = 0; i < values.length; i++) { values[i].setProto(proto.getValue(i)); } }
protected void decompile(EnumDescriptorProto enumDescriptor) throws IOException { indentedFormat("enum %s {", enumDescriptor.getName()); indent++; if (enumDescriptor.hasOptions()) { decompileOptions(enumDescriptor.getOptions()); } for (EnumValueDescriptorProto value : enumDescriptor.getValueList()) { indentedFormat("%s = %d%s;", value.getName(), value.getNumber(), value.hasOptions() ? bracketedOptions(value.getOptions()) : ""); } indent--; indentedFormat("}"); }
private static String appendEnumToFullName( EnumDescriptorProto enumDescriptor, List<Integer> path, String fullNameSoFar) { fullNameSoFar = appendNameComponent(fullNameSoFar, enumDescriptor.getName()); if (path.size() > 2) { fullNameSoFar = appendFieldComponent(fullNameSoFar, enumDescriptor.getValue(path.get(3)).getName()); } return fullNameSoFar; }
/** * See {@link FileDescriptor#setProto}. */ private void setProto (final EnumDescriptorProto proto) { this.proto = proto; for (int i = 0; i < values.length; i++) { values[i].setProto (proto.getValue (i)); } }
@Override public void enterEnumStatement(final EnumStatementContext ctx) { super.enterEnumStatement(ctx); locationBuilder.addEnumLocation().comments(ctx).setAllSpan(ctx) .addLocationForPrimitive(EnumDescriptorProto.NAME_FIELD_NUMBER).setAllSpan(ctx.identifier()); }
private void buildAllOptions(final EnumDescriptorProto.Builder proto) { if (!buildOptions(proto.getOptionsBuilder())) { proto.clearOptions(); } for (final EnumValueDescriptorProto.Builder enumValueProto : proto.getValueBuilderList()) { if (!buildOptions(enumValueProto.getOptionsBuilder())) { enumValueProto.clearOptions(); } } }
private boolean isCanonical(final FileDescriptor file) { final FileDescriptorProto proto = file.toProto(); if (proto.hasOptions() && proto.getOptions().getUninterpretedOptionCount() > 0) { return false; } for (final FieldDescriptorProto field : proto.getExtensionList()) { if (!isFieldCanonical(field)) { return false; } } for (final ServiceDescriptorProto serviceProto : proto.getServiceList()) { if (!isCanonical(serviceProto)) { return false; } } for (final EnumDescriptorProto enumProto : proto.getEnumTypeList()) { if (!isCanonical(enumProto)) { return false; } } for (final DescriptorProto message : proto.getMessageTypeList()) { if (!isMessageRefsCanonical(message)) { return false; } } return true; }
private boolean isCanonical(final EnumDescriptorProto enumProto) { if (enumProto.hasOptions() && enumProto.getOptions().getUninterpretedOptionCount() > 0) { return false; } for (final EnumValueDescriptorProto enumValue : enumProto.getValueList()) { if (enumValue.hasOptions() && enumValue.getOptions().getUninterpretedOptionCount() > 0) { return false; } } return true; }
private FileDescriptorProto createFileDescriptorProto(String fileName, String packageName, UnknownFieldSet unknownFields) { FileDescriptorProto.Builder fileBuilder = FileDescriptorProto.newBuilder(); return fileBuilder .setName(fileName) .setPackage(packageName) .setUnknownFields(unknownFields) .addAllDependency(Collections.<String>emptyList()) .addAllEnumType(Collections.<EnumDescriptorProto>emptyList()) .addAllExtension(Collections.<FieldDescriptorProto>emptyList()) .addAllMessageType(Collections.<DescriptorProto>emptyList()) .addAllPublicDependency(Collections.<Integer>emptyList()) .addAllService(Collections.<ServiceDescriptorProto>emptyList()) .build(); }
private DescriptorProto createMessageDescriptorProto(String messageName, UnknownFieldSet unknownFields) { DescriptorProto.Builder messageBuilder = DescriptorProto.newBuilder(); return messageBuilder .setName(messageName) .setUnknownFields(unknownFields) .addAllEnumType(Collections.<EnumDescriptorProto>emptyList()) .addAllExtension(Collections.<FieldDescriptorProto>emptyList()) .addAllField(Collections.<FieldDescriptorProto>emptyList()) .addAllNestedType(Collections.<DescriptorProto>emptyList()) .build(); }
DescriptorProtos.EnumDescriptorProto exchangeProto( DescriptorProtos.EnumDescriptorProto arg);
@Override public EnumDescriptorProto exchangeProto(EnumDescriptorProto arg) { return arg; }
public static List<Option> getOptions(EnumDescriptorProto descriptor) { return getOptions(descriptor, true); }
public static List<Option> getOptions(EnumDescriptorProto descriptor, boolean withDefaults) { return toCoreOptions(maybeCombineOptionsWithDefault(withDefaults, descriptor.getOptions().getAllFields(), DEFAULT_ENUM_OPTIONS)); }
/** * Creates an enum backed up by the given proto. */ public static EnumType create(ProtoContainerElement parent, EnumDescriptorProto proto, String path) { return new EnumType(parent, proto, path); }
/** * Returns the underlying proto representation. */ public EnumDescriptorProto getProto() { return proto; }
/** Convert the descriptor to its protocol message representation. */ @Override public EnumDescriptorProto toProto() { return proto; }
private void testCallsInternal(Configuration conf) throws Exception { RpcServer rpcServer = HBaseRPC.getServer(new TestImpl(), new Class<?>[] {TestProtocol.class}, "localhost", // BindAddress is IP we got for this server. 0, // port number 2, // number of handlers 0, // we dont use high priority handlers in master conf.getBoolean("hbase.rpc.verbose", false), conf, 0); RpcEngine rpcEngine = null; try { rpcServer.start(); rpcEngine = HBaseRPC.getProtocolEngine(conf); InetSocketAddress isa = rpcServer.getListenerAddress(); TestProtocol proxy = HBaseRPC.waitForProxy(rpcEngine, TestProtocol.class, TestProtocol.VERSION, isa, conf, -1, 8000, 8000); String stringResult = proxy.echo("foo"); assertEquals(stringResult, "foo"); stringResult = proxy.echo((String)null); assertEquals(stringResult, null); Text utf8Result = (Text)proxy.echo(new Text("hello world")); assertEquals(utf8Result, new Text("hello world")); utf8Result = (Text)proxy.echo((Text)null); assertEquals(utf8Result, null); // Test protobufs EnumDescriptorProto sendProto = EnumDescriptorProto.newBuilder().setName("test").build(); EnumDescriptorProto retProto = proxy.exchangeProto(sendProto); assertEquals(sendProto, retProto); assertNotSame(sendProto, retProto); } finally { rpcServer.stop(); if (rpcEngine != null) { rpcEngine.close(); } } }