CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-com-google-protobuf--protobuf-java

Google's Protocol Buffers implementation for Java, providing comprehensive serialization of structured data with efficient encoding and extensive API coverage

Pending
Overview
Eval results
Files

reflection-descriptors.mddocs/

Reflection and Descriptors

Comprehensive reflection API for runtime introspection of protocol buffer types, enabling dynamic message handling, tooling, and runtime type discovery without generated classes.

Capabilities

File Descriptors

Describes a complete .proto file and its contents, including all message types, enums, services, and extensions defined within it.

/**
 * Describes a .proto file and its contents
 */
public static class Descriptors.FileDescriptor {
    // Basic information
    /** Get the file name */
    public String getName();
    
    /** Get the proto package name */
    public String getPackage();
    
    /** Get file syntax (proto2 or proto3) */
    public Syntax getSyntax();
    
    /** Get file options */
    public DescriptorProtos.FileOptions getOptions();
    
    // Type discovery
    /** Get all message types defined in this file */
    public List<Descriptor> getMessageTypes();
    
    /** Get all enum types defined in this file */  
    public List<EnumDescriptor> getEnumTypes();
    
    /** Get all services defined in this file */
    public List<ServiceDescriptor> getServices();
    
    /** Get all extensions defined in this file */
    public List<FieldDescriptor> getExtensions();
    
    // Dependencies
    /** Get files this file depends on */
    public List<FileDescriptor> getDependencies();
    
    /** Get public dependencies */
    public List<FileDescriptor> getPublicDependencies();
    
    // Lookup methods
    /** Find message type by name within this file */
    public Descriptor findMessageTypeByName(String name);
    
    /** Find enum type by name within this file */
    public EnumDescriptor findEnumTypeByName(String name);
    
    /** Find service by name within this file */
    public ServiceDescriptor findServiceByName(String name);
    
    /** Find extension by name within this file */
    public FieldDescriptor findExtensionByName(String name);
    
    // Building file descriptors
    /** Build from descriptor proto with dependencies */
    public static FileDescriptor buildFrom(DescriptorProtos.FileDescriptorProto proto,
                                          FileDescriptor[] dependencies)
            throws DescriptorValidationException;
            
    /** Build from descriptor proto with dependencies and allow unknown dependencies */
    public static FileDescriptor buildFrom(DescriptorProtos.FileDescriptorProto proto,
                                          FileDescriptor[] dependencies,
                                          boolean allowUnknownDependencies)
            throws DescriptorValidationException;
    
    /** Convert to descriptor proto */
    public DescriptorProtos.FileDescriptorProto toProto();
}

Message Descriptors

Describes a protocol buffer message type, including all its fields, nested types, and metadata.

/**
 * Describes a protocol buffer message type
 */
public static class Descriptors.Descriptor implements GenericDescriptor {
    // Basic information
    /** Get message type name */
    public String getName();
    
    /** Get fully qualified message name */
    public String getFullName();
    
    /** Get containing file descriptor */
    public FileDescriptor getFile();
    
    /** Get containing message type (for nested messages) */
    public Descriptor getContainingType();
    
    /** Get message options */
    public DescriptorProtos.MessageOptions getOptions();
    
    // Field access
    /** Get all fields in this message */
    public List<FieldDescriptor> getFields();
    
    /** Get all oneof groups in this message */
    public List<OneofDescriptor> getOneofs();
    
    /** Get all extensions that can extend this message */
    public List<FieldDescriptor> getExtensions();
    
    // Nested type access
    /** Get all nested message types */
    public List<Descriptor> getNestedTypes();
    
    /** Get all nested enum types */
    public List<EnumDescriptor> getEnumTypes();
    
    // Capability queries
    /** Check if this message type is extendable */
    public boolean isExtendable();
    
    /** Check if this is a map entry message */
    public boolean isMapEntry();
    
    // Field lookup methods
    /** Find field by name */
    public FieldDescriptor findFieldByName(String name);
    
    /** Find field by field number */
    public FieldDescriptor findFieldByNumber(int number);
    
    /** Find nested message type by name */
    public Descriptor findNestedTypeByName(String name);
    
    /** Find nested enum type by name */
    public EnumDescriptor findEnumTypeByName(String name);
    
    // Proto conversion
    /** Convert to descriptor proto */
    public DescriptorProtos.DescriptorProto toProto();
}

Field Descriptors

Describes individual fields within protocol buffer messages, including type information, constraints, and metadata.

/**
 * Describes a field within a message
 */
