什么是类描述符(Class Descriptor)?
它是一个特定类的Class
对象吗?
Class
对象是某个“类”的类描述符。Class
对象。Class
方法进行反射描述类型的示例: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);
}
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 )
Class.getName()
您提供的上下文很少,但是“类描述符”可以用来描述反序列化对象所需的数据:
http://java.sun.com/javase/6/docs/platform/serialization/spec/class.html
在这种情况下,“类描述符”实际上是java.io.ObjectStreamClass
。 ObjectStreamClass描述一个类,但它与类本身不同。