Java reflection-based verification framework

Source: Internet
Author: User

<H1> preface <H1> getting started Java code
Demoentity de = new demoentity (); // demoentity is a javabean. // General cloudbindingresult result = validatorutils. validateentity (de, "str", notemptyrule. class); system. out. println (result. getfielderrormessage ("str"); // custom verification information result = validatorutils. validateentity (de, "str", new notemptyrule ("here is an example of specifying verification information! "); System. out. println (result. getfielderrormessage ("str"); // verify both attributes result = validatorutils. validateentity (de, "num, date", notnullrule. class); system. out. println (result. getfielderrormessage ("num") + "|" + result. getfielderrormessage ("date"); // use multiple verification rules result = validatorutils. validateentity (de, "str", notemptyrule. class, singleemailrule. class); system. out. println (result. getfielderrormessa Ge ("str"); // check the printed verification information! // Multiple attributes use multiple rules at the same time. result = validatorutils. validateentity (de, "str, col", notemptyrule. class, singleemailrule. class); system. out. println (result. getfielderrormessage ("str") + "|" + result. getfielderrormessage ("col "));
<Br> the running result is:
Java code
Cannot be blank! Here is an example of specifying verification information! Cannot be blank! | Cannot be blank! Cannot be blank !; Incorrect email format! Cannot be blank !; Incorrect email format! | Cannot be blank !; Incorrect email format!
<Br> 3. Note that sometimes, we need to verify whether a single string or number meets a certain rule. At this time, such a value is not a value in the javabean, in this case, the validatevalue method in validatorutils solves such separate verification and can directly verify the given content. Example:
Java code
// Note that the return value of the following method is of the string type. System. out. println (validatorutils. validatevalue ("hello", notemptyrule. class, singleemail. class); system. out. println (validatorutils. validatevalue ("", notemptyrule. class, singleemail. class); system. out. println (validatorutils. validatevalue (null, notemptyrule. class, singleemail. class); system. out. println (validatorutils. validatevalue ("hello", notemptyrule. class ));
<Br> the running result is as follows:
Java code
Incorrect email format! Cannot be blank !; Incorrect email format! Cannot be blank !; Incorrect email format!
 
<H1> Custom Rules Java code
Package com. cloudframework. validation. rules;/*** API used to verify the rule. All specific verification rules are implemented from this interface. ** @ Author taofucheng **/public interface validaterule {/*** verifies the logic of specific rules. ** @ Param value * indicates the specific verification content. * @ Return true: verification passed; false: Verification Failed */public boolean validate (object value);/*** get the error message that the verification failed. If the user-specified authentication information is null, the default error message is returned. ** @ Return returns the verification error message */public string getvalidateinfo ();}
<Br> for specific implementation and rule use examples, refer to the existing three rule classes (notemptyrule, notnullrule, singleemailrule ).
<H1> Design The relationship between the design idea and each other has been described in detail. I will not go into details here.
<H1> postscript <H2> <br> extensible as annotation <H2> supplement

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.