Class Descriptor


  • public class Descriptor
    extends java.lang.Object
    A support class for dealing with descriptors.

    See chapter 4.3 in "The Java Virtual Machine Specification (2nd ed.)"

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Descriptor.Iterator
      An Iterator over a descriptor.
    • Constructor Summary

      Constructors 
      Constructor Description
      Descriptor()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String appendParameter​(java.lang.String classname, java.lang.String desc)
      Appends a parameter type to the parameter list represented by the given descriptor.
      static java.lang.String appendParameter​(CtClass type, java.lang.String descriptor)
      Appends a parameter type to the parameter list represented by the given descriptor.
      static int arrayDimension​(java.lang.String desc)
      Computes the dimension of the array represented by the given descriptor.
      static java.lang.String changeReturnType​(java.lang.String classname, java.lang.String desc)
      Changes the return type included in the given descriptor.
      static int dataSize​(java.lang.String desc)
      Computes the data size specified by the given descriptor.
      static boolean eqParamTypes​(java.lang.String desc1, java.lang.String desc2)
      Returns true if the list of the parameter types of desc1 is equal to that of desc2.
      static java.lang.String getParamDescriptor​(java.lang.String decl)
      Returns the signature of the given descriptor.
      static CtClass[] getParameterTypes​(java.lang.String desc, ClassPool cp)
      Returns the CtClass objects representing the parameter types specified by the given descriptor.
      static CtClass getReturnType​(java.lang.String desc, ClassPool cp)
      Returns the CtClass object representing the return type specified by the given descriptor.
      static java.lang.String insertParameter​(java.lang.String classname, java.lang.String desc)
      Inserts a parameter type at the beginning of the parameter list represented by the given descriptor.
      static java.lang.String insertParameter​(CtClass type, java.lang.String descriptor)
      Inserts a parameter type at the beginning of the parameter list represented by the given descriptor.
      static int numOfParameters​(java.lang.String desc)
      Returns the number of the prameters included in the given descriptor.
      static java.lang.String of​(java.lang.String classname)
      Converts to a descriptor from a Java class name
      static java.lang.String of​(CtClass type)
      Returns the descriptor representing the given type.
      static java.lang.String ofConstructor​(CtClass[] paramTypes)
      Returns the descriptor representing a constructor receiving the given parameter types.
      static java.lang.String ofMethod​(CtClass returnType, CtClass[] paramTypes)
      Returns the descriptor representing a method that receives the given parameter types and returns the given type.
      static java.lang.String ofParameters​(CtClass[] paramTypes)
      Returns the descriptor representing a list of parameter types.
      static int paramSize​(java.lang.String desc)
      Computes the data size of parameters.
      static java.lang.String rename​(java.lang.String desc, java.lang.String oldname, java.lang.String newname)
      Substitutes a class name in the given descriptor string.
      static java.lang.String rename​(java.lang.String desc, java.util.Map<java.lang.String,​java.lang.String> map)
      Substitutes class names in the given descriptor string according to the given map.
      static java.lang.String toArrayComponent​(java.lang.String desc, int dim)
      Returns the descriptor of the type of the array component.
      static java.lang.String toClassName​(java.lang.String descriptor)
      Converts to a Java class name from a descriptor.
      static CtClass toCtClass​(java.lang.String desc, ClassPool cp)
      Returns a CtClass object representing the type specified by the given descriptor.
      static java.lang.String toJavaName​(java.lang.String classname)
      Converts a class name from the internal representation used in the JVM to the normal one used in Java.
      static java.lang.String toJvmName​(java.lang.String classname)
      Converts a class name into the internal representation used in the JVM.
      static java.lang.String toJvmName​(CtClass clazz)
      Returns the internal representation of the class name in the JVM.
      static java.lang.String toString​(java.lang.String desc)
      Returns a human-readable representation of the given descriptor.
      • Methods inherited from class java.lang.Object

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

      • Descriptor

        public Descriptor()
    • Method Detail

      • toJvmName

        public static java.lang.String toJvmName​(java.lang.String classname)
        Converts a class name into the internal representation used in the JVM.

        Note that toJvmName(toJvmName(s)) is equivalent to toJvmName(s).

      • toJavaName

        public static java.lang.String toJavaName​(java.lang.String classname)
        Converts a class name from the internal representation used in the JVM to the normal one used in Java. This method does not deal with an array type name such as "[Ljava/lang/Object;" and "[I;". For such names, use toClassName().
        See Also:
        toClassName(String)
      • toJvmName

        public static java.lang.String toJvmName​(CtClass clazz)
        Returns the internal representation of the class name in the JVM.
      • toClassName

        public static java.lang.String toClassName​(java.lang.String descriptor)
        Converts to a Java class name from a descriptor.
        Parameters:
        descriptor - type descriptor.
      • of

        public static java.lang.String of​(java.lang.String classname)
        Converts to a descriptor from a Java class name
      • rename

        public static java.lang.String rename​(java.lang.String desc,
                                              java.lang.String oldname,
                                              java.lang.String newname)
        Substitutes a class name in the given descriptor string.
        Parameters:
        desc - descriptor string
        oldname - replaced JVM class name
        newname - substituted JVM class name
        See Also:
        toJvmName(String)
      • rename

        public static java.lang.String rename​(java.lang.String desc,
                                              java.util.Map<java.lang.String,​java.lang.String> map)
        Substitutes class names in the given descriptor string according to the given map.
        Parameters:
        map - a map between replaced and substituted JVM class names.
        See Also:
        toJvmName(String)
      • of

        public static java.lang.String of​(CtClass type)
        Returns the descriptor representing the given type.
      • ofConstructor

        public static java.lang.String ofConstructor​(CtClass[] paramTypes)
        Returns the descriptor representing a constructor receiving the given parameter types.
        Parameters:
        paramTypes - parameter types
      • ofMethod

        public static java.lang.String ofMethod​(CtClass returnType,
                                                CtClass[] paramTypes)
        Returns the descriptor representing a method that receives the given parameter types and returns the given type.
        Parameters:
        returnType - return type
        paramTypes - parameter types
      • ofParameters

        public static java.lang.String ofParameters​(CtClass[] paramTypes)
        Returns the descriptor representing a list of parameter types. For example, if the given parameter types are two int, then this method returns "(II)".
        Parameters:
        paramTypes - parameter types
      • appendParameter

        public static java.lang.String appendParameter​(java.lang.String classname,
                                                       java.lang.String desc)
        Appends a parameter type to the parameter list represented by the given descriptor.

        classname must not be an array type.

        Parameters:
        classname - parameter type (not primitive type)
        desc - descriptor
      • insertParameter

        public static java.lang.String insertParameter​(java.lang.String classname,
                                                       java.lang.String desc)
        Inserts a parameter type at the beginning of the parameter list represented by the given descriptor.

        classname must not be an array type.

        Parameters:
        classname - parameter type (not primitive type)
        desc - descriptor
      • appendParameter

        public static java.lang.String appendParameter​(CtClass type,
                                                       java.lang.String descriptor)
        Appends a parameter type to the parameter list represented by the given descriptor. The appended parameter becomes the last parameter.
        Parameters:
        type - the type of the appended parameter.
        descriptor - the original descriptor.
      • insertParameter

        public static java.lang.String insertParameter​(CtClass type,
                                                       java.lang.String descriptor)
        Inserts a parameter type at the beginning of the parameter list represented by the given descriptor.
        Parameters:
        type - the type of the inserted parameter.
        descriptor - the descriptor of the method.
      • changeReturnType

        public static java.lang.String changeReturnType​(java.lang.String classname,
                                                        java.lang.String desc)
        Changes the return type included in the given descriptor.

        classname must not be an array type.

        Parameters:
        classname - return type
        desc - descriptor
      • getParameterTypes

        public static CtClass[] getParameterTypes​(java.lang.String desc,
                                                  ClassPool cp)
                                           throws NotFoundException
        Returns the CtClass objects representing the parameter types specified by the given descriptor.
        Parameters:
        desc - descriptor
        cp - the class pool used for obtaining a CtClass object.
        Throws:
        NotFoundException
      • eqParamTypes

        public static boolean eqParamTypes​(java.lang.String desc1,
                                           java.lang.String desc2)
        Returns true if the list of the parameter types of desc1 is equal to that of desc2. For example, "(II)V" and "(II)I" are equal.
      • getParamDescriptor

        public static java.lang.String getParamDescriptor​(java.lang.String decl)
        Returns the signature of the given descriptor. The signature does not include the return type. For example, the signature of "(I)V" is "(I)".
      • getReturnType

        public static CtClass getReturnType​(java.lang.String desc,
                                            ClassPool cp)
                                     throws NotFoundException
        Returns the CtClass object representing the return type specified by the given descriptor.
        Parameters:
        desc - descriptor
        cp - the class pool used for obtaining a CtClass object.
        Throws:
        NotFoundException
      • numOfParameters

        public static int numOfParameters​(java.lang.String desc)
        Returns the number of the prameters included in the given descriptor.
        Parameters:
        desc - descriptor
      • toCtClass

        public static CtClass toCtClass​(java.lang.String desc,
                                        ClassPool cp)
                                 throws NotFoundException
        Returns a CtClass object representing the type specified by the given descriptor.

        This method works even if the package-class separator is not / but . (period). For example, it accepts Ljava.lang.Object; as well as Ljava/lang/Object;.

        Parameters:
        desc - descriptor.
        cp - the class pool used for obtaining a CtClass object.
        Throws:
        NotFoundException
      • arrayDimension

        public static int arrayDimension​(java.lang.String desc)
        Computes the dimension of the array represented by the given descriptor. For example, if the descriptor is "[[I", then this method returns 2.
        Parameters:
        desc - the descriptor.
        Returns:
        0 if the descriptor does not represent an array type.
      • toArrayComponent

        public static java.lang.String toArrayComponent​(java.lang.String desc,
                                                        int dim)
        Returns the descriptor of the type of the array component. For example, if the given descriptor is "[[Ljava/lang/String;" and the given dimension is 2, then this method returns "Ljava/lang/String;".
        Parameters:
        desc - the descriptor.
        dim - the array dimension.
      • dataSize

        public static int dataSize​(java.lang.String desc)
        Computes the data size specified by the given descriptor. For example, if the descriptor is "D", this method returns 2.

        If the descriptor represents a method type, this method returns (the size of the returned value) - (the sum of the data sizes of all the parameters). For example, if the descriptor is "(I)D", then this method returns 1 (= 2 - 1).

        Parameters:
        desc - descriptor
      • paramSize

        public static int paramSize​(java.lang.String desc)
        Computes the data size of parameters. If one of the parameters is double type, the size of that parameter is 2 words. For example, if the given descriptor is "(IJ)D", then this method returns 3. The size of the return type is not computed.
        Parameters:
        desc - a method descriptor.
      • toString

        public static java.lang.String toString​(java.lang.String desc)
        Returns a human-readable representation of the given descriptor. For example, Ljava/lang/Object; is converted into java.lang.Object. (I[I)V is converted into (int, int[]) (the return type is ignored).