Java Code Examples for com.google.protobuf.Descriptors.FileDescriptor#findExtensionByName()

The following examples show how to use com.google.protobuf.Descriptors.FileDescriptor#findExtensionByName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GeneratedMessage.java    From play-store-api with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 2
Source File: GeneratedMessage.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}