反射出类中的字段,构造函数,方法(私有的,静态的l诶)

来源:互联网 发布:冬季骑行服 知乎 编辑:程序博客网 时间:2024/05/06 20:12

反射技术: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() */@Testpublic 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 blocke.printStackTrace();} catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InstantiationException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * 反射出public Person(int test3) */@Testpublic 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 blocke.printStackTrace();} catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InstantiationException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * public Person(double test4,String names) */@Testpublic 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 blocke.printStackTrace();} catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InstantiationException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * public  Person(List list) */@Testpublic 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 blocke.printStackTrace();} catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InstantiationException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * private  Person(Map map) */@Testpublic 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 blocke.printStackTrace();} catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InstantiationException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.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() */@Testpublic 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 blocke.printStackTrace();} catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}}@Testpublic 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 blocke.printStackTrace();} catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * public Class[] getParamMethod(String name,int[] arr) */@Testpublic 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 blocke.printStackTrace();} catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}}@Testpublic 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 blocke.printStackTrace();} catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** *  */@Testpublic 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 blocke.printStackTrace();} catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
反射出字段:

import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import org.junit.Test;/** *  * @author allen * */public class TestClazzField {/** * public * @throws Exception */@Testpublic 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 blocke.printStackTrace();}catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * private * @throws Exception */@Testpublic 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 blocke.printStackTrace();}catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * static * @throws Exception */@Testpublic 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 blocke.printStackTrace();}catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}



0 0
原创粉丝点击