Class Signatures


  • public class Signatures
    extends java.lang.Object
    This class is compiled against 1.5 or later to provide access to the generic signatures. It 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: cf12175aae65bac45771d15b455807c13d2f486e $
    • 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 java.lang.Object call​(java.lang.Object gd, java.lang.String string)  
      private void declaration​(java.lang.StringBuilder sb, java.lang.Object 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.
      boolean hasGenerics()
      Check if the environment has generics, i.e.
      private boolean isInstance​(java.lang.Class<?> type, java.lang.String string)  
      private boolean isInterface​(java.lang.Object 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.Object 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.Object 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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Signatures

        public Signatures()
    • Method Detail

      • hasGenerics

        public boolean hasGenerics()
                            throws java.lang.Exception
        Check if the environment has generics, i.e. later than Java 5 VM.
        Returns:
        true if generics are supported
        Throws:
        java.lang.Exception
      • 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.Object 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.Object 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.Object 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.Object 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
      • isInstance

        private boolean isInstance​(java.lang.Class<?> type,
                                   java.lang.String string)
      • call

        private java.lang.Object call​(java.lang.Object gd,
                                      java.lang.String string)
                               throws java.lang.Exception
        Throws:
        java.lang.Exception