Search in sources :

Example 1 with MethodMatcher

use of io.micronaut.data.processor.visitors.finders.MethodMatcher in project micronaut-data by micronaut-projects.

the class RepositoryTypeElementVisitor method visitMethod.

@Override
public void visitMethod(MethodElement element, VisitorContext context) {
    if (currentRepository == null || failing) {
        return;
    }
    ClassElement genericReturnType = element.getGenericReturnType();
    if (queryEncoder != null && currentClass != null && element.isAbstract() && !element.isStatic() && methodsMatchers != null) {
        ParameterElement[] parameters = element.getParameters();
        Map<String, Element> parametersInRole = new HashMap<>(2);
        for (ParameterElement parameter : parameters) {
            ClassElement type = parameter.getType();
            this.typeRoles.entrySet().stream().filter(entry -> {
                String roleType = entry.getKey();
                return type.isAssignable(roleType);
            }).forEach(entry -> parametersInRole.put(entry.getValue(), parameter));
        }
        if (element.hasDeclaredAnnotation(DataMethod.class)) {
            // explicitly handled
            return;
        }
        MatchContext matchContext = new MatchContext(queryEncoder, currentRepository, context, element, typeRoles, genericReturnType, parameters);
        try {
            SourcePersistentEntity entity = resolvePersistentEntity(element, parametersInRole);
            MethodMatchContext methodMatchContext = new MethodMatchContext(queryEncoder, currentRepository, entity, context, genericReturnType, element, parametersInRole, typeRoles, parameters, entityResolver);
            for (MethodMatcher finder : methodsMatchers) {
                MethodMatcher.MethodMatch matcher = finder.match(methodMatchContext);
                if (matcher == null) {
                    continue;
                }
                MethodMatchInfo methodInfo = matcher.buildMatchInfo(methodMatchContext);
                if (methodInfo == null) {
                    continue;
                }
                processMethodInfo(methodMatchContext, methodInfo);
                return;
            }
            if (matchContext.isPossiblyFailing()) {
                matchContext.logPossibleFailures();
            } else {
                String messageStart = matchContext.getUnableToImplementMessage();
                context.fail(messageStart + "No possible implementations found.", element);
            }
            this.failing = true;
        } catch (MatchFailedException e) {
            context.fail(matchContext.getUnableToImplementMessage() + e.getMessage(), e.getElement() == null ? element : e.getElement());
            this.failing = true;
        } catch (Exception e) {
            matchContext.fail(e.getMessage());
            this.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) HashMap(java.util.HashMap) SourcePersistentEntity(io.micronaut.data.processor.model.SourcePersistentEntity) MethodElement(io.micronaut.inject.ast.MethodElement) ClassElement(io.micronaut.inject.ast.ClassElement) ParameterElement(io.micronaut.inject.ast.ParameterElement) TypedElement(io.micronaut.inject.ast.TypedElement) Element(io.micronaut.inject.ast.Element) ClassElement(io.micronaut.inject.ast.ClassElement) ParameterElement(io.micronaut.inject.ast.ParameterElement) MethodMatchInfo(io.micronaut.data.processor.visitors.finders.MethodMatchInfo) MethodMatcher(io.micronaut.data.processor.visitors.finders.MethodMatcher)

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