Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade 'org.reflections:reflections' dependency to version 0.10.2 #46

Merged
merged 7 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<inceptionYear>2014</inceptionYear>

<prerequisites>
<maven>3.5.2</maven>
<maven>3.9.4</maven>
</prerequisites>

<licenses>
Expand Down Expand Up @@ -105,7 +105,7 @@
<junit.version>5.10.0</junit.version>
<lombok.version>1.18.28</lombok.version>
<mockito.version>5.5.0</mockito.version>
<reflections.version>0.9.9</reflections.version>
<reflections.version>0.10.2</reflections.version>
<servlet-api.version>6.0.0</servlet-api.version>
<slf4j.version>2.0.9</slf4j.version>
<spring.version>6.0.11</spring.version>
Expand Down
7 changes: 3 additions & 4 deletions valdr-bean-validation-demo/valdr-bean-validation-demo.iml
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,16 @@
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="valdr-bean-validation" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:2.0.9" level="project" />
<orderEntry type="library" name="Maven: org.reflections:reflections:0.9.9" level="project" />
<orderEntry type="library" name="Maven: org.javassist:javassist:3.18.2-GA" level="project" />
<orderEntry type="library" name="Maven: com.google.code.findbugs:annotations:2.0.1" level="project" />
<orderEntry type="library" name="Maven: org.reflections:reflections:0.10.2" level="project" />
<orderEntry type="library" name="Maven: org.javassist:javassist:3.28.0-GA" level="project" />
<orderEntry type="library" name="Maven: com.google.code.findbugs:jsr305:3.0.2" level="project" />
<orderEntry type="library" name="Maven: jakarta.validation:jakarta.validation-api:3.0.2" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.14.3" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.14.3" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.14.3" level="project" />
<orderEntry type="library" name="Maven: com.google.guava:guava:32.1.2-jre" level="project" />
<orderEntry type="library" name="Maven: com.google.guava:failureaccess:1.0.1" level="project" />
<orderEntry type="library" name="Maven: com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava" level="project" />
<orderEntry type="library" name="Maven: com.google.code.findbugs:jsr305:3.0.2" level="project" />
<orderEntry type="library" name="Maven: org.checkerframework:checker-qual:3.33.0" level="project" />
<orderEntry type="library" name="Maven: com.google.errorprone:error_prone_annotations:2.18.0" level="project" />
<orderEntry type="library" name="Maven: com.google.j2objc:j2objc-annotations:2.8" level="project" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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;

import java.lang.annotation.Annotation;
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
Expand All @@ -27,7 +27,7 @@ public class AnnotatedClass {
* AnnotatedClass#extractValidationRules()} is invoked
*/
AnnotatedClass(Class clazz, List<String> excludedFields, Iterable<Class<? extends Annotation>>
relevantAnnotationClasses) {
relevantAnnotationClasses) {
this.clazz = clazz;
this.excludedFields = excludedFields;
this.relevantAnnotationClasses = relevantAnnotationClasses;
Expand All @@ -41,16 +41,20 @@ public class AnnotatedClass {
*/
ClassConstraints extractValidationRules() {
final ClassConstraints classConstraints = new ClassConstraints();
Set<Field> 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<Field> 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;
}

Expand All @@ -59,11 +63,12 @@ private boolean isNotExcluded(Field field) {
return !excludedFields.contains(fullyQualifiedFieldName);
}

private Predicate<? super Field> buildAnnotationsPredicate() {
Collection<Predicate<? super Field>> predicates = Lists.newArrayList();
private Optional<Predicate<Field>> buildAnnotationsPredicate() {
Collection<Predicate<Field>> predicates = Lists.newArrayList();
for (Class<? extends Annotation> annotationClass : relevantAnnotationClasses) {
predicates.add(ReflectionUtils.withAnnotation(annotationClass));
}
return Predicates.or(predicates);

return predicates.stream().reduce(Predicate::or);
}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -37,8 +35,10 @@ public ClasspathScanner(Options options) {
* @see Options
*/
public Set<Class<?>> 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);
}
Expand All @@ -57,11 +57,11 @@ private Predicate<String> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
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;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* <p> Parses classes in defined packages for supported <a href="http://beanvalidation.org/">Bean Validation (JSR
Expand Down Expand Up @@ -79,21 +78,20 @@ private String toJson(Map<String, ClassConstraints> classNameToValidationRulesMa
return ow.writeValueAsString(classNameToValidationRulesMap);
}

@SuppressWarnings("unchecked")
private Iterable<? extends Class<? extends Annotation>> getConfiguredCustomAnnotations() {
return Iterables.transform(options.getCustomAnnotationClasses(), new Function<String,
Class<? extends Annotation>>() {
@Override
@SuppressWarnings("unchecked")
public Class<? extends Annotation> apply(String className) {
Class<?> validatorClass = ReflectionUtils.forName(className);
if (validatorClass.isAnnotation()) {
return (Class<? extends Annotation>) 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<? extends Annotation>) validatorClass;
}
} catch (ClassNotFoundException e) {
logger.warn("The configured class '{}' can not be found. It will be ignored.", className);
}
});
return null;
}).filter(Objects::nonNull).toList();
}
}
7 changes: 3 additions & 4 deletions valdr-bean-validation/valdr-bean-validation.iml
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,16 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:2.0.9" level="project" />
<orderEntry type="library" name="Maven: org.reflections:reflections:0.9.9" level="project" />
<orderEntry type="library" name="Maven: org.javassist:javassist:3.18.2-GA" level="project" />
<orderEntry type="library" name="Maven: com.google.code.findbugs:annotations:2.0.1" level="project" />
<orderEntry type="library" name="Maven: org.reflections:reflections:0.10.2" level="project" />
<orderEntry type="library" name="Maven: org.javassist:javassist:3.28.0-GA" level="project" />
<orderEntry type="library" name="Maven: com.google.code.findbugs:jsr305:3.0.2" level="project" />
<orderEntry type="library" name="Maven: jakarta.validation:jakarta.validation-api:3.0.2" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.14.3" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.14.3" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.14.3" level="project" />
<orderEntry type="library" name="Maven: com.google.guava:guava:32.1.2-jre" level="project" />
<orderEntry type="library" name="Maven: com.google.guava:failureaccess:1.0.1" level="project" />
<orderEntry type="library" name="Maven: com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava" level="project" />
<orderEntry type="library" name="Maven: com.google.code.findbugs:jsr305:3.0.2" level="project" />
<orderEntry type="library" name="Maven: org.checkerframework:checker-qual:3.33.0" level="project" />
<orderEntry type="library" name="Maven: com.google.errorprone:error_prone_annotations:2.18.0" level="project" />
<orderEntry type="library" name="Maven: com.google.j2objc:j2objc-annotations:2.8" level="project" />
Expand Down