Package aQute.bnd.compatibility
Class Signatures
- java.lang.Object
-
- aQute.bnd.compatibility.Signatures
-
public class Signatures extends java.lang.Object
This class can convert a Class, Field, Method or constructor to a generic signature and it can normalize a signature. Both are methods. Normalized signatures can be string compared and match even if the type variable names differ.- Version:
- $Id: d9bcbd3da7e14f43f4fe3db7e1f6ef4c27ff122c $
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
Signatures.Rover
Helper class to track an index in a string.
-
Constructor Summary
Constructors Constructor Description Signatures()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private java.lang.String
assign(java.util.Map<java.lang.String,java.lang.String> map, java.lang.String name)
Handles the assignment of type variables to index names so that we have a normalized name for each type var.private void
body(java.lang.StringBuilder sb, java.util.Map<java.lang.String,java.lang.String> map, Signatures.Rover rover)
Because classes can be nested the body handles the part that can be nested, the reference handles the enclosing L ...private void
declaration(java.lang.StringBuilder sb, java.lang.reflect.GenericDeclaration gd)
Classes, Methods, or Constructors can have a declaration that provides nested a scope for type variables.private void
declare(java.lang.StringBuilder sb, java.util.Map<java.lang.String,java.lang.String> map, Signatures.Rover rover)
Handle the declaration part.java.lang.String
getSignature(java.lang.Class<?> c)
Calculate the generic signature of a Class.java.lang.String
getSignature(java.lang.Object c)
Calculate the generic signature of a Class,Method,Field, or Constructor.java.lang.String
getSignature(java.lang.reflect.Constructor<?> c)
Calculate the generic signature of a Constructor.java.lang.String
getSignature(java.lang.reflect.Field f)
Calculate the generic signature of a Field.java.lang.String
getSignature(java.lang.reflect.Method m)
Calculate the generic signature of a Method.private boolean
isInterface(java.lang.reflect.Type type)
Verify that the type is an interface.java.lang.String
normalize(java.lang.String signature)
Normalize a signature to make sure the name of the variables are always the same.private void
parameterizedType(java.lang.StringBuilder sb, java.lang.reflect.ParameterizedType pt)
Creates the signature for a Parameterized Type.private char
primitive(java.lang.Class<?> type)
Handle primitives, these need to be translated to a single char.private void
reference(java.lang.StringBuilder sb, java.lang.reflect.Type t)
This is the heart of the signature builder.private void
reference(java.lang.StringBuilder sb, java.util.Map<java.lang.String,java.lang.String> map, Signatures.Rover rover, boolean primitivesAllowed)
The heart of the routine.
-
-
-
Method Detail
-
getSignature
public java.lang.String getSignature(java.lang.Object c) throws java.lang.Exception
Calculate the generic signature of a Class,Method,Field, or Constructor.- Throws:
java.lang.Exception
-
getSignature
public java.lang.String getSignature(java.lang.Class<?> c) throws java.lang.Exception
Calculate the generic signature of a Class. A Class consists of:class ::= declaration? reference reference*
- Throws:
java.lang.Exception
-
getSignature
public java.lang.String getSignature(java.lang.reflect.Method m) throws java.lang.Exception
Calculate the generic signature of a Method. A Method consists of:method ::= declaration? '(' reference* ')' reference
- Throws:
java.lang.Exception
-
getSignature
public java.lang.String getSignature(java.lang.reflect.Constructor<?> c) throws java.lang.Exception
Calculate the generic signature of a Constructor. A Constructor consists of:constructor ::= declaration? '(' reference* ')V'
- Parameters:
c
-- Throws:
java.lang.Exception
-
getSignature
public java.lang.String getSignature(java.lang.reflect.Field f) throws java.lang.Exception
Calculate the generic signature of a Field. A Field consists of:constructor ::= reference
- Throws:
java.lang.Exception
-
declaration
private void declaration(java.lang.StringBuilder sb, java.lang.reflect.GenericDeclaration gd) throws java.lang.Exception
Classes, Methods, or Constructors can have a declaration that provides nested a scope for type variables. A Method/Constructor inherits the type variables from its class and a class inherits its type variables from its outer class. The declaration consists of the following syntax:declarations ::= '<' declaration ( ',' declaration )* '>' declaration ::= identifier ':' declare declare ::= types | variable types ::= ( 'L' class ';' )? ( ':' 'L' interface ';' )* variable ::= 'T' id ';'
- Parameters:
sb
-gd
-- Throws:
java.lang.Exception
-
isInterface
private boolean isInterface(java.lang.reflect.Type type) throws java.lang.Exception
Verify that the type is an interface.- Parameters:
type
- the type to check.- Returns:
- true if this is a class that is an interface or a Parameterized Type that is an interface
- Throws:
java.lang.Exception
-
reference
private void reference(java.lang.StringBuilder sb, java.lang.reflect.Type t) throws java.lang.Exception
This is the heart of the signature builder. A reference is used in a lot of places. It referes to another type.reference ::= array | class | primitive | variable array ::= '[' reference class ::= 'L' body ( '.' body )* ';' body ::= id ( '<' ( wildcard | reference )* '>' )? variable ::= 'T' id ';' primitive ::= PRIMITIVE
- Parameters:
sb
-t
-- Throws:
java.lang.Exception
-
parameterizedType
private void parameterizedType(java.lang.StringBuilder sb, java.lang.reflect.ParameterizedType pt) throws java.lang.Exception
Creates the signature for a Parameterized Type. A Parameterized Type has a raw class and a set of type variables.- Parameters:
sb
-pt
-- Throws:
java.lang.Exception
-
primitive
private char primitive(java.lang.Class<?> type)
Handle primitives, these need to be translated to a single char.- Parameters:
type
- the primitive class- Returns:
- the single char associated with the primitive
-
normalize
public java.lang.String normalize(java.lang.String signature)
Normalize a signature to make sure the name of the variables are always the same. We change the names of the type variables to _n, where n is an integer. n is incremented for every new name and already used names are replaced with the _n name.- Returns:
- a normalized signature
-
reference
private void reference(java.lang.StringBuilder sb, java.util.Map<java.lang.String,java.lang.String> map, Signatures.Rover rover, boolean primitivesAllowed)
The heart of the routine. Handle a reference to a type. Can be an array, a class, a type variable, or a primitive.- Parameters:
sb
-map
-rover
-primitivesAllowed
-
-
body
private void body(java.lang.StringBuilder sb, java.util.Map<java.lang.String,java.lang.String> map, Signatures.Rover rover)
Because classes can be nested the body handles the part that can be nested, the reference handles the enclosing L ... ;- Parameters:
sb
-map
-rover
-
-
declare
private void declare(java.lang.StringBuilder sb, java.util.Map<java.lang.String,java.lang.String> map, Signatures.Rover rover)
Handle the declaration part.- Parameters:
sb
-map
-rover
-
-
assign
private java.lang.String assign(java.util.Map<java.lang.String,java.lang.String> map, java.lang.String name)
Handles the assignment of type variables to index names so that we have a normalized name for each type var.- Parameters:
map
- the map with variables.name
- The name of the variable- Returns:
- the index name, like _1
-
-