Java反射機(jī)制:類(lèi)

使用Java反射機(jī)制湿颅,你可以在運(yùn)行時(shí)檢視Java類(lèi)。檢視Java類(lèi)通常是你使用反射機(jī)制的第一步菩貌。從類(lèi)對(duì)象你可以獲取到如下信息

還有其它一些和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.

Next: Java Reflection - Constructors

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末载荔,一起剝皮案震驚了整個(gè)濱河市盾饮,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌懒熙,老刑警劉巖丘损,帶你破解...
    沈念sama閱讀 216,919評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件谜嫉,死亡現(xiàn)場(chǎng)離奇詭異嘴瓤,居然都是意外死亡移盆,警方通過(guò)查閱死者的電腦和手機(jī)蝴乔,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,567評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén)插佛,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)癞尚,“玉大人毡惜,你說(shuō)我怎么就攤上這事笆怠∈哒悖” “怎么了猪落?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,316評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)畴博。 經(jīng)常有香客問(wèn)我笨忌,道長(zhǎng),這世上最難降的妖魔是什么俱病? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,294評(píng)論 1 292
  • 正文 為了忘掉前任官疲,我火速辦了婚禮袱结,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘途凫。我一直安慰自己垢夹,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,318評(píng)論 6 390
  • 文/花漫 我一把揭開(kāi)白布维费。 她就那樣靜靜地躺著果元,像睡著了一般。 火紅的嫁衣襯著肌膚如雪犀盟。 梳的紋絲不亂的頭發(fā)上而晒,一...
    開(kāi)封第一講書(shū)人閱讀 51,245評(píng)論 1 299
  • 那天,我揣著相機(jī)與錄音阅畴,去河邊找鬼倡怎。 笑死,一個(gè)胖子當(dāng)著我的面吹牛贱枣,可吹牛的內(nèi)容都是我干的监署。 我是一名探鬼主播,決...
    沈念sama閱讀 40,120評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼纽哥,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼钠乏!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起昵仅,我...
    開(kāi)封第一講書(shū)人閱讀 38,964評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤缓熟,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后摔笤,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,376評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡垦写,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,592評(píng)論 2 333
  • 正文 我和宋清朗相戀三年吕世,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片梯投。...
    茶點(diǎn)故事閱讀 39,764評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡命辖,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出分蓖,到底是詐尸還是另有隱情尔艇,我是刑警寧澤,帶...
    沈念sama閱讀 35,460評(píng)論 5 344
  • 正文 年R本政府宣布么鹤,位于F島的核電站终娃,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏蒸甜。R本人自食惡果不足惜棠耕,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,070評(píng)論 3 327
  • 文/蒙蒙 一余佛、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧窍荧,春花似錦辉巡、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,697評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至瓤荔,卻和暖如春痢甘,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背茉贡。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,846評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工塞栅, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人腔丧。 一個(gè)月前我還...
    沈念sama閱讀 47,819評(píng)論 2 370
  • 正文 我出身青樓放椰,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親愉粤。 傳聞我的和親對(duì)象是個(gè)殘疾皇子砾医,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,665評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • 吳萱予是我朋友中最漂亮的女孩。 吳萱予結(jié)婚那天下著雨衣厘,濕噠噠的如蚜,和她盼望的大晴天正好相反,我看出她的不悅影暴,當(dāng)然错邦,不...
    解憂少年閱讀 744評(píng)論 7 29
  • 有一個(gè)故事撬呢,說(shuō)人家送小明一箱蘋(píng)果。打開(kāi)一看妆兑,大部分新鮮青翠魂拦,有幾個(gè)卻已經(jīng)開(kāi)始變色。 小明的母親伸手去拿那快腐壞的搁嗓;...
    補(bǔ)拙莫如勤LV閱讀 203評(píng)論 0 0
  • 總得在路上 氣喘吁吁 然后才失望 皺著眉頭眼中有光 總得有思念 輾轉(zhuǎn)難忘 然后才幻想 扭過(guò)頭去心在偷望 總得有煩惱...
    0969fa688661閱讀 176評(píng)論 0 2