Reflect反射的工具类
自己写了一个工具类,希望对大家有用。
/** * */ package com; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** * @version 1.0.0 * @since 2010.1.25 * @author cnchenhl * */ public class JavaReflectUtil { /** * get Class Of Name * @param str * @return * @throws ClassNotFoundException */ public static Class<?> getReflectClass(String str) throws ClassNotFoundException { Class<?> classReflect = null; classReflect = Class.forName(str); return classReflect; } /** * get Method of className * @param str * @return * @throws SecurityException * @throws ClassNotFoundException */ public static Method[] getDeclaredMethod(String str) throws SecurityException, ClassNotFoundException { Method[] method = getReflectClass(str).getDeclaredMethods(); return method; } /** * * @param method */ public static void printMehthodArray(Method[] method) { for (int i = 0; i < method.length; i++) { System.out.println(method[i].toString()); } } /** * * @param str * @return * @throws SecurityException * @throws NoSuchMethodException * @throws ClassNotFoundException */ public static Constructor<?> getConsructorDefault(String str) throws SecurityException, NoSuchMethodException, ClassNotFoundException { Constructor<?> constructor = null; constructor = getReflectClass(str).getConstructor(); return constructor; } /** * * @param str * @param classType * @return * @throws SecurityException * @throws NoSuchMethodException * @throws ClassNotFoundException */ public static Constructor<?> getConstructor(String str, Class<?>[] classType) throws SecurityException, NoSuchMethodException, ClassNotFoundException { Constructor<?> constructor = null; constructor = getReflectClass(str).getConstructor(classType); return constructor; } /** * * @param str * @return * @throws IllegalArgumentException * @throws InstantiationException * @throws IllegalAccessException * @throws InvocationTargetException * @throws SecurityException * @throws NoSuchMethodException * @throws ClassNotFoundException */ public static Object getClassObject(String str) throws IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException, ClassNotFoundException { Object obj = null; obj = getConsructorDefault(str).newInstance(); return obj; } /** * * @param str * @return * @throws SecurityException * @throws ClassNotFoundException */ public static Field[] getClassField(String str) throws SecurityException, ClassNotFoundException { Field[] field = null; field = getReflectClass(str).getDeclaredFields(); return field; } /** * * @param field */ public static void printFieldArray(Field[] field) { for (int i = 0; i < field.length; i++) { System.out.println(field[i]); } } /** * * @param method * @return */ public static String getMehtodName(Method method) { String str = method.getName(); return str; } /** * * @param field * @return */ public static String getFieldName(Field field) { String str = field.getName(); return str; } /** * * @param field * @param typeofMethod * @return */ public static String getFieldOfGetMethod(Field field, String typeofMethod) { String fieldName = field.getName(); String firstLetter = fieldName.substring(0, 1).toUpperCase(); String getMethodName = typeofMethod + firstLetter + fieldName.substring(1); return getMethodName; } /** * * @param str * @param methidName * @return * @throws SecurityException * @throws NoSuchMethodException * @throws ClassNotFoundException */ public static Method getMethod(String str, String methidName) throws SecurityException, NoSuchMethodException, ClassNotFoundException { Method method = getReflectClass(str).getMethod(methidName); return method; } /** * * @param field * @param str * @return * @throws SecurityException * @throws NoSuchMethodException * @throws ClassNotFoundException */ public static Method getFieldOfMethod(Field field, String str) throws SecurityException, NoSuchMethodException, ClassNotFoundException { String getMethodName = getFieldOfGetMethod(field, "get"); Class<?> classType = getReflectClass(str); Method getMethod = classType.getMethod(getMethodName, new Class[] {}); return getMethod; } /** * * @param field * @param str * @return * @throws SecurityException * @throws NoSuchMethodException * @throws ClassNotFoundException */ public static Method setFieldOfMethod(Field field, String str) throws SecurityException, NoSuchMethodException, ClassNotFoundException { String setMethodName = getFieldOfGetMethod(field, "set"); Class<?> classType = getReflectClass(str); Method setMethod = classType.getMethod(setMethodName, new Class[] { field.getType() }); return setMethod; } /** * * @param field * @param str * @return * @throws SecurityException * @throws NoSuchMethodException * @throws IllegalArgumentException * @throws IllegalAccessException * @throws InvocationTargetException * @throws ClassNotFoundException */ public static Object invokeGetMethod(Field field, String str) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, ClassNotFoundException { Method method = getFieldOfMethod(field, str); Object value = method.invoke(new Object[] {}); return value; } /** * * @param field * @param str * @param value * @throws SecurityException * @throws NoSuchMethodException * @throws IllegalArgumentException * @throws IllegalAccessException * @throws InvocationTargetException * @throws ClassNotFoundException */ public static void invokeSetMehtod(Field field, String str, Object value) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, ClassNotFoundException { Method method = setFieldOfMethod(field, str); method.invoke(new Object[] { value }); } }