VerifyProcessor.java 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783
  1. package cn.com.ty.lift.common.verify;
  2. import lombok.extern.slf4j.Slf4j;
  3. import javax.validation.constraints.*;
  4. import java.io.ObjectStreamException;
  5. import java.lang.annotation.Annotation;
  6. import java.lang.invoke.MethodHandle;
  7. import java.lang.invoke.MethodHandles;
  8. import java.lang.invoke.MethodType;
  9. import java.lang.reflect.Array;
  10. import java.lang.reflect.Field;
  11. import java.lang.reflect.InvocationTargetException;
  12. import java.lang.reflect.Method;
  13. import java.math.BigDecimal;
  14. import java.math.BigInteger;
  15. import java.net.IDN;
  16. import java.time.*;
  17. import java.time.chrono.HijrahDate;
  18. import java.time.chrono.JapaneseDate;
  19. import java.time.chrono.MinguoDate;
  20. import java.time.chrono.ThaiBuddhistDate;
  21. import java.time.temporal.TemporalAccessor;
  22. import java.util.*;
  23. import java.util.concurrent.ConcurrentHashMap;
  24. import java.util.regex.Matcher;
  25. import java.util.regex.PatternSyntaxException;
  26. import java.util.stream.Collectors;
  27. import static java.util.regex.Pattern.CASE_INSENSITIVE;
  28. /**
  29. * the validation for parameter implements {@linkplain javax.validation.constraints},
  30. * reform from hibernate validator (v6.0.16.Final)
  31. *
  32. * @author wcz
  33. * @since 2020/2/15
  34. */
  35. @Slf4j
  36. public class VerifyProcessor {
  37. /**
  38. * the name of the verify method.
  39. */
  40. private final String verifyMethod = "verify";
  41. /**
  42. * the list of validation annotation & method can be work in {@code javax.validation.constraints.*}
  43. */
  44. private final Map<Class<?>, Method> verifyMethodMap = new ConcurrentHashMap<>();
  45. /**
  46. * whether write log to file. it's {@code true} by default.
  47. */
  48. private boolean logWrite = true;
  49. /**
  50. * the message to complete verify.
  51. */
  52. private final ThreadLocal<String> message = new ThreadLocal<>();
  53. /**
  54. * the type of message ,1: verify ,2: illegal argument
  55. */
  56. private final ThreadLocal<Integer> code = new ThreadLocal<>();
  57. /**
  58. * the max length for a valid email address local part.
  59. */
  60. private final int MAX_LOCAL_PART_LENGTH = 64;
  61. /**
  62. * the regular expression for local part of a valid email address.
  63. */
  64. private final String LOCAL_PART_ATOM = "[a-z0-9!#$%&'*+/=?^_`{|}~\u0080-\uFFFF-]";
  65. /**
  66. * the regular expression for the local part of an email address.
  67. */
  68. private final String LOCAL_PART_INSIDE_QUOTES_ATOM = "([a-z0-9!#$%&'*.(),<>\\[\\]:; @+/=?^_`{|}~\u0080-\uFFFF-]|\\\\\\\\|\\\\\\\")";
  69. /**
  70. * Regular expression for the local part of an email address (everything before '@')
  71. */
  72. private final java.util.regex.Pattern LOCAL_PART_PATTERN = java.util.regex.Pattern.compile(
  73. "(" + LOCAL_PART_ATOM + "+|\"" + LOCAL_PART_INSIDE_QUOTES_ATOM + "+\")" +
  74. "(\\." + "(" + LOCAL_PART_ATOM + "+|\"" + LOCAL_PART_INSIDE_QUOTES_ATOM + "+\")" + ")*", CASE_INSENSITIVE
  75. );
  76. /**
  77. * This is the maximum length of a domain name. But be aware that each label (parts separated by a dot) of the
  78. * domain name must be at most 63 characters long. This is verified by {@link IDN#toASCII(String)}.
  79. */
  80. private final int MAX_DOMAIN_PART_LENGTH = 255;
  81. private final String DOMAIN_CHARS_WITHOUT_DASH = "[a-z\u0080-\uFFFF0-9!#$%&'*+/=?^_`{|}~]";
  82. private final String DOMAIN_LABEL = "(" + DOMAIN_CHARS_WITHOUT_DASH + "-*)*" + DOMAIN_CHARS_WITHOUT_DASH + "+";
  83. /**
  84. * the regex for check domain
  85. */
  86. private final String DOMAIN = DOMAIN_LABEL + "+(\\." + DOMAIN_LABEL + "+)*";
  87. /**
  88. * regex for check IP v4
  89. */
  90. private final String IP_DOMAIN = "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}";
  91. /**
  92. * IP v6 regex taken from http://stackoverflow.com/questions/53497/regular-expression-that-matches-valid-ipv6-addresses
  93. */
  94. private final String IP_V6_DOMAIN = "(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))";
  95. /**
  96. * Regular expression for the domain part of an URL
  97. * A host string must be a domain string, an IPv4 address string, or "[", followed by an IPv6 address string, followed by "]".
  98. */
  99. private final java.util.regex.Pattern DOMAIN_PATTERN = java.util.regex.Pattern.compile(DOMAIN + "|\\[" + IP_V6_DOMAIN + "\\]", CASE_INSENSITIVE);
  100. /**
  101. * Regular expression for the domain part of an email address (everything after '@')
  102. */
  103. private final java.util.regex.Pattern EMAIL_DOMAIN_PATTERN = java.util.regex.Pattern.compile(
  104. DOMAIN + "|\\[" + IP_DOMAIN + "\\]|" + "\\[IPv6:" + IP_V6_DOMAIN + "\\]", CASE_INSENSITIVE
  105. );
  106. /**
  107. * the minimum value of compare two number for the infinity check when double or float.
  108. */
  109. private final OptionalInt LESS_THAN = OptionalInt.of(-1);
  110. /**
  111. * the empty OptionalInt(value = 0) of compare two number for the infinity check when double or float.
  112. */
  113. private final OptionalInt FINITE_VALUE = OptionalInt.empty();
  114. /**
  115. * the maximun value of compare two number for the infinity check when double or float.
  116. */
  117. private final OptionalInt GREATER_THAN = OptionalInt.of(1);
  118. /**
  119. * short 0
  120. */
  121. private final short SHORT_ZERO = (short) 0;
  122. /**
  123. * byte 0
  124. */
  125. private final byte BYTE_ZERO = (byte) 0;
  126. /**
  127. * to private the constrctor.
  128. */
  129. private VerifyProcessor() {
  130. collectVerifyClassMethod();
  131. }
  132. /**
  133. * collect all vaild validation annotation from the methods.
  134. */
  135. private void collectVerifyClassMethod() {
  136. Method[] declaredMethods = this.getClass().getDeclaredMethods();
  137. List<Method> verifyMethods = Arrays.stream(declaredMethods).filter(method -> verifyMethod.equals(method.getName())).collect(Collectors.toList());
  138. isTrue(verifyMethods.isEmpty(), "No any method named 'verify' in VerifyProcessor.");
  139. for (Method method : verifyMethods) {
  140. Optional<Class<?>> classOptional = Arrays.stream(method.getParameterTypes()).filter(type -> Annotation.class.isAssignableFrom(type)).findFirst();
  141. classOptional.ifPresent(verifyClass -> {
  142. if (!method.isAccessible()) {
  143. method.setAccessible(true);
  144. }
  145. verifyMethodMap.put(verifyClass, method);
  146. log.info("@interface: {}", verifyClass.getCanonicalName());
  147. });
  148. }
  149. isTrue(verifyMethodMap.isEmpty(), "No valid validation annotation was resolved in VerifyProcessor.");
  150. }
  151. /**
  152. * the static inner class to builder a singleton instance.
  153. */
  154. private static class Builder {
  155. private static VerifyProcessor INSTANCE = new VerifyProcessor();
  156. }
  157. /**
  158. * get the singleton instance of this
  159. */
  160. private static VerifyProcessor processor() {
  161. return Builder.INSTANCE;
  162. }
  163. /**
  164. * to do verify by using the singleton instance.
  165. *
  166. * @param logWrite {@code true} log information, otherwise no log to file.
  167. * @param object the target object to verify.
  168. * @param fields the list of fields to verify.
  169. */
  170. public static void perform(final boolean logWrite, final Object object, final String... fields) {
  171. Builder.INSTANCE.logWrite(logWrite).action(object, fields);
  172. }
  173. /**
  174. * to do verify by using the singleton instance.
  175. *
  176. * @param object the target object to verify.
  177. * @param fields the list of fields to verify.
  178. */
  179. public static void perform(final Object object, final String... fields) {
  180. Builder.INSTANCE.action(object, fields);
  181. }
  182. /**
  183. * 因为单例模式虽然能保证线程安全,但在序列化和反序列化的情况下会出现生成多个对象的情况.
  184. * readResolve()方法,其实当JVM从内存中反序列化地"组装"一个新对象时,
  185. * 就会自动调用这个readResolve方法来返回指定好的对象, 单例规则也就得到了保证。
  186. * readResolve()的出现允许程序员自行控制通过反序列化得到的对象。
  187. */
  188. protected Object readResolve() throws ObjectStreamException {
  189. return Builder.INSTANCE;
  190. }
  191. /**
  192. * setting whether write log to file.
  193. *
  194. * @param logWrite {@code true} to write log, {@code false} to no.
  195. * @return this instance.
  196. */
  197. public VerifyProcessor logWrite(boolean logWrite) {
  198. this.logWrite = logWrite;
  199. return this;
  200. }
  201. /**
  202. * set the verify message in the ThreadLocal.(code : 1)
  203. */
  204. private void setVerify(String message, Object... values) {
  205. this.code.set(1);
  206. this.message.set(format(message, values));
  207. }
  208. /**
  209. * set the illegal argument message in the ThreadLocal.(code : 2)
  210. */
  211. private void setIllegalArg(String message, Object... values) {
  212. this.code.set(2);
  213. this.message.set(format(message, values));
  214. }
  215. /**
  216. * get the systemDefaultZone {@link Clock#systemDefaultZone()}. for the date compare.
  217. */
  218. private Clock systemDefaultClock() {
  219. return Clock.offset(Clock.systemDefaultZone(), Duration.ZERO.abs().negated());
  220. }
  221. /**
  222. * verify the value is {@code null} or no.
  223. *
  224. * @param value any value.
  225. * @return if return {@code true},that the value is null, otherwise no.
  226. */
  227. private boolean isNull(Object value) {
  228. return null == value;
  229. }
  230. /**
  231. * verify the value isn't {@code null} or no.
  232. *
  233. * @param value any value.
  234. * @return if return {@code true}, that the value isn't noll, otherwise no.
  235. */
  236. private boolean notNull(Object value) {
  237. return null != value;
  238. }
  239. /**
  240. * verify the int value is greater than zero.
  241. *
  242. * @param value any int value.
  243. * @return if return {@code true}, the value is greater than zero, otherwise no.
  244. */
  245. private boolean gt0(int value) {
  246. return value > 0;
  247. }
  248. /**
  249. * verify the int value is less than zero.
  250. *
  251. * @param value any int value.
  252. * @return if return {@code true}, the value is less than zero, otherwise no
  253. */
  254. private boolean lt0(int value) {
  255. return value < 0;
  256. }
  257. /**
  258. * verify the int value is greater than zero or equal to zero.
  259. *
  260. * @param value any int value.
  261. * @return if return {@code true}, the value is greater than zero or equal to zero, otherwise no
  262. */
  263. private boolean gte0(int value) {
  264. return value >= 0;
  265. }
  266. /**
  267. * verify the int value is less than zero or equal to zero.
  268. *
  269. * @param value any int value.
  270. * @return if return {@code true}, the value is less than zero or equal to zero, otherwise no
  271. */
  272. private boolean lte0(int value) {
  273. return value <= 0;
  274. }
  275. /**
  276. * verify the value is instance of the Boolean.
  277. *
  278. * @param value any value.
  279. * @return if return {@code true}, the value is Boolean, otherwise no
  280. */
  281. private boolean isBoolean(Object value) {
  282. return (value instanceof Boolean);
  283. }
  284. /**
  285. * verify the value is instance of the Number.
  286. *
  287. * @param value any value.
  288. * @return if return {@code true}, the value is Number, otherwise no
  289. */
  290. private boolean isNumber(Object value) {
  291. return (value instanceof Number);
  292. }
  293. /**
  294. * verify the value is instance of the CharSequence.
  295. *
  296. * @param value any value.
  297. * @return if return {@code true}, the value is CharSequence, otherwise no
  298. */
  299. private boolean isCharSequence(Object value) {
  300. return (value instanceof CharSequence);
  301. }
  302. /**
  303. * verify the value is instance of the BigDecimal.
  304. *
  305. * @param value any value.
  306. * @return if return {@code true}, the value is BigDecimal, otherwise no
  307. */
  308. private boolean isBigDecimal(Object value) {
  309. return (value instanceof BigDecimal);
  310. }
  311. /**
  312. * verify the value is instance of the BigInteger.
  313. *
  314. * @param value any value.
  315. * @return if return {@code true}, the value is BigInteger, otherwise no
  316. */
  317. private boolean isBigInteger(Object value) {
  318. return (value instanceof BigInteger);
  319. }
  320. /**
  321. * verify the value is instance of the Long.
  322. *
  323. * @param value any value.
  324. * @return if return {@code true}, the value is Long, otherwise no
  325. */
  326. private boolean isLong(Object value) {
  327. return (value instanceof Long);
  328. }
  329. /**
  330. * verify the value is instance of the Integer.
  331. *
  332. * @param value any value.
  333. * @return if return {@code true}, the value is Integer, otherwise no
  334. */
  335. private boolean isInteger(Object value) {
  336. return (value instanceof Integer);
  337. }
  338. /**
  339. * verify the value is instance of the Double.
  340. *
  341. * @param value any value.
  342. * @return if return {@code true}, the value is Double, otherwise no
  343. */
  344. private boolean isDouble(Object value) {
  345. return (value instanceof Double);
  346. }
  347. /**
  348. * verify the value is instance of the Float.
  349. *
  350. * @param value any value.
  351. * @return if return {@code true}, the value is Float, otherwise no
  352. */
  353. private boolean isFloat(Object value) {
  354. return (value instanceof Float);
  355. }
  356. /**
  357. * verify the value is instance of the Byte.
  358. *
  359. * @param value any value.
  360. * @return if return {@code true}, the value is Byte, otherwise no
  361. */
  362. private boolean isByte(Object value) {
  363. return (value instanceof Byte);
  364. }
  365. /**
  366. * verify the value is instance of the Short.
  367. *
  368. * @param value any value.
  369. * @return if return {@code true}, the value is Short, otherwise no
  370. */
  371. private boolean isShort(Object value) {
  372. return (value instanceof Short);
  373. }
  374. /**
  375. * verify the value is instance of the String.
  376. *
  377. * @param value any value.
  378. * @return if return {@code true}, the value is String, otherwise no
  379. */
  380. private boolean isString(Object value) {
  381. return (value instanceof String);
  382. }
  383. /**
  384. * verify the value is instance of the Collection.
  385. *
  386. * @param value any value.
  387. * @return if return {@code true}, the value is Collection, otherwise no
  388. */
  389. private boolean isCollection(Object value) {
  390. return (value instanceof Collection);
  391. }
  392. /**
  393. * verify the value is instance of the Map.
  394. *
  395. * @param value any value.
  396. * @return if return {@code true}, the value is Map, otherwise no
  397. */
  398. private boolean isMap(Object value) {
  399. return (value instanceof Map);
  400. }
  401. /**
  402. * verify the value is a Array.
  403. *
  404. * @param value any value.
  405. * @return if return {@code true}, the value is Array, otherwise no
  406. */
  407. private boolean isArray(Object value) {
  408. return value.getClass().isArray();
  409. }
  410. /**
  411. * verify the value is instance of the Iterator.
  412. *
  413. * @param value any value.
  414. * @return if return {@code true}, the value is Iterator, otherwise no
  415. */
  416. private boolean isIterator(Object value) {
  417. return (value instanceof Iterator);
  418. }
  419. /**
  420. * verify the value is instance of the Enumeration.
  421. *
  422. * @param value any value.
  423. * @return if return {@code true}, the value is Enumeration, otherwise no
  424. */
  425. private boolean isEnumeration(Object value) {
  426. return (value instanceof Enumeration);
  427. }
  428. /**
  429. * verify the value is instance of the TemporalAccessor(java 8).
  430. *
  431. * @param value any value.
  432. * @return if return {@code true}, the value is TemporalAccessor, otherwise no
  433. */
  434. private boolean isTemporalAccessor(Object value) {
  435. return (value instanceof TemporalAccessor);
  436. }
  437. /**
  438. * verify the value is instance of the java.util.Date.
  439. *
  440. * @param value any value.
  441. * @return if return {@code true}, the value is Date, otherwise no
  442. */
  443. private boolean isDate(Object value) {
  444. return (value instanceof Date);
  445. }
  446. /**
  447. * verify the value is instance of the java.util.Calendar.
  448. *
  449. * @param value any value.
  450. * @return if return true, the value is Calendar, otherwise no
  451. */
  452. private boolean isCalendar(Object value) {
  453. return (value instanceof Calendar);
  454. }
  455. /**
  456. * new a BigDecimal object with a CharSequence value.
  457. *
  458. * @param value any CharSequence value
  459. * @return a new {@link BigDecimal} object or null when occur any exception.
  460. */
  461. private BigDecimal newBigDecimal(CharSequence value) {
  462. try {
  463. BigDecimal bd;
  464. if (isString(value)) {
  465. bd = new BigDecimal((String) value);
  466. } else {
  467. bd = new BigDecimal(value.toString());
  468. }
  469. return bd;
  470. } catch (Exception e) {
  471. setIllegalArg("Failed to convert '%s' to a valid BigDecimal. Exception: %s", value, e.getMessage());
  472. return null;
  473. }
  474. }
  475. /**
  476. * new a BigDecimal object with a Number value, base on the Specific type of the value.
  477. *
  478. * @param value any Number value
  479. * @return a new {@link BigDecimal} object or null when occur any exception.
  480. */
  481. private BigDecimal newBigDecimal(Number value) {
  482. try {
  483. BigDecimal bd;
  484. if (isLong(value)) {
  485. bd = BigDecimal.valueOf((Long) value);
  486. } else if (isBigDecimal(value)) {
  487. bd = ((BigDecimal) value);
  488. } else if (isBigInteger(value)) {
  489. bd = new BigDecimal((BigInteger) value);
  490. } else if (isDouble(value)) {
  491. bd = BigDecimal.valueOf((Double) value);
  492. } else if (isFloat(value)) {
  493. bd = BigDecimal.valueOf((Float) value);
  494. } else {
  495. bd = BigDecimal.valueOf(value.doubleValue());
  496. }
  497. return bd;
  498. } catch (Exception e) {
  499. setIllegalArg("Failed to convert '%s' to a valid BigDecimal. Exception: %s", value, e.getMessage());
  500. return null;
  501. }
  502. }
  503. /**
  504. * verify whether a double value is infinity with less or greater.
  505. *
  506. * @param number any double value.
  507. * @param treatNanAs when the value isn't a number, return this value.
  508. * @return a OptionalInt value with the result of compare to infinity.
  509. * -1 for equal {@link Double.NEGATIVE_INFINITY}
  510. * 1 for equal {@link Double.POSITIVE_INFINITY}
  511. */
  512. private OptionalInt infinityCheck(Double number, OptionalInt treatNanAs) {
  513. OptionalInt result = FINITE_VALUE;
  514. if (number == Double.NEGATIVE_INFINITY) {
  515. result = LESS_THAN;
  516. } else if (number.isNaN()) {
  517. result = treatNanAs;
  518. } else if (number == Double.POSITIVE_INFINITY) {
  519. result = GREATER_THAN;
  520. }
  521. return result;
  522. }
  523. /**
  524. * verify whether a float vlaue is infinity with less or greater.
  525. *
  526. * @param number any float value.
  527. * @param treatNanAs when the value isn't a number, return this value.
  528. * @return a OptionalInt value with the result of compare to infinity.
  529. * -1 for equal {@link Float.NEGATIVE_INFINITY}
  530. * 1 for equal {@link Float.POSITIVE_INFINITY}
  531. */
  532. private OptionalInt infinityCheck(Float number, OptionalInt treatNanAs) {
  533. OptionalInt result = FINITE_VALUE;
  534. if (number == Float.NEGATIVE_INFINITY) {
  535. result = LESS_THAN;
  536. } else if (number.isNaN()) {
  537. result = treatNanAs;
  538. } else if (number == Float.POSITIVE_INFINITY) {
  539. result = GREATER_THAN;
  540. }
  541. return result;
  542. }
  543. /**
  544. * Checks the validity of the domain name used in an email. To be valid it should be either a valid host name, or an
  545. * IP address wrapped in [].
  546. *
  547. * @param domain domain to check for validity
  548. * @return {@code true} if the provided string is a valid domain, {@code false} otherwise
  549. */
  550. private boolean isValidEmailDomainAddress(String domain) {
  551. return isValidDomainAddress(domain, EMAIL_DOMAIN_PATTERN);
  552. }
  553. /**
  554. * Checks validity of a domain name.
  555. *
  556. * @param domain the domain to check for validity
  557. * @return {@code true} if the provided string is a valid domain, {@code false} otherwise
  558. */
  559. private boolean isValidDomainAddress(String domain) {
  560. return isValidDomainAddress(domain, DOMAIN_PATTERN);
  561. }
  562. /**
  563. * verify validity of a domain name by the pattern.
  564. *
  565. * @param domain the domain to check for
  566. * @param pattern the pattern for the check.
  567. * @return if return {@code true} that the domain is valid, otherwise no.
  568. */
  569. private boolean isValidDomainAddress(String domain, java.util.regex.Pattern pattern) {
  570. // if we have a trailing dot the domain part we have an invalid email address.
  571. // the regular expression match would take care of this, but IDN.toASCII drops the trailing '.'
  572. if (domain.endsWith(".")) {
  573. return false;
  574. }
  575. Matcher matcher = pattern.matcher(domain);
  576. if (!matcher.matches()) {
  577. return false;
  578. }
  579. String asciiString;
  580. try {
  581. asciiString = IDN.toASCII(domain);
  582. } catch (IllegalArgumentException e) {
  583. setIllegalArg("Failed to convert domain string to ASCII code. Exception: %s", e.getMessage());
  584. return false;
  585. }
  586. if (isNull(asciiString) || asciiString.length() > MAX_DOMAIN_PART_LENGTH) {
  587. return false;
  588. }
  589. return true;
  590. }
  591. /**
  592. * verify whether the value isn't {@code null}.
  593. *
  594. * @param value any object value.
  595. * @param annotation the {@link NotNull} annotation get from the field.
  596. * @return if return {@code true} that the value isn't null, otherwise no.
  597. */
  598. private boolean verify(Object value, NotNull annotation) {
  599. if (isNull(annotation)) {
  600. return true;
  601. }
  602. setVerify(annotation.message());
  603. return notNull(value);
  604. }
  605. /**
  606. * verify whether the value is {@code null}.
  607. *
  608. * @param value any object value.
  609. * @param annotation the {@link Null} annotation get from the field.
  610. * @return if return {@code true} that the value is null, otherwise no.
  611. */
  612. private boolean verify(Object value, Null annotation) {
  613. if (isNull(annotation)) {
  614. return true;
  615. }
  616. setVerify(annotation.message());
  617. return isNull(value);
  618. }
  619. /**
  620. * verify whether the value is {@code true}.
  621. *
  622. * @param value any object value, but only work for {@link Boolean}
  623. * @param annotation the {@link AssertTrue} annotation get from the field.
  624. * @return if return {@code true} that the value is true, otherwise no.
  625. */
  626. private boolean verify(Object value, AssertTrue annotation) {
  627. if (isNull(annotation)) {
  628. return true;
  629. }
  630. setVerify(annotation.message());
  631. if (isNull(value)) {
  632. return false;
  633. }
  634. if (!isBoolean(value)) {
  635. setIllegalArg("The @AssertTrue only supports Boolean.");
  636. return false;
  637. }
  638. return ((Boolean) value);
  639. }
  640. /**
  641. * verify whether the value is {@code false}.
  642. *
  643. * @param value any object value, but only work for {@link Boolean}
  644. * @param annotation the {@link AssertFalse} annotation get from the field.
  645. * @return if return {@code true} that the value is false, otherwise no.
  646. */
  647. private boolean verify(Object value, AssertFalse annotation) {
  648. if (isNull(annotation)) {
  649. return true;
  650. }
  651. setVerify(annotation.message());
  652. if (isNull(value)) {
  653. return false;
  654. }
  655. if (!isBoolean(value)) {
  656. setIllegalArg("The @AssertFalse only supports Boolean.");
  657. return false;
  658. }
  659. return !((Boolean) value);
  660. }
  661. /**
  662. * verify whether the value is less than the bigdecimal maximum value.
  663. *
  664. * @param value any object value, but only work for {@link Number} and {@link CharSequence}
  665. * @param annotation the {@link DecimalMax} annotation get from the field.
  666. * @return if return {@code true} that the value is less than the bigdecimal maximum, otherwise no.
  667. */
  668. private boolean verify(Object value, DecimalMax annotation) {
  669. if (isNull(annotation)) {
  670. return true;
  671. }
  672. setVerify(annotation.message());
  673. if (isNull(value)) {
  674. return false;
  675. }
  676. final boolean isNumber = isNumber(value);
  677. final boolean isCharSequence = isCharSequence(value);
  678. if (!isNumber && !isCharSequence) {
  679. setIllegalArg("The @DecimalMax only supports Number & CharSequence.");
  680. return false;
  681. }
  682. final String maxValue = annotation.value();
  683. if (isNull(maxValue)) {
  684. setIllegalArg("The value of @DecimalMax is null, a invalid BigDecimal format.");
  685. return false;
  686. }
  687. final BigDecimal max = newBigDecimal(maxValue);
  688. if (isNull(max)) {
  689. return false;
  690. }
  691. final BigDecimal val;
  692. if (isNumber) {
  693. val = newBigDecimal((Number) value);
  694. } else {
  695. val = newBigDecimal((CharSequence) value);
  696. }
  697. if (isNull(val)) {
  698. return false;
  699. }
  700. final int compare = decimalComparator(value, val, max, GREATER_THAN);
  701. final boolean inclusive = annotation.inclusive();
  702. //inclusive ? comparisonResult <= 0 : comparisonResult < 0;
  703. if (inclusive) {
  704. return lte0(compare);
  705. } else {
  706. return lt0(compare);
  707. }
  708. }
  709. /**
  710. * verify whether the value is greater than the bigdecimal minimum value.
  711. *
  712. * @param value any object value, but only work for {@link Number} and {@link CharSequence}
  713. * @param annotation the {@link DecimalMin} annotation get from the field.
  714. * @return if return {@code true} that the value is greater than the bigdecimal minimum, otherwise no.
  715. */
  716. private boolean verify(Object value, DecimalMin annotation) {
  717. if (isNull(annotation)) {
  718. return true;
  719. }
  720. setVerify(annotation.message());
  721. if (isNull(value)) {
  722. return false;
  723. }
  724. final boolean isNumber = isNumber(value);
  725. final boolean isCharSequence = isCharSequence(value);
  726. if (!isNumber && !isCharSequence) {
  727. setIllegalArg("The @DecimalMin only supports Number & CharSequence.");
  728. return false;
  729. }
  730. final String minValue = annotation.value();
  731. if (isNull(minValue)) {
  732. setIllegalArg("The value of @DecimalMin is null, a invalid BigDecimal format.");
  733. return false;
  734. }
  735. final BigDecimal min = newBigDecimal(minValue);
  736. if (isNull(min)) {
  737. return false;
  738. }
  739. final BigDecimal val;
  740. if (isNumber) {
  741. val = newBigDecimal((Number) value);
  742. } else {
  743. val = newBigDecimal((CharSequence) value);
  744. }
  745. if (isNull(val)) {
  746. return false;
  747. }
  748. final int compare = decimalComparator(value, val, min, LESS_THAN);
  749. final boolean inclusive = annotation.inclusive();
  750. //inclusive ? comparisonResult >= 0 : comparisonResult > 0;
  751. if (inclusive) {
  752. return gte0(compare);
  753. } else {
  754. return gt0(compare);
  755. }
  756. }
  757. /**
  758. * compare two bigdecimal value
  759. *
  760. * @param value any object value
  761. * @param val the bigdecimal value transform from the object value
  762. * @param bounds the boundary of the maximum or minimum
  763. * @param treatNanAs return for the value isn't number when check for infinity
  764. * @return return {@code -1} for the val is less than boundary, {@code 0} for equal,{@code 1} for greater than boundary.
  765. */
  766. private int decimalComparator(Object value, BigDecimal val, BigDecimal bounds, OptionalInt treatNanAs) {
  767. int compare;
  768. if (isLong(value) || isBigInteger(value) || isBigDecimal(value)) {
  769. compare = val.compareTo(bounds);
  770. } else if (isDouble(value)) {
  771. Double v = (Double) value;
  772. OptionalInt infinity = infinityCheck(v, treatNanAs);
  773. if (infinity.isPresent()) {
  774. compare = infinity.getAsInt();
  775. } else {
  776. compare = val.compareTo(bounds);
  777. }
  778. } else if (isFloat(value)) {
  779. Float v = (Float) value;
  780. OptionalInt infinity = infinityCheck(v, treatNanAs);
  781. if (infinity.isPresent()) {
  782. compare = infinity.getAsInt();
  783. } else {
  784. compare = val.compareTo(bounds);
  785. }
  786. } else {
  787. compare = val.compareTo(bounds);
  788. }
  789. return compare;
  790. }
  791. /**
  792. * verify whether the value isn't null and not empty.
  793. *
  794. * @param value any object value,but only work for {@link CharSequence},{@link Collection},{@link Map},{@link Array},{@link Iterator}and {@link Enumeration}
  795. * @param annotation the {@link NotEmpty} annotation get from the field.
  796. * @return if return {@code true} that the value isn't empty(with some length or size), otherwise no.
  797. */
  798. private boolean verify(Object value, NotEmpty annotation) {
  799. if (isNull(annotation)) {
  800. return true;
  801. }
  802. setVerify(annotation.message());
  803. if (isNull(value)) {
  804. return false;
  805. }
  806. final int length = length(value);
  807. if(-1 == length){
  808. setIllegalArg("The @NotEmpty only supports CharSequence & Collection & Map & Array & Iterator & Enumeration.");
  809. return false;
  810. }
  811. // length > 0
  812. return gt0(length);
  813. }
  814. /**
  815. * get the length or size of the value.
  816. *
  817. * @param value any object value
  818. * @return the length or size of the value when it's working, but return zero when the value is null.
  819. */
  820. private int length(Object value) {
  821. if (isNull(value)) {
  822. return 0;
  823. }
  824. if (isCharSequence(value)) {
  825. return ((CharSequence) value).length();
  826. }
  827. if (isCollection(value)) {
  828. return ((Collection<?>) value).size();
  829. }
  830. if (isMap(value)) {
  831. return ((Map<?, ?>) value).size();
  832. }
  833. if (isArray(value)) {
  834. return Array.getLength(value);
  835. }
  836. int count;
  837. if (isIterator(value)) {
  838. Iterator<?> iter = (Iterator<?>) value;
  839. count = 0;
  840. while (iter.hasNext()) {
  841. count++;
  842. iter.next();
  843. }
  844. return count;
  845. }
  846. if (isEnumeration(value)) {
  847. Enumeration<?> enumeration = (Enumeration<?>) value;
  848. count = 0;
  849. while (enumeration.hasMoreElements()) {
  850. count++;
  851. enumeration.nextElement();
  852. }
  853. return count;
  854. }
  855. return -1;
  856. }
  857. /**
  858. * verify whether the value satisfy the size with maximum and minimun.
  859. *
  860. * @param value any object value, but only work for {@link CharSequence},{@link Collection},{@link Map},{@link Array},{@link Iterator}and {@link Enumeration}
  861. * @param annotation the {@link Size} annotation get from the field.
  862. * @return if return {@code true} that the value satisfy the size, otherwise no.
  863. */
  864. private boolean verify(Object value, Size annotation) {
  865. if (isNull(annotation)) {
  866. return true;
  867. }
  868. final int min = annotation.min();
  869. final int max = annotation.max();
  870. String message = annotation.message();
  871. if (notBlank(message)) {
  872. if(message.contains("{min}")){
  873. message = message.replace("{min}", String.valueOf(min));
  874. }
  875. if(message.contains("{max}")){
  876. message = message.replace("{max}", String.valueOf(max));
  877. }
  878. }
  879. setVerify(message);
  880. if (isNull(value)) {
  881. return false;
  882. }
  883. if (lt0(min)) {
  884. setIllegalArg("The min (%s) parameter of @Size cannot be negative.", min);
  885. return false;
  886. }
  887. if (lt0(max)) {
  888. setIllegalArg("The max (%s) parameter of @Size cannot be negative.", max);
  889. return false;
  890. }
  891. if (min > max) {
  892. setIllegalArg("The min (%s) and max (%s) length of @Size cannot be negative.", min, max);
  893. return false;
  894. }
  895. final int length = length(value);
  896. if(-1 == length){
  897. setIllegalArg("The @Size only supports CharSequence & Collection & Map & Array & Iterator & Enumeration.");
  898. return false;
  899. }
  900. //size >= min && size <= max
  901. return length >= min && length <= max;
  902. }
  903. /**
  904. * verify that the {@code Number} being validated matches the pattern
  905. *
  906. * @param value any object value, but only work for {@link CharSequence} and {@link Number}
  907. * @param annotation the {@link Digits} annotation get from the field.
  908. * @return if return {@code true} that the value is matches pattern, otherwise no.
  909. */
  910. private boolean verify(Object value, Digits annotation) {
  911. if (isNull(annotation)) {
  912. return true;
  913. }
  914. final int maxInteger = annotation.integer();
  915. final int maxFraction = annotation.fraction();
  916. String message = annotation.message();
  917. if (notBlank(message)) {
  918. if(message.contains("{integer}")){
  919. message = message.replace("{integer}", String.valueOf(maxInteger));
  920. }
  921. if(message.contains("{fraction}")){
  922. message = message.replace("{fraction}", String.valueOf(maxFraction));
  923. }
  924. }
  925. setVerify(message);
  926. if (isNull(value)) {
  927. return false;
  928. }
  929. final boolean isNumber = isNumber(value);
  930. final boolean isCharSequence = isCharSequence(value);
  931. if (!isNumber && !isCharSequence) {
  932. setIllegalArg("The @Digits only supports Number & CharSequence.");
  933. return false;
  934. }
  935. if (lt0(maxInteger)) {
  936. setIllegalArg("The length of the integer '%s' part cannot be negative.", maxInteger);
  937. return false;
  938. }
  939. if (lt0(maxFraction)) {
  940. setIllegalArg("The length of the fraction '%s' part cannot be negative.", maxFraction);
  941. return false;
  942. }
  943. BigDecimal val;
  944. if (isNumber) {
  945. if (isBigDecimal(value)) {
  946. val = (BigDecimal) value;
  947. } else {
  948. val = newBigDecimal(value.toString());
  949. if (isNull(val)) {
  950. return false;
  951. }
  952. val = val.stripTrailingZeros();
  953. }
  954. } else {
  955. val = newBigDecimal((CharSequence) value);
  956. }
  957. if (isNull(val)) {
  958. return false;
  959. }
  960. int integerPart = val.precision() - val.scale();
  961. int fractionPart = val.scale() < 0 ? 0 : val.scale();
  962. //maxInteger >= integerPart && maxFraction >= fractionPart
  963. return maxInteger >= integerPart && maxFraction >= fractionPart;
  964. }
  965. /**
  966. * verify that a character sequence is not {@code null} nor empty after removing any leading or trailing whitespace.
  967. *
  968. * @param value any object value, but only work for {@link CharSequence}
  969. * @param annotation the {@link NotBlank} annotation get from the field.
  970. * @return if return {@code true} that the value isn't blank, otherwise no.
  971. */
  972. private boolean verify(Object value, NotBlank annotation) {
  973. if (isNull(annotation)) {
  974. return true;
  975. }
  976. setVerify(annotation.message());
  977. if (isNull(value)) {
  978. return false;
  979. }
  980. if (!isCharSequence(value)) {
  981. setIllegalArg("The @NotBlank only supports CharSequence.");
  982. return false;
  983. }
  984. return ((CharSequence) value).toString().trim().isEmpty();
  985. }
  986. /**
  987. * verify whether the value is a valid email address.
  988. *
  989. * @param value any object value, but only work for {@link CharSequence}
  990. * @param annotation the {@link Email} annotation get from the field.
  991. * @return if return {@code true} that the value is a valid email address, otherwise no.
  992. */
  993. private boolean verify(Object value, Email annotation) {
  994. if (isNull(annotation)) {
  995. return true;
  996. }
  997. setVerify(annotation.message());
  998. if (isNull(value)) {
  999. return false;
  1000. }
  1001. if (!isCharSequence(value)) {
  1002. setIllegalArg("The @Email only supports CharSequence.");
  1003. return false;
  1004. }
  1005. //@Email need trim
  1006. final String val = ((CharSequence) value).toString().trim();
  1007. if (val.isEmpty()) {
  1008. return false;
  1009. }
  1010. // cannot split email string at @ as it can be a part of quoted local part of email.
  1011. // so we need to split at a position of last @ present in the string:
  1012. int splitPosition = val.lastIndexOf('@');
  1013. if (lt0(splitPosition)) {
  1014. return false;
  1015. }
  1016. String localPart = val.substring(0, splitPosition);
  1017. String domainPart = val.substring(splitPosition + 1);
  1018. if (localPart.length() > MAX_LOCAL_PART_LENGTH) {
  1019. return false;
  1020. }
  1021. if (!LOCAL_PART_PATTERN.matcher(localPart).matches()) {
  1022. return false;
  1023. }
  1024. if (!isValidEmailDomainAddress(domainPart)) {
  1025. return false;
  1026. }
  1027. final Pattern.Flag[] flags = annotation.flags();
  1028. final String regexp = annotation.regexp();
  1029. int intFlag = 0;
  1030. for (Pattern.Flag flag : flags) {
  1031. intFlag = intFlag | flag.getValue();
  1032. }
  1033. java.util.regex.Pattern pattern = null;
  1034. // we only apply the regexp if there is one to apply
  1035. if (!".*".equals(regexp) || flags.length > 0) {
  1036. try {
  1037. pattern = java.util.regex.Pattern.compile(regexp, intFlag);
  1038. } catch (PatternSyntaxException e) {
  1039. setIllegalArg("Failed to Compile the regexp and flag for @Email. Exception: %s", e.getMessage());
  1040. return false;
  1041. }
  1042. }
  1043. if (isNull(pattern)) {
  1044. setIllegalArg("The regexp for @Email is Invalid regular expression.");
  1045. return false;
  1046. }
  1047. return pattern.matcher(val).matches();
  1048. }
  1049. /**
  1050. * verify whether the value is matches the pattern.
  1051. *
  1052. * @param value any object value, but only work for {@link CharSequence}
  1053. * @param annotation the {@link Pattern} annotation get from the field.
  1054. * @return if return {@code true} that the value is mathches the pattern, otherwise no.
  1055. */
  1056. private boolean verify(Object value, Pattern annotation) {
  1057. if (isNull(annotation)) {
  1058. return true;
  1059. }
  1060. setVerify(annotation.message());
  1061. if (isNull(value)) {
  1062. return false;
  1063. }
  1064. if (!isCharSequence(value)) {
  1065. setIllegalArg("The @Pattern only supports CharSequence.");
  1066. return false;
  1067. }
  1068. //@Pattern no trim
  1069. final String val = ((CharSequence) value).toString();
  1070. if (val.isEmpty()) {
  1071. return false;
  1072. }
  1073. final Pattern.Flag[] flags = annotation.flags();
  1074. final String regexp = annotation.regexp();
  1075. int intFlag = 0;
  1076. for (Pattern.Flag flag : flags) {
  1077. intFlag = intFlag | flag.getValue();
  1078. }
  1079. java.util.regex.Pattern pattern;
  1080. try {
  1081. pattern = java.util.regex.Pattern.compile(regexp, intFlag);
  1082. } catch (PatternSyntaxException e) {
  1083. setIllegalArg("Failed to Compile the regexp and flag for @Pattern. Exception: %s", e.getMessage());
  1084. return false;
  1085. }
  1086. if (isNull(pattern)) {
  1087. setIllegalArg("The regexp for @Pattern is Invalid regular expression.");
  1088. return false;
  1089. }
  1090. return pattern.matcher(val).matches();
  1091. }
  1092. /**
  1093. * verify whether the value less then or equal the maximum value.
  1094. *
  1095. * @param value any object value, but only work for {@link CharSequence} and {@link Number}
  1096. * @param annotation the {@link Max} annotation get from the field.
  1097. * @return if return {@code ture} that the value is less than or equal the maximum, otherwise no.
  1098. */
  1099. private boolean verify(Object value, Max annotation) {
  1100. if (isNull(annotation)) {
  1101. return true;
  1102. }
  1103. setVerify(annotation.message());
  1104. if (isNull(value)) {
  1105. return false;
  1106. }
  1107. final boolean isNumber = isNumber(value);
  1108. final boolean isCharSequence = isCharSequence(value);
  1109. if (!isCharSequence && !isNumber) {
  1110. setIllegalArg("The @Max only supports Number & CharSequence.");
  1111. return false;
  1112. }
  1113. final long max = annotation.value();
  1114. final int compare;
  1115. if (isNumber) {
  1116. compare = numberComparator((Number) value, max, GREATER_THAN);
  1117. } else {
  1118. String v = ((CharSequence) value).toString().trim();
  1119. if (v.isEmpty()) {
  1120. return false;
  1121. }
  1122. BigDecimal val = newBigDecimal(v);
  1123. if (isNull(val)) {
  1124. return false;
  1125. }
  1126. compare = val.compareTo(BigDecimal.valueOf(max));
  1127. }
  1128. //compare <= 0
  1129. return lte0(compare);
  1130. }
  1131. /**
  1132. * verify whether the value greater then or equal the minimum value.
  1133. *
  1134. * @param value any object value, but only work for {@link CharSequence} and {@link Number}
  1135. * @param annotation the {@link Max} annotation get from the field.
  1136. * @return if return {@code ture} that the value is less than or equal the minimum, otherwise no.
  1137. */
  1138. private boolean verify(Object value, Min annotation) {
  1139. if (isNull(annotation)) {
  1140. return true;
  1141. }
  1142. setVerify(annotation.message());
  1143. if (isNull(value)) {
  1144. return false;
  1145. }
  1146. final boolean isNumber = isNumber(value);
  1147. final boolean isCharSequence = isCharSequence(value);
  1148. if (!isCharSequence && !isNumber) {
  1149. setIllegalArg("The @Min only supports Number & CharSequence.");
  1150. return false;
  1151. }
  1152. final long min = annotation.value();
  1153. final int compare;
  1154. if (isNumber) {
  1155. compare = numberComparator((Number) value, min, LESS_THAN);
  1156. } else {
  1157. String v = ((CharSequence) value).toString().trim();
  1158. if (v.isEmpty()) {
  1159. return false;
  1160. }
  1161. BigDecimal val = newBigDecimal(v);
  1162. if (isNull(val)) {
  1163. return false;
  1164. }
  1165. compare = val.compareTo(BigDecimal.valueOf(min));
  1166. }
  1167. //compare >= 0
  1168. return gte0(compare);
  1169. }
  1170. /**
  1171. * compare two number value with less than or greater than.
  1172. *
  1173. * @param value any number value
  1174. * @param bounds the other value
  1175. * @param treatNanAs return when the value is {@code NaN}.
  1176. * @return {@code -1} value less than limit, {@code 0} value equal limit, {@code 1} value greater than limit.
  1177. */
  1178. private int numberComparator(Number value, long bounds, OptionalInt treatNanAs) {
  1179. int compare;
  1180. if (isLong(value)) {
  1181. compare = ((Long) value).compareTo(bounds);
  1182. } else if (isDouble(value)) {
  1183. Double val = (Double) value;
  1184. OptionalInt infinity = infinityCheck(val, treatNanAs);
  1185. if (infinity.isPresent()) {
  1186. compare = infinity.getAsInt();
  1187. } else {
  1188. compare = Double.compare(val, bounds);
  1189. }
  1190. } else if (isFloat(value)) {
  1191. Float val = (Float) value;
  1192. OptionalInt infinity = infinityCheck(val, treatNanAs);
  1193. if (infinity.isPresent()) {
  1194. compare = infinity.getAsInt();
  1195. } else {
  1196. compare = Float.compare(val, bounds);
  1197. }
  1198. } else if (isBigDecimal(value)) {
  1199. compare = ((BigDecimal) value).compareTo(BigDecimal.valueOf(bounds));
  1200. } else if (isBigInteger(value)) {
  1201. compare = ((BigInteger) value).compareTo(BigInteger.valueOf(bounds));
  1202. } else {
  1203. compare = Long.compare(value.longValue(), bounds);
  1204. }
  1205. return compare;
  1206. }
  1207. /**
  1208. * get the sign number of the value.
  1209. *
  1210. * @param value any number value
  1211. * @param treatNanAs return when the value equal infinity.
  1212. * @return {@code -1} the value less than zero, {@code 0} the value equal zero, {@code 1} the value greater than zero.
  1213. */
  1214. private int signum(Number value, OptionalInt treatNanAs) {
  1215. int signum;
  1216. if (isLong(value)) {
  1217. signum = Long.signum((Long) value);
  1218. } else if (isInteger(value)) {
  1219. signum = Integer.signum((Integer) value);
  1220. } else if (isBigDecimal(value)) {
  1221. signum = ((BigDecimal) value).signum();
  1222. } else if (isBigInteger(value)) {
  1223. signum = ((BigInteger) value).signum();
  1224. } else if (isDouble(value)) {
  1225. Double val = (Double) value;
  1226. OptionalInt infinity = infinityCheck(val, treatNanAs);
  1227. if (infinity.isPresent()) {
  1228. signum = infinity.getAsInt();
  1229. } else {
  1230. signum = val.compareTo(0D);
  1231. }
  1232. } else if (isFloat(value)) {
  1233. Float val = (Float) value;
  1234. OptionalInt infinity = infinityCheck(val, treatNanAs);
  1235. if (infinity.isPresent()) {
  1236. signum = infinity.getAsInt();
  1237. } else {
  1238. signum = val.compareTo(0F);
  1239. }
  1240. } else if (isByte(value)) {
  1241. signum = ((Byte) value).compareTo(BYTE_ZERO);
  1242. } else if (isShort(value)) {
  1243. signum = ((Short) value).compareTo(SHORT_ZERO);
  1244. } else {
  1245. signum = Double.compare(value.doubleValue(), 0D);
  1246. }
  1247. return signum;
  1248. }
  1249. /**
  1250. * verify whether the value
  1251. *
  1252. * @param value any object value, but only work for {@link Number}
  1253. * @param annotation the {@link Negative} annotation get from the field.
  1254. * @return
  1255. */
  1256. private boolean verify(Object value, Negative annotation) {
  1257. if (isNull(annotation)) {
  1258. return true;
  1259. }
  1260. setVerify(annotation.message());
  1261. if (isNull(value)) {
  1262. return false;
  1263. }
  1264. if (!isNumber(value)) {
  1265. setIllegalArg("The @Negative only supports Number.");
  1266. return false;
  1267. }
  1268. return lt0(signum((Number) value, GREATER_THAN));
  1269. }
  1270. /**
  1271. * @param value any object value, but only work for {@link Number}
  1272. * @param annotation the {@link NegativeOrZero} annotation get from the field.
  1273. * @return
  1274. */
  1275. private boolean verify(Object value, NegativeOrZero annotation) {
  1276. if (isNull(annotation)) {
  1277. return true;
  1278. }
  1279. setVerify(annotation.message());
  1280. if (isNull(value)) {
  1281. return false;
  1282. }
  1283. if (!isNumber(value)) {
  1284. setIllegalArg("The @NegativeOrZero only supports Number.");
  1285. return false;
  1286. }
  1287. return lte0(signum((Number) value, GREATER_THAN));
  1288. }
  1289. /**
  1290. * @param value any object value, but only work for {@link Number}
  1291. * @param annotation the {@link Positive} annotation get from the field.
  1292. * @return
  1293. */
  1294. private boolean verify(Object value, Positive annotation) {
  1295. if (isNull(annotation)) {
  1296. return true;
  1297. }
  1298. setVerify(annotation.message());
  1299. if (isNull(value)) {
  1300. return false;
  1301. }
  1302. if (!isNumber(value)) {
  1303. setIllegalArg("The @Positive only supports Number.");
  1304. return false;
  1305. }
  1306. return gt0(signum((Number) value, LESS_THAN));
  1307. }
  1308. /**
  1309. * @param value any object value, but only work for {@link Number}
  1310. * @param annotation the {@link PositiveOrZero} annotation get from the field.
  1311. * @return
  1312. */
  1313. private boolean verify(Object value, PositiveOrZero annotation) {
  1314. if (isNull(annotation)) {
  1315. return true;
  1316. }
  1317. setVerify(annotation.message());
  1318. if (isNull(value)) {
  1319. return false;
  1320. }
  1321. if (!isNumber(value)) {
  1322. setIllegalArg("The @PositiveOrZero only supports Number.");
  1323. return false;
  1324. }
  1325. return gte0(signum((Number) value, LESS_THAN));
  1326. }
  1327. /**
  1328. * compare the TemporalAccessor value with now , verify whether the value is before or after now.
  1329. *
  1330. * @param value any object value, but only work for {@link TemporalAccessor}, {@link Date} and {@link Calendar}
  1331. * @param isTemporalAccessor whether the value is instance of {@link TemporalAccessor}
  1332. * @param isDate whether the value is instance of {@link Date}
  1333. * @return {@code -1} the value is before now, {@code 0} is now, {@code 1} after now. otherwise {@code -2} not support the type.
  1334. */
  1335. private int dateComparator(Object value, boolean isTemporalAccessor, boolean isDate) {
  1336. Clock clock = systemDefaultClock();
  1337. int compare;
  1338. if (isTemporalAccessor) {
  1339. if (value instanceof Instant) {
  1340. compare = ((Instant) value).compareTo(Instant.now(clock));
  1341. } else if (value instanceof LocalDateTime) {
  1342. compare = ((LocalDateTime) value).compareTo(LocalDateTime.now(clock));
  1343. } else if (value instanceof LocalDate) {
  1344. compare = ((LocalDate) value).compareTo(LocalDate.now(clock));
  1345. } else if (value instanceof LocalTime) {
  1346. compare = ((LocalTime) value).compareTo(LocalTime.now(clock));
  1347. } else if (value instanceof MonthDay) {
  1348. compare = ((MonthDay) value).compareTo(MonthDay.now(clock));
  1349. } else if (value instanceof HijrahDate) {
  1350. compare = ((HijrahDate) value).compareTo(HijrahDate.now(clock));
  1351. } else if (value instanceof JapaneseDate) {
  1352. compare = ((JapaneseDate) value).compareTo(JapaneseDate.now(clock));
  1353. } else if (value instanceof MinguoDate) {
  1354. compare = ((MinguoDate) value).compareTo(MinguoDate.now(clock));
  1355. } else if (value instanceof OffsetDateTime) {
  1356. compare = ((OffsetDateTime) value).compareTo(OffsetDateTime.now(clock));
  1357. } else if (value instanceof OffsetTime) {
  1358. compare = ((OffsetTime) value).compareTo(OffsetTime.now(clock));
  1359. } else if (value instanceof ThaiBuddhistDate) {
  1360. compare = ((ThaiBuddhistDate) value).compareTo(ThaiBuddhistDate.now(clock));
  1361. } else if (value instanceof Year) {
  1362. compare = ((Year) value).compareTo(Year.now(clock));
  1363. } else if (value instanceof YearMonth) {
  1364. compare = ((YearMonth) value).compareTo(YearMonth.now(clock));
  1365. } else if (value instanceof ZonedDateTime) {
  1366. compare = ((ZonedDateTime) value).compareTo(ZonedDateTime.now(clock));
  1367. }else{
  1368. setIllegalArg("The '%s' is not a supported TemporalAccessor class temporarily.", value.getClass().getCanonicalName());
  1369. compare = Integer.MAX_VALUE;
  1370. }
  1371. } else if (isDate) {
  1372. Date val = (Date) value;
  1373. compare = val.toInstant().compareTo(Instant.now(clock));
  1374. } else {
  1375. Calendar val = (Calendar) value;
  1376. compare = val.toInstant().compareTo(Instant.now(clock));
  1377. }
  1378. return compare;
  1379. }
  1380. /**
  1381. * verify whether the value is a future time.
  1382. *
  1383. * @param value any object value, but only work for {@link TemporalAccessor}, {@link Date} and {@link Calendar}
  1384. * @param annotation the {@link Future} annotation get from the field.
  1385. * @return if return {@code true} that the value is a future time, otherwise no.
  1386. */
  1387. private boolean verify(Object value, Future annotation) {
  1388. if (isNull(annotation)) {
  1389. return true;
  1390. }
  1391. setVerify(annotation.message());
  1392. if (isNull(value)) {
  1393. return false;
  1394. }
  1395. final boolean isTemporalAccessor = isTemporalAccessor(value);
  1396. final boolean isDate = isDate(value);
  1397. final boolean isCalendar = isCalendar(value);
  1398. if (!isTemporalAccessor && !isDate && !isCalendar) {
  1399. setIllegalArg("The @Future only supports TemporalAccessor & Calendar & Date.");
  1400. return false;
  1401. }
  1402. final int compare = dateComparator(value, isTemporalAccessor, isDate);
  1403. if (Integer.MAX_VALUE == compare) {
  1404. return false;
  1405. }
  1406. //compare > 0
  1407. return gt0(compare);
  1408. }
  1409. /**
  1410. * verify whether the value is a future time or present.
  1411. *
  1412. * @param value any object value, but only work for {@link TemporalAccessor}, {@link Date} and {@link Calendar}
  1413. * @param annotation the {@link FutureOrPresent} annotation get from the field.
  1414. * @return if return {@code true} that the value is a future time or present, otherwise no.
  1415. */
  1416. private boolean verify(Object value, FutureOrPresent annotation) {
  1417. if (isNull(annotation)) {
  1418. return true;
  1419. }
  1420. setVerify(annotation.message());
  1421. if (isNull(value)) {
  1422. return false;
  1423. }
  1424. final boolean isTemporalAccessor = isTemporalAccessor(value);
  1425. final boolean isDate = isDate(value);
  1426. final boolean isCalendar = isCalendar(value);
  1427. if (!isTemporalAccessor && !isDate && !isCalendar) {
  1428. setIllegalArg("The @FutureOrPresent only supports TemporalAccessor & Calendar & Date.");
  1429. return false;
  1430. }
  1431. final int compare = dateComparator(value, isTemporalAccessor, isDate);
  1432. if (Integer.MAX_VALUE == compare) {
  1433. return false;
  1434. }
  1435. //compare >= 0
  1436. return gte0(compare);
  1437. }
  1438. /**
  1439. * verify whether the value is a past time.
  1440. *
  1441. * @param value any object value, but only work for {@link TemporalAccessor}, {@link Date} and {@link Calendar}
  1442. * @param annotation the {@link Past} annotation get from the field.
  1443. * @return if return {@code true} that the value is a past time, otherwise no.
  1444. */
  1445. private boolean verify(Object value, Past annotation) {
  1446. if (isNull(annotation)) {
  1447. return true;
  1448. }
  1449. setVerify(annotation.message());
  1450. if (isNull(value)) {
  1451. return false;
  1452. }
  1453. final boolean isTemporalAccessor = isTemporalAccessor(value);
  1454. final boolean isDate = isDate(value);
  1455. final boolean isCalendar = isCalendar(value);
  1456. if (!isTemporalAccessor && !isDate && !isCalendar) {
  1457. setIllegalArg("The @Past only supports TemporalAccessor & Calendar & Date.");
  1458. return false;
  1459. }
  1460. final int compare = dateComparator(value, isTemporalAccessor, isDate);
  1461. if (Integer.MAX_VALUE == compare) {
  1462. return false;
  1463. }
  1464. //compare < 0
  1465. return lt0(compare);
  1466. }
  1467. /**
  1468. * verify whether the value is a future time or present.
  1469. *
  1470. * @param value any object value, but only work for {@link TemporalAccessor}, {@link Date} and {@link Calendar}
  1471. * @param annotation the {@link Future} annotation get from the field.
  1472. * @return if return {@code true} that the value is a past time or present, otherwise no.
  1473. */
  1474. private boolean verify(Object value, PastOrPresent annotation) {
  1475. if (isNull(annotation)) {
  1476. return true;
  1477. }
  1478. setVerify(annotation.message());
  1479. if (isNull(value)) {
  1480. return false;
  1481. }
  1482. final boolean isTemporalAccessor = isTemporalAccessor(value);
  1483. final boolean isDate = isDate(value);
  1484. final boolean isCalendar = isCalendar(value);
  1485. if (!isTemporalAccessor && !isDate && !isCalendar) {
  1486. setIllegalArg("The @PastOrPresent only supports TemporalAccessor & Calendar & Date.");
  1487. return false;
  1488. }
  1489. final int compare = dateComparator(value, isTemporalAccessor, isDate);
  1490. if (Integer.MAX_VALUE == compare) {
  1491. return false;
  1492. }
  1493. //compare <= 0
  1494. return lte0(compare);
  1495. }
  1496. /**
  1497. * get field by the names
  1498. *
  1499. * @param object the target object
  1500. * @param fields the names of field
  1501. * @return list of field
  1502. */
  1503. private List<Field> fieldFilter(final Object object, final String... fields) {
  1504. Class<?> objectClass = object.getClass();
  1505. // get fields, if has specify fields, otherwise get all fields on the object.
  1506. if (isEmpty(fields)) {
  1507. return Collections.unmodifiableList(Arrays.asList(objectClass.getDeclaredFields()));
  1508. }
  1509. List<Field> verifyFields = new ArrayList<>();
  1510. for (String field : fields) {
  1511. try {
  1512. Field verifyField = objectClass.getDeclaredField(field);
  1513. verifyFields.add(verifyField);
  1514. } catch (NoSuchFieldException e) {
  1515. throw newIllegalArgException("No such field '%s' in : %s.", field, objectClass.getCanonicalName());
  1516. }
  1517. }
  1518. //if vaild fields is empty
  1519. if (verifyFields.isEmpty()) {
  1520. throw newIllegalArgException("No field specify to verify.");
  1521. }
  1522. return Collections.unmodifiableList(verifyFields);
  1523. }
  1524. /**
  1525. * get all validation annotation on the field
  1526. *
  1527. * @param field the target field
  1528. * @return list of validation annotation
  1529. */
  1530. private List<Annotation> annotationFilter(final Field field) {
  1531. if (!field.isAccessible()) {
  1532. field.setAccessible(true);
  1533. }
  1534. //get all annotation of the field
  1535. Annotation[] annotations = field.getDeclaredAnnotations();
  1536. if (isEmpty(annotations)) {
  1537. return null;
  1538. }
  1539. //filter the validation annotation
  1540. List<Annotation> verifyAnnos = Arrays.stream(annotations).filter(anno -> verifyMethodMap.keySet().contains(anno.annotationType())).collect(Collectors.toList());
  1541. if (verifyAnnos.isEmpty()) {
  1542. return null;
  1543. }
  1544. return Collections.unmodifiableList(verifyAnnos);
  1545. }
  1546. /**
  1547. * get value of the field on the object
  1548. *
  1549. * @param object the target object
  1550. * @param field the field
  1551. * @return the value
  1552. */
  1553. private Object getFieldValue(final Object object, final Field field) {
  1554. final Object value;
  1555. try {
  1556. value = field.get(object);
  1557. } catch (IllegalAccessException e) {
  1558. throw newIllegalArgException("Illegal Access '%s' in : %s.", field.getName(), object.getClass().getCanonicalName());
  1559. }
  1560. return value;
  1561. }
  1562. private MethodHandle methodFilter(final MethodHandles.Lookup lookup, final String methodName, final MethodType methodType) {
  1563. final MethodHandle methodHandle;
  1564. try {
  1565. methodHandle = lookup.findVirtual(lookup.lookupClass(), methodName, methodType);
  1566. } catch (NoSuchMethodException | IllegalAccessException e) {
  1567. throw newIllegalArgException("No such method %s%s in VerifyProcessor.", methodName, methodType);
  1568. }
  1569. return methodHandle;
  1570. }
  1571. private boolean invokeMethod(final Object target, final MethodHandle methodHandle, final Object... args) {
  1572. final boolean invoke;
  1573. try {
  1574. invoke = (boolean) methodHandle.bindTo(target).invokeWithArguments(args);
  1575. } catch (Throwable throwable) {
  1576. throw newIllegalArgException("Invoke the target method [%s] failed.", methodHandle);
  1577. }
  1578. return invoke;
  1579. }
  1580. /**
  1581. * invoke the target method to verify.
  1582. */
  1583. private boolean invokeMethod(final Object target, final Method method, final Object value, final Annotation annotation) {
  1584. final boolean invoke;
  1585. try {
  1586. invoke = (boolean) method.invoke(target, value, annotation);
  1587. } catch (IllegalAccessException | InvocationTargetException e) {
  1588. throw newIllegalArgException("Invoke the target method [%s(Object,%s)] failed.", method.getName(), annotation.annotationType().getName());
  1589. }
  1590. return invoke;
  1591. }
  1592. /**
  1593. * verify the fields of the object base on the annotation present on it.
  1594. * if it is not valid that will throw a {@link VerifyException}.
  1595. * or a {@link IllegalArgumentException} when argument is illegal.
  1596. *
  1597. * @param object any object value. with some validation annotation in {@code javax.validation.constraints.*}
  1598. * @param fields the list of fields to verify. if no, that will verify all fields in the object.
  1599. */
  1600. private void action(final Object object, final String... fields) {
  1601. isNull(object, "The object to verify must not be null.");
  1602. List<Field> verifyFields = fieldFilter(object, fields);
  1603. for (Field verifyField : verifyFields) {
  1604. //filter the validation annotation
  1605. List<Annotation> verifyAnnos = annotationFilter(verifyField);
  1606. if (isEmpty(verifyAnnos)) continue;
  1607. //get value.
  1608. Object value = getFieldValue(object, verifyField);
  1609. for (Annotation verifyAnno : verifyAnnos) {
  1610. Class<? extends Annotation> annotationType = verifyAnno.annotationType();
  1611. //get method
  1612. Method method = verifyMethodMap.get(annotationType);
  1613. // result.
  1614. boolean invoke = invokeMethod(this, method, value, verifyAnno);
  1615. if (invoke) continue;
  1616. if (logWrite) {
  1617. info(verifyField, value, method, verifyAnno);
  1618. }
  1619. if(1 == code.get()){
  1620. throw newVerifyException(message.get());
  1621. }else{
  1622. throw newIllegalArgException(message.get());
  1623. }
  1624. }
  1625. }
  1626. }
  1627. /**
  1628. * the format of the {@code info} level log.
  1629. *
  1630. * @param validField the field to valid.
  1631. * @param value the value of the field.
  1632. * @param method the method to action
  1633. * @param verifyAnno the validation annotation of the field.
  1634. */
  1635. private void info(Field verifyField, Object value, Method method, Annotation verifyAnno) {
  1636. log.info("###| FIELD : {}", verifyField);
  1637. log.info("###| FIELD_VALUE : {}", value);
  1638. log.info("###| METHOD : (false) {}(Object,{})", method.getName(), verifyAnno.annotationType().getName());
  1639. }
  1640. /**
  1641. * create a {@link IllegalArgumentException} with the message.
  1642. *
  1643. * @param message the message of exception.
  1644. * @return a {@link IllegalArgumentException}
  1645. */
  1646. private IllegalArgumentException newIllegalArgException(String message, Object... values) {
  1647. return new IllegalArgumentException(format(message, values));
  1648. }
  1649. /**
  1650. * create a {@link VerifyException} with the message.
  1651. *
  1652. * @param message the message of exception.
  1653. * @return a {@link VerifyException}
  1654. */
  1655. private VerifyException newVerifyException(String message, Object... values) {
  1656. return new VerifyException(format(message, values));
  1657. }
  1658. /**
  1659. * format the message with some values
  1660. *
  1661. * @param message the message string.
  1662. * @param values some values.
  1663. * @return the message has format if necessary
  1664. */
  1665. private String format(String message, Object... values) {
  1666. if(message.contains("%s") && null != values && values.length > 0) {
  1667. return String.format(message, values);
  1668. }
  1669. return message;
  1670. }
  1671. /**
  1672. * verify whether the expression is {@code true}.
  1673. *
  1674. * @param expression any boolean expression.
  1675. * @param message if {@code true} that throw a {@link IllegalArgumentException} with message.
  1676. */
  1677. private void isNull(Object object, String message) {
  1678. if (null == object) {
  1679. throw newIllegalArgException(message);
  1680. }
  1681. }
  1682. private void isTrue(boolean expression, String message) {
  1683. if (expression) {
  1684. throw newIllegalArgException(message);
  1685. }
  1686. }
  1687. public <T> boolean isEmpty(T[] array) {
  1688. return array == null || array.length == 0;
  1689. }
  1690. public boolean isEmpty(Iterable<?> iterable) {
  1691. return null == iterable || isEmpty(iterable.iterator());
  1692. }
  1693. public boolean isEmpty(Iterator<?> Iterator) {
  1694. return null == Iterator || !Iterator.hasNext();
  1695. }
  1696. public boolean notBlank(String value) {
  1697. return null != value && !value.trim().isEmpty();
  1698. }
  1699. }