Complete Java generic signature parsing and resolution system supporting all aspects of the Java generic type system including wildcards, bounds, and complex type hierarchies. This module provides comprehensive support for parsing and manipulating Java's generic type signatures as used in class files.
Java generic signature representation using structured objects that model the complete generic type system including type parameters, bounds, wildcards, and complex inheritance hierarchies.
/**
* Base interface for all signature types
*/
public interface Signature {
// Marker interface for all signature objects
}
/**
* Base interface for all Java type signatures
*/
public interface JavaTypeSignature extends Result {
/**
* Parse a Java type signature from string representation
* @param signature The signature string to parse
* @return Parsed JavaTypeSignature instance
*/
@SuppressWarnings("unchecked")
static <T extends JavaTypeSignature> T of(String signature);
}
/**
* Interface for reference type signatures (classes, interfaces, arrays, type variables)
*/
public interface ReferenceTypeSignature extends JavaTypeSignature {
// Marker interface for reference types
}Usage Examples:
import aQute.bnd.signatures.ClassSignature;
import aQute.bnd.signatures.MethodSignature;
import aQute.bnd.signatures.FieldSignature;
// Parse class signature with generics
ClassSignature classSig = ClassSignature.of(
"<T:Ljava/lang/Object;>Ljava/lang/Object;Ljava/util/List<TT;>;"
);
System.out.println("Type parameters: " + classSig.typeParameters.length);
// Parse method signature
MethodSignature methodSig = MethodSignature.of(
"<U:Ljava/lang/Object;>(TT;TU;)Ljava/util/Map<TT;TU;>;"
);
System.out.println("Parameters: " + methodSig.parameters.length);
// Parse field signature
FieldSignature fieldSig = FieldSignature.of("Ljava/util/List<Ljava/lang/String;>;");
// Access signature components
System.out.println("Super class: " + classSig.superClass.binary);Representation of class-level generic signatures including type parameters and inheritance.
/**
* Represents class-level generic signatures
*/
public class ClassSignature implements Signature {
/**
* Formal type parameters declared on the class
* Example: <T, U extends Number> results in two TypeParameter objects
*/
public final TypeParameter[] typeParameters;
/**
* Generic signature of the superclass
* Example: extends AbstractList<T>
*/
public final ClassTypeSignature superClass;
/**
* Generic signatures of implemented interfaces
* Example: implements List<T>, Serializable
*/
public final ClassTypeSignature[] superInterfaces;
/**
* Parse class signature from string
* @param signature Generic signature string from class file
* @return Parsed ClassSignature object
*/
public static ClassSignature of(String signature);
public ClassSignature(TypeParameter[] typeParameters,
ClassTypeSignature superClass,
ClassTypeSignature[] superInterfaces);
}
/**
* Represents type parameters in generic signatures
*/
public class TypeParameter {
/**
* Name of the type parameter
* Example: "T" in <T extends Number>
*/
public final String identifier;
/**
* Class bound for the type parameter
* Example: Number in <T extends Number>
*/
public final ReferenceTypeSignature classBound;
/**
* Interface bounds for the type parameter
* Example: Serializable in <T extends Number & Serializable>
*/
public final List<ReferenceTypeSignature> interfaceBounds;
public TypeParameter(String identifier, ReferenceTypeSignature classBound, List<ReferenceTypeSignature> interfaceBounds);
}Representation of method-level generic signatures including parameters and return types.
/**
* Represents method-level generic signatures
*/
public class MethodSignature implements Signature {
/**
* Formal type parameters declared on the method
* Example: <U> in <U> List<U> transform(Function<T, U> mapper)
*/
public final TypeParameter[] typeParameters;
/**
* Parameter type signatures
* Ordered list of parameter types including generics
*/
public final JavaTypeSignature[] parameterTypes;
/**
* Return type signature
* Either JavaTypeSignature for typed returns or VoidDescriptor for void
*/
public final Result resultType;
/**
* Exception signatures for throws declarations
* Generic signatures of declared exceptions
*/
public final ThrowsSignature[] throwTypes;
/**
* Parse method signature from string
* @param signature Generic signature string from method
* @return Parsed MethodSignature object
*/
public static MethodSignature of(String signature);
public MethodSignature(TypeParameter[] typeParameters,
JavaTypeSignature[] parameterTypes,
Result resultType,
ThrowsSignature[] throwTypes);
}
/**
* Represents void return type
*/
public enum VoidDescriptor implements Result {
V;
}Representation of field-level generic signatures.
/**
* Represents field-level generic signatures
*/
public class FieldSignature implements Signature {
/**
* Type signature of the field
* Generic type information for the field
*/
public final ReferenceTypeSignature referenceTypeSignature;
public FieldSignature(ReferenceTypeSignature referenceTypeSignature);
}Core type signature interfaces and implementations.
/**
* Base interface for all Java type signatures
*/
public interface JavaTypeSignature extends Signature {
// Marker interface for all Java types (primitive and reference)
}
/**
* Interface for reference type signatures (classes, interfaces, arrays, type variables)
*/
public interface ReferenceTypeSignature extends JavaTypeSignature {
// Marker interface for reference types
}
/**
* Interface for exception signatures in throws clauses
*/
public interface ThrowsSignature {
// Marker interface for throwable types
}
/**
* Enumeration of Java primitive types in signatures
*/
public enum BaseType implements JavaTypeSignature {
B, // byte
C, // char
D, // double
F, // float
I, // int
J, // long
S, // short
Z; // boolean
}
/**
* Represents array type signatures
*/
public class ArrayTypeSignature implements ReferenceTypeSignature {
/**
* Component type of the array (can be primitive or reference)
*/
public final JavaTypeSignature javaTypeSignature;
public ArrayTypeSignature(JavaTypeSignature javaTypeSignature);
}
/**
* Represents class type signatures with generic parameters
*/
public class ClassTypeSignature implements ReferenceTypeSignature, ThrowsSignature {
/**
* Package name portion of the class
* Example: "java/util/" for java.util.List
*/
public final String packageSpecifier;
/**
* Simple class type signatures for the class hierarchy
* Handles nested classes and their generic parameters
*/
public final List<SimpleClassTypeSignature> simpleClassTypeSignatures;
public ClassTypeSignature(String packageSpecifier, List<SimpleClassTypeSignature> simpleClassTypeSignatures);
}
/**
* Represents type variable signatures
*/
public class TypeVariableSignature implements ReferenceTypeSignature, ThrowsSignature {
/**
* Name of the type variable
* Example: "T" in List<T>
*/
public final String identifier;
public TypeVariableSignature(String identifier);
}Detailed representation of class types and their generic arguments.
/**
* Simple class type signature without package qualification
*/
public class SimpleClassTypeSignature {
/**
* Simple name of the class
* Example: "List" for java.util.List<String>
*/
public final String identifier;
/**
* Type arguments for generic instantiation
* Example: [String] for List<String>
*/
public final List<TypeArgument> typeArguments;
public SimpleClassTypeSignature(String identifier, List<TypeArgument> typeArguments);
}
/**
* Represents type arguments in generic signatures
*/
public class TypeArgument {
/**
* Wildcard indicator for the type argument
* NONE for concrete types, EXTENDS/SUPER for wildcards
*/
public final WildcardIndicator wildcardIndicator;
/**
* Reference type signature for the argument
* The actual type or bound for the argument
*/
public final ReferenceTypeSignature referenceTypeSignature;
public TypeArgument(WildcardIndicator wildcardIndicator, ReferenceTypeSignature referenceTypeSignature);
}
/**
* Wildcard indicators for generic type arguments
*/
public enum WildcardIndicator {
NONE, // T (concrete type)
EXTENDS, // ? extends T
SUPER; // ? super T
}Classes for resolving generic signatures in inheritance contexts.
/**
* Resolves class signatures in inheritance hierarchy
*/
public class ClassResolver {
/**
* Create resolver for class signature
* @param signature Class signature to resolve
*/
public ClassResolver(ClassSignature signature);
// Resolution methods for type variables and bounds
}
/**
* Resolves field signatures in class context
*/
public class FieldResolver extends ClassResolver {
/**
* Create resolver for field signature
* @param classSignature Containing class signature
* @param fieldSignature Field signature to resolve
*/
public FieldResolver(ClassSignature classSignature, FieldSignature fieldSignature);
}
/**
* Resolves method signatures in class context
*/
public class MethodResolver extends ClassResolver {
/**
* Create resolver for method signature
* @param classSignature Containing class signature
* @param methodSignature Method signature to resolve
*/
public MethodResolver(ClassSignature classSignature, MethodSignature methodSignature);
}Usage Examples:
import aQute.bnd.signatures.*;
// Parse complex class signature
String complexSig = "<T:Ljava/lang/Number;U::Ljava/io/Serializable;>" +
"Ljava/util/AbstractList<TT;>;" +
"Ljava/util/List<TT;>;Ljava/lang/Cloneable;";
ClassSignature parsed = Signatures.parseClassSignature(complexSig);
// Examine type parameters
for (TypeParameter param : parsed.formalTypeParameters) {
System.out.println("Type parameter: " + param.identifier);
if (param.classBound != null) {
System.out.println(" Class bound: " + param.classBound);
}
for (ReferenceTypeSignature bound : param.interfaceBounds) {
System.out.println(" Interface bound: " + bound);
}
}
// Parse method with wildcards
String methodSig = "<U:Ljava/lang/Object;>" +
"(Ljava/util/List<+TT;>;Ljava/util/Map<-TU;*>;)" +
"Ljava/util/Set<TU;>;";
MethodSignature method = Signatures.parseMethodSignature(methodSig);
System.out.println("Method type parameters: " + method.formalTypeParameters.size());
System.out.println("Parameters: " + method.parameters.size());
// Resolve in inheritance context
ClassResolver resolver = new ClassResolver(parsed);
// Use resolver to resolve type variables in specific contexts/**
* Base interface for all signature types
*/
public interface Signature {
// Marker interface for all signature objects
}
/**
* Result interface for method return types
* Implemented by JavaTypeSignature and VoidDescriptor
*/
public interface Result {
// Marker interface for method results
}