All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.javers.core.metamodel.scanner.ClassAnnotationsScanner Maven / Gradle / Ivy

There is a newer version: 7.6.2
Show newest version
package org.javers.core.metamodel.scanner;

import org.javers.common.collections.Lists;
import org.javers.common.collections.Sets;
import org.javers.common.validation.Validate;
import org.javers.core.metamodel.annotation.*;

import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import static org.javers.core.metamodel.scanner.JaversAnnotationsNameSpace.IGNORE_DECLARED_PROPERTIES_ANN;

/**
 * Should scan well known annotations at class level
 *
 * @author bartosz walacik
 */
class ClassAnnotationsScanner {
    private final AnnotationNamesProvider annotationNamesProvider;
    private List> JAVERS_TYPE_ANNOTATIONS = Lists.immutableListOf(
            DiffIgnore.class,
            Entity.class,
            ShallowReference.class,
            ValueObject.class,
            Value.class
    );

    ClassAnnotationsScanner(AnnotationNamesProvider annotationNamesProvider) {
        this.annotationNamesProvider = annotationNamesProvider;
    }

    public ClassAnnotationsScan scan(Class javaClass){
        Validate.argumentIsNotNull(javaClass);

        Set annotations = Sets.asSet(javaClass.getAnnotations());
        Set> annTypes = annotations.stream()
                .map(a -> a.annotationType())
                .collect(Collectors.toSet());

        Optional typeName = annotationNamesProvider.findTypeNameAnnValue(annotations);

        boolean hasIgnoreDeclaredProperties = annTypes.contains(IGNORE_DECLARED_PROPERTIES_ANN);

        return new ClassAnnotationsScan(typeFromAnnotation(annTypes),
                                        hasIgnoreDeclaredProperties,
                                        typeName);
    }

    private TypeFromAnnotation typeFromAnnotation(Set> annTypes) {
        Optional> javersTypeAnnotation =
                JAVERS_TYPE_ANNOTATIONS.stream().filter(annTypes::contains).findFirst();

        if (javersTypeAnnotation.isPresent()) {
            return new TypeFromAnnotation(javersTypeAnnotation.get());
        }
        else {
            boolean hasValue = annotationNamesProvider.hasValueAnnAlias(annTypes);
            boolean hasValueObject = annotationNamesProvider.hasValueObjectAnnAlias(annTypes);
            boolean hasEntity = annotationNamesProvider.hasEntityAnnAlias(annTypes);
            return new TypeFromAnnotation(hasEntity, hasValueObject, hasValue);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy