使用Java反射機(jī)制湿颅,你可以在運(yùn)行時(shí)檢視Java類(lèi)。檢視Java類(lèi)通常是你使用反射機(jī)制的第一步菩貌。從類(lèi)對(duì)象你可以獲取到如下信息
- Class Name(類(lèi)名稱(chēng))
- Class Modifies (類(lèi)修飾 public, private, synchronized etc.)
- Package Info (包信息)
- Superclass (父類(lèi))
- Implemented Interfaces (實(shí)現(xiàn)接口)
- Constructors (構(gòu)造函數(shù))
- Methods (方法)
- Fields (變量)
- Annotations (注解)
還有其它一些和Java類(lèi)相關(guān)的信息哪亿。完整的列表可以查看 JavaDoc for java.lang.Class檩电。本文將簡(jiǎn)單的描述上面列出來(lái)的信息的獲取,部分主題也會(huì)在后續(xù)單獨(dú)文章中進(jìn)行詳細(xì)說(shuō)明现斋。舉個(gè)例子喜最,本文將向你展示如何獲取全部方法或特定方法,同時(shí)會(huì)有一篇單獨(dú)的文章向你展示如何調(diào)用方法庄蹋、如何通過(guò)給定參數(shù)集合來(lái)從一組相同方法名的方法集中查找匹配的方法瞬内、通過(guò)反射調(diào)用方法會(huì)拋什么異常迷雪、如何查找 getter/setter
方法等。本文主要介紹Class
對(duì)象虫蝶,以及通過(guò)它你可以獲取的信息振乏。
The Class Object
在對(duì)一個(gè)類(lèi)做任何檢視之前,我們需要首先獲取一個(gè)Class
對(duì)象秉扑,在Java中任何類(lèi)型慧邮,包括基本類(lèi)型(int,float,long etc)、數(shù)組都有與之關(guān)聯(lián)的類(lèi)對(duì)象舟陆。如果在編譯階段你就知道一個(gè)類(lèi)的名稱(chēng)误澳,你可以像下面的代碼一樣來(lái)獲取一個(gè)類(lèi)對(duì)象:
<pre>
Class myObjectClass = MyObject.class
</pre>
如果你在編譯的階段不知道類(lèi)的名稱(chēng),但是在運(yùn)行時(shí)有字符串形式的類(lèi)名秦躯,那么可以像下面的代碼一樣來(lái)獲取一個(gè)類(lèi)對(duì)象
<pre>
String className = ... //obtain class name as string at runtime
Class class = Class.forName(className);
</pre>
在使用Class.forName()
時(shí)忆谓,你必須確保使用了類(lèi)名稱(chēng)的完整形式,也就是包含有包名的類(lèi)名稱(chēng)踱承。比如倡缠,如果類(lèi)MyObject
位于com.jenkov.myapp
包路徑下,那么完整的類(lèi)名稱(chēng)為com.jenkov.myapp.MyObject
茎活。
如果在運(yùn)行時(shí)的classpath中找不到對(duì)應(yīng)的類(lèi)昙沦,Class.forName()
方法可能會(huì)拋出ClassNotFoundException
異常。
Class Name
From a Class
object you can obtain its name in two versions. The fully qualified class name (including package name) is obtained using the getName()
method like this:
Class aClass = ... //obtain Class object. See prev. section String className = aClass.getName();
If you want the class name without the pacakge name you can obtain it using the getSimpleName()
method, like this:
Class aClass = ... //obtain Class object. See prev. section String simpleClassName = aClass.getSimpleName();
Modifiers
You can access the modifiers of a class via the Class
object. The class modifiers are the keywords "public", "private", "static" etc. You obtain the class modifiers like this:
Class aClass = ... //obtain Class object. See prev. section int modifiers = aClass.getModifiers();
The modifiers are packed into an int
where each modifier is a flag bit that is either set or cleared. You can check the modifiers using these methods in the class java.lang.reflect.Modifier
:
Modifier.isAbstract(int modifiers) Modifier.isFinal(int modifiers) Modifier.isInterface(int modifiers) Modifier.isNative(int modifiers) Modifier.isPrivate(int modifiers) Modifier.isProtected(int modifiers) Modifier.isPublic(int modifiers) Modifier.isStatic(int modifiers) Modifier.isStrict(int modifiers) Modifier.isSynchronized(int modifiers) Modifier.isTransient(int modifiers) Modifier.isVolatile(int modifiers)
Package Info
You can obtain information about the package from a Class
object like this:
Class aClass = ... //obtain Class object. See prev. sectionPackage package = aClass.getPackage();
From the Package
object you have access to information about the package like its name. You can also access information specified for this package in the Manifest
file of the JAR file this package is located in on the classpath. For instance, you can specify package version numbers in the Manifest
file. You can read more about the Package
class here: java.lang.Package
Superclass
From the Class
object you can access the superclass of the class. Here is how:
Class superclass = aClass.getSuperclass();
The superclass class object is a Class
object like any other, so you can continue doing class reflection on that too.
Implemented Interfaces
It is possible to get a list of the interfaces implemented by a given class. Here is how:
Class aClass = ... //obtain Class object. See prev. sectionClass[] interfaces = aClass.getInterfaces();
A class can implement many interfaces. Therefore an array of Class
is returned. Interfaces are also represented by Class
objects in Java Reflection.
NOTE: Only the interfaces specifically declared implemented by a given class is returned. If a superclass of the class implements an interface, but the class doesn't specifically state that it also implements that interface, that interface will not be returned in the array. Even if the class in practice implements that interface, because the superclass does.
To get a complete list of the interfaces implemented by a given class you will have to consult both the class and its superclasses recursively.
Constructors
You can access the constructors of a class like this:
Constructor[] constructors = aClass.getConstructors();
Constructors are covered in more detail in the text on Constructors.
Methods
You can access the methods of a class like this:
<pre>
Method[] method = aClass.getMethods();
</pre>
Methods are covered in more detail in the text on Methods.
Fields
You can access the fields (member variables) of a class like this:
Field[] method = aClass.getFields();
Fields are covered in more detail in the text on Fields.
Annotations
You can access the class annotations of a class like this:
Annotation[] annotations = aClass.getAnnotations();
Annotations are covered in more detail in the text on Annotations.