本文整理汇总了Java中com.google.protobuf.Descriptors.FileDescriptor类的典型用法代码示例。如果您正苦于以下问题:Java FileDescriptor类的具体用法?Java FileDescriptor怎么用?Java FileDescriptor使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
FileDescriptor类属于com.google.protobuf.Descriptors包,在下文中一共展示了FileDescriptor类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: build
import com.google.protobuf.Descriptors.FileDescriptor; //导入依赖的package包/类
GqlInputConverter build() {
HashBiMap<String, Descriptor> mapping = HashBiMap.create();
HashBiMap<String, EnumDescriptor> enumMapping = HashBiMap.create(getEnumMap(enumDescriptors));
LinkedList<Descriptor> loop = new LinkedList<>(descriptors);
Set<FileDescriptor> fileDescriptorSet = ProtoRegistry.extractDependencies(fileDescriptors);
for (FileDescriptor fileDescriptor : fileDescriptorSet) {
loop.addAll(fileDescriptor.getMessageTypes());
enumMapping.putAll(getEnumMap(fileDescriptor.getEnumTypes()));
}
while (!loop.isEmpty()) {
Descriptor descriptor = loop.pop();
if (!mapping.containsKey(descriptor.getFullName())) {
mapping.put(getReferenceName(descriptor), descriptor);
loop.addAll(descriptor.getNestedTypes());
enumMapping.putAll(getEnumMap(descriptor.getEnumTypes()));
}
}
return new GqlInputConverter(
ImmutableBiMap.copyOf(mapping), ImmutableBiMap.copyOf(enumMapping));
}
示例2: extractDependencies
import com.google.protobuf.Descriptors.FileDescriptor; //导入依赖的package包/类
static Set<FileDescriptor> extractDependencies(List<FileDescriptor> fileDescriptors) {
LinkedList<FileDescriptor> loop = new LinkedList<>(fileDescriptors);
HashSet<FileDescriptor> fileDescriptorSet = new HashSet<>(fileDescriptors);
while (!loop.isEmpty()) {
FileDescriptor fileDescriptor = loop.pop();
for (FileDescriptor dependency : fileDescriptor.getDependencies()) {
if (!fileDescriptorSet.contains(dependency)) {
fileDescriptorSet.add(dependency);
loop.push(dependency);
}
}
}
return ImmutableSet.copyOf(fileDescriptorSet);
}
示例3: parseEnum
import com.google.protobuf.Descriptors.FileDescriptor; //导入依赖的package包/类
private EnumValueDescriptor parseEnum(EnumDescriptor enumDescriptor, JsonElement json)
throws InvalidProtocolBufferException {
String value = json.getAsString();
EnumValueDescriptor result = enumDescriptor.findValueByName(value);
if (result == null) {
// Try to interpret the value as a number.
try {
int numericValue = parseInt32(json);
if (enumDescriptor.getFile().getSyntax() == FileDescriptor.Syntax.PROTO3) {
result = enumDescriptor.findValueByNumberCreatingIfUnknown(numericValue);
} else {
result = enumDescriptor.findValueByNumber(numericValue);
}
} catch (InvalidProtocolBufferException e) {
// Fall through. This exception is about invalid int32 value we get from parseInt32() but
// that's not the exception we want the user to see. Since result == null, we will throw
// an exception later.
}
if (result == null) {
throw new InvalidProtocolBufferException(
"Invalid enum value: " + value + " for enum type: " + enumDescriptor.getFullName());
}
}
return result;
}
示例4: fromFileDescriptorSet
import com.google.protobuf.Descriptors.FileDescriptor; //导入依赖的package包/类
/** Creates a resolver which searches the supplied {@link FileDescriptorSet}. */
public static ServiceResolver fromFileDescriptorSet(FileDescriptorSet descriptorSet) {
ImmutableMap<String, FileDescriptorProto> descriptorProtoIndex =
computeDescriptorProtoIndex(descriptorSet);
Map<String, FileDescriptor> descriptorCache = new HashMap<>();
ImmutableList.Builder<FileDescriptor> result = ImmutableList.builder();
for (FileDescriptorProto descriptorProto : descriptorSet.getFileList()) {
try {
result.add(descriptorFromProto(descriptorProto, descriptorProtoIndex, descriptorCache));
} catch (DescriptorValidationException e) {
logger.warn("Skipped descriptor " + descriptorProto.getName() + " due to error", e);
continue;
}
}
return new ServiceResolver(result.build());
}
示例5: descriptorFromProto
import com.google.protobuf.Descriptors.FileDescriptor; //导入依赖的package包/类
/**
* Recursively constructs file descriptors for all dependencies of the supplied proto and returns
* a {@link FileDescriptor} for the supplied proto itself. For maximal efficiency, reuse the
* descriptorCache argument across calls.
*/
private static FileDescriptor descriptorFromProto(
FileDescriptorProto descriptorProto,
ImmutableMap<String, FileDescriptorProto> descriptorProtoIndex,
Map<String, FileDescriptor> descriptorCache) throws DescriptorValidationException {
// First, check the cache.
String descritorName = descriptorProto.getName();
if (descriptorCache.containsKey(descritorName)) {
return descriptorCache.get(descritorName);
}
// Then, fetch all the required dependencies recursively.
ImmutableList.Builder<FileDescriptor> dependencies = ImmutableList.builder();
for (String dependencyName : descriptorProto.getDependencyList()) {
if (!descriptorProtoIndex.containsKey(dependencyName)) {
throw new IllegalArgumentException("Could not find dependency: " + dependencyName);
}
FileDescriptorProto dependencyProto = descriptorProtoIndex.get(dependencyName);
dependencies.add(descriptorFromProto(dependencyProto, descriptorProtoIndex, descriptorCache));
}
// Finally, construct the actual descriptor.
FileDescriptor[] empty = new FileDescriptor[0];
return FileDescriptor.buildFrom(descriptorProto, dependencies.build().toArray(empty));
}
示例6: registerGpbMsgDesc
import com.google.protobuf.Descriptors.FileDescriptor; //导入依赖的package包/类
public void registerGpbMsgDesc(FileDescriptor fileDescriptor) {
if (fileDescriptor == null) return;
// service
for (ServiceDescriptor service : fileDescriptor.getServices()) {
for (MethodDescriptor method : service.getMethods()) {
if (gpbMsgDescMap.containsKey(method.getName())) {
LOG.error("[Gpb] the method [" + method.getName() + "] already registered.");
}
registerGpbMessage(method.getInputType());
methodInputTypeMap.put(method.getName(), method.getInputType().getName());
}
}
// message
for (Descriptor descriptor : fileDescriptor.getMessageTypes()) {
registerGpbMessage(descriptor);
}
}
示例7: loadService
import com.google.protobuf.Descriptors.FileDescriptor; //导入依赖的package包/类
private void loadService() {
LOG.info("Load service definition is starting...");
InputStream in = null;
FileDescriptorSet descriptorSet;
try {
in = ClassHelper.getClassLoader().getResourceAsStream(GrpcConstants.PROTO_DESC_FILENAME);
descriptorSet = FileDescriptorSet.parseFrom(in);
for (FileDescriptorProto fdp : descriptorSet.getFileList()) {
FileDescriptor fd = FileDescriptor.buildFrom(fdp, new FileDescriptor[] {}, true);
for (com.google.protobuf.Descriptors.ServiceDescriptor service : fd.getServices()) {
addServiceDenifition(service.getName(),
fd.getOptions().getJavaPackage() + '.' + service.getFullName());
}
}
LOG.info("Load service denifition is finished, total {} service are found.", services.size());
} catch (Exception ex) {
LOG.error("Load service denifition error happened.", ex);
throw new RuntimeException(ex);
} finally {
IOUtils.closeInputStream(in);
}
}
示例8: forGroup
import com.google.protobuf.Descriptors.FileDescriptor; //导入依赖的package包/类
public static ProtobufRowDataConverter forGroup(Group group,
FileDescriptor fileDescriptor) {
// Find the group message.
Descriptor groupMessage = null;
List<Descriptor> messages = fileDescriptor.getMessageTypes();
for (int i = messages.size() - 1; i >= 0; i--) {
Descriptor message = messages.get(i);
if (message.getOptions().getExtension(TableOptions.fdbsql).getIsGroup()) {
groupMessage = message;
break;
}
}
if (groupMessage != null) {
return new GroupConverter(group, groupMessage);
}
else {
assert (messages.size() == 1 && group.getRoot().getChildJoins().isEmpty());
return new TableConverter(group.getRoot(), messages.get(0));
}
}
示例9: forGroup
import com.google.protobuf.Descriptors.FileDescriptor; //导入依赖的package包/类
public static ProtobufRowConverter forGroup(Group group,
FileDescriptor fileDescriptor) {
// Find the group message.
Descriptor groupMessage = null;
List<Descriptor> messages = fileDescriptor.getMessageTypes();
for (int i = messages.size() - 1; i >= 0; i--) {
Descriptor message = messages.get(i);
if (message.getOptions().getExtension(TableOptions.fdbsql).getIsGroup()) {
groupMessage = message;
break;
}
}
if (groupMessage != null) {
return new GroupConverter(group, groupMessage);
}
else {
assert (messages.size() == 1 && group.getRoot().getChildJoins().isEmpty());
return new TableConverter(group.getRoot(), messages.get(0));
}
}
示例10: testDependencyOrder
import com.google.protobuf.Descriptors.FileDescriptor; //导入依赖的package包/类
public void testDependencyOrder() throws Exception {
FileDescriptorProto fooProto = FileDescriptorProto.newBuilder()
.setName("foo.proto").build();
FileDescriptorProto barProto = FileDescriptorProto.newBuilder()
.setName("bar.proto")
.addDependency("foo.proto")
.build();
FileDescriptorProto bazProto = FileDescriptorProto.newBuilder()
.setName("baz.proto")
.addDependency("foo.proto")
.addDependency("bar.proto")
.addPublicDependency(0)
.addPublicDependency(1)
.build();
FileDescriptor fooFile = Descriptors.FileDescriptor.buildFrom(fooProto,
new FileDescriptor[0]);
FileDescriptor barFile = Descriptors.FileDescriptor.buildFrom(barProto,
new FileDescriptor[] {fooFile});
// Items in the FileDescriptor array can be in any order.
Descriptors.FileDescriptor.buildFrom(bazProto,
new FileDescriptor[] {fooFile, barFile});
Descriptors.FileDescriptor.buildFrom(bazProto,
new FileDescriptor[] {barFile, fooFile});
}
示例11: testInvalidPublicDependency
import com.google.protobuf.Descriptors.FileDescriptor; //导入依赖的package包/类
public void testInvalidPublicDependency() throws Exception {
FileDescriptorProto fooProto = FileDescriptorProto.newBuilder()
.setName("foo.proto").build();
FileDescriptorProto barProto = FileDescriptorProto.newBuilder()
.setName("boo.proto")
.addDependency("foo.proto")
.addPublicDependency(1) // Error, should be 0.
.build();
FileDescriptor fooFile = Descriptors.FileDescriptor.buildFrom(fooProto,
new FileDescriptor[0]);
try {
Descriptors.FileDescriptor.buildFrom(barProto,
new FileDescriptor[] {fooFile});
fail("DescriptorValidationException expected");
} catch (DescriptorValidationException e) {
assertTrue(
e.getMessage().indexOf("Invalid public dependency index.") != -1);
}
}
示例12: testPackedEnumField
import com.google.protobuf.Descriptors.FileDescriptor; //导入依赖的package包/类
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]);
}
示例13: makeCanonicalProto
import com.google.protobuf.Descriptors.FileDescriptor; //导入依赖的package包/类
private FileDescriptorProto makeCanonicalProto(final FileDescriptor fileDescriptor) {
final FileDescriptorProto.Builder protoBuilder =
FileDescriptorProto.newBuilder(fileDescriptor.toProto());
for (final FieldDescriptorProto.Builder field : protoBuilder.getExtensionBuilderList()) {
makeCanonicalField(field, fileDescriptor.findExtensionByName(field.getName()));
}
for (final DescriptorProto.Builder message : protoBuilder.getMessageTypeBuilderList()) {
makeCanonicalMessage(message, fileDescriptor.findMessageTypeByName(message.getName()));
}
// for (EnumDescriptorProto.Builder enumProto :
// protoBuilder.getEnumTypeBuilderList()) {
// makeCanonicalEnum(enumProto,
// fileDescriptor.findEnumTypeByName(enumProto.getName()));
// }
for (final ServiceDescriptorProto.Builder serviceProto : protoBuilder.getServiceBuilderList()) {
makeCanonicalService(serviceProto, fileDescriptor.findServiceByName(serviceProto.getName()));
}
// TODO: incorporate options' tree walking into canonicalization to eliminate double walking
return OptionResolver.newBuilder().setCustomOptionsAsExtensions(false)
.resolveAllOptionsFor(fileDescriptor, protoBuilder).build();
}
示例14: resolveAllRefs
import com.google.protobuf.Descriptors.FileDescriptor; //导入依赖的package包/类
public boolean resolveAllRefs(final Collection<FileDescriptor> dependencies,
final IProtoErrorListener errorListener) {
if (isBuilt()) {
throw new IllegalStateException("not supported when proto is already built");
}
final Map<String, NameContext> cache = new HashMap<String, NameContext>();
if (resolveAllRefs(dependencies, errorListener, cache)) {
return true;
}
for (final Entry<FieldDescriptorProto.Builder, FieldContext> entry : unresolved) {
reportUnresolvedTypeNameError(entry.getKey(), entry.getValue(), errorListener);
}
return false;
}
示例15: buildAndCacheProto
import com.google.protobuf.Descriptors.FileDescriptor; //导入依赖的package包/类
private boolean buildAndCacheProto(final Map<String, FileDescriptor> cache,
final ParsedContext fileProto, final FileDescriptor[] dependencies,
final IBaseProtoErrorListener errorListener) {
try {
// if (fileProto.resolveAllRefs(Arrays.asList(dependencies), errorListener)) {
// TODO: implement ProtoFileParser.hasCustomOption(); then skip build's proto
// reserialization and OptionResolver if hasCustomOption() is true!
final FileDescriptor fileDescriptor =
fileBuilder.setProto(fileProto.getProto()).addDependencies(dependencies).build();
cache.put(fileProto.getProto().getName(), fileDescriptor);
return true;
// }
} catch (final DescriptorValidationRuntimeException e) {
errorListener.setProtoName(fileProto.getProto().getName());
errorListener.validationError(null, null, e.getDescription(), e);
}
return false;
}