Class Signatures


  • public final class Signatures
    extends java.lang.Object
    Java specifies 6 major string formats to represent a type. This class contains static methods to test strings, access their parts, and convert among the formats.

    The class is not yet exhaustive; let the maintainers know if it lacks something you need.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Signatures.ClassnameAndDimensions
      A representation of an array: A pair of class name (a binary name or primitive) and the number of array dimensions.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static @BinaryName java.lang.String addPackage​(@Nullable @DotSeparatedIdentifiers java.lang.String packagename, @BinaryName java.lang.String classname)
      Given a package name and a class name, combine them to form a qualified class name.
      static java.lang.String arglistFromJvm​(java.lang.String arglist)
      Convert an argument list from JVML format to Java format.
      static java.lang.String arglistToJvm​(java.lang.String arglist)
      Convert a fully-qualified argument list from Java format to JVML format.
      static @ClassGetName java.lang.String binaryNameToClassGetName​(@BinaryName java.lang.String bn)
      Convert from a BinaryName to the format of Class.getName().
      static @FieldDescriptor java.lang.String binaryNameToFieldDescriptor​(@FqBinaryName java.lang.String typename)
      Convert a binary name to a field descriptor.
      static @FullyQualifiedName java.lang.String binaryNameToFullyQualified​(@BinaryName java.lang.String binaryName)
      Converts a binary name to a fully-qualified name.
      static @BinaryName java.lang.String classfilenameToBaseName​(java.lang.String classfilename)
      Given a filename ending with ".class", return the simple (unqualified) binary name of the class.
      static @BinaryName java.lang.String classfilenameToBinaryName​(java.lang.String classfilename)
      Given a filename ending with ".class", return the binary name of the class.
      static @BinaryName java.lang.String fieldDescriptorToBinaryName​(@FieldDescriptor java.lang.String typename)
      Convert a field descriptor to a binary name.
      static @ClassGetName java.lang.String fieldDescriptorToClassGetName​(@FieldDescriptor java.lang.String fd)
      Convert from a FieldDescriptor to the format of Class.getName().
      static @FullyQualifiedName java.lang.String fieldDescriptorToFullyQualified​(@FieldDescriptor java.lang.String typename)
      Convert a field descriptor to a fully-qualified name.
      static @ClassGetName java.lang.String getArrayElementType​(@FqBinaryName java.lang.String fqBinaryName)
      Returns the element type for the given type name, which results from removing all the array brackets.
      static @BinaryName java.lang.String internalFormToBinaryName​(@InternalForm java.lang.String internalForm)
      Given a class name in internal form, return it in as a binary name.
      static @ClassGetName java.lang.String internalFormToClassGetName​(@InternalForm java.lang.String internalForm)
      Given a class name in internal form, return it in ClassGetName form.
      static @FullyQualifiedName java.lang.String internalFormToFullyQualified​(@InternalForm java.lang.String internalForm)
      Given a class name in internal form, return it in as a fully-qualified name.
      static boolean isArrayWithoutPackage​(java.lang.String s)
      Returns true if the argument has the format of an ArrayWithoutPackage.
      static boolean isBinaryName​(java.lang.String s)
      Returns true if the argument has the format of a BinaryName.
      static boolean isBinaryNameOrPrimitiveType​(java.lang.String s)
      Returns true if the argument has the format of a BinaryNameOrPrimitiveType.
      static boolean isBinaryNameWithoutPackage​(java.lang.String s)
      Returns true if the argument has the format of a BinaryNameWithoutPackage.
      static boolean isClassGetName​(java.lang.String s)
      Returns true if the argument has the format of a ClassGetName.
      static boolean isClassGetSimpleName​(java.lang.String s)
      Returns true if the argument has the format of a ClassGetSimpleName.
      static boolean isDotSeparatedIdentifiers​(java.lang.String s)
      Returns true if the argument has the format of a DotSeparatedIdentifiers.
      static boolean isDotSeparatedIdentifiersOrPrimitiveType​(java.lang.String s)
      Returns true if the argument has the format of a DotSeparatedIdentifiersOrPrimitiveType.
      static boolean isFieldDescriptor​(java.lang.String s)
      Returns true if the argument has the format of a FieldDescriptor.
      static boolean isFieldDescriptorForPrimitive​(java.lang.String s)
      Returns true if the argument has the format of a FieldDescriptorForPrimitive.
      static boolean isFieldDescriptorWithoutPackage​(java.lang.String s)
      Returns true if the argument has the format of a FieldDescriptorWithoutPackage.
      static boolean isFqBinaryName​(java.lang.String s)
      Returns true if the argument has the format of a FqBinaryName.
      static boolean isFullyQualifiedName​(java.lang.String s)
      Returns true if the argument has the format of a FullyQualifiedName.
      static boolean isIdentifier​(java.lang.String s)
      Returns true if the argument has the format of a Identifier.
      static boolean isIdentifierOrPrimitiveType​(java.lang.String s)
      Returns true if the argument has the format of a IdentifierOrPrimitiveType.
      static boolean isInternalForm​(java.lang.String s)
      Returns true if the argument has the format of a InternalForm.
      static boolean isPrimitiveType​(java.lang.String s)
      Returns true if the argument has the format of a PrimitiveType.
      static @Nullable @FieldDescriptor java.lang.String methodDescriptorToReturnType​(@MethodDescriptor java.lang.String methodDescriptor)
      Returns the return type of the given method descriptor, or null if the method is void.
      static @FieldDescriptor java.lang.String primitiveTypeNameToFieldDescriptor​(@PrimitiveType java.lang.String primitiveName)
      Convert a primitive Java type name (e.g., "int", "double", etc.) to a field descriptor (e.g., "I", "D", etc.).
      static @BinaryName java.lang.String[] splitJavaArglist​(java.lang.String javaArglist)
      Split a fully-qualified argument list from Java format into an array of Java-format types.
      static java.util.List<@FieldDescriptor java.lang.String> splitJvmArglist​(java.lang.String jvmArglist)
      Split an argument list from JVML format into an array of JVML format types.
      • Methods inherited from class java.lang.Object

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

      • getArrayElementType

        public static @ClassGetName java.lang.String getArrayElementType​(@FqBinaryName java.lang.String fqBinaryName)
        Returns the element type for the given type name, which results from removing all the array brackets.
        Parameters:
        fqBinaryName - "a fully-qualified binary name" (@FqBinaryNome)
        Returns:
        the base element type of the argument, with all array brackets stripped
      • classfilenameToBinaryName

        public static @BinaryName java.lang.String classfilenameToBinaryName​(java.lang.String classfilename)
        Given a filename ending with ".class", return the binary name of the class.
        Parameters:
        classfilename - the name of a classfile, relative to a directory on the CLASSPATH
        Returns:
        the basename of the classfile
      • classfilenameToBaseName

        public static @BinaryName java.lang.String classfilenameToBaseName​(java.lang.String classfilename)
        Given a filename ending with ".class", return the simple (unqualified) binary name of the class.
        Parameters:
        classfilename - the name of a classfile
        Returns:
        the basename of the classfile
      • addPackage

        public static @BinaryName java.lang.String addPackage​(@Nullable @DotSeparatedIdentifiers java.lang.String packagename,
                                                              @BinaryName java.lang.String classname)
        Given a package name and a class name, combine them to form a qualified class name.
        Parameters:
        packagename - the package name
        classname - the class name
        Returns:
        the qualified class name
      • isArrayWithoutPackage

        @EnsuresQualifierIf(result=true,
                            expression="#1",
                            qualifier=org.checkerframework.checker.signature.qual.ArrayWithoutPackage.class)
        public static boolean isArrayWithoutPackage​(java.lang.String s)
        Returns true if the argument has the format of an ArrayWithoutPackage. The type it refers to might or might not exist.
        Parameters:
        s - a string
        Returns:
        true if the string is a @ArrayWithoutPackage
      • isBinaryName

        @EnsuresQualifierIf(result=true,
                            expression="#1",
                            qualifier=org.checkerframework.checker.signature.qual.BinaryName.class)
        public static boolean isBinaryName​(java.lang.String s)
        Returns true if the argument has the format of a BinaryName. The type it refers to might or might not exist.
        Parameters:
        s - a string
        Returns:
        true if the string is a @BinaryName
      • isBinaryNameWithoutPackage

        @EnsuresQualifierIf(result=true,
                            expression="#1",
                            qualifier=org.checkerframework.checker.signature.qual.BinaryNameWithoutPackage.class)
        public static boolean isBinaryNameWithoutPackage​(java.lang.String s)
        Returns true if the argument has the format of a BinaryNameWithoutPackage. The type it refers to might or might not exist.
        Parameters:
        s - a string
        Returns:
        true if the string is a @BinaryNameWithoutPackage
      • isBinaryNameOrPrimitiveType

        @EnsuresQualifierIf(result=true,
                            expression="#1",
                            qualifier=org.checkerframework.checker.signature.qual.BinaryNameOrPrimitiveType.class)
        public static boolean isBinaryNameOrPrimitiveType​(java.lang.String s)
        Returns true if the argument has the format of a BinaryNameOrPrimitiveType. The type it refers to might or might not exist.
        Parameters:
        s - a string
        Returns:
        true if the string is a @BinaryNameOrPrimitiveType
      • isClassGetName

        @EnsuresQualifierIf(result=true,
                            expression="#1",
                            qualifier=org.checkerframework.checker.signature.qual.ClassGetName.class)
        public static boolean isClassGetName​(java.lang.String s)
        Returns true if the argument has the format of a ClassGetName. The type it refers to might or might not exist.
        Parameters:
        s - a string
        Returns:
        true if the string is a @ClassGetName
      • isClassGetSimpleName

        @EnsuresQualifierIf(result=true,
                            expression="#1",
                            qualifier=org.checkerframework.checker.signature.qual.ClassGetSimpleName.class)
        public static boolean isClassGetSimpleName​(java.lang.String s)
        Returns true if the argument has the format of a ClassGetSimpleName. The type it refers to might or might not exist.
        Parameters:
        s - a string
        Returns:
        true if the string is a @ClassGetSimpleName
      • isDotSeparatedIdentifiers

        @EnsuresQualifierIf(result=true,
                            expression="#1",
                            qualifier=org.checkerframework.checker.signature.qual.DotSeparatedIdentifiers.class)
        public static boolean isDotSeparatedIdentifiers​(java.lang.String s)
        Returns true if the argument has the format of a DotSeparatedIdentifiers. The package or type it refers to might or might not exist.
        Parameters:
        s - a string
        Returns:
        true if the string is a @DotSeparatedIdentifiers
      • isDotSeparatedIdentifiersOrPrimitiveType

        @EnsuresQualifierIf(result=true,
                            expression="#1",
                            qualifier=org.checkerframework.checker.signature.qual.DotSeparatedIdentifiersOrPrimitiveType.class)
        public static boolean isDotSeparatedIdentifiersOrPrimitiveType​(java.lang.String s)
        Returns true if the argument has the format of a DotSeparatedIdentifiersOrPrimitiveType. The package or type it refers to might or might not exist.
        Parameters:
        s - a string
        Returns:
        true if the string is a @DotSeparatedIdentifiersOrPrimitiveType
      • isFieldDescriptor

        @EnsuresQualifierIf(result=true,
                            expression="#1",
                            qualifier=org.checkerframework.checker.signature.qual.FieldDescriptor.class)
        public static boolean isFieldDescriptor​(java.lang.String s)
        Returns true if the argument has the format of a FieldDescriptor. The type it refers to might or might not exist.
        Parameters:
        s - a string
        Returns:
        true if the string is a @FieldDescriptor
      • isFieldDescriptorWithoutPackage

        @EnsuresQualifierIf(result=true,
                            expression="#1",
                            qualifier=org.checkerframework.checker.signature.qual.FieldDescriptorWithoutPackage.class)
        public static boolean isFieldDescriptorWithoutPackage​(java.lang.String s)
        Returns true if the argument has the format of a FieldDescriptorWithoutPackage. The type it refers to might or might not exist.
        Parameters:
        s - a string
        Returns:
        true if the string is a @FieldDescriptorWithoutPackage
      • isFieldDescriptorForPrimitive

        @EnsuresQualifierIf(result=true,
                            expression="#1",
                            qualifier=org.checkerframework.checker.signature.qual.FieldDescriptorForPrimitive.class)
        public static boolean isFieldDescriptorForPrimitive​(java.lang.String s)
        Returns true if the argument has the format of a FieldDescriptorForPrimitive.
        Parameters:
        s - a string
        Returns:
        true if the string is a @FieldDescriptorForPrimitive
      • isFqBinaryName

        @EnsuresQualifierIf(result=true,
                            expression="#1",
                            qualifier=org.checkerframework.checker.signature.qual.FqBinaryName.class)
        public static boolean isFqBinaryName​(java.lang.String s)
        Returns true if the argument has the format of a FqBinaryName. The type it refers to might or might not exist.
        Parameters:
        s - a string
        Returns:
        true if the string is a @FqBinaryName
      • isFullyQualifiedName

        @EnsuresQualifierIf(result=true,
                            expression="#1",
                            qualifier=org.checkerframework.checker.signature.qual.FullyQualifiedName.class)
        public static boolean isFullyQualifiedName​(java.lang.String s)
        Returns true if the argument has the format of a FullyQualifiedName. The type it refers to might or might not exist.
        Parameters:
        s - a string
        Returns:
        true if the string is a @FullyQualifiedName
      • isIdentifier

        @EnsuresQualifierIf(result=true,
                            expression="#1",
                            qualifier=org.checkerframework.checker.signature.qual.Identifier.class)
        public static boolean isIdentifier​(java.lang.String s)
        Returns true if the argument has the format of a Identifier. The type it refers to might or might not exist.
        Parameters:
        s - a string
        Returns:
        true if the string is a @Identifier
      • isIdentifierOrPrimitiveType

        @EnsuresQualifierIf(result=true,
                            expression="#1",
                            qualifier=org.checkerframework.checker.signature.qual.IdentifierOrPrimitiveType.class)
        public static boolean isIdentifierOrPrimitiveType​(java.lang.String s)
        Returns true if the argument has the format of a IdentifierOrPrimitiveType. The type it refers to might or might not exist.
        Parameters:
        s - a string
        Returns:
        true if the string is a @IdentifierOrPrimitiveType
      • isInternalForm

        @EnsuresQualifierIf(result=true,
                            expression="#1",
                            qualifier=org.checkerframework.checker.signature.qual.InternalForm.class)
        public static boolean isInternalForm​(java.lang.String s)
        Returns true if the argument has the format of a InternalForm. The type it refers to might or might not exist.
        Parameters:
        s - a string
        Returns:
        true if the string is a @InternalForm
      • isPrimitiveType

        @EnsuresQualifierIf(result=true,
                            expression="#1",
                            qualifier=org.checkerframework.checker.signature.qual.PrimitiveType.class)
        public static boolean isPrimitiveType​(java.lang.String s)
        Returns true if the argument has the format of a PrimitiveType.
        Parameters:
        s - a string
        Returns:
        true if the string is a @PrimitiveType
      • binaryNameToFieldDescriptor

        public static @FieldDescriptor java.lang.String binaryNameToFieldDescriptor​(@FqBinaryName java.lang.String typename)
        Convert a binary name to a field descriptor. For example, convert "java.lang.Object[]" to "[Ljava/lang/Object;" or "int" to "I" or "pkg.Outer$Inner" to "Lpkg/Outer$Inner;".

        There are no binary names for primitives or array types. Nonetheless, this method works for them. It converts "java.lang.Object[]" to "[Ljava/lang/Object;" or "int" to "I".

        Parameters:
        typename - name of the type, in fully-qualified binary name format
        Returns:
        name of the class, in field descriptor format
      • primitiveTypeNameToFieldDescriptor

        public static @FieldDescriptor java.lang.String primitiveTypeNameToFieldDescriptor​(@PrimitiveType java.lang.String primitiveName)
        Convert a primitive Java type name (e.g., "int", "double", etc.) to a field descriptor (e.g., "I", "D", etc.).
        Parameters:
        primitiveName - name of the type, in Java format
        Returns:
        name of the type, in field descriptor format
        Throws:
        java.lang.IllegalArgumentException - if primitiveName is not a valid primitive type name
      • binaryNameToClassGetName

        public static @ClassGetName java.lang.String binaryNameToClassGetName​(@BinaryName java.lang.String bn)
        Convert from a BinaryName to the format of Class.getName().

        There are no binary names for primitives or array types. Nonetheless, this method works for them. It converts "java.lang.Object[]" to "[Ljava.lang.Object;" or "int" to "int".

        Parameters:
        bn - the binary name to convert
        Returns:
        the class name, in Class.getName format
      • binaryNameToFullyQualified

        public static @FullyQualifiedName java.lang.String binaryNameToFullyQualified​(@BinaryName java.lang.String binaryName)
        Converts a binary name to a fully-qualified name.
        Parameters:
        binaryName - a type in binary name format
        Returns:
        a fully-qualified name
      • fieldDescriptorToClassGetName

        public static @ClassGetName java.lang.String fieldDescriptorToClassGetName​(@FieldDescriptor java.lang.String fd)
        Convert from a FieldDescriptor to the format of Class.getName().
        Parameters:
        fd - the class, in field descriptor format
        Returns:
        the class name, in Class.getName format
      • fieldDescriptorToBinaryName

        public static @BinaryName java.lang.String fieldDescriptorToBinaryName​(@FieldDescriptor java.lang.String typename)
        Convert a field descriptor to a binary name. For example, convert "[Ljava/util/Map$Entry;" to "java.lang.Map$Entry[]" or "I" to "int".
        Parameters:
        typename - a field descriptor (the name of a type in JVML format)
        Returns:
        the corresponding binary name
      • fieldDescriptorToFullyQualified

        public static @FullyQualifiedName java.lang.String fieldDescriptorToFullyQualified​(@FieldDescriptor java.lang.String typename)
        Convert a field descriptor to a fully-qualified name. For example, convert "[Ljava/util/Map$Entry;" to "java.util.Map.Entry[]" or "I" to "int".
        Parameters:
        typename - a field descriptor (the name of a type in JVML format)
        Returns:
        the corresponding fully-qualified name (what you would write in Java source code)
      • internalFormToClassGetName

        public static @ClassGetName java.lang.String internalFormToClassGetName​(@InternalForm java.lang.String internalForm)
        Given a class name in internal form, return it in ClassGetName form.
        Parameters:
        internalForm - a class name in internal form
        Returns:
        the class name in ClassGetName form
      • internalFormToBinaryName

        public static @BinaryName java.lang.String internalFormToBinaryName​(@InternalForm java.lang.String internalForm)
        Given a class name in internal form, return it in as a binary name.
        Parameters:
        internalForm - a class name in internal form
        Returns:
        the class name sa a binary name
      • internalFormToFullyQualified

        public static @FullyQualifiedName java.lang.String internalFormToFullyQualified​(@InternalForm java.lang.String internalForm)
        Given a class name in internal form, return it in as a fully-qualified name.
        Parameters:
        internalForm - a type in internal form
        Returns:
        a fully-qualified name
      • splitJavaArglist

        public static @BinaryName java.lang.String[] splitJavaArglist​(java.lang.String javaArglist)
        Split a fully-qualified argument list from Java format into an array of Java-format types. For example, convert "(java.lang.Integer[], int, java.lang.Integer[][])" to ["java.lang.Integer[]", "int", "java.lang.Integer[][]"].
        Parameters:
        javaArglist - an argument list, in Java format
        Returns:
        argument list, in JVML format
      • arglistToJvm

        public static java.lang.String arglistToJvm​(java.lang.String arglist)
        Convert a fully-qualified argument list from Java format to JVML format. For example, convert "(java.lang.Integer[], int, java.lang.Integer[][])" to "([Ljava/lang/Integer;I[[Ljava/lang/Integer;)".
        Parameters:
        arglist - an argument list, in Java format
        Returns:
        argument list, in JVML format
      • splitJvmArglist

        public static java.util.List<@FieldDescriptor java.lang.String> splitJvmArglist​(java.lang.String jvmArglist)
        Split an argument list from JVML format into an array of JVML format types. For example, convert "([Ljava/lang/Integer;I[[Ljava/lang/Integer;)" to ["[Ljava/lang/Integer;", "I", "[[Ljava/lang/Integer;"].
        Parameters:
        jvmArglist - an argument list, in JVML format
        Returns:
        argument list, in JVML format
      • arglistFromJvm

        public static java.lang.String arglistFromJvm​(java.lang.String arglist)
        Convert an argument list from JVML format to Java format. For example, convert "([Ljava/lang/Integer;I[[Ljava/lang/Integer;)" to "(java.lang.Integer[], int, java.lang.Integer[][])".
        Parameters:
        arglist - an argument list, in JVML format
        Returns:
        argument list, in Java format
      • methodDescriptorToReturnType

        public static @Nullable @FieldDescriptor java.lang.String methodDescriptorToReturnType​(@MethodDescriptor java.lang.String methodDescriptor)
        Returns the return type of the given method descriptor, or null if the method is void.
        Parameters:
        methodDescriptor - a method descriptor
        Returns:
        the return type of the given method descriptor, or null if the method is void