What is Class Descriptor?

前端 未结 3 1173
既然无缘
既然无缘 2021-02-04 15:04

What is Class Descriptor?

Is it a Class object of a particular class?

相关标签:
3条回答
  • 2021-02-04 15:15

    Yes, a Class object is a class descriptor for a certain "class".

    From the API:

    Instances of this class represent classes and interfaces in a running Java application. An enum is a kind of class and an annotation is a kind of interface. Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.

    Here's an example of a simple usage of Class methods to reflectively describe types:

    static void describe(Class<?> clazz, String pad, String leadin) {
        if (clazz == null) return;
        String type =
            clazz.isInterface() ? "interface" :
            clazz.isArray() ? "array" :
            clazz.isPrimitive() ? "primitive" :
            clazz.isEnum() ? "enum" :
            "class";
        System.out.printf("%s%s%s %s ( %s )%n",
            pad, leadin, type, clazz.getSimpleName(), clazz.getName());
        for (Class<?> interfaze : clazz.getInterfaces()) {
            describe(interfaze, pad + "   ", "implements ");
        }
        describe(clazz.getComponentType(), pad + "   ", "elements are ");
        describe(clazz.getSuperclass(), pad + "   ", "extends ");
    }
    static void describe(Class<?> clazz) {
        describe(clazz, "", "");
        System.out.println();
    }
    public static void main(String[] args) {
        describe(boolean[][].class);
        describe(java.math.RoundingMode.class);
        describe(java.util.ArrayList.class);
        describe(void.class);
    }
    

    The above snippet produces the following output:

    array boolean[][] ( [[Z )
       implements interface Cloneable ( java.lang.Cloneable )
       implements interface Serializable ( java.io.Serializable )
       elements are array boolean[] ( [Z )
          implements interface Cloneable ( java.lang.Cloneable )
          implements interface Serializable ( java.io.Serializable )
          elements are primitive boolean ( boolean )
          extends class Object ( java.lang.Object )
       extends class Object ( java.lang.Object )
    
    enum RoundingMode ( java.math.RoundingMode )
       extends class Enum ( java.lang.Enum )
          implements interface Comparable ( java.lang.Comparable )
          implements interface Serializable ( java.io.Serializable )
          extends class Object ( java.lang.Object )
    
    class ArrayList ( java.util.ArrayList )
       implements interface List ( java.util.List )
          implements interface Collection ( java.util.Collection )
             implements interface Iterable ( java.lang.Iterable )
       implements interface RandomAccess ( java.util.RandomAccess )
       implements interface Cloneable ( java.lang.Cloneable )
       implements interface Serializable ( java.io.Serializable )
       extends class AbstractList ( java.util.AbstractList )
          implements interface List ( java.util.List )
             implements interface Collection ( java.util.Collection )
                implements interface Iterable ( java.lang.Iterable )
          extends class AbstractCollection ( java.util.AbstractCollection )
             implements interface Collection ( java.util.Collection )
                implements interface Iterable ( java.lang.Iterable )
             extends class Object ( java.lang.Object )
    
    primitive void ( void )
    

    API links

    • Class.getName()
      • Explains the "funky" names for arrays and primitives

    References

    • Java Tutorials/Reflection API
      • See also: Effective Java 2nd Edition, Item 53: Prefer interfaces to reflection
    • JLS 15.8.2 Class literals
    0 讨论(0)
  • 2021-02-04 15:24

    Yes. See the Class docs.

    0 讨论(0)
  • 2021-02-04 15:37

    You've given very little context, but "class descriptor" can be a termed used to describe the data necessary to deserialize an object:

    http://java.sun.com/javase/6/docs/platform/serialization/spec/class.html

    In that case, a "class descriptor" is actually java.io.ObjectStreamClass. An ObjectStreamClass describes a class, but it is different from the class itself.

    0 讨论(0)
提交回复
热议问题