詳細(xì)介紹java的反射技術(shù)
反射的定義:審查元數(shù)據(jù)并收集關(guān)于它的類型信息的能力。下面介紹java的反射技術(shù)。
Lesson: 檢測(cè)類examing class
1.Retrieving Class Objects
獲取一個(gè)Class對(duì)象(metadata)
a,從對(duì)象的實(shí)例獲取。
- Class c = mystery.getClass();//(return Class)
b,從子類的實(shí)例獲取
- TextField t = new TextField();
- Class c = t.getClass();
- Class s = c.getSuperclass();
c,知道類名,則可以把.class加入到名字之后來獲取。
- Class c = java.awt.Button.class;
d,如果類名在編譯時(shí)是未知的,則可以使用Class.forName()方法來獲取.
- Class c = Class.forName(classString);
2.Getting the Class Name
獲取類名稱
- c.getName();
例如:
- import java.lang.reflect.*;
- import java.awt.*;
- class SampleName {
- public static void main(String[] args) {
- Button b = new Button();
- printName(b);
- }
- static void printName(Object o) {
- Class c = o.getClass();
- String s = c.getName();
- System.out.println(s);
- }
- }
3.Discovering Class Modifiers
檢索修改符
a.通過getModifiers()方法獲取一個(gè)整型標(biāo)識(shí)值。
b.通過java.reflect.Modifier對(duì)象的isPublic, isAbstract, 和 isFinal方法判斷此值.
例如:
- import java.lang.reflect.*;
- import java.awt.*;
- class SampleModifier {
- public static void main(String[] args) {
- String s = new String();
- printModifiers(s);
- }
- public static void printModifiers(Object o) {
- Class c = o.getClass();
- int m = c.getModifiers();
- if (Modifier.isPublic(m))
- System.out.println("public");
- if (Modifier.isAbstract(m))
- System.out.println("abstract");
- if (Modifier.isFinal(m))
- System.out.println("final");
- }
- }
4.Finding Superclasses
檢索父類
例如:
- import java.lang.reflect.*;
- import java.awt.*;
- class SampleSuper {
- public static void main(String[] args) {
- Button b = new Button();
- printSuperclasses(b);
- }
- static void printSuperclasses(Object o) {
- Class subclass = o.getClass();
- Class superclass = subclass.getSuperclass();
- while (superclass != null) {
- String className = superclass.getName();
- System.out.println(className);
- subclass = superclass;
- superclass = subclass.getSuperclass();
- }
- }
- }
5.Identifying the Interfaces Implemented by a Class
檢索指定類實(shí)現(xiàn)的接口
例如:
- import java.lang.reflect.*;
- import java.io.*;
- class SampleInterface {
- public static void main(String[] args) {
- try {
- RandomAccessFile r = new RandomAccessFile("myfile", "r");
- printInterfaceNames(r);
- } catch (IOException e) {
- System.out.println(e);
- }
- }
- static void printInterfaceNames(Object o) {
- Class c = o.getClass();
- Class[] theInterfaces = c.getInterfaces();
- for (int i = 0; i < theInterfaces.length; i++) {
- String interfaceName = theInterfaces[i].getName();
- System.out.println(interfaceName);
- }
- }
- }
6.Examining Interfaces
判定一個(gè)類是不是接口
- import java.lang.reflect.*;
- import java.util.*;
- class SampleCheckInterface {
- public static void main(String[] args) {
- Class thread = Thread.class;
- Class runnable = Runnable.class;
- verifyInterface(thread);
- verifyInterface(runnable);
- }
- static void verifyInterface(Class c) {
- String name = c.getName();
- if (c.isInterface()) {
- System.out.println(name + " is an interface.");
- } else {
- System.out.println(name + " is a class.");
- }
- }
- }
如:c.isInterface()
7.Identifying Class Fields
找出指定類所有的域成員
每個(gè)數(shù)據(jù)成員可以用java.reflect.Field來封閉其名稱,類型,修改符的集合。也可以通過相應(yīng)的方法獲取或設(shè)置到該成員的值。
如:
- import java.lang.reflect.*;
- import java.awt.*;
- class SampleField {
- public static void main(String[] args) {
- GridBagConstraints g = new GridBagConstraints();
- printFieldNames(g);
- }
- static void printFieldNames(Object o) {
- Class c = o.getClass();
- Field[] publicFields = c.getFields();
- for (int i = 0; i < publicFields.length; i++) {
- String fieldName = publicFields[i].getName();
- Class typeClass = publicFields[i].getType();
- String fieldType = typeClass.getName();
- System.out.println("Name: " + fieldName +
- ", Type: " + fieldType);
- }
- }
- }
8.Discovering Class Constructors
檢索指定類的構(gòu)造函數(shù)
當(dāng)創(chuàng)建一個(gè)類的實(shí)例時(shí),是通過檢造方法來作的,這種方法可以被重載。
每一個(gè)構(gòu)造方法可以用類Constructor來描述,,包括名稱,修飾符,參數(shù)類型(Class[]),和異常列表。
可以通過一個(gè)Class的getConstructors方法獲取到該類的Constructor數(shù)組。
例程:
- import java.lang.reflect.*;
- import java.awt.*;
- class SampleConstructor {
- public static void main(String[] args) {
- Rectangle r = new Rectangle();
- showConstructors(r);
- }
- static void showConstructors(Object o) {
- Class c = o.getClass();
- Constructor[] theConstructors = c.getConstructors();
- for (int i = 0; i < theConstructors.length; i++) {
- System.out.print("( ");
- Class[] parameterTypes =
- theConstructors[i].getParameterTypes();
- for (int k = 0; k < parameterTypes.length; k ++) {
- String parameterString = parameterTypes[k].getName();
- System.out.print(parameterString + " ");
- }
- System.out.println(")");
- }
- }
- }
9.Obtaining Method Information
檢索方法
可以找到隸屬于一個(gè)類的所有方法,通過getMethods包含Method數(shù)組,進(jìn)而得到該方法的返回類型,修飾符,方法名稱,參數(shù)列表
步驟:
a.指定類的Class Object
b.getMethods()獲取Method[]對(duì)象
c,遍歷該數(shù)組對(duì)象
例程:
- import java.lang.reflect.*;
- import java.awt.*;
- class SampleMethod {
- public static void main(String[] args) {
- Polygon p = new Polygon();
- showMethods(p);
- }
- static void showMethods(Object o) {
- Class c = o.getClass();
- Method[] theMethods = c.getMethods();
- for (int i = 0; i < theMethods.length; i++) {
- String methodString = theMethods[i].getName();
- System.out.println("Name: " + methodString);
- String returnString =
- theMethods[i].getReturnType().getName();
- System.out.println(" Return Type: " + returnString);
- Class[] parameterTypes = theMethods[i].getParameterTypes();
- System.out.print(" Parameter Types:");
- for (int k = 0; k < parameterTypes.length; k ++) {
- String parameterString = parameterTypes[k].getName();
- System.out.print(" " + parameterString);
- }
- System.out.println();
- }
- }
- }
希望通過以上內(nèi)容的介紹,能夠給你帶來幫助。