java @Target 自定义注解 校验参数是否为空

第一步自定义类
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 ParamsRequired {
    /**
     * 必须参数
     * @return
     */
    boolean requrie() default true;
}
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.Date;

public class BasePojo {

    public boolean validate() throws Exception {

        Field[] fields = this.getClass().getDeclaredFields();

        for (Field field : fields) {
            if(field.isAnnotationPresent(ParamsRequired.class)) {
                ParamsRequired paramsRequired = field.getAnnotation(ParamsRequired.class);
                if(paramsRequired.requrie()) {
                    // 如果类型是String
                    if (field.getGenericType().toString().equals(
                            "class java.lang.String")) { // 如果type是类类型,则前面包含"class ",后面跟类名
                        // 拿到该属性的gettet方法
                        /**
                         * 这里需要说明一下:他是根据拼凑的字符来找你写的getter方法的
                         * 在Boolean值的时候是isXXX(默认使用ide生成getter的都是isXXX)
                         * 如果出现NoSuchMethod异常 就说明它找不到那个gettet方法 需要做个规范
                         */
                        Method m = this.getClass().getMethod(
                                "get" + getMethodName(field.getName()));

                        String val = (String) m.invoke(this);// 调用getter方法获取属性值
                        if(StringUtils.isEmpty(val)) {
                            throw new Exception(field.getName() + " 不能为空!");
                        } else if (val != null) {
                            System.out.println("String type:" + val);
                        }

                    } else if (field.getGenericType().toString().equals(
                            "class java.lang.Integer")) { // 如果type是类类型,则前面包含"class ",后面跟类名
                        Method m = this.getClass().getMethod(
                                "get" + getMethodName(field.getName()));

                        Integer val = (Integer) m.invoke(this);// 调用getter方法获取属性值
                        if(val==null) {
                            throw new Exception(field.getName() + " 不能为空!");
                        } else if (val != null) {
                            System.out.println("Integer type:" + val);
                        }

                    }else if (field.getGenericType().toString().equals(
                            "class java.util.Date")) {
                        Method m = this.getClass().getMethod(
                                "get" + getMethodName(field.getName()));

                        Date val = (Date) m.invoke(this);// 调用getter方法获取属性值
                        if(val==null) {
                            throw new Exception(field.getName() + " 不能为空!");
                        } else if (val != null) {
                            System.out.println("Date type:" + val);
                        }

                    }else if (field.getGenericType().toString().equals(
                            "class java.math.BigDecimal")) {
                        Method m = this.getClass().getMethod(
                                "get" + getMethodName(field.getName()));

                        BigDecimal val = (BigDecimal) m.invoke(this);// 调用getter方法获取属性值
                        if(val==null) {
                            throw new Exception(field.getName() + " 不能为空!");
                        } else if (val != null) {
                            System.out.println("BigDecimal type:" + val);
                        }

                    }else if (field.getGenericType().toString().equals(
                            "class java.lang.Double")) {
                        Method m = this.getClass().getMethod(
                                "get" + getMethodName(field.getName()));

                        Double val = (Double) m.invoke(this);// 调用getter方法获取属性值
                        if(val==null) {
                            throw new Exception(field.getName() + " 不能为空!");
                        } else if (val != null) {
                            System.out.println("double type:" + val);
                        }

                    }else if (field.getGenericType().toString().equals(
                            "class java.lang.Float")) {
                        Method m = this.getClass().getMethod(
                                "get" + getMethodName(field.getName()));

                        Float val = (Float) m.invoke(this);// 调用getter方法获取属性值
                        if(val==null) {
                            throw new Exception(field.getName() + " 不能为空!");
                        } else if (val != null) {
                            System.out.println("float type:" + val);
                        }

                    }
                }

            }
        }
        return  true;
    }

    /**
     * 把一个字符串的第一个字母大写、效率是最高的、
     */
    private String getMethodName(String fildeName) throws Exception{
        byte[] items = fildeName.getBytes();
        items[0] = (byte) ((char) items[0] - 'a' + 'A');
        return new String(items);
    }


}
import java.math.BigDecimal;
import java.util.Date;

public class Text extends BasePojo {


    @ParamsRequired
    private String name;

    @ParamsRequired
    private Integer age;

    @ParamsRequired
    private Date createTime;

    @ParamsRequired
    private Double adouble;
    @ParamsRequired
    private Float afloat;
    @ParamsRequired
    private BigDecimal decimal;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Double getAdouble() {
        return adouble;
    }

    public void setAdouble(Double adouble) {
        this.adouble = adouble;
    }

    public Float getAfloat() {
        return afloat;
    }

    public void setAfloat(Float afloat) {
        this.afloat = afloat;
    }

    public BigDecimal getDecimal() {
        return decimal;
    }

    public void setDecimal(BigDecimal decimal) {
        this.decimal = decimal;
    }
}

 public static void main(String[] args){
       

  Text req = new Text();
   req.setName("张三");
    req.setAge(1);
req.setDecimal(new BigDecimal(12212));
req.setCreateTime(new Date());


        
    }

  
 

猜你喜欢

转载自blog.csdn.net/u012269637/article/details/83659516