Class CstString

All Implemented Interfaces:
TypeBearer, ToHuman, Comparable<Constant>

public final class CstString extends TypedConstant
Constants of type CONSTANT_Utf8_info or CONSTANT_String_info.
  • Field Details

    • EMPTY_STRING

      public static final CstString EMPTY_STRING
      non-null; instance representing "", that is, the empty string
  • Constructor Details

    • CstString

      public CstString(String string)
      Constructs an instance from a String.
      Parameters:
      string - non-null; the UTF-8 value as a string
    • CstString

      public CstString(ByteArray bytes)
      Constructs an instance from some UTF-8 bytes.
      Parameters:
      bytes - non-null; array of the UTF-8 bytes
  • Method Details

    • stringToUtf8Bytes

      public static byte[] stringToUtf8Bytes(String string)
      Converts a string into its MUTF-8 form. MUTF-8 differs from normal UTF-8 in the handling of character '\0' and surrogate pairs.
      Parameters:
      string - non-null; the string to convert
      Returns:
      non-null; the UTF-8 bytes for it
    • utf8BytesToString

      public static String utf8BytesToString(ByteArray bytes)
      Converts an array of UTF-8 bytes into a string.
      Parameters:
      bytes - non-null; the bytes to convert
      Returns:
      non-null; the converted string
    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • compareTo0

      protected int compareTo0(Constant other)
      Compare the values of this and another instance, which are guaranteed to be of the same class. Subclasses must implement this.
      Specified by:
      compareTo0 in class Constant
      Parameters:
      other - non-null; the instance to compare to
      Returns:
      -1, 0, or 1, as usual for a comparison
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • typeName

      public String typeName()
      Returns the human name for the particular type of constant this instance is.
      Specified by:
      typeName in class Constant
      Returns:
      non-null; the name
    • isCategory2

      public boolean isCategory2()
      Returns true if this instance is a category-2 constant, meaning it takes up two slots in the constant pool, or false if this instance is category-1.
      Specified by:
      isCategory2 in class Constant
      Returns:
      true iff this instance is category-2
    • toHuman

      public String toHuman()
      Return the "human" string form of this instance. This is generally less "debuggy" than toString().
      Returns:
      non-null; the human string form
    • toQuoted

      public String toQuoted()
      Gets the value as a human-oriented string, surrounded by double quotes.
      Returns:
      non-null; the quoted string
    • toQuoted

      public String toQuoted(int maxLength)
      Gets the value as a human-oriented string, surrounded by double quotes, but ellipsizes the result if it is longer than the given maximum length
      Parameters:
      maxLength - >= 5; the maximum length of the string to return
      Returns:
      non-null; the quoted string
    • getString

      public String getString()
      Gets the UTF-8 value as a string. The returned string is always already interned.
      Returns:
      non-null; the UTF-8 value as a string
    • getBytes

      public ByteArray getBytes()
      Gets the UTF-8 value as UTF-8 encoded bytes.
      Returns:
      non-null; an array of the UTF-8 bytes
    • getUtf8Size

      public int getUtf8Size()
      Gets the size of this instance as UTF-8 code points. That is, get the number of bytes in the UTF-8 encoding of this instance.
      Returns:
      >= 0; the UTF-8 size
    • getUtf16Size

      public int getUtf16Size()
      Gets the size of this instance as UTF-16 code points. That is, get the number of 16-bit chars in the UTF-16 encoding of this instance. This is the same as the length of the Java String representation of this instance.
      Returns:
      >= 0; the UTF-16 size
    • getType

      public Type getType()
      Description copied from interface: TypeBearer
      Gets the type associated with this instance.
      Returns:
      non-null; the type