Java反射机制如何解决数据传值为空的问题

反射机制数据传值为空的问题

两个小方法,用于解决BeanUtils.copyProperties(x, y);中源对象的值为空问题

1.通过实体注解数据库字段为Map的Key,需要的非空值为Value封装数据

@Override
    public Map setNodeParamItems(DispatchInfoItem dispatchInfoItem) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Map map = new HashMap<>();
        DispatchInfo dispatchInfo = new DispatchInfo();
        if (null != dispatchInfoItem) {
            BeanUtils.copyProperties(dispatchInfoItem, dispatchInfo);
        }
        Method[] methods = dispatchInfo.getClass().getDeclaredMethods();
        if (methods != null) {
            for (Method method : methods) {
                String methodName = method.getName();
                if (methodName.startsWith("get")) {
                    Column column = dispatchInfo.getClass().getDeclaredMethod(methodName).getAnnotation(Column.class);
                    Object value = method.invoke(dispatchInfo);
                    if (null != column && StringUtils.isNotBlank(StringHelper.getString(value))) {
                        map.put(column.name(), value);
                    }
                }
            }
        }
        return map;
    }

2.根据获取的值注入;

public void getMethods(DispatchInfo dispatchInfo, Map map) throws Exception {
        //获取方法上的注解值
        Method[] methods = dispatchInfo.getClass().getDeclaredMethods();
        if (methods != null) {
            for (Method method : methods) {
                String methodName = method.getName();
                if (methodName.startsWith("get")) {
                    Column column = dispatchInfo.getClass().getDeclaredMethod(methodName).getAnnotation(Column.class);
                    if (column != null) {
                        String setMethodName = methodName.replaceFirst("(get)", "set");
                        Method setMethod = dispatchInfo.getClass().getMethod(setMethodName, method.getReturnType());
                        ;
                        if (null != map.get(column.name())) {
                            setMethod.invoke(dispatchInfo, map.get(column.name()));
                        }
                    }
                }
            }
        }
    }

3.根据值进行实际的操作 

java 反射 处理 空值

package org.zkdg.utils.spring.annotations.impl;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.sql.SQLException;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.zkdg.utils.entity.AjaxEntity;
import org.zkdg.utils.spring.annotations.NNull;
@Aspect
@Component
/**
 * 
 * @author 王海明
 * @createData 2017年7月13日 上午8:36:23
 * @说明 :出了一些空值。。。
 */
public class AjaxEntityHandler {
    // @Pointcut("@annotation(org.zkdg.utils.annotations.AfterHandler)")
    @Pointcut("@annotation(org.zkdg.utils.spring.annotations.NullValidate)")
    // @Pointcut("execution(* org.dcexam.*.service.*.*(..))")
    public void beforeCall() {
        // service方法调用之前,检测参数,仅限第一个参数, 不能为空值
    }
    /**
     * service发生异常时调用
     */
    @Pointcut("execution(* org.dcexam.*.service.*.*(..))")
    public void afterThrowEx() {
        System.out.println("************\n\n\n\n\n\n\n\n\n\n\n\n*******");
    }
    @Around(value = "beforeCall()")
    public AjaxEntity doBefore(ProceedingJoinPoint point) throws Throwable {
        // TODO Auto-generated method stub
        // 判断不能为空
        Object[] args = point.getArgs();
        if (args == null || args[0] == null) {
            return new AjaxEntity("warning", "未选择任何数据。。。");
        }
        // 获取代理对象类方法参数
        MethodSignature target = (MethodSignature) point.getSignature();
        Annotation[][] annotations = target.getMethod().getParameterAnnotations();
        int argsIndex = 0;
        StringBuilder sb = new StringBuilder();
        for (Annotation[] annotation : annotations) {
            NNull nn = (NNull) annotation[0];
            String[] descs = nn.desc();
            String[] fields = nn.field();
            if (fields.length > 0 && fields.length > 0 && descs.length == fields.length) {
                for (int i = 0; i < fields.length; i++) {
                    Field field = args[argsIndex].getClass().getDeclaredField(fields[i]);
                    // 允许访问
                    field.setAccessible(true);
                    Object object = field.get(args[argsIndex]);
                    if (object == null) {
                        sb.append(descs[i]).append("不能为空。。。
"); } if (object instanceof String) { String string = (String) object; if (string.trim().length() == 0) sb.append(descs[i]).append("不能为空。。。
"); else if (string.trim().equals("0")) sb.append("未选择" + descs[i] + "。。。
"); } else if (object instanceof Number) { Integer integer = (Integer) object; if (integer == 0) sb.append("未选择" + descs[i] + "。。。
"); } } if (sb.length() > 0) return AjaxEntity.ERROR(sb.toString()); } argsIndex++; } // 加上@Nullvalidate 注解,不允许出现空 值 for (Object obj : args) { if (obj == null) { return AjaxEntity.WARNING("出现了不允许的空值"); } else if (obj instanceof String) { if (((String) obj).length() == 0) { return AjaxEntity.WARNING("出现了不允许的空值"); } } } AjaxEntity ajax = (AjaxEntity) point.proceed(args); return ajax == null ? AjaxEntity.ERROR("操作失败") : ajax; } /** * * @param joinPoint * 连接点 * @param ex * 异常 * @return AjaxEntity 异常信息 */ @AfterThrowing(value = "afterThrowEx()", throwing = "ex") public void doAfterThrowEx(JoinPoint joinPoint, Exception ex) { AjaxEntity ajax = new AjaxEntity(); if (ex.getCause() instanceof SQLException) { // 数据库操作异常 ajax = AjaxEntity.ERROR("操作数据库时出现异常"); } } }

另外,java 命名时 。属性最好不要有下划线,否则可能会粗错。。。。。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

你可能感兴趣的:(Java反射机制如何解决数据传值为空的问题)