JAVA反射 比你想要的还要多

最近有个需求,需要将不同对象中同一个字段做同一的处理,但是他可能是某个对象的属性,也可能是对象属性的属性。。。。这样嵌套下去。

 

这种需求,首先想到使用反射,然后算法上使用递归。

 

来上代码:

 public void manCountryCode(Object businessData, Integer operationType)
    {
        busLogger.enterFuncDebugLog(businessData);
        
        if (null == businessData)
        {
            busLogger.exitFuncDebugLog();
            return;
        }
        
        try
        {
            manCountryCodeByOperationType(businessData, operationType);
        }
        catch (Throwable e)
        {
            busLogger.excepFuncDebugLog(e);
        }
        
        busLogger.exitFuncDebugLog(businessData);
    }
    


 

    private void manCountryCodeByOperationType(Object businessData,
            Integer operationType) throws Exception
    {
        busLogger.enterFuncDebugLog(businessData);
        
        if (recurseDepth == 0)
        {
            recurseDepth = 100;
            return;
        }
        recurseDepth--;
        
        for (Method method : businessData.getClass().getMethods())
        {
            //不是getter方法或者没有找到setter方法,则跳过该字段
            if (!isGetter(method)
                    || null == findSetterByGetter(businessData, method))
            {
                continue;
            }
            
            Class fieldType = method.getReturnType();
            if (isSimpleFieldType(fieldType))
            {
                if (isSubNoField(method))
                {
                    manSubNoCountryCode(businessData, method, operationType);
                }
            }
            else if (isCollectionType(fieldType))
            {
                Object returnCollection = method.invoke(businessData);
                processCollectionField(returnCollection, operationType);
            }
            else if (Map.class.isAssignableFrom(fieldType))
            {
                Map mapField = (Map) method.invoke(businessData);
                for (Object value : mapField.values())
                {
                    manCountryCode(value, operationType);
                }
            }
            else
            {
                processComplexField(businessData, method, operationType);
            }
        }
        busLogger.exitFuncDebugLog();
    }


 

 

 private void processCollectionField(Object returnCollection,
            Integer operationType) throws Exception
    {
        busLogger.enterFuncDebugLog();
        if (null != returnCollection)
        {
            for (Object element : (Collection) returnCollection)
            {
                if (!isSimpleFieldType(element.getClass()))
                {
                    manCountryCode(element, operationType);
                }
            }
        }
        busLogger.exitFuncDebugLog();
    }
    
    private void processComplexField(Object businessData, Method method,
            Integer operationType) throws Exception
    {
        busLogger.enterFuncDebugLog();
        Object field = method.invoke(businessData);
        if (null != field && isCollectionType(field.getClass()))
        {
            for (Object element : (Collection) field)
            {
                if (!isSimpleFieldType(element.getClass()))
                {
                    manCountryCode(element, operationType);
                }
            }
        }
        else
        {
            manCountryCode(field, operationType);
        }
        busLogger.exitFuncDebugLog(field);
    }


 

 

    
    private boolean isCollectionType(Class fieldType)
    {
        return Collection.class.isAssignableFrom(fieldType);
    }


 

这样就能将对象中的所有的某个字段都同一的处理了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值