ClassDesc interface in Java

java.lang.constant.ClassDesc

A nominal descriptor for a Class constant.

For common system types, including all the primitive types, there are predefined ClassDesc constants in ConstantDescs.To create a ClassDesc for a class or interface type, use of or of Descriptor(String); to create a ClassDesc for an array type, use of Descriptor(String), or first obtain a ClassDesc for the component type and then call the arrayType() or arrayType(int) methods.

Superinterfaces:

ConstantDesc

TypeDescriptor

TypeDescriptor.OfField<ClassDesc>

Declaration

public interface ClassDesc extends ConstantDesc, TypeDescriptor.OfField<ClassDesc> {

    static ClassDesc of(String name) {
        ConstantUtils.validateBinaryClassName(requireNonNull(name));
        return ClassDesc.ofDescriptor("L" + binaryToInternal(name) + ";");
    }

    static ClassDesc of(String packageName, String className) {
        ConstantUtils.validateBinaryClassName(requireNonNull(packageName));
        if (packageName.isEmpty()) {
            return of(className);
        }
        validateMemberName(requireNonNull(className), false);
        return ofDescriptor(
                "L" + binaryToInternal(packageName) + (packageName.length() > 0 ? "/" : "") + className + ";");
    }

    static ClassDesc ofDescriptor(String descriptor) {
        requireNonNull(descriptor);
        if (descriptor.isEmpty()) {
            throw new IllegalArgumentException("not a valid reference type descriptor: " + descriptor);
        }
        int depth = ConstantUtils.arrayDepth(descriptor);
        if (depth > ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS) {
            throw new IllegalArgumentException("Cannot create an array type descriptor with more than "
                    + ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS + " dimensions");
        }
        return (descriptor.length() == 1) ? new PrimitiveClassDescImpl(descriptor)
                : new ReferenceClassDescImpl(descriptor);
    }

    default ClassDesc arrayType() {
        int depth = ConstantUtils.arrayDepth(descriptorString());
        if (depth >= ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS) {
            throw new IllegalStateException("Cannot create an array type descriptor with more than "
                    + ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS + " dimensions");
        }
        return arrayType(1);
    }

    default ClassDesc arrayType(int rank) {
        int currentDepth = ConstantUtils.arrayDepth(descriptorString());
        if (rank <= 0 || currentDepth + rank > ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS)
            throw new IllegalArgumentException("rank: " + currentDepth + rank);
        return ClassDesc.ofDescriptor("[".repeat(rank) + descriptorString());
    }

    default ClassDesc nested(String nestedName) {
        validateMemberName(nestedName, false);
        if (!isClassOrInterface())
            throw new IllegalStateException("Outer class is not a class or interface type");
        return ClassDesc.ofDescriptor(dropLastChar(descriptorString()) + "$" + nestedName + ";");
    }

    default ClassDesc nested(String firstNestedName, String... moreNestedNames) {
        if (!isClassOrInterface())
            throw new IllegalStateException("Outer class is not a class or interface type");
        validateMemberName(firstNestedName, false);
        requireNonNull(moreNestedNames);
        for (String addNestedNames : moreNestedNames) {
            validateMemberName(addNestedNames, false);
        }
        return moreNestedNames.length == 0 ? nested(firstNestedName)
                : nested(firstNestedName + Stream.of(moreNestedNames).collect(joining("$", "$", "")));
    }

    default boolean isArray() {
        return descriptorString().startsWith("[");
    }

    default boolean isPrimitive() {
        return descriptorString().length() == 1;
    }

    default boolean isClassOrInterface() {
        return descriptorString().startsWith("L");
    }

    default ClassDesc componentType() {
        return isArray() ? ClassDesc.ofDescriptor(descriptorString().substring(1)) : null;
    }

    default String packageName() {
        if (!isClassOrInterface())
            return "";
        String className = internalToBinary(ConstantUtils.dropFirstAndLastChar(descriptorString()));
        int index = className.lastIndexOf('.');
        return (index == -1) ? "" : className.substring(0, index);
    }

    default String displayName() {
        if (isPrimitive())
            return Wrapper.forBasicType(descriptorString().charAt(0)).primitiveSimpleName();
        else if (isClassOrInterface()) {
            return descriptorString().substring(Math.max(1, descriptorString().lastIndexOf('/') + 1),
                    descriptorString().length() - 1);
        } else if (isArray()) {
            int depth = ConstantUtils.arrayDepth(descriptorString());
            ClassDesc c = this;
            for (int i = 0; i < depth; i++)
                c = c.componentType();
            return c.displayName() + "[]".repeat(depth);
        } else
            throw new IllegalStateException(descriptorString());
    }

    String descriptorString();

    boolean equals(Object o);
}

Methods

1. of(String packageName, String className)

ClassDesc java.lang.constant.ClassDesc.of(String packageName, String className)

This method takes two arguments. This method returns a ClassDesc for a class or interface type, given a package name and the unqualified name for the class or interface.

Parameters: Two parameters are required for this method.

packageName: the package name (dot-separated); if the package name is the empty string, the class is considered to be in the unnamed package.

className: the unqualified (simple) class name.

Returns: a ClassDesc describing the desired class.

Throws:

1. NullPointerException - if any argument is null.

2. IllegalArgumentException - if the package name or class name is not in the correct format


2. ofDescriptor(String descriptor)

ClassDesc java.lang.constant.ClassDesc.ofDescriptor(String descriptor)

This method takes one argument. This method returns a ClassDesc given a descriptor string for a class, interface, array, or primitive type.

