一,理解jvm虚拟机原理
二,反射
2.1什么是反射
反射是什么呢?当我们的程序在运行时,需要动态的加载一些类这些类可能之前用不到所以不用加载到jvm,而是在运行时根据需要才加载。在Java运行时环境中,对于任意一个类,可以知道这个类有哪些属性和方法。对于任意一个对象,可以调用它的任意一个方法。这种动态获取类的信息以及动态调用对象的方法的功能来自于Java 语言的反射(Reflection)机制。
特点:动态按需加载
2.2理解java反射机制
Java的反射机制的实现要借助于4个类:class,Constructor,Field,Method;
其中:
Class - 类对象,实现反射的基础
Constructor-类的构造器对象
Field-类的属性对象,
Method-类的方法对象。
通过这四个对象我们可以粗略的看到一个类的各个组成部分。
2.3从反正两方面理解对比反射和常规用法
a)创建对象
常规调用:
Student sd = new Student();//实例化对象
反射调用:
Class<?> cls = Class.forName("com.test.student") ; // 取得Class对象
Object obj = cls.newInstance(); // 实例化对象
b)方法调用
常规调用:
Student sd = new Student();//实例化对象
sd.getName();
反射调用:
Class<?> cls = Class.forName("com.test.student") ; // 取得Class对象
Object obj = cls.newInstance(); // 实例化对象
Method getMet = cls.getMethod("getName");// getName()
setMet.invoke(obj) ;
c)成员变量
常规调用:
Student sd = new Student();//实例化对象
sd.name = "frank";//name属性
反射调用:
Class<?> cls = Class.forName("com.test.student"); // 取得Class对象
Object obj = cls.newInstance(); // 对象实例化属性才会分配空间
Field nameField = cls.getDeclaredField("name") ; // 找到name属性
nameField.setAccessible(true) ; // 解除封装了
nameField.set(obj, "张三") ; // Person对象.name = "张三"
三,反射的特点
参考:http://qindongliang.iteye.com/blog/2202833 增加程序的灵活性。 如struts中。请求的派发控制。 当请求来到时。struts通过查询配置文件。找到该请求对应的action。已经方法。 然后通过反射实例化action。并调用响应method。 如果不适用反射,那么你就只能写死到代码里了。 所以说,一个灵活,一个不灵活。 很少情况下是非用反射不可的。大多数情况下反射是为了提高程序的灵活性。 因此一般框架中使用较多。因为框架要适用更多的情况。对灵活性要求较高。
优点:
(1)能够运行时动态获取类的实例,大大提高系统的灵活性和扩展性。
(2)与Java动态编译相结合,可以实现无比强大的功能
缺点:
(1)使用反射的性能较低
(2)使用反射相对来说不安全
(3)破坏了类的封装性,可以通过反射获取这个类的私有方法和属性
四,常用工具方法
package com.test.reflect.util;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* 反射工具类
*
*/
@SuppressWarnings("unchecked")
public class ReflectUtil {
/**
* 对象方法调用
*
* @param target 调用目标对象
* @param returnType 返回类型
* @param method 方法名称
* @param parameterTypes 方法参数类型
* @param values 参数
* @return 反射调用返回值
* @throws NoSuchMethodException
* @throws SecurityException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InvocationTargetException
*/
public static <T> T callObjectMethod(Object target, Class<T> returnType, String method, Class<?>[] parameterTypes, Object... values)
throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Class<? extends Object> clazz = target.getClass();
Method declaredMethod = clazz.getDeclaredMethod(method, parameterTypes);
declaredMethod.setAccessible(true);
return (T) declaredMethod.invoke(target, values);
}
public static Object callObjectMethod(Object target, String method, Class<?>[] parameterTypes, Object... values)
throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Class<? extends Object> clazz = target.getClass();
Method declaredMethod = clazz.getDeclaredMethod(method, parameterTypes);
declaredMethod.setAccessible(true);
return declaredMethod.invoke(target, values);
}
public static Object callObjectMethod(Object target, String method, Class<?> clazz, Class<?>[] parameterTypes, Object... values)
throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method declaredMethod = clazz.getDeclaredMethod(method, parameterTypes);
declaredMethod.setAccessible(true);
return declaredMethod.invoke(target, values);
}
/**
* 静态方法调用
*
* @param clazz
* @param returnType
* @param method
* @param parameterTypes
* @param values
* @return
* @throws NoSuchMethodException
* @throws SecurityException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InvocationTargetException
*/
public static <T> T callStaticObjectMethod(Class<?> clazz, Class<T> returnType, String method, Class<?>[] parameterTypes,
Object... values)
throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method declaredMethod = clazz.getDeclaredMethod(method, parameterTypes);
declaredMethod.setAccessible(true);
return (T) declaredMethod.invoke(null, values);
}
public static Object callStaticObjectMethod(Class<?> clazz, String method, Class<?>[] parameterTypes, Object... values)
throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Method declaredMethod = clazz.getDeclaredMethod(method, parameterTypes);
declaredMethod.setAccessible(true);
return declaredMethod.invoke(null, values);
}
/**
* 对象设置值
*
* @param target
* @param field
* @param value
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static void setObjectField(Object target, String field, Object value) throws NoSuchFieldException, SecurityException,
IllegalArgumentException, IllegalAccessException {
Class<? extends Object> clazz = target.getClass();
Field declaredField = clazz.getDeclaredField(field);
declaredField.setAccessible(true);
declaredField.set(target, value);
}
/**
* 对象获取值
*
* @param target
* @param field
* @return
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static Object getObjectField(Object target, String field) throws NoSuchFieldException, SecurityException,
IllegalArgumentException, IllegalAccessException {
Class<? extends Object> clazz = target.getClass();
Field declaredField = clazz.getDeclaredField(field);
declaredField.setAccessible(true);
return declaredField.get(target);
}
public static <T> T getObjectField(Object target, String field, Class<T> returnType) throws NoSuchFieldException, SecurityException,
IllegalArgumentException, IllegalAccessException {
Class<? extends Object> clazz = target.getClass();
Field declaredField = clazz.getDeclaredField(field);
declaredField.setAccessible(true);
return (T) declaredField.get(target);
}
/**
* 静态变量设置值
*
* @param clazz
* @param field
* @param value
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static void setStaticObjectField(Class<?> clazz, String field, Object value) throws NoSuchFieldException, SecurityException,
IllegalArgumentException, IllegalAccessException {
Field declaredField = clazz.getDeclaredField(field);
declaredField.setAccessible(true);
declaredField.set(null, value);
}
/**
* 静态变量获取值
*
* @param clazz
* @param field
* @return
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static Object getStaticObjectField(Class<?> clazz, String field) throws NoSuchFieldException, SecurityException,
IllegalArgumentException, IllegalAccessException {
Field declaredField = clazz.getDeclaredField(field);
declaredField.setAccessible(true);
return declaredField.get(null);
}
public static <T> T getStaticObjectField(Class<?> clazz, String field, Class<T> returnType) throws NoSuchFieldException,
SecurityException,
IllegalArgumentException, IllegalAccessException {
Field declaredField = clazz.getDeclaredField(field);
declaredField.setAccessible(true);
return (T) declaredField.get(null);
}
}