Java中反射技术点记录
2014-06-23 13:00
330 查看
为了避免遗忘,将Reflect中常用方法和步骤记录下来。
测试所用的person类如下:
2.JUnit测试代码如下:
测试所用的person类如下:
package neuq.test; public class Person { private String name; private int age; public Person(String name, int age) { super(); this.name = name; this.age = age; } public Person(){ super(); }
public static void staticMethod(){ System.out.println("staticMethod run!!"); }; public void paramMethod(String name,int age){ System.out.println(name+"--------"+age); } public void noParamMethod(){ System.out.println("noParamMethod run!!!!"); } }
2.JUnit测试代码如下:
package neuq.test; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import org.junit.Test; public class ReflectTest { /* * 测试拿到Class的三种方法 * * @author:Dean * @data:2014-6-23 */ @Test public void testGetClass() throws ClassNotFoundException{ //方法一:一般不用 Person p=new Person(); Class c1=p.getClass(); //方法二:很少用 Class c2=Person.class; //方法三:常用 String className="neuq.test.Person"; Class c3=Class.forName(className); System.out.println(c1); System.out.println(c2); System.out.println(c3); } /* * 测试通过指定的构造器实例化对象。 * @author:Dean * @date:1994-6-23 * */ @Test public void testGetSpecifiedConstructor() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{ //通过类名拿到Class String className="neuq.test.Person"; Class clazz=Class.forName(className); //通过Class对象拿到Constructor,并制定参数类型。 Constructor constructor=clazz.getConstructor(String.class,int.class); //通过Constructor实例化对象。 Person p=(Person) constructor.newInstance("Dean",20); System.out.println(p); } /* * 测试反射字段的方法&以及暴力访问 * * @author:Dean * @date:2014-6-23 */ @Test public void testGetField() throws Exception{ //首先拿到Class对象,并实例化 String className="neuq.test.Person"; Class clazz=Class.forName(className); Constructor constructor=clazz.getConstructor(String.class,int.class); Object obj= constructor.newInstance("Dean",20); /* * 拿到字段,并设置值 * 私有属性只能根据getDeclaredXXX()拿到。否则会出NoSuchXXX异常 */ Field field=clazz.getDeclaredField("name"); String value="Sam"; //暴力访问私有属性,设置可访问性。 field.setAccessible(true); field.set(obj, value); } /* *测试反射 静态方法 带参数的方法 不带参数的方法 * @ staticMethod() * @ paramMethod() * @ noParamMethod() * * @author:Dean * @date:2014-6-23 */ @Test public void testGetMethod() throws Exception{ //先拿到Class的对象 String className="neuq.test.Person"; Class clazz=Class.forName(className); Constructor constructor=clazz.getConstructor(String.class,int.class); Object obj=constructor.newInstance("Dean",20); //先测试静态方法,不需要实例化对象 Method method1=clazz.getMethod("staticMethod"); method1.invoke(null, null); //测试无参数方法,需要实例化对象,不需要指定参数 Method method2=clazz.getMethod("noParamMethod"); method2.invoke(obj, null); //测试带参数方法,需要实例化对象,需要指定参数 Method method3=clazz.getMethod("paramMethod", String.class,int.class); method3.invoke(obj, "Sam",20); } }
相关文章推荐
- .net反射技术的应用—如何调用Java的COM接口
- .Net反射技术应用—与Java互操作
- java的反射技术功能十分强大,整理一些资料!!
- java反射技术的简单使用
- 【转】Android系统原理与源码分析:利用Java反射技术阻止通过按钮关闭对话框
- [原创]java反射技术(一)
- 详细介绍java的反射技术
- 利用JAVA反射技术查找一个类的所有属性
- Android系统原理与源码分析(1):利用Java反射技术阻止通过按钮关闭对话框
- java反射--记录
- java的反射技术功能十分强大,整理一些资料!!
- 利用java反射技术提取类信息
- java的反射技术功能十分强大,整理一些资料!!
- java的反射技术功能十分强大,整理一些资料!!
- Android系统原理与源码分析(1):利用Java反射技术阻止通过按钮关闭对话框
- JAVA 反射技术 万用分页标签 核心代码 (图)
- java反射技术
- 在做一个大型java项目,从现在起记录一些技术应用框架配置,一、svn+apace+权限配置
- Android系统原理与源码分析(1):利用Java反射技术阻止通过按钮关闭对话框
- Android系统原理与源码分析(1):利用Java反射技术阻止通过按钮关闭对话框