Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Class Descriptor?

Tags:

java

What is Class Descriptor?

Is it a Class object of a particular class?

like image 432
learner Avatar asked May 24 '10 06:05

learner


People also ask

What is a descriptor object?

“Descriptors” are objects that describe some attribute of an object. They are found in the dictionary of type objects.

What is method descriptor in Java?

A MethodDescriptor describes a particular method that a Java Bean supports for external access from other components.

What are descriptors in Python?

Descriptors are Python objects that implement a method of the descriptor protocol, which gives you the ability to create objects that have special behavior when they're accessed as attributes of other objects.

What is __ set __ in Python?

The __set__() method is invoked when the value is set to the attribute, and unlike the __get__() method, it returns nothing. It has two arguments apart from the descriptor object itself, i.e., the instance which is the same as the __get__() method and the value argument, which is the value you assign to the attribute.


1 Answers

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
like image 157
polygenelubricants Avatar answered Oct 16 '22 03:10

polygenelubricants