反射的代碼示例
- 通過(guò)反射的方式可以獲取class對(duì)象中的屬性、方法胃惜、構(gòu)造函數(shù)等,一下是實(shí)例
package cn.java.reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
public class MyReflect {
public String className = null;
@SuppressWarnings("rawtypes")
public Class personClass = null;
/**
* 反射Person類(lèi)
* @throws Exception
*/
@Before
public void init() throws Exception {
className = "cn.java.reflect.Person";
personClass = Class.forName(className);
}
/**
*獲取某個(gè)class文件對(duì)象
*/
@Test
public void getClassName() throws Exception {
System.out.println(personClass);
}
/**
*獲取某個(gè)class文件對(duì)象的另一種方式
*/
@Test
public void getClassName2() throws Exception {
System.out.println(Person.class);
}
/**
*創(chuàng)建一個(gè)class文件表示的真實(shí)對(duì)象,底層會(huì)調(diào)用空參數(shù)的構(gòu)造方法
*/
@Test
public void getNewInstance() throws Exception {
System.out.println(personClass.newInstance());
}
/**
*獲取非私有的構(gòu)造函數(shù)
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void getPublicConstructor() throws Exception {
Constructor constructor = personClass.getConstructor(Long.class,String.class);
Person person = (Person)constructor.newInstance(100L,"zhangsan");
System.out.println(person.getId());
System.out.println(person.getName());
}
/**
*獲得私有的構(gòu)造函數(shù)
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void getPrivateConstructor() throws Exception {
Constructor con = personClass.getDeclaredConstructor(String.class);
con.setAccessible(true);//強(qiáng)制取消Java的權(quán)限檢測(cè)
Person person2 = (Person)con.newInstance("zhangsan");
System.out.println(person2.getName());
}
/**
*獲取非私有的成員變量
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void getNotPrivateField() throws Exception {
Constructor constructor = personClass.getConstructor(Long.class,String.class);
Object obj = constructor.newInstance(100L,"zhangsan");
Field field = personClass.getField("name");
field.set(obj, "lisi");
System.out.println(field.get(obj));
}
/**
*獲取私有的成員變量
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void getPrivateField() throws Exception {
Constructor constructor = personClass.getConstructor(Long.class);
Object obj = constructor.newInstance(100L);
Field field2 = personClass.getDeclaredField("id");
field2.setAccessible(true);//強(qiáng)制取消Java的權(quán)限檢測(cè)
field2.set(obj,10000L);
System.out.println(field2.get(obj));
}
/**
*獲取非私有的成員函數(shù)
*/
@SuppressWarnings({ "unchecked" })
@Test
public void getNotPrivateMethod() throws Exception {
System.out.println(personClass.getMethod("toString"));
Object obj = personClass.newInstance();//獲取空參的構(gòu)造函數(shù)
Object object = personClass.getMethod("toString").invoke(obj);
System.out.println(object);
}
/**
*獲取私有的成員函數(shù)
*/
@SuppressWarnings("unchecked")
@Test
public void getPrivateMethod() throws Exception {
Object obj = personClass.newInstance();//獲取空參的構(gòu)造函數(shù)
Method method = personClass.getDeclaredMethod("getSomeThing");
method.setAccessible(true);
Object value = method.invoke(obj);
System.out.println(value);
}
/**
*
*/
@Test
public void otherMethod() throws Exception {
//當(dāng)前加載這個(gè)class文件的那個(gè)類(lèi)加載器對(duì)象
System.out.println(personClass.getClassLoader());
//獲取某個(gè)類(lèi)實(shí)現(xiàn)的所有接口
Class[] interfaces = personClass.getInterfaces();
for (Class class1 : interfaces) {
System.out.println(class1);
}
//反射當(dāng)前這個(gè)類(lèi)的直接父類(lèi)
System.out.println(personClass.getGenericSuperclass());
/**
* getResourceAsStream這個(gè)方法可以獲取到一個(gè)輸入流绊困,這個(gè)輸入流會(huì)關(guān)聯(lián)到name所表示的那個(gè)文件上。
*/
//path 不以’/'開(kāi)頭時(shí)默認(rèn)是從此類(lèi)所在的包下取資源适刀,以’/'開(kāi)頭則是從ClassPath根下獲取秤朗。其只是通過(guò)path構(gòu)造一個(gè)絕對(duì)路徑,最終還是由ClassLoader獲取資源笔喉。
System.out.println(personClass.getResourceAsStream("/log4j.properties"));
//默認(rèn)則是從ClassPath根下獲取取视,path不能以’/'開(kāi)頭,最終是由ClassLoader獲取資源常挚。
System.out.println(personClass.getResourceAsStream("/log4j.properties"));
//判斷當(dāng)前的Class對(duì)象表示是否是數(shù)組
System.out.println(personClass.isArray());
System.out.println(new String[3].getClass().isArray());
//判斷當(dāng)前的Class對(duì)象表示是否是枚舉類(lèi)
System.out.println(personClass.isEnum());
System.out.println(Class.forName("cn.java.reflect.City").isEnum());
//判斷當(dāng)前的Class對(duì)象表示是否是接口
System.out.println(personClass.isInterface());
System.out.println(Class.forName("cn.java.reflect.TestInterface").isInterface());
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者