Search in sources :

Example 1 with TypeRole

use of io.micronaut.data.annotation.TypeRole in project micronaut-data by micronaut-projects.

the class RepositoryTypeElementVisitor method visitClass.

@Override
public void visitClass(ClassElement element, VisitorContext context) {
    String interfaceName = element.getName();
    if (failing) {
        return;
    }
    if (visitedRepositories.contains(interfaceName)) {
        // prevent duplicate visits
        currentRepository = null;
        currentClass = null;
        return;
    }
    this.currentClass = element;
    entityResolver = new Function<ClassElement, SourcePersistentEntity>() {

        MappedEntityVisitor mappedEntityVisitor = new MappedEntityVisitor();

        MappedEntityVisitor embeddedMappedEntityVisitor = new MappedEntityVisitor(false);

        @Override
        public SourcePersistentEntity apply(ClassElement classElement) {
            return entityMap.computeIfAbsent(classElement.getName(), s -> {
                if (classElement.hasAnnotation("io.micronaut.data.annotation.Embeddable")) {
                    embeddedMappedEntityVisitor.visitClass(classElement, context);
                } else {
                    mappedEntityVisitor.visitClass(classElement, context);
                }
                return new SourcePersistentEntity(classElement, this);
            });
        }
    };
    if (element.hasDeclaredStereotype(Repository.class)) {
        visitedRepositories.add(interfaceName);
        currentRepository = element;
        queryEncoder = QueryBuilder.newQueryBuilder(element.getAnnotationMetadata());
        this.dataTypes = Utils.getConfiguredDataTypes(currentRepository);
        AnnotationMetadata annotationMetadata = element.getAnnotationMetadata();
        List<AnnotationValue<TypeRole>> roleArray = annotationMetadata.findAnnotation(RepositoryConfiguration.class).map(av -> av.getAnnotations("typeRoles", TypeRole.class)).orElse(Collections.emptyList());
        for (AnnotationValue<TypeRole> parameterRole : roleArray) {
            String role = parameterRole.stringValue("role").orElse(null);
            AnnotationClassValue cv = parameterRole.get("type", AnnotationClassValue.class).orElse(null);
            if (StringUtils.isNotEmpty(role) && cv != null) {
                context.getClassElement(cv.getName()).ifPresent(ce -> typeRoles.put(ce.getName(), role));
            }
        }
        if (element.isAssignable(SPRING_REPO)) {
            context.getClassElement("org.springframework.data.domain.Pageable").ifPresent(ce -> typeRoles.put(ce.getName(), TypeRole.PAGEABLE));
            context.getClassElement("org.springframework.data.domain.Page").ifPresent(ce -> typeRoles.put(ce.getName(), TypeRole.PAGE));
            context.getClassElement("org.springframework.data.domain.Slice").ifPresent(ce -> typeRoles.put(ce.getName(), TypeRole.SLICE));
            context.getClassElement("org.springframework.data.domain.Sort").ifPresent(ce -> typeRoles.put(ce.getName(), TypeRole.SORT));
        }
        if (queryEncoder == null) {
            context.fail("QueryEncoder not present on annotation processor path", element);
            failing = true;
        }
    }
}
Also used : DataType(io.micronaut.data.model.DataType) OrderUtil(io.micronaut.core.order.OrderUtil) Arrays(java.util.Arrays) TypeRole(io.micronaut.data.annotation.TypeRole) Internal(io.micronaut.core.annotation.Internal) DataMethod(io.micronaut.data.intercept.annotation.DataMethod) SoftServiceLoader(io.micronaut.core.io.service.SoftServiceLoader) SqlQueryBuilder(io.micronaut.data.model.query.builder.sql.SqlQueryBuilder) Map(java.util.Map) QueryResult(io.micronaut.data.model.query.builder.QueryResult) AnnotationClassValue(io.micronaut.core.annotation.AnnotationClassValue) PersistentPropertyPath(io.micronaut.data.model.PersistentPropertyPath) PersistentProperty(io.micronaut.data.model.PersistentProperty) SourceParameterExpressionImpl(io.micronaut.data.processor.model.criteria.impl.SourceParameterExpressionImpl) Sort(io.micronaut.data.model.Sort) Query(io.micronaut.data.annotation.Query) Set(java.util.Set) Collectors(java.util.stream.Collectors) GenericRepository(io.micronaut.data.repository.GenericRepository) StringUtils(io.micronaut.core.util.StringUtils) List(java.util.List) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) MethodElement(io.micronaut.inject.ast.MethodElement) MethodMatchInfo(io.micronaut.data.processor.visitors.finders.MethodMatchInfo) QueryParameterBinding(io.micronaut.data.model.query.builder.QueryParameterBinding) Parameter(io.micronaut.context.annotation.Parameter) RepositoryConfiguration(io.micronaut.data.annotation.RepositoryConfiguration) SourcePersistentProperty(io.micronaut.data.processor.model.SourcePersistentProperty) ClassElement(io.micronaut.inject.ast.ClassElement) HashMap(java.util.HashMap) Function(java.util.function.Function) ParameterElement(io.micronaut.inject.ast.ParameterElement) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) TypeElementVisitor(io.micronaut.inject.visitor.TypeElementVisitor) TypedElement(io.micronaut.inject.ast.TypedElement) Nullable(io.micronaut.core.annotation.Nullable) MethodMatcher(io.micronaut.data.processor.visitors.finders.MethodMatcher) Page(io.micronaut.data.model.Page) Pageable(io.micronaut.data.model.Pageable) TypeUtils(io.micronaut.data.processor.visitors.finders.TypeUtils) Element(io.micronaut.inject.ast.Element) Iterator(java.util.Iterator) Slice(io.micronaut.data.model.Slice) SourcePersistentEntity(io.micronaut.data.processor.model.SourcePersistentEntity) NonNull(io.micronaut.core.annotation.NonNull) BindingParameter(io.micronaut.data.model.query.BindingParameter) VisitorContext(io.micronaut.inject.visitor.VisitorContext) AnnotationValueBuilder(io.micronaut.core.annotation.AnnotationValueBuilder) PersistentEntity(io.micronaut.data.model.PersistentEntity) CollectionUtils(io.micronaut.core.util.CollectionUtils) QueryBuilder(io.micronaut.data.model.query.builder.QueryBuilder) Repository(io.micronaut.data.annotation.Repository) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) DataMethodQueryParameter(io.micronaut.data.intercept.annotation.DataMethodQueryParameter) Collections(java.util.Collections) SourcePersistentEntity(io.micronaut.data.processor.model.SourcePersistentEntity) ClassElement(io.micronaut.inject.ast.ClassElement) AnnotationClassValue(io.micronaut.core.annotation.AnnotationClassValue) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) TypeRole(io.micronaut.data.annotation.TypeRole)

