Class Signatures

java.lang.Object
aQute.bnd.compatibility.Signatures

public class Signatures extends 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 $
  • Constructor Details

    • Signatures

      public Signatures()
  • Method Details

    • getSignature

      public String getSignature(Object c) throws Exception
      Calculate the generic signature of a Class,Method,Field, or Constructor.
      Throws:
      Exception
    • getSignature

      public String getSignature(Class<?> c) throws Exception
      Calculate the generic signature of a Class. A Class consists of:
       class ::= declaration? reference reference*
       
      Throws:
      Exception
    • getSignature

      public String getSignature(Method m) throws Exception
      Calculate the generic signature of a Method. A Method consists of:
       method ::= declaration? '(' reference* ')' reference
       
      Throws:
      Exception
    • getSignature

      public String getSignature(Constructor<?> c) throws Exception
      Calculate the generic signature of a Constructor. A Constructor consists of:
        constructor ::= declaration? '(' reference* ')V'
       
      Parameters:
      c -
      Throws:
      Exception
    • getSignature

      public String getSignature(Field f) throws Exception
      Calculate the generic signature of a Field. A Field consists of:
       constructor ::= reference
       
      Throws:
      Exception
    • declaration

      private void declaration(StringBuilder sb, GenericDeclaration gd) throws 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:
      Exception
    • isInterface

      private boolean isInterface(Type type) throws 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:
      Exception
    • reference

      private void reference(StringBuilder sb, Type t) throws 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:
      Exception
    • parameterizedType

      private void parameterizedType(StringBuilder sb, ParameterizedType pt) throws 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:
      Exception
    • primitive

      private char primitive(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 String normalize(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(StringBuilder sb, Map<String,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(StringBuilder sb, Map<String,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(StringBuilder sb, Map<String,String> map, Signatures.Rover rover)
      Handle the declaration part.
      Parameters:
      sb -
      map -
      rover -
    • assign

      private String assign(Map<String,String> map, 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