Bootstrap

java注解判断字段是否存在_使用注解和反射判断指定的字段不能为空

我们在写项目的时候,如何类比较少。判别指定对象的属性值是否为空,那确实可以,但是随着类的增多,判别对象的属性是否为空就非常的繁琐,所以可以使用自定义注解和反射来判定指定的字段是否为空。

第一步:创建一个自定义注解

package com.zczy.test.annoation;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

@Target(ElementType.FIELD)

@Retention(RetentionPolicy.RUNTIME)

public @interface ParamsCheck {

/**

* 此注解就是过滤不需要的字段,来判断需要的字段是否为空

*/

String attributeName();

}

第二步:创建一个类,并在类的属性上加上自定义注解的标识,就是为了判断这些字段是否为空

package com.zczy.test.bean;

import com.zczy.test.annoation.ParamsCheck;

import lombok.Data;

@Data

public class Person {

@ParamsCheck(attributeName = "userName")

private String userName;

@ParamsCheck(attributeName = "id")

private String id;

private String sex;

public Person(String userName,String id,String sex){

this.userName = userName;

this.id = id;

this.sex = sex;

}

}

第三部:自定义一个返回类型的类

package com.zczy.test.utils;

import lombok.Data;

@Data

public class ValidResult {

private String msg;

private String code;

public ValidResult(){

}

public ValidResult(String msg,String code){

this.msg = msg;

this.code = code;

}

}

第四步:编写反射的代码

package com.zczy.test.utils;

import com.zczy.test.annoation.ParamsCheck;

import java.lang.annotation.Annotation;

import java.lang.reflect.Field;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

import java.util.ArrayList;

import java.util.List;

public class CheckPointParamsHandlar {

public Class clazz;

private Field[] fields;

private T t;

public CheckPointParamsHandlar(T t){

this.t = t;

this.clazz = t.getClass();

//getDeclaredFields()返回Class中所有的字段,包括私有字段;

//getFields 只返回公共字段,即有public修饰的字段

this.fields = t.getClass().getDeclaredFields();

}

//判断已经注解的字段是否为空

public ValidResult getParamChecksResults(){

List list = new ArrayList<>();

ValidResult res = new ValidResult();

Boolean flag = true;

StringBuffer sb = new StringBuffer();

for(Field field : fields ){

//System.out.println(field);

//private java.lang.String com.zczy.test.bean.Person.userName

//private java.lang.String com.zczy.test.bean.Person.id

//private java.lang.String com.zczy.test.bean.Person.sex

//查看注解的必要字段不能为空--如果没有注解直接跳过

Annotation annotation=null;

annotation = field.getAnnotation(ParamsCheck.class);

if(annotation == null){

continue;

}

//判断注解过的字段是否为空

// System.out.println("annotation>>>>>"+annotation);

//annotation>>>>>@com.zczy.test.annoation.ParamsCheck(attributeName=userName)

//annotation>>>>>@com.zczy.test.annoation.ParamsCheck(attributeName=id)

String name = field.getName();

// System.out.println(name);

// userName

// id

// sex

char[] tempChar = name.toCharArray();

tempChar[0] -= 32;

String tempStr ="get"+ String.valueOf(tempChar);

list.add(tempStr);

//System.out.println("list>>>>>"+list);

// list>>>>>[getUserName, getId]

}

for(String str : list){

try {

Method method = clazz.getMethod(str);

//System.out.println("method>>>"+method);

// method>>>public java.lang.String com.zczy.test.bean.Person.getUserName()

//method>>>public java.lang.String com.zczy.test.bean.Person.getId()

Object invoke = method.invoke(t);

// System.out.println(invoke);

if(null == invoke || "".equals(invoke)){

flag = false;

sb.append(str.toLowerCase()+"不能为空").append(",");

}

} catch (NoSuchMethodException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

}

}

if(flag == false){

res = new ValidResult(sb.toString(),"1");

}else{

res = new ValidResult("校验成功!","0");

}

return res;

}

}

测试类:

package com.zczy.test.Test;

import com.zczy.test.bean.Person;

import com.zczy.test.utils.CheckPointParamsHandlar;

import com.zczy.test.utils.ValidResult;

public class ParamsCheckTest {

public static void main(String[] args) {

Person person = new Person("1111","22","111");

ValidResult paramChecksResults = new CheckPointParamsHandlar<>(person).getParamChecksResults();

System.out.println(paramChecksResults.toString());

}

}

结果:

测试1:

ec5f25595ce84c98f0a0235cb93bf288.png

ec401e64ab892462b708ccb4d81c21f6.png

测试2:

dc9b296e0eca1e618487fbbcfaa07c3b.png

325609a2a6ef2edb183311ae35cd967c.png

测试3:

579983c3ce34c8b0821b3cd55c91793f.png

测试4:

4164e06216f39afdde31c77831ad7ece.png

本文地址:https://blog.csdn.net/trainhui/article/details/107336671

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

;