diff options
Diffstat (limited to 'java/core/src/main/java/com/google/protobuf/Descriptors.java')
-rw-r--r-- | java/core/src/main/java/com/google/protobuf/Descriptors.java | 1089 |
1 files changed, 559 insertions, 530 deletions
diff --git a/java/core/src/main/java/com/google/protobuf/Descriptors.java b/java/core/src/main/java/com/google/protobuf/Descriptors.java index 75b16fe3..8f751925 100644 --- a/java/core/src/main/java/com/google/protobuf/Descriptors.java +++ b/java/core/src/main/java/com/google/protobuf/Descriptors.java @@ -49,29 +49,25 @@ import java.util.logging.Logger; /** * Contains a collection of classes which describe protocol message types. * - * Every message type has a {@link Descriptor}, which lists all - * its fields and other information about a type. You can get a message - * type's descriptor by calling {@code MessageType.getDescriptor()}, or - * (given a message object of the type) {@code message.getDescriptorForType()}. - * Furthermore, each message is associated with a {@link FileDescriptor} for - * a relevant {@code .proto} file. You can obtain it by calling - * {@code Descriptor.getFile()}. A {@link FileDescriptor} contains descriptors - * for all the messages defined in that file, and file descriptors for all the - * imported {@code .proto} files. + * <p>Every message type has a {@link Descriptor}, which lists all its fields and other information + * about a type. You can get a message type's descriptor by calling {@code + * MessageType.getDescriptor()}, or (given a message object of the type) {@code + * message.getDescriptorForType()}. Furthermore, each message is associated with a {@link + * FileDescriptor} for a relevant {@code .proto} file. You can obtain it by calling {@code + * Descriptor.getFile()}. A {@link FileDescriptor} contains descriptors for all the messages defined + * in that file, and file descriptors for all the imported {@code .proto} files. * - * Descriptors are built from DescriptorProtos, as defined in - * {@code google/protobuf/descriptor.proto}. + * <p>Descriptors are built from DescriptorProtos, as defined in {@code + * google/protobuf/descriptor.proto}. * * @author kenton@google.com Kenton Varda */ public final class Descriptors { - private static final Logger logger = - Logger.getLogger(Descriptors.class.getName()); + private static final Logger logger = Logger.getLogger(Descriptors.class.getName()); /** - * Describes a {@code .proto} file, including everything defined within. - * That includes, in particular, descriptors for all the messages and - * file descriptors for all other imported {@code .proto} files - * (dependencies). + * Describes a {@code .proto} file, including everything defined within. That includes, in + * particular, descriptors for all the messages and file descriptors for all other imported {@code + * .proto} files (dependencies). */ public static final class FileDescriptor extends GenericDescriptor { /** Convert the descriptor to its protocol message representation. */ @@ -99,14 +95,17 @@ public final class Descriptors { } /** - * Get the proto package name. This is the package name given by the - * {@code package} statement in the {@code .proto} file, which differs - * from the Java package. + * Get the proto package name. This is the package name given by the {@code package} statement + * in the {@code .proto} file, which differs from the Java package. */ - public String getPackage() { return proto.getPackage(); } + public String getPackage() { + return proto.getPackage(); + } /** Get the {@code FileOptions}, defined in {@code descriptor.proto}. */ - public FileOptions getOptions() { return proto.getOptions(); } + public FileOptions getOptions() { + return proto.getOptions(); + } /** Get a list of top-level message types declared in this file. */ public List<Descriptor> getMessageTypes() { @@ -147,6 +146,7 @@ public final class Descriptors { Syntax(String name) { this.name = name; } + private final String name; } @@ -159,7 +159,7 @@ public final class Descriptors { } /** - * Find a message type in the file by name. Does not find nested types. + * Find a message type in the file by name. Does not find nested types. * * @param name The unqualified type name to look for. * @return The message type's descriptor, or {@code null} if not found. @@ -174,16 +174,15 @@ public final class Descriptors { name = getPackage() + '.' + name; } final GenericDescriptor result = pool.findSymbol(name); - if (result != null && result instanceof Descriptor && - result.getFile() == this) { - return (Descriptor)result; + if (result != null && result instanceof Descriptor && result.getFile() == this) { + return (Descriptor) result; } else { return null; } } /** - * Find an enum type in the file by name. Does not find nested types. + * Find an enum type in the file by name. Does not find nested types. * * @param name The unqualified type name to look for. * @return The enum type's descriptor, or {@code null} if not found. @@ -198,9 +197,8 @@ public final class Descriptors { name = getPackage() + '.' + name; } final GenericDescriptor result = pool.findSymbol(name); - if (result != null && result instanceof EnumDescriptor && - result.getFile() == this) { - return (EnumDescriptor)result; + if (result != null && result instanceof EnumDescriptor && result.getFile() == this) { + return (EnumDescriptor) result; } else { return null; } @@ -222,17 +220,15 @@ public final class Descriptors { name = getPackage() + '.' + name; } final GenericDescriptor result = pool.findSymbol(name); - if (result != null && result instanceof ServiceDescriptor && - result.getFile() == this) { - return (ServiceDescriptor)result; + if (result != null && result instanceof ServiceDescriptor && result.getFile() == this) { + return (ServiceDescriptor) result; } else { return null; } } /** - * Find an extension in the file by name. Does not find extensions nested - * inside message types. + * Find an extension in the file by name. Does not find extensions nested inside message types. * * @param name The unqualified extension name to look for. * @return The extension's descriptor, or {@code null} if not found. @@ -245,9 +241,8 @@ public final class Descriptors { name = getPackage() + '.' + name; } final GenericDescriptor result = pool.findSymbol(name); - if (result != null && result instanceof FieldDescriptor && - result.getFile() == this) { - return (FieldDescriptor)result; + if (result != null && result instanceof FieldDescriptor && result.getFile() == this) { + return (FieldDescriptor) result; } else { return null; } @@ -257,36 +252,31 @@ public final class Descriptors { * Construct a {@code FileDescriptor}. * * @param proto The protocol message form of the FileDescriptor. - * @param dependencies {@code FileDescriptor}s corresponding to all of - * the file's dependencies. - * @throws DescriptorValidationException {@code proto} is not a valid - * descriptor. This can occur for a number of reasons, e.g. - * because a field has an undefined type or because two messages - * were defined with the same name. + * @param dependencies {@code FileDescriptor}s corresponding to all of the file's dependencies. + * @throws DescriptorValidationException {@code proto} is not a valid descriptor. This can occur + * for a number of reasons, e.g. because a field has an undefined type or because two + * messages were defined with the same name. */ - public static FileDescriptor buildFrom(final FileDescriptorProto proto, - final FileDescriptor[] dependencies) - throws DescriptorValidationException { + public static FileDescriptor buildFrom( + final FileDescriptorProto proto, final FileDescriptor[] dependencies) + throws DescriptorValidationException { return buildFrom(proto, dependencies, false); } - /** * Construct a {@code FileDescriptor}. * * @param proto The protocol message form of the FileDescriptor. - * @param dependencies {@code FileDescriptor}s corresponding to all of - * the file's dependencies. - * @param allowUnknownDependencies If true, non-exist dependenncies will be - * ignored and undefined message types will be replaced with a - * placeholder type. - * @throws DescriptorValidationException {@code proto} is not a valid - * descriptor. This can occur for a number of reasons, e.g. - * because a field has an undefined type or because two messages - * were defined with the same name. + * @param dependencies {@code FileDescriptor}s corresponding to all of the file's dependencies. + * @param allowUnknownDependencies If true, non-exist dependenncies will be ignored and + * undefined message types will be replaced with a placeholder type. + * @throws DescriptorValidationException {@code proto} is not a valid descriptor. This can occur + * for a number of reasons, e.g. because a field has an undefined type or because two + * messages were defined with the same name. */ public static FileDescriptor buildFrom( - final FileDescriptorProto proto, final FileDescriptor[] dependencies, + final FileDescriptorProto proto, + final FileDescriptor[] dependencies, final boolean allowUnknownDependencies) throws DescriptorValidationException { // Building descriptors involves two steps: translating and linking. @@ -298,18 +288,16 @@ public final class Descriptors { // FieldDescriptor for an embedded message contains a pointer directly // to the Descriptor for that message's type. We also detect undefined // types in the linking step. - final DescriptorPool pool = new DescriptorPool( - dependencies, allowUnknownDependencies); - final FileDescriptor result = new FileDescriptor( - proto, dependencies, pool, allowUnknownDependencies); + final DescriptorPool pool = new DescriptorPool(dependencies, allowUnknownDependencies); + final FileDescriptor result = + new FileDescriptor(proto, dependencies, pool, allowUnknownDependencies); result.crossLink(); return result; } /** - * This method is to be called by generated code only. It is equivalent - * to {@code buildFrom} except that the {@code FileDescriptorProto} is - * encoded in protocol buffer wire format. + * This method is to be called by generated code only. It is equivalent to {@code buildFrom} + * except that the {@code FileDescriptorProto} is encoded in protocol buffer wire format. */ public static void internalBuildGeneratedFileFrom( final String[] descriptorDataParts, @@ -339,7 +327,7 @@ public final class Descriptors { proto = FileDescriptorProto.parseFrom(descriptorBytes); } catch (InvalidProtocolBufferException e) { throw new IllegalArgumentException( - "Failed to parse protocol buffer descriptor for generated code.", e); + "Failed to parse protocol buffer descriptor for generated code.", e); } final FileDescriptor result; @@ -349,11 +337,10 @@ public final class Descriptors { result = buildFrom(proto, dependencies, true); } catch (DescriptorValidationException e) { throw new IllegalArgumentException( - "Invalid embedded descriptor for \"" + proto.getName() + "\".", e); + "Invalid embedded descriptor for \"" + proto.getName() + "\".", e); } - final ExtensionRegistry registry = - descriptorAssigner.assignDescriptors(result); + final ExtensionRegistry registry = descriptorAssigner.assignDescriptors(result); if (registry != null) { // We must re-parse the proto using the registry. @@ -361,8 +348,7 @@ public final class Descriptors { proto = FileDescriptorProto.parseFrom(descriptorBytes, registry); } catch (InvalidProtocolBufferException e) { throw new IllegalArgumentException( - "Failed to parse protocol buffer descriptor for generated code.", - e); + "Failed to parse protocol buffer descriptor for generated code.", e); } result.setProto(proto); @@ -370,8 +356,8 @@ public final class Descriptors { } /** - * This method is to be called by generated code only. It uses Java - * reflection to load the dependencies' descriptors. + * This method is to be called by generated code only. It uses Java reflection to load the + * dependencies' descriptors. */ public static void internalBuildGeneratedFileFrom( final String[] descriptorDataParts, @@ -382,54 +368,46 @@ public final class Descriptors { List<FileDescriptor> descriptors = new ArrayList<FileDescriptor>(); for (int i = 0; i < dependencies.length; i++) { try { - Class<?> clazz = - descriptorOuterClass.getClassLoader().loadClass(dependencies[i]); - descriptors.add( - (FileDescriptor) clazz.getField("descriptor").get(null)); + Class<?> clazz = descriptorOuterClass.getClassLoader().loadClass(dependencies[i]); + descriptors.add((FileDescriptor) clazz.getField("descriptor").get(null)); } catch (Exception e) { // We allow unknown dependencies by default. If a dependency cannot // be found we only generate a warning. - logger.warning("Descriptors for \"" + dependencyFileNames[i] + - "\" can not be found."); + logger.warning("Descriptors for \"" + dependencyFileNames[i] + "\" can not be found."); } } FileDescriptor[] descriptorArray = new FileDescriptor[descriptors.size()]; descriptors.toArray(descriptorArray); - internalBuildGeneratedFileFrom( - descriptorDataParts, descriptorArray, descriptorAssigner); + internalBuildGeneratedFileFrom(descriptorDataParts, descriptorArray, descriptorAssigner); } /** - * This method is to be called by generated code only. It is used to - * update the FileDescriptorProto associated with the descriptor by - * parsing it again with the given ExtensionRegistry. This is needed to - * recognize custom options. + * This method is to be called by generated code only. It is used to update the + * FileDescriptorProto associated with the descriptor by parsing it again with the given + * ExtensionRegistry. This is needed to recognize custom options. */ public static void internalUpdateFileDescriptor( - final FileDescriptor descriptor, - final ExtensionRegistry registry) { + final FileDescriptor descriptor, final ExtensionRegistry registry) { ByteString bytes = descriptor.proto.toByteString(); FileDescriptorProto proto; try { proto = FileDescriptorProto.parseFrom(bytes, registry); } catch (InvalidProtocolBufferException e) { throw new IllegalArgumentException( - "Failed to parse protocol buffer descriptor for generated code.", e); + "Failed to parse protocol buffer descriptor for generated code.", e); } descriptor.setProto(proto); } /** - * This class should be used by generated code only. When calling - * {@link FileDescriptor#internalBuildGeneratedFileFrom}, the caller - * provides a callback implementing this interface. The callback is called - * after the FileDescriptor has been constructed, in order to assign all - * the global variables defined in the generated code which point at parts - * of the FileDescriptor. The callback returns an ExtensionRegistry which - * contains any extensions which might be used in the descriptor -- that - * is, extensions of the various "Options" messages defined in - * descriptor.proto. The callback may also return null to indicate that - * no extensions are used in the descriptor. + * This class should be used by generated code only. When calling {@link + * FileDescriptor#internalBuildGeneratedFileFrom}, the caller provides a callback implementing + * this interface. The callback is called after the FileDescriptor has been constructed, in + * order to assign all the global variables defined in the generated code which point at parts + * of the FileDescriptor. The callback returns an ExtensionRegistry which contains any + * extensions which might be used in the descriptor -- that is, extensions of the various + * "Options" messages defined in descriptor.proto. The callback may also return null to indicate + * that no extensions are used in the descriptor. */ public interface InternalDescriptorAssigner { ExtensionRegistry assignDescriptors(FileDescriptor root); @@ -444,16 +422,16 @@ public final class Descriptors { private final FileDescriptor[] publicDependencies; private final DescriptorPool pool; - private FileDescriptor(final FileDescriptorProto proto, - final FileDescriptor[] dependencies, - final DescriptorPool pool, - boolean allowUnknownDependencies) - throws DescriptorValidationException { + private FileDescriptor( + final FileDescriptorProto proto, + final FileDescriptor[] dependencies, + final DescriptorPool pool, + boolean allowUnknownDependencies) + throws DescriptorValidationException { this.pool = pool; this.proto = proto; this.dependencies = dependencies.clone(); - HashMap<String, FileDescriptor> nameToFileMap = - new HashMap<String, FileDescriptor>(); + HashMap<String, FileDescriptor> nameToFileMap = new HashMap<String, FileDescriptor>(); for (FileDescriptor file : dependencies) { nameToFileMap.put(file.getName(), file); } @@ -461,15 +439,13 @@ public final class Descriptors { for (int i = 0; i < proto.getPublicDependencyCount(); i++) { int index = proto.getPublicDependency(i); if (index < 0 || index >= proto.getDependencyCount()) { - throw new DescriptorValidationException(this, - "Invalid public dependency index."); + throw new DescriptorValidationException(this, "Invalid public dependency index."); } String name = proto.getDependency(index); FileDescriptor file = nameToFileMap.get(name); if (file == null) { if (!allowUnknownDependencies) { - throw new DescriptorValidationException(this, - "Invalid public dependency: " + name); + throw new DescriptorValidationException(this, "Invalid public dependency: " + name); } // Ignore unknown dependencies. } else { @@ -483,8 +459,7 @@ public final class Descriptors { messageTypes = new Descriptor[proto.getMessageTypeCount()]; for (int i = 0; i < proto.getMessageTypeCount(); i++) { - messageTypes[i] = - new Descriptor(proto.getMessageType(i), this, null, i); + messageTypes[i] = new Descriptor(proto.getMessageType(i), this, null, i); } enumTypes = new EnumDescriptor[proto.getEnumTypeCount()]; @@ -499,20 +474,19 @@ public final class Descriptors { extensions = new FieldDescriptor[proto.getExtensionCount()]; for (int i = 0; i < proto.getExtensionCount(); i++) { - extensions[i] = new FieldDescriptor( - proto.getExtension(i), this, null, i, true); + extensions[i] = new FieldDescriptor(proto.getExtension(i), this, null, i, true); } } - /** - * Create a placeholder FileDescriptor for a message Descriptor. - */ - FileDescriptor(String packageName, Descriptor message) - throws DescriptorValidationException { + /** Create a placeholder FileDescriptor for a message Descriptor. */ + FileDescriptor(String packageName, Descriptor message) throws DescriptorValidationException { this.pool = new DescriptorPool(new FileDescriptor[0], true); - this.proto = FileDescriptorProto.newBuilder() - .setName(message.getFullName() + ".placeholder.proto") - .setPackage(packageName).addMessageType(message.toProto()).build(); + this.proto = + FileDescriptorProto.newBuilder() + .setName(message.getFullName() + ".placeholder.proto") + .setPackage(packageName) + .addMessageType(message.toProto()) + .build(); this.dependencies = new FileDescriptor[0]; this.publicDependencies = new FileDescriptor[0]; @@ -541,14 +515,12 @@ public final class Descriptors { } /** - * Replace our {@link FileDescriptorProto} with the given one, which is - * identical except that it might contain extensions that weren't present - * in the original. This method is needed for bootstrapping when a file - * defines custom options. The options may be defined in the file itself, - * so we can't actually parse them until we've constructed the descriptors, - * but to construct the descriptors we have to have parsed the descriptor - * protos. So, we have to parse the descriptor protos a second time after - * constructing the descriptors. + * Replace our {@link FileDescriptorProto} with the given one, which is identical except that it + * might contain extensions that weren't present in the original. This method is needed for + * bootstrapping when a file defines custom options. The options may be defined in the file + * itself, so we can't actually parse them until we've constructed the descriptors, but to + * construct the descriptors we have to have parsed the descriptor protos. So, we have to parse + * the descriptor protos a second time after constructing the descriptors. */ private void setProto(final FileDescriptorProto proto) { this.proto = proto; @@ -580,19 +552,24 @@ public final class Descriptors { /** Describes a message type. */ public static final class Descriptor extends GenericDescriptor { /** - * Get the index of this descriptor within its parent. In other words, - * given a {@link FileDescriptor} {@code file}, the following is true: + * Get the index of this descriptor within its parent. In other words, given a {@link + * FileDescriptor} {@code file}, the following is true: + * * <pre> * for all i in [0, file.getMessageTypeCount()): * file.getMessageType(i).getIndex() == i * </pre> + * * Similarly, for a {@link Descriptor} {@code messageType}: + * * <pre> * for all i in [0, messageType.getNestedTypeCount()): * messageType.getNestedType(i).getIndex() == i * </pre> */ - public int getIndex() { return index; } + public int getIndex() { + return index; + } /** Convert the descriptor to its protocol message representation. */ @Override @@ -607,14 +584,15 @@ public final class Descriptors { } /** - * Get the type's fully-qualified name, within the proto language's - * namespace. This differs from the Java name. For example, given this - * {@code .proto}: + * Get the type's fully-qualified name, within the proto language's namespace. This differs from + * the Java name. For example, given this {@code .proto}: + * * <pre> * package foo.bar; * option java_package = "com.example.protos" * message Baz {} * </pre> + * * {@code Baz}'s full name is "foo.bar.Baz". */ @Override @@ -629,10 +607,14 @@ public final class Descriptors { } /** If this is a nested type, get the outer descriptor, otherwise null. */ - public Descriptor getContainingType() { return containingType; } + public Descriptor getContainingType() { + return containingType; + } /** Get the {@code MessageOptions}, defined in {@code descriptor.proto}. */ - public MessageOptions getOptions() { return proto.getOptions(); } + public MessageOptions getOptions() { + return proto.getOptions(); + } /** Get a list of this message type's fields. */ public List<FieldDescriptor> getFields() { @@ -661,8 +643,7 @@ public final class Descriptors { /** Determines if the given field number is an extension. */ public boolean isExtensionNumber(final int number) { - for (final DescriptorProto.ExtensionRange range : - proto.getExtensionRangeList()) { + for (final DescriptorProto.ExtensionRange range : proto.getExtensionRangeList()) { if (range.getStart() <= number && number < range.getEnd()) { return true; } @@ -672,8 +653,7 @@ public final class Descriptors { /** Determines if the given field number is reserved. */ public boolean isReservedNumber(final int number) { - for (final DescriptorProto.ReservedRange range : - proto.getReservedRangeList()) { + for (final DescriptorProto.ReservedRange range : proto.getReservedRangeList()) { if (range.getStart() <= number && number < range.getEnd()) { return true; } @@ -693,8 +673,8 @@ public final class Descriptors { } /** - * Indicates whether the message can be extended. That is, whether it has - * any "extensions x to y" ranges declared on it. + * Indicates whether the message can be extended. That is, whether it has any "extensions x to + * y" ranges declared on it. */ public boolean isExtendable() { return proto.getExtensionRangeList().size() != 0; @@ -702,14 +682,14 @@ public final class Descriptors { /** * Finds a field by name. + * * @param name The unqualified name of the field (e.g. "foo"). * @return The field's descriptor, or {@code null} if not found. */ public FieldDescriptor findFieldByName(final String name) { - final GenericDescriptor result = - file.pool.findSymbol(fullName + '.' + name); + final GenericDescriptor result = file.pool.findSymbol(fullName + '.' + name); if (result != null && result instanceof FieldDescriptor) { - return (FieldDescriptor)result; + return (FieldDescriptor) result; } else { return null; } @@ -717,24 +697,24 @@ public final class Descriptors { /** * Finds a field by field number. + * * @param number The field number within this message type. * @return The field's descriptor, or {@code null} if not found. */ public FieldDescriptor findFieldByNumber(final int number) { - return file.pool.fieldsByNumber.get( - new DescriptorPool.DescriptorIntPair(this, number)); + return file.pool.fieldsByNumber.get(new DescriptorPool.DescriptorIntPair(this, number)); } /** * Finds a nested message type by name. + * * @param name The unqualified name of the nested type (e.g. "Foo"). * @return The types's descriptor, or {@code null} if not found. */ public Descriptor findNestedTypeByName(final String name) { - final GenericDescriptor result = - file.pool.findSymbol(fullName + '.' + name); + final GenericDescriptor result = file.pool.findSymbol(fullName + '.' + name); if (result != null && result instanceof Descriptor) { - return (Descriptor)result; + return (Descriptor) result; } else { return null; } @@ -742,14 +722,14 @@ public final class Descriptors { /** * Finds a nested enum type by name. + * * @param name The unqualified name of the nested type (e.g. "Foo"). * @return The types's descriptor, or {@code null} if not found. */ public EnumDescriptor findEnumTypeByName(final String name) { - final GenericDescriptor result = - file.pool.findSymbol(fullName + '.' + name); + final GenericDescriptor result = file.pool.findSymbol(fullName + '.' + name); if (result != null && result instanceof EnumDescriptor) { - return (EnumDescriptor)result; + return (EnumDescriptor) result; } else { return null; } @@ -776,9 +756,12 @@ public final class Descriptors { packageName = fullname.substring(0, pos); } this.index = 0; - this.proto = DescriptorProto.newBuilder().setName(name).addExtensionRange( - DescriptorProto.ExtensionRange.newBuilder().setStart(1) - .setEnd(536870912).build()).build(); + this.proto = + DescriptorProto.newBuilder() + .setName(name) + .addExtensionRange( + DescriptorProto.ExtensionRange.newBuilder().setStart(1).setEnd(536870912).build()) + .build(); this.fullName = fullname; this.containingType = null; @@ -792,11 +775,12 @@ public final class Descriptors { this.file = new FileDescriptor(packageName, this); } - private Descriptor(final DescriptorProto proto, - final FileDescriptor file, - final Descriptor parent, - final int index) - throws DescriptorValidationException { + private Descriptor( + final DescriptorProto proto, + final FileDescriptor file, + final Descriptor parent, + final int index) + throws DescriptorValidationException { this.index = index; this.proto = proto; fullName = computeFullName(file, parent, proto.getName()); @@ -805,32 +789,27 @@ public final class Descriptors { oneofs = new OneofDescriptor[proto.getOneofDeclCount()]; for (int i = 0; i < proto.getOneofDeclCount(); i++) { - oneofs[i] = new OneofDescriptor( - proto.getOneofDecl(i), file, this, i); + oneofs[i] = new OneofDescriptor(proto.getOneofDecl(i), file, this, i); } nestedTypes = new Descriptor[proto.getNestedTypeCount()]; for (int i = 0; i < proto.getNestedTypeCount(); i++) { - nestedTypes[i] = new Descriptor( - proto.getNestedType(i), file, this, i); + nestedTypes[i] = new Descriptor(proto.getNestedType(i), file, this, i); } enumTypes = new EnumDescriptor[proto.getEnumTypeCount()]; for (int i = 0; i < proto.getEnumTypeCount(); i++) { - enumTypes[i] = new EnumDescriptor( - proto.getEnumType(i), file, this, i); + enumTypes[i] = new EnumDescriptor(proto.getEnumType(i), file, this, i); } fields = new FieldDescriptor[proto.getFieldCount()]; for (int i = 0; i < proto.getFieldCount(); i++) { - fields[i] = new FieldDescriptor( - proto.getField(i), file, this, i, false); + fields[i] = new FieldDescriptor(proto.getField(i), file, this, i, false); } extensions = new FieldDescriptor[proto.getExtensionCount()]; for (int i = 0; i < proto.getExtensionCount(); i++) { - extensions[i] = new FieldDescriptor( - proto.getExtension(i), file, this, i, true); + extensions[i] = new FieldDescriptor(proto.getExtension(i), file, this, i, true); } for (int i = 0; i < proto.getOneofDeclCount(); i++) { @@ -891,15 +870,16 @@ public final class Descriptors { // ================================================================= /** Describes a field of a message type. */ - public static final class FieldDescriptor - extends GenericDescriptor - implements Comparable<FieldDescriptor>, - FieldSet.FieldDescriptorLite<FieldDescriptor> { + public static final class FieldDescriptor extends GenericDescriptor + implements Comparable<FieldDescriptor>, FieldSet.FieldDescriptorLite<FieldDescriptor> { /** * Get the index of this descriptor within its parent. + * * @see Descriptors.Descriptor#getIndex() */ - public int getIndex() { return index; } + public int getIndex() { + return index; + } /** Convert the descriptor to its protocol message representation. */ @Override @@ -921,6 +901,7 @@ public final class Descriptors { /** * Get the field's fully-qualified name. + * * @see Descriptors.Descriptor#getFullName() */ @Override @@ -934,10 +915,12 @@ public final class Descriptors { } /** - * Get the field's java type. This is just for convenience. Every - * {@code FieldDescriptorProto.Type} maps to exactly one Java type. + * Get the field's java type. This is just for convenience. Every {@code + * FieldDescriptorProto.Type} maps to exactly one Java type. */ - public JavaType getJavaType() { return type.getJavaType(); } + public JavaType getJavaType() { + return type.getJavaType(); + } /** For internal use only. */ @Override @@ -952,7 +935,9 @@ public final class Descriptors { } /** Get the field's declared type. */ - public Type getType() { return type; } + public Type getType() { + return type; + } /** For internal use only. */ @Override @@ -976,15 +961,15 @@ public final class Descriptors { } public boolean isMapField() { - return getType() == Type.MESSAGE && isRepeated() + return getType() == Type.MESSAGE + && isRepeated() && getMessageType().getOptions().getMapEntry(); } // I'm pretty sure values() constructs a new array every time, since there // is nothing stopping the caller from mutating the array. Therefore we // make a static copy here. - private static final WireFormat.FieldType[] table = - WireFormat.FieldType.values(); + private static final WireFormat.FieldType[] table = WireFormat.FieldType.values(); /** Is this field declared required? */ public boolean isRequired() { @@ -1002,8 +987,9 @@ public final class Descriptors { return proto.getLabel() == FieldDescriptorProto.Label.LABEL_REPEATED; } - /** Does this field have the {@code [packed = true]} option or is this field - * packable in proto3 and not explicitly setted to unpacked? + /** + * Does this field have the {@code [packed = true]} option or is this field packable in proto3 + * and not explicitly setted to unpacked? */ @Override public boolean isPacked() { @@ -1023,42 +1009,50 @@ public final class Descriptors { } /** Returns true if the field had an explicitly-defined default value. */ - public boolean hasDefaultValue() { return proto.hasDefaultValue(); } + public boolean hasDefaultValue() { + return proto.hasDefaultValue(); + } /** - * Returns the field's default value. Valid for all types except for - * messages and groups. For all other types, the object returned is of - * the same class that would returned by Message.getField(this). + * Returns the field's default value. Valid for all types except for messages and groups. For + * all other types, the object returned is of the same class that would returned by + * Message.getField(this). */ public Object getDefaultValue() { if (getJavaType() == JavaType.MESSAGE) { throw new UnsupportedOperationException( - "FieldDescriptor.getDefaultValue() called on an embedded message " + - "field."); + "FieldDescriptor.getDefaultValue() called on an embedded message field."); } return defaultValue; } /** Get the {@code FieldOptions}, defined in {@code descriptor.proto}. */ - public FieldOptions getOptions() { return proto.getOptions(); } + public FieldOptions getOptions() { + return proto.getOptions(); + } /** Is this field an extension? */ - public boolean isExtension() { return proto.hasExtendee(); } + public boolean isExtension() { + return proto.hasExtendee(); + } /** - * Get the field's containing type. For extensions, this is the type being - * extended, not the location where the extension was defined. See - * {@link #getExtensionScope()}. + * Get the field's containing type. For extensions, this is the type being extended, not the + * location where the extension was defined. See {@link #getExtensionScope()}. */ - public Descriptor getContainingType() { return containingType; } + public Descriptor getContainingType() { + return containingType; + } /** Get the field's containing oneof. */ - public OneofDescriptor getContainingOneof() { return containingOneof; } + public OneofDescriptor getContainingOneof() { + return containingOneof; + } /** - * For extensions defined nested within message types, gets the outer - * type. Not valid for non-extension fields. For example, consider - * this {@code .proto} file: + * For extensions defined nested within message types, gets the outer type. Not valid for + * non-extension fields. For example, consider this {@code .proto} file: + * * <pre> * message Foo { * extensions 1000 to max; @@ -1072,14 +1066,14 @@ public final class Descriptors { * } * } * </pre> - * Both {@code baz}'s and {@code qux}'s containing type is {@code Foo}. - * However, {@code baz}'s extension scope is {@code null} while - * {@code qux}'s extension scope is {@code Bar}. + * + * Both {@code baz}'s and {@code qux}'s containing type is {@code Foo}. However, {@code baz}'s + * extension scope is {@code null} while {@code qux}'s extension scope is {@code Bar}. */ public Descriptor getExtensionScope() { if (!isExtension()) { throw new UnsupportedOperationException( - "This field is not an extension."); + String.format("This field is not an extension. (%s)", fullName)); } return extensionScope; } @@ -1088,7 +1082,7 @@ public final class Descriptors { public Descriptor getMessageType() { if (getJavaType() != JavaType.MESSAGE) { throw new UnsupportedOperationException( - "This field is not of message type."); + String.format("This field is not of message type. (%s)", fullName)); } return messageType; } @@ -1098,27 +1092,25 @@ public final class Descriptors { public EnumDescriptor getEnumType() { if (getJavaType() != JavaType.ENUM) { throw new UnsupportedOperationException( - "This field is not of enum type."); + String.format("This field is not of enum type. (%s)", fullName)); } return enumType; } /** - * Compare with another {@code FieldDescriptor}. This orders fields in - * "canonical" order, which simply means ascending order by field number. - * {@code other} must be a field of the same type -- i.e. - * {@code getContainingType()} must return the same {@code Descriptor} for - * both fields. + * Compare with another {@code FieldDescriptor}. This orders fields in "canonical" order, which + * simply means ascending order by field number. {@code other} must be a field of the same type + * -- i.e. {@code getContainingType()} must return the same {@code Descriptor} for both fields. * - * @return negative, zero, or positive if {@code this} is less than, - * equal to, or greater than {@code other}, respectively. + * @return negative, zero, or positive if {@code this} is less than, equal to, or greater than + * {@code other}, respectively. */ @Override public int compareTo(final FieldDescriptor other) { if (other.containingType != containingType) { throw new IllegalArgumentException( - "FieldDescriptors can only be compared to other FieldDescriptors " + - "for fields of the same message type."); + "FieldDescriptors can only be compared to other FieldDescriptors " + + "for fields of the same message type."); } return getNumber() - other.getNumber(); } @@ -1145,24 +1137,24 @@ public final class Descriptors { private Object defaultValue; public enum Type { - DOUBLE (JavaType.DOUBLE ), - FLOAT (JavaType.FLOAT ), - INT64 (JavaType.LONG ), - UINT64 (JavaType.LONG ), - INT32 (JavaType.INT ), - FIXED64 (JavaType.LONG ), - FIXED32 (JavaType.INT ), - BOOL (JavaType.BOOLEAN ), - STRING (JavaType.STRING ), - GROUP (JavaType.MESSAGE ), - MESSAGE (JavaType.MESSAGE ), - BYTES (JavaType.BYTE_STRING), - UINT32 (JavaType.INT ), - ENUM (JavaType.ENUM ), - SFIXED32(JavaType.INT ), - SFIXED64(JavaType.LONG ), - SINT32 (JavaType.INT ), - SINT64 (JavaType.LONG ); + DOUBLE(JavaType.DOUBLE), + FLOAT(JavaType.FLOAT), + INT64(JavaType.LONG), + UINT64(JavaType.LONG), + INT32(JavaType.INT), + FIXED64(JavaType.LONG), + FIXED32(JavaType.INT), + BOOL(JavaType.BOOLEAN), + STRING(JavaType.STRING), + GROUP(JavaType.MESSAGE), + MESSAGE(JavaType.MESSAGE), + BYTES(JavaType.BYTE_STRING), + UINT32(JavaType.INT), + ENUM(JavaType.ENUM), + SFIXED32(JavaType.INT), + SFIXED64(JavaType.LONG), + SINT32(JavaType.INT), + SINT64(JavaType.LONG); Type(final JavaType javaType) { this.javaType = javaType; @@ -1173,7 +1165,10 @@ public final class Descriptors { public FieldDescriptorProto.Type toProto() { return FieldDescriptorProto.Type.forNumber(ordinal() + 1); } - public JavaType getJavaType() { return javaType; } + + public JavaType getJavaType() { + return javaType; + } public static Type valueOf(final FieldDescriptorProto.Type type) { return values()[type.getNumber() - 1]; @@ -1183,9 +1178,8 @@ public final class Descriptors { static { // Refuse to init if someone added a new declared type. if (Type.values().length != FieldDescriptorProto.Type.values().length) { - throw new RuntimeException("" - + "descriptor.proto has a new declared type but Descriptors.java " - + "wasn't updated."); + throw new RuntimeException( + "descriptor.proto has a new declared type but Descriptors.java wasn't updated."); } } @@ -1205,8 +1199,8 @@ public final class Descriptors { } /** - * The default default value for fields of this type, if it's a primitive - * type. This is meant for use inside this file only, hence is private. + * The default default value for fields of this type, if it's a primitive type. This is meant + * for use inside this file only, hence is private. */ private final Object defaultDefault; } @@ -1230,12 +1224,13 @@ public final class Descriptors { return result.toString(); } - private FieldDescriptor(final FieldDescriptorProto proto, - final FileDescriptor file, - final Descriptor parent, - final int index, - final boolean isExtension) - throws DescriptorValidationException { + private FieldDescriptor( + final FieldDescriptorProto proto, + final FileDescriptor file, + final Descriptor parent, + final int index, + final boolean isExtension) + throws DescriptorValidationException { this.index = index; this.proto = proto; fullName = computeFullName(file, parent, proto.getName()); @@ -1251,16 +1246,15 @@ public final class Descriptors { } if (getNumber() <= 0) { - throw new DescriptorValidationException(this, - "Field numbers must be positive integers."); + throw new DescriptorValidationException(this, "Field numbers must be positive integers."); } if (isExtension) { if (!proto.hasExtendee()) { - throw new DescriptorValidationException(this, - "FieldDescriptorProto.extendee not set for extension field."); + throw new DescriptorValidationException( + this, "FieldDescriptorProto.extendee not set for extension field."); } - containingType = null; // Will be filled in when cross-linking + containingType = null; // Will be filled in when cross-linking if (parent != null) { extensionScope = parent; } else { @@ -1268,23 +1262,23 @@ public final class Descriptors { } if (proto.hasOneofIndex()) { - throw new DescriptorValidationException(this, - "FieldDescriptorProto.oneof_index set for extension field."); + throw new DescriptorValidationException( + this, "FieldDescriptorProto.oneof_index set for extension field."); } containingOneof = null; } else { if (proto.hasExtendee()) { - throw new DescriptorValidationException(this, - "FieldDescriptorProto.extendee set for non-extension field."); + throw new DescriptorValidationException( + this, "FieldDescriptorProto.extendee set for non-extension field."); } containingType = parent; if (proto.hasOneofIndex()) { - if (proto.getOneofIndex() < 0 || - proto.getOneofIndex() >= parent.toProto().getOneofDeclCount()) { - throw new DescriptorValidationException(this, - "FieldDescriptorProto.oneof_index is out of range for type " - + parent.getName()); + if (proto.getOneofIndex() < 0 + || proto.getOneofIndex() >= parent.toProto().getOneofDeclCount()) { + throw new DescriptorValidationException( + this, + "FieldDescriptorProto.oneof_index is out of range for type " + parent.getName()); } containingOneof = parent.getOneofs().get(proto.getOneofIndex()); containingOneof.fieldCount++; @@ -1301,26 +1295,29 @@ public final class Descriptors { private void crossLink() throws DescriptorValidationException { if (proto.hasExtendee()) { final GenericDescriptor extendee = - file.pool.lookupSymbol(proto.getExtendee(), this, - DescriptorPool.SearchFilter.TYPES_ONLY); + file.pool.lookupSymbol( + proto.getExtendee(), this, DescriptorPool.SearchFilter.TYPES_ONLY); if (!(extendee instanceof Descriptor)) { - throw new DescriptorValidationException(this, - '\"' + proto.getExtendee() + "\" is not a message type."); + throw new DescriptorValidationException( + this, '\"' + proto.getExtendee() + "\" is not a message type."); } - containingType = (Descriptor)extendee; + containingType = (Descriptor) extendee; if (!getContainingType().isExtensionNumber(getNumber())) { - throw new DescriptorValidationException(this, - '\"' + getContainingType().getFullName() + - "\" does not declare " + getNumber() + - " as an extension number."); + throw new DescriptorValidationException( + this, + '\"' + + getContainingType().getFullName() + + "\" does not declare " + + getNumber() + + " as an extension number."); } } if (proto.hasTypeName()) { final GenericDescriptor typeDescriptor = - file.pool.lookupSymbol(proto.getTypeName(), this, - DescriptorPool.SearchFilter.TYPES_ONLY); + file.pool.lookupSymbol( + proto.getTypeName(), this, DescriptorPool.SearchFilter.TYPES_ONLY); if (!proto.hasType()) { // Choose field type based on symbol. @@ -1329,53 +1326,49 @@ public final class Descriptors { } else if (typeDescriptor instanceof EnumDescriptor) { type = Type.ENUM; } else { - throw new DescriptorValidationException(this, - '\"' + proto.getTypeName() + "\" is not a type."); + throw new DescriptorValidationException( + this, '\"' + proto.getTypeName() + "\" is not a type."); } } if (getJavaType() == JavaType.MESSAGE) { if (!(typeDescriptor instanceof Descriptor)) { - throw new DescriptorValidationException(this, - '\"' + proto.getTypeName() + "\" is not a message type."); + throw new DescriptorValidationException( + this, '\"' + proto.getTypeName() + "\" is not a message type."); } - messageType = (Descriptor)typeDescriptor; + messageType = (Descriptor) typeDescriptor; if (proto.hasDefaultValue()) { - throw new DescriptorValidationException(this, - "Messages can't have default values."); + throw new DescriptorValidationException(this, "Messages can't have default values."); } } else if (getJavaType() == JavaType.ENUM) { if (!(typeDescriptor instanceof EnumDescriptor)) { - throw new DescriptorValidationException(this, - '\"' + proto.getTypeName() + "\" is not an enum type."); + throw new DescriptorValidationException( + this, '\"' + proto.getTypeName() + "\" is not an enum type."); } - enumType = (EnumDescriptor)typeDescriptor; + enumType = (EnumDescriptor) typeDescriptor; } else { - throw new DescriptorValidationException(this, - "Field with primitive type has type_name."); + throw new DescriptorValidationException(this, "Field with primitive type has type_name."); } } else { - if (getJavaType() == JavaType.MESSAGE || - getJavaType() == JavaType.ENUM) { - throw new DescriptorValidationException(this, - "Field with message or enum type missing type_name."); + if (getJavaType() == JavaType.MESSAGE || getJavaType() == JavaType.ENUM) { + throw new DescriptorValidationException( + this, "Field with message or enum type missing type_name."); } } // Only repeated primitive fields may be packed. if (proto.getOptions().getPacked() && !isPackable()) { - throw new DescriptorValidationException(this, - "[packed = true] can only be specified for repeated primitive " + - "fields."); + throw new DescriptorValidationException( + this, "[packed = true] can only be specified for repeated primitive fields."); } // We don't attempt to parse the default value until here because for // enums we need the enum type's descriptor. if (proto.hasDefaultValue()) { if (isRepeated()) { - throw new DescriptorValidationException(this, - "Repeated fields cannot have default values."); + throw new DescriptorValidationException( + this, "Repeated fields cannot have default values."); } try { @@ -1428,30 +1421,26 @@ public final class Descriptors { break; case BYTES: try { - defaultValue = - TextFormat.unescapeBytes(proto.getDefaultValue()); + defaultValue = TextFormat.unescapeBytes(proto.getDefaultValue()); } catch (TextFormat.InvalidEscapeSequenceException e) { - throw new DescriptorValidationException(this, - "Couldn't parse default value: " + e.getMessage(), e); + throw new DescriptorValidationException( + this, "Couldn't parse default value: " + e.getMessage(), e); } break; case ENUM: defaultValue = enumType.findValueByName(proto.getDefaultValue()); if (defaultValue == null) { - throw new DescriptorValidationException(this, - "Unknown enum default value: \"" + - proto.getDefaultValue() + '\"'); + throw new DescriptorValidationException( + this, "Unknown enum default value: \"" + proto.getDefaultValue() + '\"'); } break; case MESSAGE: case GROUP: - throw new DescriptorValidationException(this, - "Message type had default value."); + throw new DescriptorValidationException(this, "Message type had default value."); } } catch (NumberFormatException e) { - throw new DescriptorValidationException(this, - "Could not parse default value: \"" + - proto.getDefaultValue() + '\"', e); + throw new DescriptorValidationException( + this, "Could not parse default value: \"" + proto.getDefaultValue() + '\"', e); } } else { // Determine the default default for this field. @@ -1478,16 +1467,15 @@ public final class Descriptors { file.pool.addFieldByNumber(this); } - if (containingType != null && - containingType.getOptions().getMessageSetWireFormat()) { + if (containingType != null && containingType.getOptions().getMessageSetWireFormat()) { if (isExtension()) { if (!isOptional() || getType() != Type.MESSAGE) { - throw new DescriptorValidationException(this, - "Extensions of MessageSets must be optional messages."); + throw new DescriptorValidationException( + this, "Extensions of MessageSets must be optional messages."); } } else { - throw new DescriptorValidationException(this, - "MessageSets cannot have fields, only extensions."); + throw new DescriptorValidationException( + this, "MessageSets cannot have fields, only extensions."); } } } @@ -1497,10 +1485,7 @@ public final class Descriptors { this.proto = proto; } - /** - * For internal use only. This is to satisfy the FieldDescriptorLite - * interface. - */ + /** For internal use only. This is to satisfy the FieldDescriptorLite interface. */ @Override public MessageLite.Builder internalMergeFrom(MessageLite.Builder to, MessageLite from) { // FieldDescriptors are only used with non-lite messages so we can just @@ -1517,9 +1502,12 @@ public final class Descriptors { implements Internal.EnumLiteMap<EnumValueDescriptor> { /** * Get the index of this descriptor within its parent. + * * @see Descriptors.Descriptor#getIndex() */ - public int getIndex() { return index; } + public int getIndex() { + return index; + } /** Convert the descriptor to its protocol message representation. */ @Override @@ -1535,6 +1523,7 @@ public final class Descriptors { /** * Get the type's fully-qualified name. + * * @see Descriptors.Descriptor#getFullName() */ @Override @@ -1549,10 +1538,14 @@ public final class Descriptors { } /** If this is a nested type, get the outer descriptor, otherwise null. */ - public Descriptor getContainingType() { return containingType; } + public Descriptor getContainingType() { + return containingType; + } /** Get the {@code EnumOptions}, defined in {@code descriptor.proto}. */ - public EnumOptions getOptions() { return proto.getOptions(); } + public EnumOptions getOptions() { + return proto.getOptions(); + } /** Get a list of defined values for this enum. */ public List<EnumValueDescriptor> getValues() { @@ -1561,34 +1554,34 @@ public final class Descriptors { /** * Find an enum value by name. + * * @param name The unqualified name of the value (e.g. "FOO"). * @return the value's descriptor, or {@code null} if not found. */ public EnumValueDescriptor findValueByName(final String name) { - final GenericDescriptor result = - file.pool.findSymbol(fullName + '.' + name); + final GenericDescriptor result = file.pool.findSymbol(fullName + '.' + name); if (result != null && result instanceof EnumValueDescriptor) { - return (EnumValueDescriptor)result; + return (EnumValueDescriptor) result; } else { return null; } } /** - * Find an enum value by number. If multiple enum values have the same - * number, this returns the first defined value with that number. + * Find an enum value by number. If multiple enum values have the same number, this returns the + * first defined value with that number. + * * @param number The value's number. * @return the value's descriptor, or {@code null} if not found. */ @Override public EnumValueDescriptor findValueByNumber(final int number) { - return file.pool.enumValuesByNumber.get( - new DescriptorPool.DescriptorIntPair(this, number)); + return file.pool.enumValuesByNumber.get(new DescriptorPool.DescriptorIntPair(this, number)); } /** - * Get the enum value for a number. If no enum value has this number, - * construct an EnumValueDescriptor for it. + * Get the enum value for a number. If no enum value has this number, construct an + * EnumValueDescriptor for it. */ public EnumValueDescriptor findValueByNumberCreatingIfUnknown(final int number) { EnumValueDescriptor result = findValueByNumber(number); @@ -1653,11 +1646,12 @@ public final class Descriptors { private final WeakHashMap<Integer, WeakReference<EnumValueDescriptor>> unknownValues = new WeakHashMap<Integer, WeakReference<EnumValueDescriptor>>(); - private EnumDescriptor(final EnumDescriptorProto proto, - final FileDescriptor file, - final Descriptor parent, - final int index) - throws DescriptorValidationException { + 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()); @@ -1667,14 +1661,12 @@ public final class Descriptors { 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."); + 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); + values[i] = new EnumValueDescriptor(proto.getValue(i), file, this, i); } file.pool.addSymbol(this); @@ -1693,18 +1685,20 @@ public final class Descriptors { // ================================================================= /** - * Describes one value within an enum type. Note that multiple defined - * values may have the same number. In generated Java code, all values - * with the same number after the first become aliases of the first. - * However, they still have independent EnumValueDescriptors. + * Describes one value within an enum type. Note that multiple defined values may have the same + * number. In generated Java code, all values with the same number after the first become aliases + * of the first. However, they still have independent EnumValueDescriptors. */ public static final class EnumValueDescriptor extends GenericDescriptor implements Internal.EnumLite { /** * Get the index of this descriptor within its parent. + * * @see Descriptors.Descriptor#getIndex() */ - public int getIndex() { return index; } + public int getIndex() { + return index; + } /** Convert the descriptor to its protocol message representation. */ @Override @@ -1725,10 +1719,13 @@ public final class Descriptors { } @Override - public String toString() { return proto.getName(); } + public String toString() { + return proto.getName(); + } /** * Get the value's fully-qualified name. + * * @see Descriptors.Descriptor#getFullName() */ @Override @@ -1743,12 +1740,14 @@ public final class Descriptors { } /** Get the value's enum type. */ - public EnumDescriptor getType() { return type; } + public EnumDescriptor getType() { + return type; + } - /** - * Get the {@code EnumValueOptions}, defined in {@code descriptor.proto}. - */ - public EnumValueOptions getOptions() { return proto.getOptions(); } + /** Get the {@code EnumValueOptions}, defined in {@code descriptor.proto}. */ + public EnumValueOptions getOptions() { + return proto.getOptions(); + } private final int index; private EnumValueDescriptorProto proto; @@ -1756,11 +1755,12 @@ public final class Descriptors { private final FileDescriptor file; private final EnumDescriptor type; - private EnumValueDescriptor(final EnumValueDescriptorProto proto, - final FileDescriptor file, - final EnumDescriptor parent, - final int index) - throws DescriptorValidationException { + private EnumValueDescriptor( + final EnumValueDescriptorProto proto, + final FileDescriptor file, + final EnumDescriptor parent, + final int index) + throws DescriptorValidationException { this.index = index; this.proto = proto; this.file = file; @@ -1775,12 +1775,10 @@ public final class Descriptors { private Integer number; // Create an unknown enum value. private EnumValueDescriptor( - final FileDescriptor file, - final EnumDescriptor parent, - final Integer number) { + final FileDescriptor file, final EnumDescriptor parent, final Integer number) { String name = "UNKNOWN_ENUM_VALUE_" + parent.getName() + "_" + number; - EnumValueDescriptorProto proto = EnumValueDescriptorProto - .newBuilder().setName(name).setNumber(number).build(); + EnumValueDescriptorProto proto = + EnumValueDescriptorProto.newBuilder().setName(name).setNumber(number).build(); this.index = -1; this.proto = proto; this.file = file; @@ -1802,10 +1800,11 @@ public final class Descriptors { /** Describes a service type. */ public static final class ServiceDescriptor extends GenericDescriptor { /** - * Get the index of this descriptor within its parent. - * * @see Descriptors.Descriptor#getIndex() + * Get the index of this descriptor within its parent. * @see Descriptors.Descriptor#getIndex() */ - public int getIndex() { return index; } + public int getIndex() { + return index; + } /** Convert the descriptor to its protocol message representation. */ @Override @@ -1821,6 +1820,7 @@ public final class Descriptors { /** * Get the type's fully-qualified name. + * * @see Descriptors.Descriptor#getFullName() */ @Override @@ -1835,7 +1835,9 @@ public final class Descriptors { } /** Get the {@code ServiceOptions}, defined in {@code descriptor.proto}. */ - public ServiceOptions getOptions() { return proto.getOptions(); } + public ServiceOptions getOptions() { + return proto.getOptions(); + } /** Get a list of methods for this service. */ public List<MethodDescriptor> getMethods() { @@ -1844,14 +1846,14 @@ public final class Descriptors { /** * Find a method by name. + * * @param name The unqualified name of the method (e.g. "Foo"). * @return the method's descriptor, or {@code null} if not found. */ public MethodDescriptor findMethodByName(final String name) { - final GenericDescriptor result = - file.pool.findSymbol(fullName + '.' + name); + final GenericDescriptor result = file.pool.findSymbol(fullName + '.' + name); if (result != null && result instanceof MethodDescriptor) { - return (MethodDescriptor)result; + return (MethodDescriptor) result; } else { return null; } @@ -1863,10 +1865,9 @@ public final class Descriptors { private final FileDescriptor file; private MethodDescriptor[] methods; - private ServiceDescriptor(final ServiceDescriptorProto proto, - final FileDescriptor file, - final int index) - throws DescriptorValidationException { + private ServiceDescriptor( + final ServiceDescriptorProto proto, final FileDescriptor file, final int index) + throws DescriptorValidationException { this.index = index; this.proto = proto; fullName = computeFullName(file, null, proto.getName()); @@ -1874,8 +1875,7 @@ public final class Descriptors { methods = new MethodDescriptor[proto.getMethodCount()]; for (int i = 0; i < proto.getMethodCount(); i++) { - methods[i] = new MethodDescriptor( - proto.getMethod(i), file, this, i); + methods[i] = new MethodDescriptor(proto.getMethod(i), file, this, i); } file.pool.addSymbol(this); @@ -1899,15 +1899,14 @@ public final class Descriptors { // ================================================================= - /** - * Describes one method within a service type. - */ + /** Describes one method within a service type. */ public static final class MethodDescriptor extends GenericDescriptor { /** - * Get the index of this descriptor within its parent. - * * @see Descriptors.Descriptor#getIndex() + * Get the index of this descriptor within its parent. * @see Descriptors.Descriptor#getIndex() */ - public int getIndex() { return index; } + public int getIndex() { + return index; + } /** Convert the descriptor to its protocol message representation. */ @Override @@ -1923,6 +1922,7 @@ public final class Descriptors { /** * Get the method's fully-qualified name. + * * @see Descriptors.Descriptor#getFullName() */ @Override @@ -1937,18 +1937,24 @@ public final class Descriptors { } /** Get the method's service type. */ - public ServiceDescriptor getService() { return service; } + public ServiceDescriptor getService() { + return service; + } /** Get the method's input type. */ - public Descriptor getInputType() { return inputType; } + public Descriptor getInputType() { + return inputType; + } /** Get the method's output type. */ - public Descriptor getOutputType() { return outputType; } + public Descriptor getOutputType() { + return outputType; + } - /** - * Get the {@code MethodOptions}, defined in {@code descriptor.proto}. - */ - public MethodOptions getOptions() { return proto.getOptions(); } + /** Get the {@code MethodOptions}, defined in {@code descriptor.proto}. */ + public MethodOptions getOptions() { + return proto.getOptions(); + } private final int index; private MethodDescriptorProto proto; @@ -1960,11 +1966,12 @@ public final class Descriptors { private Descriptor inputType; private Descriptor outputType; - private MethodDescriptor(final MethodDescriptorProto proto, - final FileDescriptor file, - final ServiceDescriptor parent, - final int index) - throws DescriptorValidationException { + private MethodDescriptor( + final MethodDescriptorProto proto, + final FileDescriptor file, + final ServiceDescriptor parent, + final int index) + throws DescriptorValidationException { this.index = index; this.proto = proto; this.file = file; @@ -1977,22 +1984,22 @@ public final class Descriptors { private void crossLink() throws DescriptorValidationException { final GenericDescriptor input = - file.pool.lookupSymbol(proto.getInputType(), this, - DescriptorPool.SearchFilter.TYPES_ONLY); + file.pool.lookupSymbol( + proto.getInputType(), this, DescriptorPool.SearchFilter.TYPES_ONLY); if (!(input instanceof Descriptor)) { - throw new DescriptorValidationException(this, - '\"' + proto.getInputType() + "\" is not a message type."); + throw new DescriptorValidationException( + this, '\"' + proto.getInputType() + "\" is not a message type."); } - inputType = (Descriptor)input; + inputType = (Descriptor) input; final GenericDescriptor output = - file.pool.lookupSymbol(proto.getOutputType(), this, - DescriptorPool.SearchFilter.TYPES_ONLY); + file.pool.lookupSymbol( + proto.getOutputType(), this, DescriptorPool.SearchFilter.TYPES_ONLY); if (!(output instanceof Descriptor)) { - throw new DescriptorValidationException(this, - '\"' + proto.getOutputType() + "\" is not a message type."); + throw new DescriptorValidationException( + this, '\"' + proto.getOutputType() + "\" is not a message type."); } - outputType = (Descriptor)output; + outputType = (Descriptor) output; } /** See {@link FileDescriptor#setProto}. */ @@ -2003,9 +2010,8 @@ public final class Descriptors { // ================================================================= - private static String computeFullName(final FileDescriptor file, - final Descriptor parent, - final String name) { + private static String computeFullName( + final FileDescriptor file, final Descriptor parent, final String name) { if (parent != null) { return parent.getFullName() + '.' + name; } else if (file.getPackage().length() > 0) { @@ -2018,47 +2024,47 @@ public final class Descriptors { // ================================================================= /** - * All descriptors implement this to make it easier to implement tools like - * {@code DescriptorPool}.<p> + * All descriptors implement this to make it easier to implement tools like {@code + * DescriptorPool}. * - * This class is public so that the methods it exposes can be called from - * outside of this package. However, it should only be subclassed from - * nested classes of Descriptors. + * <p>This class is public so that the methods it exposes can be called from outside of this + * package. However, it should only be subclassed from nested classes of Descriptors. */ public abstract static class GenericDescriptor { public abstract Message toProto(); + public abstract String getName(); + public abstract String getFullName(); + public abstract FileDescriptor getFile(); } - /** - * Thrown when building descriptors fails because the source DescriptorProtos - * are not valid. - */ + /** Thrown when building descriptors fails because the source DescriptorProtos are not valid. */ public static class DescriptorValidationException extends Exception { private static final long serialVersionUID = 5750205775490483148L; /** Gets the full name of the descriptor where the error occurred. */ - public String getProblemSymbolName() { return name; } + public String getProblemSymbolName() { + return name; + } - /** - * Gets the protocol message representation of the invalid descriptor. - */ - public Message getProblemProto() { return proto; } + /** Gets the protocol message representation of the invalid descriptor. */ + public Message getProblemProto() { + return proto; + } - /** - * Gets a human-readable description of the error. - */ - public String getDescription() { return description; } + /** Gets a human-readable description of the error. */ + public String getDescription() { + return description; + } private final String name; private final Message proto; private final String description; private DescriptorValidationException( - final GenericDescriptor problemDescriptor, - final String description) { + final GenericDescriptor problemDescriptor, final String description) { super(problemDescriptor.getFullName() + ": " + description); // Note that problemDescriptor may be partially uninitialized, so we @@ -2078,8 +2084,7 @@ public final class Descriptors { } private DescriptorValidationException( - final FileDescriptor problemDescriptor, - final String description) { + final FileDescriptor problemDescriptor, final String description) { super(problemDescriptor.getName() + ": " + description); // Note that problemDescriptor may be partially uninitialized, so we @@ -2094,19 +2099,19 @@ public final class Descriptors { // ================================================================= /** - * A private helper class which contains lookup tables containing all the - * descriptors defined in a particular file. + * A private helper class which contains lookup tables containing all the descriptors defined in a + * particular file. */ private static final class DescriptorPool { - /** Defines what subclass of descriptors to search in the descriptor pool. - */ + /** Defines what subclass of descriptors to search in the descriptor pool. */ enum SearchFilter { - TYPES_ONLY, AGGREGATES_ONLY, ALL_SYMBOLS + TYPES_ONLY, + AGGREGATES_ONLY, + ALL_SYMBOLS } - DescriptorPool(final FileDescriptor[] dependencies, - boolean allowUnknownDependencies) { + DescriptorPool(final FileDescriptor[] dependencies, boolean allowUnknownDependencies) { this.dependencies = new HashSet<FileDescriptor>(); this.allowUnknownDependencies = allowUnknownDependencies; @@ -2127,7 +2132,7 @@ public final class Descriptors { } } - /** Find and put public dependencies of the file into dependencies set.*/ + /** Find and put public dependencies of the file into dependencies set. */ private void importPublicDependencies(final FileDescriptor file) { for (FileDescriptor dependency : file.getPublicDependencies()) { if (dependencies.add(dependency)) { @@ -2140,27 +2145,27 @@ public final class Descriptors { private boolean allowUnknownDependencies; private final Map<String, GenericDescriptor> descriptorsByName = - new HashMap<String, GenericDescriptor>(); + new HashMap<String, GenericDescriptor>(); private final Map<DescriptorIntPair, FieldDescriptor> fieldsByNumber = - new HashMap<DescriptorIntPair, FieldDescriptor>(); - private final Map<DescriptorIntPair, EnumValueDescriptor> enumValuesByNumber - = new HashMap<DescriptorIntPair, EnumValueDescriptor>(); + new HashMap<DescriptorIntPair, FieldDescriptor>(); + private final Map<DescriptorIntPair, EnumValueDescriptor> enumValuesByNumber = + new HashMap<DescriptorIntPair, EnumValueDescriptor>(); /** Find a generic descriptor by fully-qualified name. */ GenericDescriptor findSymbol(final String fullName) { return findSymbol(fullName, SearchFilter.ALL_SYMBOLS); } - /** Find a descriptor by fully-qualified name and given option to only - * search valid field type descriptors. + /** + * Find a descriptor by fully-qualified name and given option to only search valid field type + * descriptors. */ - GenericDescriptor findSymbol(final String fullName, - final SearchFilter filter) { + GenericDescriptor findSymbol(final String fullName, final SearchFilter filter) { GenericDescriptor result = descriptorsByName.get(fullName); if (result != null) { - if ((filter==SearchFilter.ALL_SYMBOLS) || - ((filter==SearchFilter.TYPES_ONLY) && isType(result)) || - ((filter==SearchFilter.AGGREGATES_ONLY) && isAggregate(result))) { + if ((filter == SearchFilter.ALL_SYMBOLS) + || ((filter == SearchFilter.TYPES_ONLY) && isType(result)) + || ((filter == SearchFilter.AGGREGATES_ONLY) && isAggregate(result))) { return result; } } @@ -2168,9 +2173,9 @@ public final class Descriptors { for (final FileDescriptor dependency : dependencies) { result = dependency.pool.descriptorsByName.get(fullName); if (result != null) { - if ((filter==SearchFilter.ALL_SYMBOLS) || - ((filter==SearchFilter.TYPES_ONLY) && isType(result)) || - ((filter==SearchFilter.AGGREGATES_ONLY) && isAggregate(result))) { + if ((filter == SearchFilter.ALL_SYMBOLS) + || ((filter == SearchFilter.TYPES_ONLY) && isType(result)) + || ((filter == SearchFilter.AGGREGATES_ONLY) && isAggregate(result))) { return result; } } @@ -2181,28 +2186,27 @@ public final class Descriptors { /** Checks if the descriptor is a valid type for a message field. */ boolean isType(GenericDescriptor descriptor) { - return (descriptor instanceof Descriptor) || - (descriptor instanceof EnumDescriptor); + return (descriptor instanceof Descriptor) || (descriptor instanceof EnumDescriptor); } /** Checks if the descriptor is a valid namespace type. */ boolean isAggregate(GenericDescriptor descriptor) { - return (descriptor instanceof Descriptor) || - (descriptor instanceof EnumDescriptor) || - (descriptor instanceof PackageDescriptor) || - (descriptor instanceof ServiceDescriptor); + return (descriptor instanceof Descriptor) + || (descriptor instanceof EnumDescriptor) + || (descriptor instanceof PackageDescriptor) + || (descriptor instanceof ServiceDescriptor); } /** - * Look up a type descriptor by name, relative to some other descriptor. - * The name may be fully-qualified (with a leading '.'), - * partially-qualified, or unqualified. C++-like name lookup semantics - * are used to search for the matching descriptor. + * Look up a type descriptor by name, relative to some other descriptor. The name may be + * fully-qualified (with a leading '.'), partially-qualified, or unqualified. C++-like name + * lookup semantics are used to search for the matching descriptor. */ - GenericDescriptor lookupSymbol(final String name, - final GenericDescriptor relativeTo, - final DescriptorPool.SearchFilter filter) - throws DescriptorValidationException { + GenericDescriptor lookupSymbol( + final String name, + final GenericDescriptor relativeTo, + final DescriptorPool.SearchFilter filter) + throws DescriptorValidationException { // TODO(kenton): This could be optimized in a number of ways. GenericDescriptor result; @@ -2235,8 +2239,7 @@ public final class Descriptors { // We will search each parent scope of "relativeTo" looking for the // symbol. - final StringBuilder scopeToTry = - new StringBuilder(relativeTo.getFullName()); + final StringBuilder scopeToTry = new StringBuilder(relativeTo.getFullName()); while (true) { // Chop off the last component of the scope. @@ -2250,8 +2253,7 @@ public final class Descriptors { // Append firstPart and try to find scopeToTry.append(firstPart); - result = findSymbol(scopeToTry.toString(), - DescriptorPool.SearchFilter.AGGREGATES_ONLY); + result = findSymbol(scopeToTry.toString(), DescriptorPool.SearchFilter.AGGREGATES_ONLY); if (result != null) { if (firstPartLength != -1) { @@ -2274,22 +2276,23 @@ public final class Descriptors { if (result == null) { if (allowUnknownDependencies && filter == SearchFilter.TYPES_ONLY) { - logger.warning("The descriptor for message type \"" + name + - "\" can not be found and a placeholder is created for it"); + logger.warning( + "The descriptor for message type \"" + + name + + "\" can not be found and a placeholder is created for it"); // We create a dummy message descriptor here regardless of the // expected type. If the type should be message, this dummy // descriptor will work well and if the type should be enum, a // DescriptorValidationException will be thrown latter. In either // case, the code works as expected: we allow unknown message types - // but not unknwon enum types. + // but not unknown enum types. result = new Descriptor(fullname); // Add the placeholder file as a dependency so we can find the // placeholder symbol when resolving other references. this.dependencies.add(result.getFile()); return result; } else { - throw new DescriptorValidationException(relativeTo, - '\"' + name + "\" is not defined."); + throw new DescriptorValidationException(relativeTo, '\"' + name + "\" is not defined."); } } else { return result; @@ -2297,11 +2300,10 @@ public final class Descriptors { } /** - * Adds a symbol to the symbol table. If a symbol with the same name - * already exists, throws an error. + * Adds a symbol to the symbol table. If a symbol with the same name already exists, throws an + * error. */ - void addSymbol(final GenericDescriptor descriptor) - throws DescriptorValidationException { + void addSymbol(final GenericDescriptor descriptor) throws DescriptorValidationException { validateSymbolName(descriptor); final String fullName = descriptor.getFullName(); @@ -2313,47 +2315,56 @@ public final class Descriptors { if (descriptor.getFile() == old.getFile()) { if (dotpos == -1) { - throw new DescriptorValidationException(descriptor, - '\"' + fullName + "\" is already defined."); + throw new DescriptorValidationException( + descriptor, '\"' + fullName + "\" is already defined."); } else { - throw new DescriptorValidationException(descriptor, - '\"' + fullName.substring(dotpos + 1) + - "\" is already defined in \"" + - fullName.substring(0, dotpos) + "\"."); + throw new DescriptorValidationException( + descriptor, + '\"' + + fullName.substring(dotpos + 1) + + "\" is already defined in \"" + + fullName.substring(0, dotpos) + + "\"."); } } else { - throw new DescriptorValidationException(descriptor, - '\"' + fullName + "\" is already defined in file \"" + - old.getFile().getName() + "\"."); + throw new DescriptorValidationException( + descriptor, + '\"' + + fullName + + "\" is already defined in file \"" + + old.getFile().getName() + + "\"."); } } } /** - * Represents a package in the symbol table. We use PackageDescriptors - * just as placeholders so that someone cannot define, say, a message type - * that has the same name as an existing package. + * Represents a package in the symbol table. We use PackageDescriptors just as placeholders so + * that someone cannot define, say, a message type that has the same name as an existing + * package. */ private static final class PackageDescriptor extends GenericDescriptor { @Override public Message toProto() { return file.toProto(); } + @Override public String getName() { return name; } + @Override public String getFullName() { return fullName; } + @Override public FileDescriptor getFile() { return file; } - PackageDescriptor(final String name, final String fullName, - final FileDescriptor file) { + PackageDescriptor(final String name, final String fullName, final FileDescriptor file) { this.file = file; this.fullName = fullName; this.name = name; @@ -2365,13 +2376,12 @@ public final class Descriptors { } /** - * Adds a package to the symbol tables. If a package by the same name - * already exists, that is fine, but if some other kind of symbol exists - * under the same name, an exception is thrown. If the package has - * multiple components, this also adds the parent package(s). + * Adds a package to the symbol tables. If a package by the same name already exists, that is + * fine, but if some other kind of symbol exists under the same name, an exception is thrown. If + * the package has multiple components, this also adds the parent package(s). */ void addPackage(final String fullName, final FileDescriptor file) - throws DescriptorValidationException { + throws DescriptorValidationException { final int dotpos = fullName.lastIndexOf('.'); final String name; if (dotpos == -1) { @@ -2382,14 +2392,18 @@ public final class Descriptors { } final GenericDescriptor old = - descriptorsByName.put(fullName, - new PackageDescriptor(name, fullName, file)); + descriptorsByName.put(fullName, new PackageDescriptor(name, fullName, file)); if (old != null) { descriptorsByName.put(fullName, old); if (!(old instanceof PackageDescriptor)) { - throw new DescriptorValidationException(file, - '\"' + name + "\" is already defined (as something other than a " - + "package) in file \"" + old.getFile().getName() + "\"."); + throw new DescriptorValidationException( + file, + '\"' + + name + + "\" is already defined (as something other than a " + + "package) in file \"" + + old.getFile().getName() + + "\"."); } } } @@ -2408,43 +2422,46 @@ public final class Descriptors { public int hashCode() { return descriptor.hashCode() * ((1 << 16) - 1) + number; } + @Override public boolean equals(final Object obj) { if (!(obj instanceof DescriptorIntPair)) { return false; } - final DescriptorIntPair other = (DescriptorIntPair)obj; + final DescriptorIntPair other = (DescriptorIntPair) obj; return descriptor == other.descriptor && number == other.number; } } /** - * Adds a field to the fieldsByNumber table. Throws an exception if a - * field with the same containing type and number already exists. + * Adds a field to the fieldsByNumber table. Throws an exception if a field with the same + * containing type and number already exists. */ - void addFieldByNumber(final FieldDescriptor field) - throws DescriptorValidationException { + void addFieldByNumber(final FieldDescriptor field) throws DescriptorValidationException { final DescriptorIntPair key = - new DescriptorIntPair(field.getContainingType(), field.getNumber()); + new DescriptorIntPair(field.getContainingType(), field.getNumber()); final FieldDescriptor old = fieldsByNumber.put(key, field); if (old != null) { fieldsByNumber.put(key, old); - throw new DescriptorValidationException(field, - "Field number " + field.getNumber() + - " has already been used in \"" + - field.getContainingType().getFullName() + - "\" by field \"" + old.getName() + "\"."); + throw new DescriptorValidationException( + field, + "Field number " + + field.getNumber() + + " has already been used in \"" + + field.getContainingType().getFullName() + + "\" by field \"" + + old.getName() + + "\"."); } } /** - * Adds an enum value to the enumValuesByNumber table. If an enum value - * with the same type and number already exists, does nothing. (This is - * allowed; the first value define with the number takes precedence.) + * Adds an enum value to the enumValuesByNumber table. If an enum value with the same type and + * number already exists, does nothing. (This is allowed; the first value define with the number + * takes precedence.) */ void addEnumValueByNumber(final EnumValueDescriptor value) { - final DescriptorIntPair key = - new DescriptorIntPair(value.getType(), value.getNumber()); + final DescriptorIntPair key = new DescriptorIntPair(value.getType(), value.getNumber()); final EnumValueDescriptor old = enumValuesByNumber.put(key, value); if (old != null) { enumValuesByNumber.put(key, old); @@ -2454,11 +2471,11 @@ public final class Descriptors { } /** - * Verifies that the descriptor's name is valid (i.e. it contains only - * letters, digits, and underscores, and does not start with a digit). + * Verifies that the descriptor's name is valid (i.e. it contains only letters, digits, and + * underscores, and does not start with a digit). */ static void validateSymbolName(final GenericDescriptor descriptor) - throws DescriptorValidationException { + throws DescriptorValidationException { final String name = descriptor.getName(); if (name.length() == 0) { throw new DescriptorValidationException(descriptor, "Missing name."); @@ -2473,16 +2490,15 @@ public final class Descriptors { } // First character must be letter or _. Subsequent characters may // be letters, numbers, or digits. - if (Character.isLetter(c) || c == '_' || - (Character.isDigit(c) && i > 0)) { + if (Character.isLetter(c) || c == '_' || (Character.isDigit(c) && i > 0)) { // Valid } else { valid = false; } } if (!valid) { - throw new DescriptorValidationException(descriptor, - '\"' + name + "\" is not a valid identifier."); + throw new DescriptorValidationException( + descriptor, '\"' + name + "\" is not a valid identifier."); } } } @@ -2491,17 +2507,29 @@ public final class Descriptors { /** Describes an oneof of a message type. */ public static final class OneofDescriptor { /** Get the index of this descriptor within its parent. */ - public int getIndex() { return index; } + public int getIndex() { + return index; + } - public String getName() { return proto.getName(); } + public String getName() { + return proto.getName(); + } - public FileDescriptor getFile() { return file; } + public FileDescriptor getFile() { + return file; + } - public String getFullName() { return fullName; } + public String getFullName() { + return fullName; + } - public Descriptor getContainingType() { return containingType; } + public Descriptor getContainingType() { + return containingType; + } - public int getFieldCount() { return fieldCount; } + public int getFieldCount() { + return fieldCount; + } public OneofOptions getOptions() { return proto.getOptions(); @@ -2520,11 +2548,12 @@ public final class Descriptors { this.proto = proto; } - private OneofDescriptor(final OneofDescriptorProto proto, - final FileDescriptor file, - final Descriptor parent, - final int index) - throws DescriptorValidationException { + private OneofDescriptor( + final OneofDescriptorProto proto, + final FileDescriptor file, + final Descriptor parent, + final int index) + throws DescriptorValidationException { this.proto = proto; fullName = computeFullName(file, parent, proto.getName()); this.file = file; |