Aggregations

Parameter (io.micronaut.context.annotation.Parameter)1 AnnotationClassValue (io.micronaut.core.annotation.AnnotationClassValue)1 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)1 AnnotationValue (io.micronaut.core.annotation.AnnotationValue)1 AnnotationValueBuilder (io.micronaut.core.annotation.AnnotationValueBuilder)1 Internal (io.micronaut.core.annotation.Internal)1 NonNull (io.micronaut.core.annotation.NonNull)1 Nullable (io.micronaut.core.annotation.Nullable)1 SoftServiceLoader (io.micronaut.core.io.service.SoftServiceLoader)1 OrderUtil (io.micronaut.core.order.OrderUtil)1 CollectionUtils (io.micronaut.core.util.CollectionUtils)1 StringUtils (io.micronaut.core.util.StringUtils)1 Query (io.micronaut.data.annotation.Query)1 Repository (io.micronaut.data.annotation.Repository)1 RepositoryConfiguration (io.micronaut.data.annotation.RepositoryConfiguration)1 TypeRole (io.micronaut.data.annotation.TypeRole)1 DataMethod (io.micronaut.data.intercept.annotation.DataMethod)1 DataMethodQueryParameter (io.micronaut.data.intercept.annotation.DataMethodQueryParameter)1 DataType (io.micronaut.data.model.DataType)1 Page (io.micronaut.data.model.Page)1