Java annotation mode parameter check __javaweb

Source: Internet
Author: User

1, annotation class Notempty.java null value check

Package com.cmbc.umm.core.common.annotation;

Import java.lang.annotation.Documented;
Import Java.lang.annotation.ElementType;
Import java.lang.annotation.Inherited;
Import java.lang.annotation.Retention;
Import Java.lang.annotation.RetentionPolicy;
Import Java.lang.annotation.Target;


@Documented
@Inherited
@Target (Elementtype.field)
@Retention (retentionpolicy.runtime) public
@ Interface Notempty {
    //string value () default "";
}

2, annotation class Checkvalue.java regular expression checksum null value check

Package com.cmbc.umm.core.common.annotation;

Import java.lang.annotation.Documented;
Import Java.lang.annotation.ElementType;
Import java.lang.annotation.Inherited;
Import java.lang.annotation.Retention;
Import Java.lang.annotation.RetentionPolicy;
Import Java.lang.annotation.Target;

@Documented
@Inherited
@Target (Elementtype.field)
@Retention (retentionpolicy.runtime) public
@ Interface CheckValue {
    String value () default "";
}

3, Analytic tool class Annotationutil.java

Package com.cmbc.umm.core.common.util;
Import java.lang.annotation.Annotation;
Import Java.lang.reflect.Field;
Import Java.util.regex.Matcher;

Import Java.util.regex.Pattern;
Import Org.apache.commons.lang3.ArrayUtils;
Import Org.apache.commons.lang3.StringUtils;
Import Org.slf4j.Logger;

Import Org.slf4j.LoggerFactory;
Import Com.cmbc.umm.core.common.annotation.CheckValue;


Import Com.cmbc.umm.core.common.annotation.NotEmpty;

    public class Annotationutil {private static final Logger Logger = Loggerfactory.getlogger (Annotationutil.class); /** * Verify format is correct, if @checkvalue () does not have parameters, the checksum is null * Example: @CheckValue ("^[a-za-z0-9_-]{1,32}$") verify numbers, letters, underscores 1 to 32 bits: @Chec Kvalue () * Verify that the field is empty * * @param obj * @return If NULL is returned: Checksum passed/public static String Checkva
    Lue (Object obj) {return parseannotation (Checkvalue.class, obj, true); /** * checksum is null * @param obj * @return If NULL is returned: Checksum passed/public static String cheCkempty (Object obj) {return parseannotation (Notempty.class, obj, true);
        private static String parseannotation (class<? extends annotation> Aclazz, Object obj, Boolean hasparent) {
        StringBuilder sb = new StringBuilder ();

        Boolean flag = false;
        Class<?> clazz = Obj.getclass ();

        field[] fields = Clazz.getdeclaredfields ();
        field[] Bothfield = fields;
            if (hasparent) {class<?> superclazz = Clazz.getsuperclass ();
            field[] Superfields = Superclazz.getdeclaredfields ();
        Bothfield = (field[]) arrayutils.addall (fields, superfields);
            for (Field Field:bothfield) {Annotation Annotation = field.getannotation (Aclazz);
            if (annotation = = null) continue;

            Field.setaccessible (TRUE); try {if (annotation instanceof checkvalue) {checkvalue CV = (checkvalue) annotation;
                    String regex = Cv.value ();
                        if (Stringutils.isempty (regex)) {//input regular expression is null, so does not do checksum//continue;
                        Notempty ne = (notempty) annotation;
                        Object ovalue = field.get (obj);
                            if (Ovalue = = null) {sb.append ("field" + field.getname () + "cannot be null|");
                        Flag = true; } else {if (ovalue instanceof string) {String value = (Strin
                                g) Ovalue;
                                    if (Stringutils.isblank (value)) {Sb.append ("field" + field.getname () + "cannot be empty |");
                                Flag = true; } else {logger.info (field + field.getname () +) is not a string and cannot be judged to be
                      Empty ");      }} else {pattern pattern = Pattern.compile (r
                        Egex);
                        String value = (string) field.get (obj);
                        Matcher m = pattern.matcher (value);
                            if (!m.matches ()) {Sb.append ("field" + field.getname () + "format error |");
                        Flag = true; }} else if (annotation instanceof notempty) {Object Ovalue =
                    Field.get (obj);
                        if (Ovalue = = null) {sb.append ("field" + field.getname () + "cannot be null|");
                    Flag = true; else {if (ovalue instanceof string) {String value = (string) Ovalu
                            E if (Stringutils.isblank (value)) {Sb.append ("field" + field.getname () + "does notCan be empty | ");
                            Flag = true;
                        } else {logger.info ("field" + field.getname () + "is not a string and cannot be judged to be empty"); catch (Exception e) {Sb.ap
                Pend (E.getmessage ());
                Flag = true;
                Logger.error ("Parse annotation error:", e);
            E.printstacktrace ();
        } if (flag) {return sb.tostring ();
        else {return null;
 }
    }

}

4, testing
1) People.java

Package test;

Import Com.cmbc.commons.annotation.CheckValue;
Import Com.cmbc.commons.annotation.NotEmpty;

public class People {
    @CheckValue ("^[a-za-z0-9_-]{13,32}$")
    private String ID;
    @NotEmpty
    private String name;
    @CheckValue
    private int age;

    Public String GetId () {return
        ID;
    }

    public void SetId (String id) {
        this.id = ID;
    }

    Public String GetName () {return
        name;
    }

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

    public int getage () {return age
        ;
    }

    public void Setage (int age) {
        this.age = age;
    }

    @Override public
    String toString () {return
        "people [id= + ID +", name= "+ name +", age= "+ Age +"] ";
    }


}

2) Student.java

Package test;

Import Com.cmbc.commons.annotation.CheckValue;

public class Student extends people{
    @CheckValue
    private String Clazzno;

    Public String Getclazzno () {return
        Clazzno;
    }

    public void Setclazzno (String clazzno) {
        This.clazzno = Clazzno;
    }

}

3) test Code

Package test;

Import Java.util.Map;

Import Com.cmbc.commons.utils.AnnotationUtil;
Import Com.cmbc.epay.umm.utils.EfficentDevUtil;

public class Test {
    private String name;
    public static void Main (string[] args) {
        Student p = new Student ();
        P.setid ("1111");
        P.setname ("");
        P.setclazzno ("");
        String s = annotationutil.checkvalue (p);
        System.out.println (s);
    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.