public static class Descriptors.FieldDescriptor implements GenericDescriptor, Comparable<FieldDescriptor> {
    // Basic information
    /** Get field name */
    public String getName();
    
    /** Get field number */
    public int getNumber();
    
    /** Get fully qualified field name */
    public String getFullName();
    
    /** Get JSON field name */
    public String getJsonName();
    
    /** Get field options */
    public DescriptorProtos.FieldOptions getOptions();
    
    // Type information
    /** Get protocol buffer field type */
    public Type getType();
    
    /** Get corresponding Java type */
    public JavaType getJavaType();
    
    /** Get wire format type */
    public WireFormat.FieldType getLiteType();
    
    // Cardinality and constraints
    /** Check if field is required (proto2) */
    public boolean isRequired();
    
    /** Check if field is optional */
    public boolean isOptional();
    
    /** Check if field is repeated */
    public boolean isRepeated();
    
    /** Check if repeated field is packed */
    public boolean isPacked();
    
    /** Check if field is a map field */
    public boolean isMapField();
    
    /** Check if field is an extension field */
    public boolean isExtension();
    
    // Default values
    /** Check if field has explicit default value */
    public boolean hasDefaultValue();
    
    /** Get default value for this field */
    public Object getDefaultValue();
    
    // Containing types
    /** Get message type that contains this field */
    public Descriptor getContainingType();
    
    /** Get containing oneof group (if part of oneof) */
    public OneofDescriptor getContainingOneof();
    
    /** Get extension scope (for extension fields) */
    public Descriptor getExtensionScope();
    
    // Referenced types
    /** Get message type (for message fields) */
    public Descriptor getMessageType();
    
    /** Get enum type (for enum fields) */
    public EnumDescriptor getEnumType();
    
    // Proto conversion
    /** Convert to field descriptor proto */
    public DescriptorProtos.FieldDescriptorProto toProto();
    
    // Field type enumeration
    public enum Type {
        DOUBLE, FLOAT, INT64, UINT64, INT32, FIXED64, FIXED32,
        BOOL, STRING, GROUP, MESSAGE, BYTES, UINT32, ENUM,
        SFIXED32, SFIXED64, SINT32, SINT64;
        
        public JavaType getJavaType();
        public WireFormat.FieldType getLiteType();
    }
    
    public enum JavaType {
        INT, LONG, FLOAT, DOUBLE, BOOLEAN, STRING, BYTE_STRING, ENUM, MESSAGE;
        
        public Object getDefaultDefault();
    }
}

Enum Descriptors

Describes protocol buffer enum types and their values.

/**
 * Describes an enum type
 */
public static class Descriptors.EnumDescriptor implements GenericDescriptor {
    // Basic information
    /** Get enum name */
    public String getName();
    
    /** Get fully qualified enum name */
    public String getFullName();
    
    /** Get containing file */
    public FileDescriptor getFile();
    
    /** Get containing message type (for nested enums) */
    public Descriptor getContainingType();
    
    /** Get enum options */
    public DescriptorProtos.EnumOptions getOptions();
    
    // Value access
    /** Get all enum values */
    public List<EnumValueDescriptor> getValues();
    
    /** Find enum value by name */
    public EnumValueDescriptor findValueByName(String name);
    
    /** Find enum value by number */
    public EnumValueDescriptor findValueByNumber(int number);
    
    // Proto conversion
    /** Convert to enum descriptor proto */
    public DescriptorProtos.EnumDescriptorProto toProto();
}

/**
 * Describes an enum value
 */
public static class Descriptors.EnumValueDescriptor implements GenericDescriptor {
    // Basic information
    /** Get enum value name */
    public String getName();
    
    /** Get enum value number */
    public int getNumber();
    
    /** Get fully qualified name */
    public String getFullName();
    
    /** Get containing enum type */
    public EnumDescriptor getType();
    
    /** Get value options */
    public DescriptorProtos.EnumValueOptions getOptions();
    
    /** Convert to enum value descriptor proto */
    public DescriptorProtos.EnumValueDescriptorProto toProto();
}

Service Descriptors

Describes protocol buffer services and their methods (used with RPC frameworks).

/**
 * Describes a service
 */
public static class Descriptors.ServiceDescriptor implements GenericDescriptor {
    // Basic information
    /** Get service name */
    public String getName();
    
    /** Get fully qualified service name */
    public String getFullName();
    
    /** Get containing file */
    public FileDescriptor getFile();
    
    /** Get service options */
    public DescriptorProtos.ServiceOptions getOptions();
    
