Java反射机制
JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。
Java反射机制主要提供了以下功能:
1.
- public
Object getProperty(Object owner, String fieldName) throws Exception { -
Class ownerClass = owner.getClass(); -
-
Field field = ownerClass.getField(fieldName); -
-
Object property = field.get(owner); -
-
return property; - }
Class
Field
Object
2.
- public
Object getStaticProperty(String className, String fieldName) -
throws Exception { -
Class ownerClass = Class.forName(className); -
-
Field field = ownerClass.getField(fieldName); -
-
Object property = field.get(ownerClass); -
-
return property; - }
Class
Field
Object
3.
- public
Object invokeMethod(Object owner, String methodName, Object[] args) throws Exception { -
-
Class ownerClass = owner.getClass(); -
-
Class[] argsClass = new Class[args.length]; -
-
for (int i = 0, j = args.length; i < j; i++) { -
argsClass[i] = args[i].getClass(); -
} -
-
Method method = ownerClass.getMethod(methodName,argsClass); -
-
return method.invoke(owner, args); - }
Class
5~9行:配置参数的Class数组,作为寻找Method的条件。
Method
method.invoke(owner,
4.
- public
Object invokeStaticMethod(String className, String methodName, -
Object[] args) throws Exception { -
Class ownerClass = Class.forName(className); -
-
Class[] argsClass = new Class[args.length]; -
-
for (int i = 0, j = args.length; i < j; i++) { -
argsClass[i] = args[i].getClass(); -
} -
-
Method method = ownerClass.getMethod(methodName,argsClass); -
-
return method.invoke(null, args); -
}
基本的原理和实例3相同,不同点是最后一行,invoke的一个参数是null,因为这是静态方法,不需要借助实例运行。
5.
- public
Object newInstance(String className, Object[] args) throws Exception { -
Class newoneClass = Class.forName(className); -
-
Class[] argsClass = new Class[args.length]; -
-
for (int i = 0, j = args.length; i < j; i++) { -
argsClass[i] = args[i].getClass(); -
} -
-
Constructor cons = newoneClass.getConstructor(argsClass); -
-
return cons.newInstance(args); -
- }
这里说的方法是执行带参数的构造函数来新建实例的方法。如果不需要参数,可以直接使用newoneClass.newInstance()来实现。
Class
第5~第9行:得到参数的Class数组。
Constructor
cons.newInstance(args):新建实例。
6.
- public
boolean isInstance(Object obj, Class cls) { -
return cls.isInstance(obj); - }
7.
- public
Object getByArray(Object array, int index) { -
return Array.get(array,index); - }
转于:http://blog.sina.com.cn/s/blog_61f4999d0100qpu7.html
- package com.java.day01;
- import java.lang.reflect.Method;
- public class BeanUtil {
- //javaBean
- //1,公共无参构造方法;
- //2,属性存取方法符合set-get 规范;
- public static void setProperty (
- Object o,String name,Object value) throws Exception{//属性变为set方法;
- String methodName =
- "set" + name.substring(0,1).toUpperCase()+
- name.substring(1);
- Method m = null;
- Class paramClass = value.getClass();
- if(paramClass == Integer.class){
- //setAge(Integer),getAge(int)
- try {
- m = o.getClass().getMethod(methodName, new Class[]{value.getClass()});
- } catch (Exception e) {
- m = o.getClass().getMethod(methodName, new Class[]{int.class});
- }
- }else{
- m = o.getClass().getMethod(methodName, new Class[]{int.class});
- }
- m.invoke(o, new Object[]{value});
- }
- public static Object getProperty(Object o,String name) throws Exception{
- String methodName =
- "get" + name.substring(0,1).toUpperCase()+
- name.substring(1);
- Method m = o.getClass().getMethod(methodName, null);
- return m.invoke(o, null);
- }
- public static void main(String[] args) throws Exception{
- Student stu = new Student();
- setProperty(stu, "age", 23);
- System.out.println(stu.getAge());
- }
- }
- class Student{
- private String name;
- private int age;
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }