Class Utility


  • public abstract class Utility
    extends java.lang.Object
    Utility functions that do not really belong to any class in particular.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  Utility.JavaReader
      Decode characters into bytes.
      private static class  Utility.JavaWriter
      Encode bytes into valid Java identifier characters.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static int[] CHAR_MAP  
      private static java.lang.ThreadLocal<java.lang.Integer> CONSUMER_CHARS  
      private static char ESCAPE_CHAR  
      private static int FREE_CHARS  
      private static int[] MAP_CHAR  
      private static boolean wide  
    • Constructor Summary

      Constructors 
      Constructor Description
      Utility()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String accessToString​(int accessFlags)
      Convert bit field of flags into string such as 'static final'.
      static java.lang.String accessToString​(int accessFlags, boolean forClass)
      Convert bit field of flags into string such as 'static final'.
      private static short byteToShort​(byte b)
      Convert (signed) byte to (unsigned) short value, i.e., all negative values become positive.
      static java.lang.String classOrInterface​(int accessFlags)  
      static int clearBit​(int flag, int i)  
      static java.lang.String codeToString​(byte[] code, ConstantPool constantPool, int index, int length)  
      static java.lang.String codeToString​(byte[] code, ConstantPool constantPool, int index, int length, boolean verbose)
      Disassemble a byte array of JVM byte codes starting from code line 'index' and return the disassembled string representation.
      static java.lang.String codeToString​(ByteSequence bytes, ConstantPool constantPool)  
      static java.lang.String codeToString​(ByteSequence bytes, ConstantPool constantPool, boolean verbose)
      Disassemble a stream of byte codes and return the string representation.
      static java.lang.String compactClassName​(java.lang.String str)
      Shorten long class names, java/lang/String becomes String.
      static java.lang.String compactClassName​(java.lang.String str, boolean chopit)
      Shorten long class names, java/lang/String becomes java.lang.String, e.g..
      static java.lang.String compactClassName​(java.lang.String str, java.lang.String prefix, boolean chopit)
      Shorten long class name str, i.e., chop off the prefix, if the class name starts with this string and the flag chopit is true.
      static java.lang.String convertString​(java.lang.String label)
      Escape all occurrences of newline chars '\n', quotes \", etc.
      private static int countBrackets​(java.lang.String brackets)  
      static byte[] decode​(java.lang.String s, boolean uncompress)
      Decode a string back to a byte array.
      static java.lang.String encode​(byte[] bytes, boolean compress)
      Encode byte array it into Java identifier string, i.e., a string that only contains the following characters: (a, ...
      static java.lang.String fillup​(java.lang.String str, int length, boolean leftJustify, char fill)
      Fillup char with up to length characters with char 'fill' and justify it left or right.
      static java.lang.String format​(int i, int length, boolean leftJustify, char fill)
      Return a string for an integer justified left or right and filled up with 'fill' characters if necessary.
      static java.lang.String getSignature​(java.lang.String type)
      Parse Java type such as "char", or "java.lang.String[]" and return the signature in byte code format, e.g.
      static boolean isJavaIdentifierPart​(char ch)  
      static boolean isSet​(int flag, int i)  
      static java.lang.String[] methodSignatureArgumentTypes​(java.lang.String signature)
      Converts argument list portion of method signature to string with all class names compacted.
      static java.lang.String[] methodSignatureArgumentTypes​(java.lang.String signature, boolean chopit)
      Converts argument list portion of method signature to string.
      static java.lang.String methodSignatureReturnType​(java.lang.String signature)
      Converts return type portion of method signature to string with all class names compacted.
      static java.lang.String methodSignatureReturnType​(java.lang.String signature, boolean chopit)
      Converts return type portion of method signature to string.
      static java.lang.String methodSignatureToString​(java.lang.String signature, java.lang.String name, java.lang.String access)
      Converts method signature to string with all class names compacted.
      static java.lang.String methodSignatureToString​(java.lang.String signature, java.lang.String name, java.lang.String access, boolean chopit)
      Converts method signature to string.
      static java.lang.String methodSignatureToString​(java.lang.String signature, java.lang.String name, java.lang.String access, boolean chopit, LocalVariableTable vars)
      This method converts a method signature string into a Java type declaration like 'void main(String[])' and throws a 'ClassFormatException' when the parsed type is invalid.
      static java.lang.String methodTypeToSignature​(java.lang.String ret, java.lang.String[] argv)
      Converts string containing the method return and argument types to a byte code method signature.
      static java.lang.String packageToPath​(java.lang.String name)
      Converts '.'s to '/'s.
      static java.lang.String pathToPackage​(java.lang.String str)
      Converts a path to a package name.
      private static int pow2​(int n)  
      static void printArray​(java.io.PrintStream out, java.lang.Object[] obj)  
      static void printArray​(java.io.PrintWriter out, java.lang.Object[] obj)  
      static java.lang.String printArray​(java.lang.Object[] obj)  
      static java.lang.String printArray​(java.lang.Object[] obj, boolean braces)  
      static java.lang.String printArray​(java.lang.Object[] obj, boolean braces, boolean quote)  
      static java.lang.String replace​(java.lang.String str, java.lang.String old, java.lang.String new_)
      Replace all occurrences of old in str with new.
      static short searchOpcode​(java.lang.String name)
      Map opcode names to opcode numbers.
      static int setBit​(int flag, int i)  
      static java.lang.String signatureToString​(java.lang.String signature)
      Converts a signature to a string with all class names compacted.
      static java.lang.String signatureToString​(java.lang.String signature, boolean chopit)
      Converts a signature to a string.
      static java.lang.String toHexString​(byte[] bytes)
      Convert bytes into hexadecimal string
      static byte typeOfMethodSignature​(java.lang.String signature)
      Return type of method signature as a byte value as defined in Constants
      static byte typeOfSignature​(java.lang.String signature)
      Return type of signature as a byte value as defined in Constants
      private static java.lang.String typeParamTypesToString​(java.lang.String signature, boolean chopit)
      Converts a type parameter list signature to a string.
      private static java.lang.String typeParamTypeToString​(java.lang.String signature, boolean chopit)
      Converts a type parameter signature to a string.
      private static java.lang.String typeSignaturesToString​(java.lang.String signature, boolean chopit, char term)
      Converts a list of type signatures to a string.
      static java.lang.String typeSignatureToString​(java.lang.String signature, boolean chopit)
      This method converts a type signature string into a Java type declaration such as 'String[]' and throws a 'ClassFormatException' when the parsed type is invalid.
      private static int unwrap​(java.lang.ThreadLocal<java.lang.Integer> tl)  
      private static void wrap​(java.lang.ThreadLocal<java.lang.Integer> tl, int value)  
      • Methods inherited from class java.lang.Object

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

      • CONSUMER_CHARS

        private static final java.lang.ThreadLocal<java.lang.Integer> CONSUMER_CHARS
      • wide

        private static boolean wide
      • CHAR_MAP

        private static final int[] CHAR_MAP
      • MAP_CHAR

        private static final int[] MAP_CHAR
    • Constructor Detail

      • Utility

        public Utility()
    • Method Detail

      • accessToString

        public static java.lang.String accessToString​(int accessFlags)
        Convert bit field of flags into string such as 'static final'.
        Parameters:
        accessFlags - Access flags
        Returns:
        String representation of flags
      • accessToString

        public static java.lang.String accessToString​(int accessFlags,
                                                      boolean forClass)
        Convert bit field of flags into string such as 'static final'. Special case: Classes compiled with new compilers and with the 'ACC_SUPER' flag would be said to be "synchronized". This is because SUN used the same value for the flags 'ACC_SUPER' and 'ACC_SYNCHRONIZED'.
        Parameters:
        accessFlags - Access flags
        forClass - access flags are for class qualifiers ?
        Returns:
        String representation of flags
      • byteToShort

        private static short byteToShort​(byte b)
        Convert (signed) byte to (unsigned) short value, i.e., all negative values become positive.
      • classOrInterface

        public static java.lang.String classOrInterface​(int accessFlags)
        Parameters:
        accessFlags - the class flags
        Returns:
        "class" or "interface", depending on the ACC_INTERFACE flag
      • clearBit

        public static int clearBit​(int flag,
                                   int i)
        Returns:
        'flag' with bit 'i' set to 0
      • codeToString

        public static java.lang.String codeToString​(byte[] code,
                                                    ConstantPool constantPool,
                                                    int index,
                                                    int length)
      • codeToString

        public static java.lang.String codeToString​(byte[] code,
                                                    ConstantPool constantPool,
                                                    int index,
                                                    int length,
                                                    boolean verbose)
        Disassemble a byte array of JVM byte codes starting from code line 'index' and return the disassembled string representation. Decode only 'num' opcodes (including their operands), use -1 if you want to decompile everything.
        Parameters:
        code - byte code array
        constantPool - Array of constants
        index - offset in 'code' array (number of opcodes, not bytes!)
        length - number of opcodes to decompile, -1 for all
        verbose - be verbose, e.g. print constant pool index
        Returns:
        String representation of byte codes
      • codeToString

        public static java.lang.String codeToString​(ByteSequence bytes,
                                                    ConstantPool constantPool)
                                             throws java.io.IOException
        Throws:
        java.io.IOException
      • codeToString

        public static java.lang.String codeToString​(ByteSequence bytes,
                                                    ConstantPool constantPool,
                                                    boolean verbose)
                                             throws java.io.IOException
        Disassemble a stream of byte codes and return the string representation.
        Parameters:
        bytes - stream of bytes
        constantPool - Array of constants
        verbose - be verbose, e.g. print constant pool index
        Returns:
        String representation of byte code
        Throws:
        java.io.IOException - if a failure from reading from the bytes argument occurs
      • compactClassName

        public static java.lang.String compactClassName​(java.lang.String str)
        Shorten long class names, java/lang/String becomes String.
        Parameters:
        str - The long class name
        Returns:
        Compacted class name
      • compactClassName

        public static java.lang.String compactClassName​(java.lang.String str,
                                                        boolean chopit)
        Shorten long class names, java/lang/String becomes java.lang.String, e.g.. If chopit is true the prefix java.lang is also removed.
        Parameters:
        str - The long class name
        chopit - flag that determines whether chopping is executed or not
        Returns:
        Compacted class name
      • compactClassName

        public static java.lang.String compactClassName​(java.lang.String str,
                                                        java.lang.String prefix,
                                                        boolean chopit)
        Shorten long class name str, i.e., chop off the prefix, if the class name starts with this string and the flag chopit is true. Slashes / are converted to dots ..
        Parameters:
        str - The long class name
        prefix - The prefix the get rid off
        chopit - flag that determines whether chopping is executed or not
        Returns:
        Compacted class name
      • convertString

        public static java.lang.String convertString​(java.lang.String label)
        Escape all occurrences of newline chars '\n', quotes \", etc.
      • countBrackets

        private static int countBrackets​(java.lang.String brackets)
      • decode

        public static byte[] decode​(java.lang.String s,
                                    boolean uncompress)
                             throws java.io.IOException
        Decode a string back to a byte array.
        Parameters:
        s - the string to convert
        uncompress - use gzip to uncompress the stream of bytes
        Throws:
        java.io.IOException - if there's a gzip exception
      • encode

        public static java.lang.String encode​(byte[] bytes,
                                              boolean compress)
                                       throws java.io.IOException
        Encode byte array it into Java identifier string, i.e., a string that only contains the following characters: (a, ... z, A, ... Z, 0, ... 9, _, $). The encoding algorithm itself is not too clever: if the current byte's ASCII value already is a valid Java identifier part, leave it as it is. Otherwise it writes the escape character($) followed by:
        • the ASCII value as a hexadecimal string, if the value is not in the range 200..247
        • a Java identifier char not used in a lowercase hexadecimal string, if the value is in the range 200..247

        This operation inflates the original byte array by roughly 40-50%

        Parameters:
        bytes - the byte array to convert
        compress - use gzip to minimize string
        Throws:
        java.io.IOException - if there's a gzip exception
      • fillup

        public static java.lang.String fillup​(java.lang.String str,
                                              int length,
                                              boolean leftJustify,
                                              char fill)
        Fillup char with up to length characters with char 'fill' and justify it left or right.
        Parameters:
        str - string to format
        length - length of desired string
        leftJustify - format left or right
        fill - fill character
        Returns:
        formatted string
      • format

        public static java.lang.String format​(int i,
                                              int length,
                                              boolean leftJustify,
                                              char fill)
        Return a string for an integer justified left or right and filled up with 'fill' characters if necessary.
        Parameters:
        i - integer to format
        length - length of desired string
        leftJustify - format left or right
        fill - fill character
        Returns:
        formatted int
      • getSignature

        public static java.lang.String getSignature​(java.lang.String type)
        Parse Java type such as "char", or "java.lang.String[]" and return the signature in byte code format, e.g. "C" or "[Ljava/lang/String;" respectively.
        Parameters:
        type - Java type
        Returns:
        byte code signature
      • isJavaIdentifierPart

        public static boolean isJavaIdentifierPart​(char ch)
        Parameters:
        ch - the character to test if it's part of an identifier
        Returns:
        true, if character is one of (a, ... z, A, ... Z, 0, ... 9, _)
      • isSet

        public static boolean isSet​(int flag,
                                    int i)
        Returns:
        true, if bit 'i' in 'flag' is set
      • methodSignatureArgumentTypes

        public static java.lang.String[] methodSignatureArgumentTypes​(java.lang.String signature)
                                                               throws ClassFormatException
        Converts argument list portion of method signature to string with all class names compacted.
        Parameters:
        signature - Method signature
        Returns:
        String Array of argument types
        Throws:
        ClassFormatException - if a class is malformed or cannot be interpreted as a class file
      • methodSignatureArgumentTypes

        public static java.lang.String[] methodSignatureArgumentTypes​(java.lang.String signature,
                                                                      boolean chopit)
                                                               throws ClassFormatException
        Converts argument list portion of method signature to string.
        Parameters:
        signature - Method signature
        chopit - flag that determines whether chopping is executed or not
        Returns:
        String Array of argument types
        Throws:
        ClassFormatException - if a class is malformed or cannot be interpreted as a class file
      • methodSignatureReturnType

        public static java.lang.String methodSignatureReturnType​(java.lang.String signature)
                                                          throws ClassFormatException
        Converts return type portion of method signature to string with all class names compacted.
        Parameters:
        signature - Method signature
        Returns:
        String representation of method return type
        Throws:
        ClassFormatException - if a class is malformed or cannot be interpreted as a class file
      • methodSignatureReturnType

        public static java.lang.String methodSignatureReturnType​(java.lang.String signature,
                                                                 boolean chopit)
                                                          throws ClassFormatException
        Converts return type portion of method signature to string.
        Parameters:
        signature - Method signature
        chopit - flag that determines whether chopping is executed or not
        Returns:
        String representation of method return type
        Throws:
        ClassFormatException - if a class is malformed or cannot be interpreted as a class file
      • methodSignatureToString

        public static java.lang.String methodSignatureToString​(java.lang.String signature,
                                                               java.lang.String name,
                                                               java.lang.String access)
        Converts method signature to string with all class names compacted.
        Parameters:
        signature - to convert
        name - of method
        access - flags of method
        Returns:
        Human readable signature
      • methodSignatureToString

        public static java.lang.String methodSignatureToString​(java.lang.String signature,
                                                               java.lang.String name,
                                                               java.lang.String access,
                                                               boolean chopit)
        Converts method signature to string.
        Parameters:
        signature - to convert
        name - of method
        access - flags of method
        chopit - flag that determines whether chopping is executed or not
        Returns:
        Human readable signature
      • methodSignatureToString

        public static java.lang.String methodSignatureToString​(java.lang.String signature,
                                                               java.lang.String name,
                                                               java.lang.String access,
                                                               boolean chopit,
                                                               LocalVariableTable vars)
                                                        throws ClassFormatException
        This method converts a method signature string into a Java type declaration like 'void main(String[])' and throws a 'ClassFormatException' when the parsed type is invalid.
        Parameters:
        signature - Method signature
        name - Method name
        access - Method access rights
        chopit - flag that determines whether chopping is executed or not
        vars - the LocalVariableTable for the method
        Returns:
        Java type declaration
        Throws:
        ClassFormatException - if a class is malformed or cannot be interpreted as a class file
      • methodTypeToSignature

        public static java.lang.String methodTypeToSignature​(java.lang.String ret,
                                                             java.lang.String[] argv)
                                                      throws ClassFormatException
        Converts string containing the method return and argument types to a byte code method signature.
        Parameters:
        ret - Return type of method
        argv - Types of method arguments
        Returns:
        Byte code representation of method signature
        Throws:
        ClassFormatException - if the signature is for Void
      • packageToPath

        public static java.lang.String packageToPath​(java.lang.String name)
        Converts '.'s to '/'s.
        Parameters:
        name - Source
        Returns:
        converted value
        Since:
        6.7.0
      • pathToPackage

        public static java.lang.String pathToPackage​(java.lang.String str)
        Converts a path to a package name.
        Parameters:
        str - the source path.
        Returns:
        a package name.
        Since:
        6.6.0
      • pow2

        private static int pow2​(int n)
      • printArray

        public static java.lang.String printArray​(java.lang.Object[] obj)
      • printArray

        public static java.lang.String printArray​(java.lang.Object[] obj,
                                                  boolean braces)
      • printArray

        public static java.lang.String printArray​(java.lang.Object[] obj,
                                                  boolean braces,
                                                  boolean quote)
      • printArray

        public static void printArray​(java.io.PrintStream out,
                                      java.lang.Object[] obj)
      • printArray

        public static void printArray​(java.io.PrintWriter out,
                                      java.lang.Object[] obj)
      • replace

        public static java.lang.String replace​(java.lang.String str,
                                               java.lang.String old,
                                               java.lang.String new_)
        Replace all occurrences of old in str with new.
        Parameters:
        str - String to permute
        old - String to be replaced
        new_ - Replacement string
        Returns:
        new String object
      • searchOpcode

        public static short searchOpcode​(java.lang.String name)
        Map opcode names to opcode numbers. E.g., return Constants.ALOAD for "aload"
      • setBit

        public static int setBit​(int flag,
                                 int i)
        Returns:
        'flag' with bit 'i' set to 1
      • signatureToString

        public static java.lang.String signatureToString​(java.lang.String signature)
        Converts a signature to a string with all class names compacted. Class, Method and Type signatures are supported. Enum and Interface signatures are not supported.
        Parameters:
        signature - signature to convert
        Returns:
        String containg human readable signature
      • signatureToString

        public static java.lang.String signatureToString​(java.lang.String signature,
                                                         boolean chopit)
        Converts a signature to a string. Class, Method and Type signatures are supported. Enum and Interface signatures are not supported.
        Parameters:
        signature - signature to convert
        chopit - flag that determines whether chopping is executed or not
        Returns:
        String containg human readable signature
      • toHexString

        public static java.lang.String toHexString​(byte[] bytes)
        Convert bytes into hexadecimal string
        Parameters:
        bytes - an array of bytes to convert to hexadecimal
        Returns:
        bytes as hexadecimal string, e.g. 00 fa 12 ...
      • typeOfMethodSignature

        public static byte typeOfMethodSignature​(java.lang.String signature)
                                          throws ClassFormatException
        Return type of method signature as a byte value as defined in Constants
        Parameters:
        signature - in format described above
        Returns:
        type of method signature
        Throws:
        ClassFormatException - if signature is not a method signature
        See Also:
        Const
      • typeOfSignature

        public static byte typeOfSignature​(java.lang.String signature)
                                    throws ClassFormatException
        Return type of signature as a byte value as defined in Constants
        Parameters:
        signature - in format described above
        Returns:
        type of signature
        Throws:
        ClassFormatException - if signature isn't a known type
        See Also:
        Const
      • typeParamTypesToString

        private static java.lang.String typeParamTypesToString​(java.lang.String signature,
                                                               boolean chopit)
        Converts a type parameter list signature to a string.
        Parameters:
        signature - signature to convert
        chopit - flag that determines whether chopping is executed or not
        Returns:
        String containg human readable signature
      • typeParamTypeToString

        private static java.lang.String typeParamTypeToString​(java.lang.String signature,
                                                              boolean chopit)
        Converts a type parameter signature to a string.
        Parameters:
        signature - signature to convert
        chopit - flag that determines whether chopping is executed or not
        Returns:
        String containg human readable signature
      • typeSignaturesToString

        private static java.lang.String typeSignaturesToString​(java.lang.String signature,
                                                               boolean chopit,
                                                               char term)
        Converts a list of type signatures to a string.
        Parameters:
        signature - signature to convert
        chopit - flag that determines whether chopping is executed or not
        term - character indicating the end of the list
        Returns:
        String containg human readable signature
      • typeSignatureToString

        public static java.lang.String typeSignatureToString​(java.lang.String signature,
                                                             boolean chopit)
                                                      throws ClassFormatException
        This method converts a type signature string into a Java type declaration such as 'String[]' and throws a 'ClassFormatException' when the parsed type is invalid.
        Parameters:
        signature - type signature
        chopit - flag that determines whether chopping is executed or not
        Returns:
        string containing human readable type signature
        Throws:
        ClassFormatException - if a class is malformed or cannot be interpreted as a class file
        Since:
        6.4.0
      • unwrap

        private static int unwrap​(java.lang.ThreadLocal<java.lang.Integer> tl)
      • wrap

        private static void wrap​(java.lang.ThreadLocal<java.lang.Integer> tl,
                                 int value)