下面列出了com.google.protobuf.Descriptors.FileDescriptor#findExtensionByName ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Used in proto1 generated code only.
*
* After enabling bridge, we can define proto2 extensions (the extended type
* is a proto2 mutable message) in a proto1 .proto file. For these extensions
* we should generate proto2 GeneratedExtensions.
*/
public static <ContainingType extends Message, Type>
GeneratedExtension<ContainingType, Type>
newFileScopedGeneratedExtension(
final Class singularType, final Message defaultInstance,
final String descriptorOuterClass, final String extensionName) {
// For extensions scoped within a file, we load the descriptor outer
// class and rely on it to get the FileDescriptor which then can be
// used to obtain the extension's FieldDescriptor.
return new GeneratedExtension<ContainingType, Type>(
new CachedDescriptorRetriever() {
@Override
protected FieldDescriptor loadDescriptor() {
try {
Class clazz = singularType.getClassLoader().loadClass(descriptorOuterClass);
FileDescriptor file = (FileDescriptor) clazz.getField("descriptor").get(null);
return file.findExtensionByName(extensionName);
} catch (Exception e) {
throw new RuntimeException(
"Cannot load descriptors: "
+ descriptorOuterClass
+ " is not a valid descriptor class name",
e);
}
}
},
singularType,
defaultInstance,
Extension.ExtensionType.MUTABLE);
}
/**
* Used in proto1 generated code only.
*
* After enabling bridge, we can define proto2 extensions (the extended type
* is a proto2 mutable message) in a proto1 .proto file. For these extensions
* we should generate proto2 GeneratedExtensions.
*/
public static <ContainingType extends Message, Type>
GeneratedExtension<ContainingType, Type>
newFileScopedGeneratedExtension(
final Class singularType, final Message defaultInstance,
final String descriptorOuterClass, final String extensionName) {
// For extensions scoped within a file, we load the descriptor outer
// class and rely on it to get the FileDescriptor which then can be
// used to obtain the extension's FieldDescriptor.
return new GeneratedExtension<ContainingType, Type>(
new CachedDescriptorRetriever() {
protected FieldDescriptor loadDescriptor() {
try {
Class clazz =
singularType.getClassLoader().loadClass(descriptorOuterClass);
FileDescriptor file =
(FileDescriptor) clazz.getField("descriptor").get(null);
return file.findExtensionByName(extensionName);
} catch (Exception e) {
throw new RuntimeException(
"Cannot load descriptors: " + descriptorOuterClass +
" is not a valid descriptor class name", e);
}
}
},
singularType,
defaultInstance,
Extension.ExtensionType.MUTABLE);
}