Parameters: One parameter is required for this method.

descriptor: a field descriptor string.

Returns: ClassDesc describing the desired class.

Throws:

1. NullPointerException - if the argument is null.

2. IllegalArgumentException - if the name string is not in the correct format.


3. of(String name)

ClassDesc java.lang.constant.ClassDesc.of(String name)

This method takes one argument. This method returns a ClassDesc for a class or interface type, given the class name or interface, such as "java.lang.String".

Parameters: One parameter is required for this method.

name: the fully qualified (dot-separated) binary class name.

Returns: a ClassDesc describing the desired class.

Throws:

1. NullPointerException - if the argument is null.

2. IllegalArgumentException - if the name string is not in the correct format.


4. arrayType()

ClassDesc java.lang.constant.ClassDesc.arrayType()

This method returns a ClassDesc for an array type whose component type is described by this ClassDesc.

Returns: a ClassDesc describing the array type.

Throws:

1. IllegalStateException - if the resulting ClassDesc would have an array rank of greater than 255.


5. arrayType(int rank)

ClassDesc java.lang.constant.ClassDesc.arrayType(int rank)

This method takes one argument. This method returns a ClassDesc for an array type of the specified rank, whose component type is described by this ClassDesc.

Parameters: One parameter is required for this method.

rank: the rank of the array.

Returns: a ClassDesc describing the array type.

Throws:

1. IllegalArgumentException - if the rank is less than or equal to zero or if the rank of the resulting array type is greater than 255.


6. nested(String nestedName)

ClassDesc java.lang.constant.ClassDesc.nested(String nestedName)

This method takes one argument. This method returns a ClassDesc for a nested class of the class or interface type described by this ClassDesc.

Parameters: One parameter is required for this method.

nestedName: the unqualified name of the nested class.

Returns: a ClassDesc describing the nested class.

Throws:

1. NullPointerException - if the argument is null.

2. IllegalStateException - if this ClassDesc does not describe a class or interface type.

3. IllegalArgumentException - if the nested class name is invalid.


7. nested(String firstNestedName, String... moreNestedNames)

ClassDesc java.lang.constant.ClassDesc.nested(String firstNestedName, String... moreNestedNames)

This method takes two arguments. This method returns a ClassDesc for a nested class of the class or interface type described by this ClassDesc.

Parameters: Two parameters are required for this method.

firstNestedName: the unqualified name of the first level of the nested class.

moreNestedNames: the unqualified name(s) of the remaining levels of the nested class.

Returns: a ClassDesc describing the nested class.

Throws:

1. NullPointerException - if any argument or its contents is null.

2. IllegalStateException - if this ClassDesc does not describe a class or interface type.

3. IllegalArgumentException - if the nested class name is invalid.


8. isArray()

boolean java.lang.constant.ClassDesc.isArray()

This method returns whether this ClassDesc describes an array type.

Returns: whether this ClassDesc describes an array type.


9. isPrimitive()

boolean java.lang.constant.ClassDesc.isPrimitive()

This method returns whether this ClassDesc describes a primitive type.

Returns: whether this ClassDesc describes a primitive type.


10. isClassOrInterface()

boolean java.lang.constant.ClassDesc.isClassOrInterface()

This method returns whether this ClassDesc describes a class or interface type.

Returns: whether this ClassDesc describes a class or interface type.


11. componentType()

ClassDesc java.lang.constant.ClassDesc.componentType()

This method returns the component type of this ClassDesc, if it describes an array type, or null otherwise.

Returns: a ClassDesc describing the component type, or null if this descriptor does not describe an array type.


12. packageName()

String java.lang.constant.ClassDesc.packageName()

This method returns the package name of this ClassDesc if it describes a class or interface type.

Returns: the package name, or the empty string if the class is in the default package, or this ClassDesc does not describe a class or interface type.


13. displayName()

String java.lang.constant.ClassDesc.displayName()

This method returns a human-readable name for the type described by this descriptor.

Returns: the human-readable name.


14. descriptorString()

String java.lang.constant.ClassDesc.descriptorString()

This method returns a field type descriptor string for this type

Returns: the descriptor string.


15. equals(Object o)

boolean java.lang.constant.ClassDesc.equals(Object o)

This method takes one argument. This method compares the specified object with this descriptor for equality. Returns true if and only if the specified object is also a ClassDesc and both describe the same type.

Parameters: One parameter is required for this method.

o: the other object.

Returns: whether this descriptor is equal to the other object.


Some other classes/interfaces of java.lang.constant

MethodTypeDescA nominal descriptor for a MethodType constant.

MethodHandleDescA nominal descriptor for a MethodHandle constant.

DynamicConstantDescA nominal descriptor for a dynamic constant.

DynamicCallSiteDescA nominal descriptor for an invoke dynamic call site.

DirectMethodHandleDescA nominal descriptor for a direct MethodHandle. A DirectMethodHandleDesc corresponds to a Constant_MethodHandle_info entry in the constant pool of a class file.

ConstantDescsPredefined values of nominal descriptors for common constants, including descriptors for primitive class types and other common platform types, and descriptors for method handles for standard bootstrap methods.

ConstantDescA nominal descriptor for a loadable constant value. Such a descriptor can be resolved via ConstantDesc.resolveConstantDesc(MethodHandles.Lookup) to yield the constant value itself.

ConstableRepresents a type that is constable. A constable type is one whose values are constants that can be represented in the constant pool of a Java class file.

ClassDescA nominal descriptor for a Class constant.

No comments:

Post a Comment