Java中class工具类--Java免费学习网

package com.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class ClassUtils {   
    
    /**  
     * 获取对象的属性值  
     *   
     * @param object 对象实例  
     * @param property 属性名  
     * @return 属性的值  
     */   
    public static Object getObjectProperty(Object object, String property) throws NoSuchMethodException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchFieldException {   
        if (object == null)   
            return null;   
        try {   
            return getClassGetter(object.getClass(), property).invoke(object, new Object[0]);   
        } catch (NoSuchMethodException e) {
         Object o = object.getClass().getField(property).get(object);   
         System.out.println(o);
            return o;
        }   
    }   
   
    /**  
     * 查找getXXX与isXXX的属性Getter方法  
     *   
     * @param clazz 类元   
http://www.javalearns.com/
     * @param property 属性名  
     * @return 属性Getter方法  
     */   
    public static Method getClassGetter(Class clazz, String property) throws NoSuchMethodException, SecurityException {   
       // Assert.assertNotNull(clazz, "class不能为空!");   
        //Assert.assertNotEmpty(property, "property不能为空!");   
        property = property.trim();   
        String upper = property.substring(0, 1).toUpperCase() + property.substring(1);   
        try {   
            Method getter = getClassMethod(clazz, "get" + upper);   
            //Assert.assertTrue(getter.getReturnType() != Void.class, "getter返回值类型不能为void!");   
            return getter;   
        } catch (NoSuchMethodException e1) {   
            try {   
                Method getter = getClassMethod(clazz, "is" + upper);   
               // Assert.assertTrue(getter.getReturnType() != Void.class, "getter返回值类型不能为void!");   
                return getter;   
            } catch (NoSuchMethodException e2) {   
                Method getter = getClassMethod(clazz, property);   
                //Assert.assertTrue(getter.getReturnType() != Void.class, "getter返回值类型不能为void!");   
                return getter;   
            }   
        }   
    }   
   
    /**  
     * 获取类的方法 (保证返回方法的公开性)  
     *   
     * @param clazz 类  
     * @param methodName 方法名  
http://www.javalearns.com/
     * @return 公开的方法  
     */   
    public static Method getClassMethod(Class clazz, String methodName) throws NoSuchMethodException, SecurityException {   
        //Assert.assertNotNull(clazz, "class不能为空!");   
        //Assert.assertNotNull(methodName, "methodName不能为空!");   
        try {   
            return searchPublicMethod(clazz.getInterfaces(), methodName);   
        } catch (NoSuchMethodException e1) {   
            try {   
                return searchPublicMethod(clazz.getClasses(), methodName);   
            } catch (NoSuchMethodException e2) {   
                return clazz.getMethod(methodName, new Class[0]);   
            }   
        }   
    }   
   
    // 查找公开的方法 (辅助方法)   
    private static Method searchPublicMethod(Class[] classes, String methodName) throws NoSuchMethodException, SecurityException {   
        if (classes != null && classes.length > 0) {   
            for (int i = 0, n = classes.length; i < n; i ++) {   
                Class cls = classes[i];   
                if ((cls.getModifiers() & Modifier.PUBLIC) == 1) { // 首先保证类是公开的   
                    try {   
                        Method method = cls.getMethod(methodName, new Class[0]);   
                        if ((method.getModifiers() & Modifier.PUBLIC) == 1) // 再保证方法是公开的   
                            return method;   
                    } catch (NoSuchMethodException e) {   
                        // ignore, continue   
                    }   
                }   
            }   
        }   
        throw new NoSuchMethodException(); // 未找到方法   
    }   
   

}  

文章转载自  http://www.javalearns.com/Html/?1561.html

更多Java学习文章请访问  Java免费学习网 http://www.javalearns.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值