Reflect反射的工具类

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 });

    }

}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值