    // Method access
    /** Get all methods in this service */
    public List<MethodDescriptor> getMethods();
    
    /** Find method by name */
    public MethodDescriptor findMethodByName(String name);
    
    /** Convert to service descriptor proto */
    public DescriptorProtos.ServiceDescriptorProto toProto();
}

/**
 * Describes a service method
 */
public static class Descriptors.MethodDescriptor implements GenericDescriptor {
    // Basic information
    /** Get method name */
    public String getName();
    
    /** Get fully qualified method name */
    public String getFullName();
    
    /** Get containing service */
    public ServiceDescriptor getService();
    
    /** Get method options */
    public DescriptorProtos.MethodOptions getOptions();
    
    // Input/output types
    /** Get input message type */
    public Descriptor getInputType();
    
    /** Get output message type */  
    public Descriptor getOutputType();
    
    // Streaming information
    /** Check if client streaming */
    public boolean isClientStreaming();
    
    /** Check if server streaming */
    public boolean isServerStreaming();
    
    /** Convert to method descriptor proto */
    public DescriptorProtos.MethodDescriptorProto toProto();
}

Oneof Descriptors

Describes oneof field groups within messages.

/**
 * Describes a oneof field group
 */
public static class Descriptors.OneofDescriptor implements GenericDescriptor {
    // Basic information
    /** Get oneof name */
    public String getName();
    
    /** Get fully qualified oneof name */
    public String getFullName();
    
    /** Get containing message type */
    public Descriptor getContainingType();
    
    /** Get oneof options */
    public DescriptorProtos.OneofOptions getOptions();
    
    // Field access
    /** Get all fields in this oneof group */
    public List<FieldDescriptor> getFields();
    
    /** Get field by index */
    public FieldDescriptor getField(int index);
    
    /** Get number of fields in oneof */
    public int getFieldCount();
    
    /** Check if oneof is synthetic (proto3 optional) */
    public boolean isSynthetic();
    
    /** Convert to oneof descriptor proto */
    public DescriptorProtos.OneofDescriptorProto toProto();
}

Type Registry

Registry for resolving Any message types by type URL.

/**
 * Registry for resolving Any message types
 */
public class TypeRegistry {
    // Static factory methods
    /** Get empty type registry */
    public static TypeRegistry getEmptyTypeRegistry();
    
    /** Create new builder */
    public static Builder newBuilder();
    
    // Type resolution
    /** Find type by full name */
    public Descriptor find(String name);
    
    /** Find type by type URL */
    public Descriptor getDescriptorForTypeUrl(String typeUrl);
    
    /**
     * Builder for constructing type registries
     */
    public static class Builder {
        /** Add message type and all its dependencies */
        public Builder add(Descriptor messageType);
        
        /** Add multiple message types */
        public Builder add(Iterable<Descriptor> messageTypes);
        
        /** Build the type registry */
        public TypeRegistry build();
    }
}

Descriptor Validation

Exception handling for descriptor validation errors.

/**
 * Exception thrown when descriptor validation fails
 */
public static class Descriptors.DescriptorValidationException extends Exception {
    /** Create with description */
    public DescriptorValidationException(GenericDescriptor problemDescriptor, String description);
    
    /** Create with description and cause */
    public DescriptorValidationException(GenericDescriptor problemDescriptor, String description, Throwable cause);
    
    /** Get descriptor that caused the problem */
    public GenericDescriptor getProblemDescriptor();
    
    /** Get problem description */
    public String getProblemDescription();
}

/**
 * Base interface for all descriptor types
 */
public interface GenericDescriptor {
    /** Get name of this descriptor */
    String getName();
    
    /** Get fully qualified name */
    String getFullName();
    
    /** Get containing file */
    FileDescriptor getFile();
}

Usage Examples:

import com.google.protobuf.Descriptors;
import com.google.protobuf.TypeRegistry;

// Working with file descriptors
FileDescriptor fileDesc = MyMessage.getDescriptor().getFile();
System.out.println("File: " + fileDesc.getName());
System.out.println("Package: " + fileDesc.getPackage());

// Exploring message structure
Descriptor messageDesc = MyMessage.getDescriptor();
System.out.println("Message: " + messageDesc.getFullName());

