Java反射機(jī)制如何解決數(shù)據(jù)傳值為空的問(wèn)題
反射機(jī)制數(shù)據(jù)傳值為空的問(wèn)題
兩個(gè)小方法,用于解決BeanUtils.copyProperties(x, y);中源對(duì)象的值為空問(wèn)題
1.通過(guò)實(shí)體注解數(shù)據(jù)庫(kù)字段為Map的Key,需要的非空值為Value封裝數(shù)據(jù)
@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.根據(jù)獲取的值注入;
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.根據(jù)值進(jìn)行實(shí)際的操作
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
* @說(shuō)明 :出了一些空值。。。
*/
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方法調(diào)用之前,檢測(cè)參數(shù),僅限第一個(gè)參數(shù), 不能為空值
}
/**
* service發(fā)生異常時(shí)調(diào)用
*/
@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", "未選擇任何數(shù)據(jù)。。。");
}
// 獲取代理對(duì)象類(lèi)方法參數(shù)
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]);
// 允許訪問(wèn)
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 注解,不允許出現(xiàn)空 值
for (Object obj : args) {
if (obj == null) {
return AjaxEntity.WARNING("出現(xiàn)了不允許的空值");
} else if (obj instanceof String) {
if (((String) obj).length() == 0) {
return AjaxEntity.WARNING("出現(xiàn)了不允許的空值");
}
}
}
AjaxEntity ajax = (AjaxEntity) point.proceed(args);
return ajax == null ? AjaxEntity.ERROR("操作失敗") : ajax;
}
/**
*
* @param joinPoint
* 連接點(diǎn)
* @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) {
// 數(shù)據(jù)庫(kù)操作異常
ajax = AjaxEntity.ERROR("操作數(shù)據(jù)庫(kù)時(shí)出現(xiàn)異常");
}
}
}另外,java 命名時(shí) 。屬性最好不要有下劃線,否則可能會(huì)粗錯(cuò)。。。。。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis插入Oracle數(shù)據(jù)庫(kù)日期型數(shù)據(jù)過(guò)程解析
這篇文章主要介紹了Mybatis插入Oracle數(shù)據(jù)庫(kù)日期型數(shù)據(jù)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
Java接口操作(繼承父類(lèi)并實(shí)現(xiàn)多個(gè)接口)
這篇文章主要介紹了Java接口操作(繼承父類(lèi)并實(shí)現(xiàn)多個(gè)接口),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
java截取字符串中的指定字符的兩種方法(以base64圖片為例)
本文介紹了使用Java截取字符串中指定字符的方法,通過(guò)substring索引和正則實(shí)現(xiàn),文章詳細(xì)介紹了實(shí)現(xiàn)步驟和示例代碼,對(duì)于想要了解如何使用Java截取字符串指定字符的讀者具有一定的參考價(jià)值2023-08-08
mybatis自定義類(lèi)型處理器TypehHandler示例詳解
我們?cè)趯?xiě)mapper映射器的配置文件時(shí),不經(jīng)意間已經(jīng)用到類(lèi)型轉(zhuǎn)換,不過(guò)是mybatis幫我們完成的,下面這篇文章主要給大家介紹了關(guān)于mybatis自定義類(lèi)型處理器TypehHandler的相關(guān)資料,需要的朋友可以參考下2018-09-09
java處理數(shù)據(jù)庫(kù)不支持的emoji表情符問(wèn)題解決
這篇文章主要介紹了java處理數(shù)據(jù)庫(kù)不支持的emoji表情符問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
java大話(huà)之創(chuàng)建型設(shè)計(jì)模式教程示例
這篇文章主要為大家介紹了java大話(huà)之創(chuàng)建型設(shè)計(jì)模式教程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Springboot+mybatis-plus+注解實(shí)現(xiàn)數(shù)據(jù)權(quán)限隔離
本文將結(jié)合實(shí)例代碼,介紹Springboot+mybatis-plus+注解實(shí)現(xiàn)數(shù)據(jù)權(quán)限隔離,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07
Spring MVC獲取查詢(xún)參數(shù)及路徑參數(shù)代碼實(shí)例
這篇文章主要介紹了Spring MVC獲取查詢(xún)參數(shù)及路徑參數(shù)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02

