Class Type


  • public class Type
    extends java.lang.Object
    Type represents a type descriptor in a classes constant pool. A type descriptor describes the type of a field of method using a funky encoding which is described in the Java Virtual Machine Specification. For example, the field int x[] has the type descriptor:

    [I

    The method String f(int a, boolean b, Object c) has the type descriptor:

    (IZLjava/lang/Object;)Ljava/lang/String;

    See Also:
    Constant
    • Method Detail

      • getType

        public static Type getType​(java.lang.String desc)
        Returns a Type of a given descriptor. Equals descriptors will result in the same Type.
      • getType

        public static Type getType​(java.lang.Class c)
        Returns a Type that represents a given Class.
      • getType

        public static Type getType​(Type[] paramTypes,
                                   Type returnType)
        Returns the Type for a method with the given parameter and return types
      • getType

        public static Type getType​(int typeCode)
        Returns a Type for a primitive type based on its integer "type code".
      • getType

        public static Type getType​(char typeChar)
        Returns a Type of a primitive type based on a one-character type descriptor.
      • typeCode

        public int typeCode()
        Get the type code of the type (which must be a primitive type).
        Returns:
        The type code of the type.
        Throws:
        java.lang.IllegalArgumentException - If the type is not primitive.
      • shortName

        public java.lang.String shortName()
        Get a one character name for the type.
      • simple

        public Type simple()
        Get a simplification of the type. All integral types become INTEGER. All reference types (objects, arrays, returnAddress) become OBJECT.
        Returns:
        The simplified type.
      • descriptor

        public java.lang.String descriptor()
        Get a descriptor of the type.
        Returns:
        The type descriptor.
      • isMethod

        public boolean isMethod()
        Check if the type is a method type.
        Returns:
        true if a method type, false if not.
      • isNull

        public boolean isNull()
        Check if the type is a null type.
        Returns:
        true if a null type, false if not.
      • isVoid

        public boolean isVoid()
        Check if the type is a void type.
        Returns:
        true if a void type, false if not.
      • isPrimitive

        public boolean isPrimitive()
        Check if the type is a primitive type.
        Returns:
        true if a primitive type, false if not.
      • isIntegral

        public boolean isIntegral()
        Check if the type is an integral type. Integral contains BOOLEAN as far as the JVM is concerned.
        Returns:
        true if an integral type, false if not.
      • isArray

        public boolean isArray()
        Check if the type is an array type.
        Returns:
        true if an array type, false if not.
      • isObject

        public boolean isObject()
        Check if the type is an object type (not array).
        Returns:
        true if an object type, false if not.
      • isWide

        public boolean isWide()
        Check if the type takes of 2 local variable or stack positions.
        Returns:
        true if a wide type, false if not.
      • isAddress

        public boolean isAddress()
        Check if the type is a returnAddress.
        Returns:
        true if a address type, false if not.
      • isReference

        public boolean isReference()
        Check if the type is an array or object.
        Returns:
        true if a reference type, false if not.
      • classDescriptor

        public static java.lang.String classDescriptor​(java.lang.String name)
        Get the type descriptor of a class from a string representation.. For example "java/lang/String" becomes "Ljava/lang/String;"
        Parameters:
        name - The name of the class.
        Returns:
        The type descriptor of the class.
      • className

        public java.lang.String className()
        Get the class name of the type. For example if the class descriptor is "Ljava/lang/String;", the class name is "java/lang/String".
        Returns:
        The class name of the type.
      • qualifier

        public java.lang.String qualifier()
        Get the qualifier of the type. For example if the class descriptor is "Ljava/lang/String;", the qualifier is "java/lang/".
        Returns:
        The qualifier of the type.
      • dimensions

        public int dimensions()
        Get the number of dimensions of an array type.
      • arrayType

        public Type arrayType()
        Get a Type representing an array of this type.
      • arrayType

        public Type arrayType​(int dimensions)
        Create a Type representing a multidimensional array of this type.
      • elementType

        public Type elementType​(int dimensions)
        Get the element type of this array type.
        Parameters:
        dimensions - The number of times to index into the array.
        Returns:
        The element type.
        Throws:
        java.lang.IllegalArgumentException - If the type is not an array.
      • elementType

        public Type elementType()
        Get the element type of an array type.
        Returns:
        The element type.
        Throws:
        java.lang.IllegalArgumentException - If the type is not an array.
      • returnType

        public Type returnType()
        Get a return type of a method type.
        Returns:
        The return type.
      • indexedParamTypes

        public Type[] indexedParamTypes()
        If this Type is a method type, get the parameter types of the method, including empty positions for the second word of wide types. This method is good for figuring out which local variables go with parameters. Recall that wide data takes up two local variables.
      • paramTypes

        public Type[] paramTypes()
        Get the parameter types of the method, not including empty positions for the second word of wide types.
        Returns:
        The parameter types.
      • stackHeight

        public int stackHeight()
        Returns the number of slots in the stack that this Type takes up on the JVM stack. This type descriptor is intended to represent the parameters of a method. If there are no parameters, 0 is returned. Else return count each parameter as 1 except wide parameters (longs and doubles) as 2.

        It also give you an idea of how may parameters a method has.

      • hashCode

        public int hashCode()
        Hash the type.
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        The hash code.
      • equals

        public boolean equals​(java.lang.Object obj)
        Returns true if two Types are equal. Equal Types have equal descriptors.
        Overrides:
        equals in class java.lang.Object
      • comparator

        public static java.util.Comparator comparator()
        Returns a Comparator used to compare Types.
      • printComparator

        public static java.util.Comparator printComparator()
        Returns a Comparator that compares Types based on how they are displayed.
      • truncatedName

        public static java.lang.String truncatedName​(Type type)
        Returns a string representing the truncated name of a Type. For instance java/lang/String would just return String.
      • toString

        public java.lang.String toString()
        Convert the type to a string.
        Overrides:
        toString in class java.lang.Object
        Returns:
        A string representation of the type.
      • main

        public static void main​(java.lang.String[] args)
        Test truncatedName.