for (FieldDescriptor field : messageDesc.getFields()) {
    System.out.printf("Field %d: %s (%s)%n", 
        field.getNumber(), field.getName(), field.getType());
        
    if (field.isRepeated()) {
        System.out.println("  - Repeated field");
    }
    
    if (field.getType() == FieldDescriptor.Type.MESSAGE) {
        System.out.println("  - Message type: " + field.getMessageType().getFullName());
    }
    
    if (field.getContainingOneof() != null) {
        System.out.println("  - Part of oneof: " + field.getContainingOneof().getName());
    }
}

// Working with enums
for (EnumDescriptor enumDesc : messageDesc.getEnumTypes()) {
    System.out.println("Enum: " + enumDesc.getName());
    for (EnumValueDescriptor value : enumDesc.getValues()) {
        System.out.printf("  %s = %d%n", value.getName(), value.getNumber());
    }
}

// Building type registry for Any messages
TypeRegistry typeRegistry = TypeRegistry.newBuilder()
    .add(MyMessage.getDescriptor())
    .add(AnotherMessage.getDescriptor())
    .build();

// Find types at runtime
Descriptor foundType = typeRegistry.find("com.example.MyMessage");
if (foundType != null) {
    System.out.println("Found type: " + foundType.getFullName());
}

Dynamic Message Creation

Creating and manipulating messages using descriptors without generated classes.

/**
 * Dynamic message creation and manipulation using descriptors
 */

// Example: Creating dynamic messages
Descriptor messageType = getMessageDescriptor(); // Get from somewhere
DynamicMessage.Builder builder = DynamicMessage.newBuilder(messageType);

// Set fields by descriptor
FieldDescriptor nameField = messageType.findFieldByName("name");
FieldDescriptor ageField = messageType.findFieldByName("age");

builder.setField(nameField, "John Doe");
builder.setField(ageField, 30);

DynamicMessage message = builder.build();

// Read fields by descriptor
String name = (String) message.getField(nameField);
Integer age = (Integer) message.getField(ageField);

// Work with repeated fields
FieldDescriptor listField = messageType.findFieldByName("items");
if (listField.isRepeated()) {
    builder.addRepeatedField(listField, "item1");
    builder.addRepeatedField(listField, "item2");
    
    DynamicMessage messageWithList = builder.build();
    int count = messageWithList.getRepeatedFieldCount(listField);
    for (int i = 0; i < count; i++) {
        String item = (String) messageWithList.getRepeatedField(listField, i);
        System.out.println("Item " + i + ": " + item);
    }
}

Exception Handling

Common exceptions when working with descriptors:

  • DescriptorValidationException: Thrown when building descriptors from invalid proto definitions
  • IllegalArgumentException: Thrown for invalid field numbers, unknown field names, or type mismatches
  • NullPointerException: Thrown when required parameters are null

Types

// Core descriptor interfaces
public interface GenericDescriptor {
    String getName();
    String getFullName();
    FileDescriptor getFile();
}

// Syntax enumeration for proto files
public enum Syntax {
    PROTO2, PROTO3
}

// Descriptor protocol buffer definitions (from descriptor.proto)
// These are the underlying proto definitions used to build descriptors
public class DescriptorProtos {
    public static class FileDescriptorProto extends GeneratedMessage { /* ... */ }
    public static class DescriptorProto extends GeneratedMessage { /* ... */ }
    public static class FieldDescriptorProto extends GeneratedMessage { /* ... */ }
    public static class EnumDescriptorProto extends GeneratedMessage { /* ... */ }
    public static class EnumValueDescriptorProto extends GeneratedMessage { /* ... */ }
    public static class ServiceDescriptorProto extends GeneratedMessage { /* ... */ }
    public static class MethodDescriptorProto extends GeneratedMessage { /* ... */ }
    public static class OneofDescriptorProto extends GeneratedMessage { /* ... */ }
    
    // Options messages for customizing behavior
    public static class FileOptions extends GeneratedMessage { /* ... */ }
    public static class MessageOptions extends GeneratedMessage { /* ... */ }
    public static class FieldOptions extends GeneratedMessage { /* ... */ }
    public static class EnumOptions extends GeneratedMessage { /* ... */ }
    public static class EnumValueOptions extends GeneratedMessage { /* ... */ }
    public static class ServiceOptions extends GeneratedMessage { /* ... */ }
    public static class MethodOptions extends GeneratedMessage { /* ... */ }
    public static class OneofOptions extends GeneratedMessage { /* ... */ }
}

Install with Tessl CLI

npx tessl i tessl/maven-com-google-protobuf--protobuf-java

docs

core-messages.md

index.md

io-wire-format.md

json-text-format.md

reflection-descriptors.md

utilities.md

tile.json