反射技术:DEMO
实体类:
import java.io.InputStream;
import java.util.List;
import java.util.Map;
/**
*
* @author allen
*
*/
public class Person {
/**
* 字段
*/
private String test1 = "allen";
public static String test2 = "static";
public String names = "fancy";
public int test3 ;
public double test4;
public float test5;
/**
* 构造函数
*/
public Person(){
System.out.println("无参构造函数");
}
public Person(int test3){
this.test3 = test3;
System.out.println("有一个参数的构造函数");
}
public Person(double test4,String names){
this.test4 = test4;
this.names = names;
System.out.println("有俩个参数的构造函数");
}
public Person(List list){
System.out.println("List");
}
private Person(Map map){
System.out.println("private Map");
}
/**
* 方法
*/
public void getNoParamMethod(){
System.out.println("无参数传递进来");
}
public void getNoneParamMethod(String name){
System.out.println("参数传递进来"+name);
}
public Class[] getParamMethod(String name,int[] arr){
return new Class[]{String.class,int[].class};
}
private void getInputStream(InputStream inputStream){
System.out.println("private inputStream :"+inputStream);
}
public static void getStaticMethod(int number){
System.out.println("static method:"+number);
}
}
反射出构造函数:
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
/**
*
* @author allen
*
*/
public class TestClazzConstrutor {
/**
* 反射出无参数的构造函数public Person()
*/
@Test
public void testNoParamConstructor(){
Class clazz = Person.class;
try {
Constructor constructor = clazz.getConstructor(null);
Person person = (Person) constructor.newInstance(null);
System.out.println(person);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 反射出public Person(int test3)
*/
@Test
public void testNoneParamConstructor(){
Class clazz = Person.class;
try {
Constructor constructor = clazz.getConstructor(int.class);
Person person = (Person) constructor.newInstance(12);
System.out.println(person.test3);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* public Person(double test4,String names)
*/
@Test
public void testTwoParamConstructor(){
Class clazz = Person.class;
try {
Constructor constructor = clazz.getConstructor(double.class,String.class);
Person person = (Person) constructor.newInstance(12,"allen");
System.out.println(person.test4+person.names);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* public Person(List list)
*/
@Test
public void testListParamConstructor(){
Class clazz = Person.class;
try {
Constructor constructor = clazz.getConstructor(List.class);
Person person = (Person) constructor.newInstance(new ArrayList());
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* private Person(Map map)
*/
@Test
public void testPrivateMapParamConstructor(){
Class clazz = Person.class;
try {
Constructor constructor = clazz.getDeclaredConstructor(Map.class);
//对私有的构造函数
constructor.setAccessible(true);
Person person = (Person) constructor.newInstance(new HashMap());
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
反射出方法:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.junit.Test;
/**
*
* @author allen
*
*/
public class TestClazzMethod {
/**
* public void getNoParamMethod()
*/
@Test
public void testNoParamMethod(){
Class clazz = Person.class;
Person p = new Person();
try {
Method method = clazz.getDeclaredMethod("getNoParamMethod",null);
method.invoke(p, null);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void testgetNoneParamMethod(){
Class clazz = Person.class;
Person p = new Person();
try {
Method method = clazz.getDeclaredMethod("getNoneParamMethod",String.class);
method.invoke(p, "allen");
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* public Class[] getParamMethod(String name,int[] arr)
*/
@Test
public void testgetParamMethod(){
Class clazz = Person.class;
Person p = new Person();
try {
Method method = clazz.getDeclaredMethod("getParamMethod",String.class,int[].class);
Class[] clazz1 = (Class[] )method.invoke(p, "allen",new int[]{1,2,3});
System.out.println(clazz1.length);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void testgetInputStream() throws Exception{
Class clazz = Person.class;
Person p = new Person();
try {
Method method = clazz.getDeclaredMethod("getInputStream",InputStream.class);
//私有方法,反射获取
method.setAccessible(true);
method.invoke(p,new FileInputStream(new File("D://redis.txt")));
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*
*/
@Test
public void testgetStaticMethod(){
Class clazz = Person.class;
Person p = new Person();
try {
Method method = clazz.getDeclaredMethod("getStaticMethod",int.class);
method.invoke(p,12);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
反射出字段:
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import org.junit.Test;
/**
*
* @author allen
*
*/
public class TestClazzField {
/**
* public
* @throws Exception
*/
@Test
public void testField() throws Exception{
Class clazz = Person.class;
Person p = new Person();
try {
Field f = clazz.getField("names");
Class type=f.getType();
if(type.equals(String.class)){
String names = (String)f.get(p);
System.out.println(names);
//设置值
f.set(p, "xxxxxxxx");
System.out.println(f.get(p));
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* private
* @throws Exception
*/
@Test
public void testPrivateField() throws Exception{
Class clazz = Person.class;
Person p = new Person();
try {
Field f = clazz.getDeclaredField("test1");
f.setAccessible(true);
Class type=f.getType();
if(type.equals(String.class)){
String names = (String)f.get(p);
System.out.println(names);
//设置值
f.set(p, "xxxxxxxx");
System.out.println(f.get(p));
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* static
* @throws Exception
*/
@Test
public void testStaticField() throws Exception{
Class clazz = Person.class;
Person p = new Person();
try {
Field f = clazz.getDeclaredField("test2");
f.setAccessible(true);
Class type=f.getType();
if(type.equals(String.class)){
String names = (String)f.get(p);
System.out.println(names);
//设置值
f.set(p, "xxxxxxxx");
System.out.println(f.get(p));
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}