JAVA反射機制主要提供如下功能:html
一、在運行時判斷任意一個對象所屬的類java
二、在運行時構造任意一個類的對象api
三、在運行時判斷任意一個類所具備的成員變量和方法(經過反射甚至能夠調用private方法)數組
四、在運行時調用任意一個對象的方法(******注意:前提都是在運行時,而不是在編譯時)oracle
Class對象是Java反射的基礎,它包含了與類相關的信息,事實上,Class對象就是用來建立類的全部對象的。Class對象是java.lang.Class<T>這個類生成的對象,其中類型參數T表示由此 Class 對象建模的類的類型。例如,String.class的類型是 Class<String>;若是將被建模的類未知,則使用Class<?>.ide
Class沒有公共構造方法。Class對象是在加載類時由Java虛擬機以及經過調用類加載器中的defineClass方法自動構造的。函數
獲取Class對象的幾種方法:學習
static Class<?> | forName(String className) Returns the Class object associated with the class or interface with the given string name. |
static Class<?> | forName(String name, boolean initialize, ClassLoader loader) Returns the Class object associated with the class or interface with the given string name, using the given class loader. |
Class class=Class.forName("com.chuyu.util.Myreflect");//參數即爲類的完整包名
Class MyreflectClass=Myreflect.class;//(類字面常量不只能夠應用於普通的類,也能夠應用於接口、數組以及基本數據類型) Class c = Integer.TYPE;//(TYPE是基本數據類型的包裝類型的一個標準字段,它是一個引用,指向對應的基本數據類型的Class對象)
附表:
Class Summary
Class | Description |
---|---|
AccessibleObject | The AccessibleObject class is the base class for Field, Method and Constructor objects. |
Array | The Array class provides static methods to dynamically create and access Java arrays. |
Constructor<T> | Constructor provides information about, and access to, a single constructor for a class. |
Field | A Field provides information about, and dynamic access to, a single field of a class or an interface. |
Method | A Method provides information about, and access to, a single method on a class or interface. |
Modifier | The Modifier class provides static methods and constants to decode class and member access modifiers. |
Proxy | Proxy provides static methods for creating dynamic proxy classes and instances, and it is also the superclass of all dynamic proxy classes created by those methods. |
ReflectPermission | The Permission class for reflective operations. |
從Class對象獲取Constructor類的方法
Modifier and Type | Method and Description |
---|
Constructor<T> | getConstructor(Class<?>... parameterTypes) Returns a Constructor object that reflects the specified public constructor of the class represented by this Class object. |
Constructor<?>[] | getConstructors() Returns an array containing Constructor objects reflecting all the public constructors of the class represented by this Class object. |
Constructor<T> | getDeclaredConstructor(Class<?>... parameterTypes) Returns a Constructor object that reflects the specified constructor of the class or interface represented by this Class object. |
Constructor<?>[] | <getDeclaredConstructors() Returns an array of Constructor objects reflecting all the constructors declared by the class represented by this Class object. |
Method Summary
Modifier and Type | Method and Description |
---|---|
String | getName() Returns the name of this constructor, as a string. |
Class<?>[] | getParameterTypes() Returns an array of Class objects that represent the formal parameter types, in declaration order, of the constructor represented by this Constructor object. |
T | newInstance(Object... initargs) Uses the constructor represented by this Constructor object to create and initialize a new instance of the constructor's declaring class, with the specified initialization parameters. |
String | toGenericString() Returns a string describing this Constructor, including type parameters. |
String | toString() Returns a string describing this Constructor. |
(1) 獲取Constructor對象
(2) 構造函數參數
(3) 使用Constructor對象實例化對象
package com.chuyu.it.utils; import java.lang.reflect.Constructor; public class Student { public String name; public String sex; private String age; //省略set/get 方法 public Student(String name, String sex, String age) { super(); this.name = name; this.sex = sex; this.age = age; } public Student() { super(); } public static void main(String[] args) throws Exception{ Class<?> clz=Student.class; Constructor<?> constructors= clz.getConstructor(new Class[]{String.class,String.class,String.class}); Student stu=(Student) constructors.newInstance("張三","男","22");//newInstance(Object... initargs)建立實例 System.out.println(constructors.getName());//com.chuyu.it.utils.Student Class<?>[] paramsArray=constructors.getParameterTypes(); for (Class<?> class1 : paramsArray) { System.out.println(class1.getName());//java.lang.String java.lang.String java.lang.String } System.out.println(constructors.toGenericString());//public com.chuyu.it.utils.Student(java.lang.String,java.lang.String,java.lang.String) System.out.println(constructors.toString());//public com.chuyu.it.utils.Student(java.lang.String,java.lang.String,java.lang.String) } }
4.2.1 java.lang.Class<?>對象獲取Method對象的方法
Method | getDeclaredMethod(String name, Class<?>... parameterTypes) Returns a Method object that reflects the specified declared method of the class or interface represented by this Class object. |
Method[] | getDeclaredMethods() Returns an array of Method objects reflecting all the methods declared by the class or interface represented by this Class object. |
Method | getMethod(String name, Class<?>... parameterTypes) Returns a Method object that reflects the specified public member method of the class or interface represented by this Class object. |
獲取到Method類後調用
public Class<?>[] getParameterTypes() //獲取參數類型
public String getName()//獲取方法名
public Class<?> getReturnType()//獲取返回類型
public Object invoke(Object obj, Object... args) //obj實例對象調用該方法,args 方法的入參
示例:
public class Student { ...//同上 private static void sayHello(String name){ System.out.println("Hello:"+name); } public static void main(String[] args) throws Exception{ Class<?> clz=Student.class; Constructor<?> constructors= clz.getConstructor(new Class[]{String.class,String.class,String.class}); Student stu=(Student) constructors.newInstance("張三","男","22");//newInstance(Object... initargs) //public Method getDeclaredMethod(String name,Class<?>... parameterTypes) Method sayHelloMethod=clz.getDeclaredMethod("sayHello", new Class[]{String.class}); //invoke(Object obj, Object... args) sayHelloMethod.invoke(stu,stu.name);//Hello:張三 } }
一樣的,也是經過java.lang.Class<?>類中的方法獲取反射類的Field類
public Field getDeclaredField(String name)//獲取聲明的指定字段 public Field[] getDeclaredField()//獲取全部聲明的字段 public Field getField(String name)//獲取指定聲明的公告字段 public Field[] getField()//獲取全部可訪問的公共字段
在獲取到Field類後的方法(參見:http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Field.html) ,調用相應的方法.
public Object get(Object obj)//獲取字段的值 public void set(Object obj,Object value)//設置字段的值 getType/getName以及一些基本數據類型的getXxxx(Object obj)和setXxxx(Object obj,Xxxx value) //Xxxx 表示基本數據類型的包裝類
示例:
package com.chuyu.it.utils; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Type; public class Student { ...//同上 public static void main(String[] args) throws Exception{ Class<?> clz=Student.class; Constructor<?> constructors= clz.getConstructor(new Class[]{});//調用無參構造方法 Student stu=(Student) constructors.newInstance();//newInstance(Object... initargs) Field [] fields=clz.getDeclaredFields(); for (Field field : fields) { System.out.println(field.getType().getName());//java.lang.String } Field fieldforName=clz.getDeclaredField("name"); fieldforName.set(stu, "zhangsan"); System.out.println(fieldforName.get(stu));//zhangsan } }
總結: 其中對於Construct<?>,Method,Field 類都有getDeclaredXxxxxx(參數列表),以及無參的getDeclaredXxxxxx 表示的 都是獲取聲明的全部(包含private 修飾的私有)的構造方法類,方法類,字段類.而
getXxxxx則表示只獲取公共的構造方法類,方法類,字段類.
整個java反射Api大體以下:
--------------------------------分割線----------------------------------------------------
以上只是做爲我的學習的初始知識整理,後續在工做中實際遇到的再作整理
2017年4月7日14:06:06