org.javers.core.metamodel.scanner.AnnotationNamesProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javers-core Show documentation
Show all versions of javers-core Show documentation
JaVers - object auditing and diff framework for Java
package org.javers.core.metamodel.scanner;
import org.javers.common.collections.Lists;
import org.javers.common.collections.Sets;
import org.javers.common.reflection.ReflectionUtil;
import java.lang.annotation.Annotation;
import java.util.*;
/**
* @author bartosz walacik
*/
class AnnotationNamesProvider {
private final Set entityAliases = new HashSet<>();
private final Set typeNameAliases = new HashSet<>();
private final Set valueObjectAliases = new HashSet<>();
private final Set valueAliases = new HashSet<>();
private final Set transientPropertyAliases = new HashSet<>();
private final Set propertyNameAliases = new HashSet<>();
private final List namesProviders = Lists.immutableListOf(
new JPAAnnotationsNameSpace());
AnnotationNamesProvider() {
for (AnnotationsNameSpace provider : namesProviders){
entityAliases.addAll(provider.getEntityAliases());
valueObjectAliases.addAll(provider.getValueObjectAliases());
valueAliases.addAll(provider.getValueAliases());
transientPropertyAliases.addAll(provider.getTransientPropertyAliases());
typeNameAliases.addAll(provider.getTypeNameAliases());
propertyNameAliases.addAll(provider.getPropertyNameAliases());
}
}
boolean hasEntityAnnAlias(Set> annTypes) {
return annTypes.stream().anyMatch(annType -> entityAliases.contains(annType.getSimpleName()));
}
boolean hasValueObjectAnnAlias(Set> annTypes) {
return annTypes.stream().anyMatch(annType -> valueObjectAliases.contains(annType.getSimpleName()));
}
boolean hasValueAnnAlias(Set> annTypes){
return annTypes.stream().anyMatch(annType -> valueAliases.contains(annType.getSimpleName()));
}
boolean hasTransientPropertyAnn(Set> annTypes){
return annTypes.contains(JaversAnnotationsNameSpace.DIFF_IGNORE_ANN) ||
annTypes.stream().anyMatch(annType -> transientPropertyAliases.contains(annType.getSimpleName()));
}
/**
* Checks if a given class has DIFF_IGNORE_PROPERTIES_ANN
*/
boolean hasIgnorePropertiesAnn(Class type) {
return type.isAnnotationPresent(JaversAnnotationsNameSpace.DIFF_IGNORE_PROPERTIES_ANN);
}
/**
* Finds DIFF_IGNORE_PROPERTIES_ANN and returns a set of property names from the annotation
*/
Set getIgnoreFieldsAnnValue(Set annotations) {
return findAnnotation(annotations, JaversAnnotationsNameSpace.DIFF_IGNORE_PROPERTIES_ANN, new HashSet<>())
.map(an -> Sets.asSet((String[]) ReflectionUtil.getAnnotationValue(an, "value")))
.orElseGet(() -> Collections.emptySet());
}
boolean hasDiffIncludeAnn(Set> annTypes) {
return annTypes.contains(JaversAnnotationsNameSpace.DIFF_INCLUDE_ANN);
}
boolean hasShallowReferenceAnn(Set> annTypes) {
return annTypes.contains(JaversAnnotationsNameSpace.SHALLOW_REFERENCE_ANN);
}
Optional findTypeNameAnnValue(Set annotations) {
return getAnnotationValue(annotations, JaversAnnotationsNameSpace.TYPE_NAME_ANN, typeNameAliases);
}
Optional findPropertyNameAnnValue(Set annotations) {
return getAnnotationValue(annotations, JaversAnnotationsNameSpace.PROPERTY_NAME_ANN, propertyNameAliases);
}
private Optional getAnnotationValue(Set annotations, Class javersAnnType, Set aliases) {
Optional annotation = findAnnotation(annotations, javersAnnType, aliases);
return annotation.map(ann -> ReflectionUtil.getAnnotationValue(ann, "value"));
}
private Optional findAnnotation(Set annotations, Class javersAnnType, Set aliases) {
Optional jTypeName = annotations.stream()
.filter(ann -> javersAnnType.isAssignableFrom(ann.getClass()))
.findAny();
if (jTypeName.isPresent()) {
return jTypeName;
}
return annotations.stream()
.filter(ann -> aliases.contains(ann.annotationType().getSimpleName()))
.findFirst();
}
}