Class DescriptorUtil


  • public final class DescriptorUtil
    extends Object
    Utility class for JVM descriptor parsing and manipulation.
    • Method Detail

      • categorize

        public static DescriptorUtil.DescriptorCategory categorize​(char c)
        Categorizes a type descriptor character.
        Parameters:
        c - the first character of the descriptor
        Returns:
        the category, or null if not a valid descriptor character
      • categorize

        public static DescriptorUtil.DescriptorCategory categorize​(String descriptor)
        Categorizes a type descriptor string.
        Parameters:
        descriptor - the type descriptor
        Returns:
        the category, or null if empty or invalid
      • isPrimitive

        public static boolean isPrimitive​(char c)
        Checks if a character represents a primitive type descriptor.
        Parameters:
        c - the descriptor character
        Returns:
        true if this is a primitive type (Z, B, C, S, I, J, F, D, V)
      • isWideType

        public static boolean isWideType​(String descriptor)
        Checks if a type descriptor represents a wide type (takes 2 slots).
        Parameters:
        descriptor - the type descriptor
        Returns:
        true if this is a long (J) or double (D)
      • getTypeSlots

        public static int getTypeSlots​(String descriptor)
        Gets the number of slots a type occupies on the stack/locals.
        Parameters:
        descriptor - the type descriptor
        Returns:
        2 for long/double, 0 for void, 1 for everything else
      • parseParameterDescriptors

        public static List<String> parseParameterDescriptors​(String methodDescriptor)
        Parses the parameter descriptors from a method descriptor.
        Parameters:
        methodDescriptor - the full method descriptor (e.g., "(ILjava/lang/String;)V")
        Returns:
        list of individual parameter descriptors (e.g., ["I", "Ljava/lang/String;"])
      • parseReturnDescriptor

        public static String parseReturnDescriptor​(String methodDescriptor)
        Parses the return type descriptor from a method descriptor.
        Parameters:
        methodDescriptor - the full method descriptor (e.g., "(I)Ljava/lang/String;")
        Returns:
        the return type descriptor (e.g., "Ljava/lang/String;")
      • countParameters

        public static int countParameters​(String methodDescriptor)
        Counts the number of parameters in a method descriptor.
        Parameters:
        methodDescriptor - the full method descriptor
        Returns:
        the number of parameters
      • countParameterSlots

        public static int countParameterSlots​(String methodDescriptor)
        Counts the total number of stack/local slots needed for method parameters.
        Parameters:
        methodDescriptor - the full method descriptor
        Returns:
        the total slot count (long/double count as 2)
      • extractClassNames

        public static Set<String> extractClassNames​(String descriptor)
        Extracts all class names referenced in a descriptor (internal format).
        Parameters:
        descriptor - a type or method descriptor
        Returns:
        set of internal class names (e.g., "java/lang/String")
      • getArrayElementType

        public static String getArrayElementType​(String arrayDescriptor)
        Gets the element type from an array descriptor.
        Parameters:
        arrayDescriptor - the array type descriptor (e.g., "[[I" or "[Ljava/lang/String;")
        Returns:
        the element type (e.g., "I" or "Ljava/lang/String;"), or null if not an array
      • getArrayDimensions

        public static int getArrayDimensions​(String arrayDescriptor)
        Gets the number of array dimensions.
        Parameters:
        arrayDescriptor - the array type descriptor
        Returns:
        the number of dimensions, or 0 if not an array
      • extractClassName

        public static String extractClassName​(String descriptor)
        Extracts the class name from an object type descriptor.
        Parameters:
        descriptor - the object type descriptor (e.g., "Ljava/lang/String;")
        Returns:
        the internal class name (e.g., "java/lang/String"), or null if not an object type
      • toObjectDescriptor

        public static String toObjectDescriptor​(String internalName)
        Creates an object type descriptor from an internal class name.
        Parameters:
        internalName - the internal class name (e.g., "java/lang/String")
        Returns:
        the object type descriptor (e.g., "Ljava/lang/String;")