反射机制数据传值为空的问题
两个小方法,用于解决BeanUtils.copyProperties(x, y);中源对象的值为空问题
1.通过实体注解数据库字段为Map的Key,需要的非空值为Value封装数据
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
@Override public Map<String, Object> setNodeParamItems(DispatchInfoItem dispatchInfoItem) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { Map<String, Object> 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.根据获取的值注入;
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public void getMethods(DispatchInfo dispatchInfo, Map<String, Object> 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 反射 处理 空值
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
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( "不能为空。。。<br>" ); } if (object instanceof String) { String string = (String) object; if (string.trim().length() == 0 ) sb.append(descs[i]).append( "不能为空。。。<br>" ); else if (string.trim().equals( "0" )) sb.append( "未选择" + descs[i] + "。。。<br>" ); } else if (object instanceof Number) { Integer integer = (Integer) object; if (integer == 0 ) sb.append( "未选择" + descs[i] + "。。。<br>" ); } } 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 命名时 。属性最好不要有下划线,否则可能会粗错。。。。。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
原文链接:https://blog.csdn.net/ng_xiaohe/article/details/102738586
查看更多关于Java反射机制如何解决数据传值为空的问题的详细内容...