From 5d3828131408311d843e7ffcc15d1f742c97c1ac Mon Sep 17 00:00:00 2001 From: "ddg()" Date: Tue, 19 Sep 2023 11:39:39 +0200 Subject: [PATCH] Upgrade 'org.reflections:reflections' dependency to version 0.10.2 (#46) * Adapt bean validation links - move from javax to jakarta javaDocs * Bump Maven version to 3.9.4 * Bump 'org.reflections:reflections' dependency to version 0.10.2 - bump the version in the maven pom.xml - adapt valdr codebase in order to deal with the org.reflections api changes --------- Co-authored-by: Davide De --- README.md | 24 +++++++------- pom.xml | 4 +-- .../valdr-bean-validation-demo.iml | 7 ++-- .../java/com/github/valdr/AnnotatedClass.java | 33 +++++++++++-------- .../com/github/valdr/ClasspathScanner.java | 16 ++++----- .../com/github/valdr/ConstraintParser.java | 26 +++++++-------- .../valdr-bean-validation.iml | 7 ++-- 7 files changed, 59 insertions(+), 58 deletions(-) diff --git a/README.md b/README.md index 1db93e1..c6cf118 100644 --- a/README.md +++ b/README.md @@ -122,18 +122,18 @@ kind-of represents the "major" version. The [BuiltInConstraint.java](https://github.com/netceteragroup/valdr-bean-validation/blob/master/valdr-bean-validation/src/main/java/com/github/valdr/BuiltInConstraint.java) enum defines the mapping of Bean Validation constraints to valdr constraints. -| Bean Validation | valdr | Comment | -|-----------------|-------|---------| -| [NotNull](https://docs.jboss.org/hibernate/stable/beanvalidation/api/javax/validation/constraints/NotNull.html) | [required](https://github.com/netceteragroup/valdr#required) | | -| [Min](https://docs.jboss.org/hibernate/stable/beanvalidation/api/javax/validation/constraints/Min.html) | [min](https://github.com/netceteragroup/valdr#min--max) | | -| [Max](https://docs.jboss.org/hibernate/stable/beanvalidation/api/javax/validation/constraints/Max.html) | [max](https://github.com/netceteragroup/valdr#min--max) | | -| [Size](https://docs.jboss.org/hibernate/stable/beanvalidation/api/javax/validation/constraints/Size.html) | [size](https://github.com/netceteragroup/valdr#size) | | -| [Digits](https://docs.jboss.org/hibernate/stable/beanvalidation/api/javax/validation/constraints/Digits.html) | [digits](https://github.com/netceteragroup/valdr#digits) | | -| [Pattern](https://docs.jboss.org/hibernate/stable/beanvalidation/api/javax/validation/constraints/Pattern.html) | [pattern](https://github.com/netceteragroup/valdr#partern) | Java regex pattern is transformed to JavaScript pattern | -| [Future](https://docs.jboss.org/hibernate/stable/beanvalidation/api/javax/validation/constraints/Future.html) | [future](https://github.com/netceteragroup/valdr#future--past) | | -| [Past](https://docs.jboss.org/hibernate/stable/beanvalidation/api/javax/validation/constraints/Past.html) | [past](https://github.com/netceteragroup/valdr#future--past) | | -| [Email](https://docs.jboss.org/hibernate/stable/beanvalidation/api/javax/validation/constraints/Email.html) |[email](https://github.com/netceteragroup/valdr#email) | | -| [URL](https://docs.jboss.org/hibernate/validator/5.1/api/org/hibernate/validator/constraints/URL.html) |[url](https://github.com/netceteragroup/valdr#url) | proprietary Hibernate Validator (not in Bean Validation spec) | +| Bean Validation | valdr | Comment | +|-----------------------------------------------------------------------------------------------------------------|-------|---------| +| [NotNull](https://jakarta.ee/specifications/bean-validation/3.0/apidocs/jakarta/validation/constraints/notnull) | [required](https://github.com/netceteragroup/valdr#required) | | +| [Min](https://jakarta.ee/specifications/bean-validation/3.0/apidocs/jakarta/validation/constraints/min) | [min](https://github.com/netceteragroup/valdr#min--max) | | +| [Max](https://jakarta.ee/specifications/bean-validation/3.0/apidocs/jakarta/validation/constraints/max) | [max](https://github.com/netceteragroup/valdr#min--max) | | +| [Size](https://jakarta.ee/specifications/bean-validation/3.0/apidocs/jakarta/validation/constraints/size) | [size](https://github.com/netceteragroup/valdr#size) | | +| [Digits](https://jakarta.ee/specifications/bean-validation/3.0/apidocs/jakarta/validation/constraints/digits) | [digits](https://github.com/netceteragroup/valdr#digits) | | +| [Pattern](https://jakarta.ee/specifications/bean-validation/3.0/apidocs/jakarta/validation/constraints/pattern) | [pattern](https://github.com/netceteragroup/valdr#partern) | Java regex pattern is transformed to JavaScript pattern | +| [Future](https://jakarta.ee/specifications/bean-validation/3.0/apidocs/jakarta/validation/constraints/future) | [future](https://github.com/netceteragroup/valdr#future--past) | | +| [Past](https://jakarta.ee/specifications/bean-validation/3.0/apidocs/jakarta/validation/constraints/past) | [past](https://github.com/netceteragroup/valdr#future--past) | | +| [Email](https://jakarta.ee/specifications/bean-validation/3.0/apidocs/jakarta/validation/constraints/email) |[email](https://github.com/netceteragroup/valdr#email) | | +| [URL](https://docs.jboss.org/hibernate/stable/validator/api/org/hibernate/validator/constraints/URL.html) |[url](https://github.com/netceteragroup/valdr#url) | proprietary Hibernate Validator (not in Bean Validation spec) | ## Support diff --git a/pom.xml b/pom.xml index 6e657f2..add31b0 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ 2014 - 3.5.2 + 3.9.4 @@ -105,7 +105,7 @@ 5.10.0 1.18.28 5.5.0 - 0.9.9 + 0.10.2 6.0.0 2.0.9 6.0.11 diff --git a/valdr-bean-validation-demo/valdr-bean-validation-demo.iml b/valdr-bean-validation-demo/valdr-bean-validation-demo.iml index df0413e..de4cc12 100644 --- a/valdr-bean-validation-demo/valdr-bean-validation-demo.iml +++ b/valdr-bean-validation-demo/valdr-bean-validation-demo.iml @@ -24,9 +24,9 @@ - - - + + + @@ -34,7 +34,6 @@ - diff --git a/valdr-bean-validation/src/main/java/com/github/valdr/AnnotatedClass.java b/valdr-bean-validation/src/main/java/com/github/valdr/AnnotatedClass.java index fdd3f6a..a4e9244 100644 --- a/valdr-bean-validation/src/main/java/com/github/valdr/AnnotatedClass.java +++ b/valdr-bean-validation/src/main/java/com/github/valdr/AnnotatedClass.java @@ -1,7 +1,5 @@ package com.github.valdr; -import com.google.common.base.Predicate; -import com.google.common.base.Predicates; import com.google.common.collect.Lists; import org.reflections.ReflectionUtils; @@ -9,7 +7,9 @@ import java.lang.reflect.Field; import java.util.Collection; import java.util.List; +import java.util.Optional; import java.util.Set; +import java.util.function.Predicate; /** * Wrapper around a class with Bean Validation (and possibly other) annotations. Allows to extract validation rules @@ -27,7 +27,7 @@ public class AnnotatedClass { * AnnotatedClass#extractValidationRules()} is invoked */ AnnotatedClass(Class clazz, List excludedFields, Iterable> - relevantAnnotationClasses) { + relevantAnnotationClasses) { this.clazz = clazz; this.excludedFields = excludedFields; this.relevantAnnotationClasses = relevantAnnotationClasses; @@ -41,16 +41,20 @@ public class AnnotatedClass { */ ClassConstraints extractValidationRules() { final ClassConstraints classConstraints = new ClassConstraints(); - Set allFields = ReflectionUtils.getAllFields(clazz, buildAnnotationsPredicate()); - for (Field field : allFields) { - if (isNotExcluded(field)) { - FieldConstraints fieldValidationRules = new AnnotatedField(field, - relevantAnnotationClasses).extractValidationRules(); - if (fieldValidationRules.size() > 0) { - classConstraints.put(field.getName(), fieldValidationRules); + + buildAnnotationsPredicate().ifPresent(annotationsPredicate -> { + Set allFields = ReflectionUtils.getAllFields(clazz, annotationsPredicate); + for (Field field : allFields) { + if (isNotExcluded(field)) { + FieldConstraints fieldValidationRules = new AnnotatedField(field, + relevantAnnotationClasses).extractValidationRules(); + if (fieldValidationRules.size() > 0) { + classConstraints.put(field.getName(), fieldValidationRules); + } } } - } + }); + return classConstraints; } @@ -59,11 +63,12 @@ private boolean isNotExcluded(Field field) { return !excludedFields.contains(fullyQualifiedFieldName); } - private Predicate buildAnnotationsPredicate() { - Collection> predicates = Lists.newArrayList(); + private Optional> buildAnnotationsPredicate() { + Collection> predicates = Lists.newArrayList(); for (Class annotationClass : relevantAnnotationClasses) { predicates.add(ReflectionUtils.withAnnotation(annotationClass)); } - return Predicates.or(predicates); + + return predicates.stream().reduce(Predicate::or); } } diff --git a/valdr-bean-validation/src/main/java/com/github/valdr/ClasspathScanner.java b/valdr-bean-validation/src/main/java/com/github/valdr/ClasspathScanner.java index f5e6cd2..d2776c2 100644 --- a/valdr-bean-validation/src/main/java/com/github/valdr/ClasspathScanner.java +++ b/valdr-bean-validation/src/main/java/com/github/valdr/ClasspathScanner.java @@ -1,19 +1,17 @@ package com.github.valdr; -import com.google.common.base.Predicate; import com.google.common.collect.Sets; import org.apache.commons.lang3.StringUtils; import org.reflections.Reflections; -import org.reflections.scanners.SubTypesScanner; +import org.reflections.scanners.Scanners; import org.reflections.util.ClasspathHelper; import org.reflections.util.ConfigurationBuilder; import org.reflections.util.FilterBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.net.URL; import java.util.Collection; import java.util.Set; +import java.util.function.Predicate; /** * Provides means to scan the classpath for model classes that need to be parsed for constraint annotations. @@ -37,8 +35,10 @@ public ClasspathScanner(Options options) { * @see Options */ public Set> findClassesToParse() { - Reflections reflections = new Reflections(new ConfigurationBuilder(). - setUrls(buildClassLoaderUrls()).setScanners(new SubTypesScanner(false)).filterInputsBy(buildPackagePredicates())); + Reflections reflections = new Reflections(new ConfigurationBuilder() + .setUrls(buildClassLoaderUrls()) + .setScanners(Scanners.SubTypes.filterResultsBy(s -> true)) + .filterInputsBy(buildPackagePredicates())); return reflections.getSubTypesOf(Object.class); } @@ -57,11 +57,11 @@ private Predicate buildPackagePredicates() { FilterBuilder filterBuilder = new FilterBuilder(); // Include package names for (String packageName : options.getModelPackages()) { - filterBuilder.include(FilterBuilder.prefix(packageName)); + filterBuilder.includePackage(packageName); } // Exclude class names for (String excludedClassName : options.getExcludedClasses()) { - filterBuilder.exclude("^" + StringUtils.replace(excludedClassName, ".", "\\.") + "\\.class$"); + filterBuilder.excludePattern("^" + StringUtils.replace(excludedClassName, ".", "\\.") + "\\.class$"); } return filterBuilder; } diff --git a/valdr-bean-validation/src/main/java/com/github/valdr/ConstraintParser.java b/valdr-bean-validation/src/main/java/com/github/valdr/ConstraintParser.java index 2a99f80..8ef8abf 100644 --- a/valdr-bean-validation/src/main/java/com/github/valdr/ConstraintParser.java +++ b/valdr-bean-validation/src/main/java/com/github/valdr/ConstraintParser.java @@ -4,10 +4,8 @@ import com.fasterxml.jackson.databind.ObjectWriter; import com.fasterxml.jackson.databind.module.SimpleModule; import com.github.valdr.serializer.MinimalMapSerializer; -import com.google.common.base.Function; import com.google.common.collect.Iterables; import lombok.SneakyThrows; -import org.reflections.ReflectionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -15,6 +13,7 @@ import java.lang.annotation.Annotation; import java.util.HashMap; import java.util.Map; +import java.util.Objects; /** *

Parses classes in defined packages for supported Bean Validation (JSR @@ -79,21 +78,20 @@ private String toJson(Map classNameToValidationRulesMa return ow.writeValueAsString(classNameToValidationRulesMap); } + @SuppressWarnings("unchecked") private Iterable> getConfiguredCustomAnnotations() { - return Iterables.transform(options.getCustomAnnotationClasses(), new Function>() { - @Override - @SuppressWarnings("unchecked") - public Class apply(String className) { - Class validatorClass = ReflectionUtils.forName(className); - if (validatorClass.isAnnotation()) { - return (Class) validatorClass; + return options.getCustomAnnotationClasses().stream().map(className -> { + try { + Class validatorClass = Class.forName(className); + if (!validatorClass.isAnnotation()) { + logger.warn("The configured custom annotation class '{}' is not an annotation. It will be ignored.", validatorClass); } else { - logger.warn("The configured custom annotation class '{}' is not an annotation. It will be ignored.", - validatorClass); - return null; + return (Class) validatorClass; } + } catch (ClassNotFoundException e) { + logger.warn("The configured class '{}' can not be found. It will be ignored.", className); } - }); + return null; + }).filter(Objects::nonNull).toList(); } } diff --git a/valdr-bean-validation/valdr-bean-validation.iml b/valdr-bean-validation/valdr-bean-validation.iml index 059c9f4..bdb994f 100644 --- a/valdr-bean-validation/valdr-bean-validation.iml +++ b/valdr-bean-validation/valdr-bean-validation.iml @@ -24,9 +24,9 @@ - - - + + + @@ -34,7 +34,6 @@ -