美文网首页
@RemoveEmptyOrNullParameter 移除Ma

@RemoveEmptyOrNullParameter 移除Ma

作者: M_ENG | 来源:发表于2019-01-22 18:44 被阅读0次
    package com.meng.d_common.annotation;
    
    import java.lang.annotation.*;
    
    
    /**
     * 移除参数中为 空字符串 或者 null 的参数
     *
     * @author MENG
     * @version 2017/7/13
     * @see
     */
    @Target({ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface RemoveEmptyOrNullParameter
    {
        String description()  default "";
    }
    
    

    解析类AOP

    package com.meng.d_common.annotation;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.stereotype.Component;
    
    import java.util.Iterator;
    import java.util.Map;
    
    
    /**
     * 注解RemoveEmptyOrNullParamter aop
     *
     * @author MENG
     * @version 2017/7/13
     * @see
     */
    @Aspect
    @Component
    public class RemoveEmptyOrNullParameterAspect
    {
        /**
         * 切入点
         */
        @Pointcut("@annotation(com.meng.d_common.annotation.RemoveEmptyOrNullParameter)")
        public void pointCutMethod()
        {
        }
    
        /**
         * 方法执行之前
         *
         * @param joinPoint 参数
         */
        @Before("pointCutMethod()")
        public void before(JoinPoint joinPoint)
        {
            Object[] objects = joinPoint.getArgs();
    
            for (Object obj : objects)
            {
                if (obj != null)
                {
                    if (obj instanceof Map)
                    {
                        @SuppressWarnings("unchecked")
                        Map map = (Map<String, Object>)obj;
    
                        @SuppressWarnings("unckecked")
                        Iterator iterator = map.keySet().iterator();
    
                        while (iterator.hasNext())
                        {
                            Object key = iterator.next();
    
                            if (map.get(key) == null || ("").equals(map.get(key).toString().trim()))
                            {
                                iterator.remove();
                            }
                        }
                    }
    
                }
            }
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:@RemoveEmptyOrNullParameter 移除Ma

          本文链接:https://www.haomeiwen.com/subject/ctyrjqtx.html