Search in sources :

Example 1 with MergedAnnotation

use of org.springframework.core.annotation.MergedAnnotation in project spring-data-commons by spring-projects.

the class QuerydslPredicateArgumentResolverSupport method getPredicate.

Predicate getPredicate(MethodParameter parameter, MultiValueMap<String, String> queryParameters) {
    MergedAnnotations annotations = MergedAnnotations.from(parameter.getParameter());
    MergedAnnotation<QuerydslPredicate> predicateAnnotation = annotations.get(QuerydslPredicate.class);
    TypeInformation<?> domainType = extractTypeInfo(parameter, predicateAnnotation).getRequiredActualType();
    Optional<Class<? extends QuerydslBinderCustomizer<?>>> bindingsAnnotation = // 
    predicateAnnotation.getValue("bindings").map(CastUtils::cast);
    QuerydslBindings bindings = // 
    bindingsAnnotation.map(// 
    it -> bindingsFactory.createBindingsFor(domainType, it)).orElseGet(() -> bindingsFactory.createBindingsFor(domainType));
    return predicateBuilder.getPredicate(domainType, queryParameters, bindings);
}
Also used : HandlerMethodArgumentResolver(org.springframework.web.method.support.HandlerMethodArgumentResolver) QuerydslBinderCustomizer(org.springframework.data.querydsl.binding.QuerydslBinderCustomizer) MultiValueMap(org.springframework.util.MultiValueMap) TypeInformation(org.springframework.data.util.TypeInformation) MergedAnnotation(org.springframework.core.annotation.MergedAnnotation) QuerydslPredicateBuilder(org.springframework.data.querydsl.binding.QuerydslPredicateBuilder) ClassTypeInformation(org.springframework.data.util.ClassTypeInformation) MergedAnnotations(org.springframework.core.annotation.MergedAnnotations) MethodParameter(org.springframework.core.MethodParameter) Optional(java.util.Optional) Nullable(org.springframework.lang.Nullable) ResolvableType(org.springframework.core.ResolvableType) QuerydslBindingsFactory(org.springframework.data.querydsl.binding.QuerydslBindingsFactory) Predicate(com.querydsl.core.types.Predicate) Method(java.lang.reflect.Method) ConversionService(org.springframework.core.convert.ConversionService) QuerydslPredicate(org.springframework.data.querydsl.binding.QuerydslPredicate) QuerydslBindings(org.springframework.data.querydsl.binding.QuerydslBindings) CastUtils(org.springframework.data.util.CastUtils) Assert(org.springframework.util.Assert) QuerydslBinderCustomizer(org.springframework.data.querydsl.binding.QuerydslBinderCustomizer) QuerydslPredicate(org.springframework.data.querydsl.binding.QuerydslPredicate) QuerydslBindings(org.springframework.data.querydsl.binding.QuerydslBindings) MergedAnnotations(org.springframework.core.annotation.MergedAnnotations) CastUtils(org.springframework.data.util.CastUtils)

Example 2 with MergedAnnotation

use of org.springframework.core.annotation.MergedAnnotation in project spring-boot by spring-projects.

the class AnnotationsPropertySource method getProperties.

private void getProperties(Class<?> source, Map<String, Object> properties) {
    MergedAnnotations.from(source, SearchStrategy.SUPERCLASS).stream().filter(MergedAnnotationPredicates.unique(MergedAnnotation::getType)).forEach((annotation) -> {
        Class<Annotation> type = annotation.getType();
        MergedAnnotation<?> typeMapping = MergedAnnotations.from(type).get(PropertyMapping.class, MergedAnnotation::isDirectlyPresent);
        String prefix = typeMapping.getValue(MergedAnnotation.VALUE, String.class).orElse("");
        SkipPropertyMapping defaultSkip = typeMapping.getValue("skip", SkipPropertyMapping.class).orElse(SkipPropertyMapping.YES);
        for (Method attribute : type.getDeclaredMethods()) {
            collectProperties(prefix, defaultSkip, annotation, attribute, properties);
        }
    });
    if (TestContextAnnotationUtils.searchEnclosingClass(source)) {
        getProperties(source.getEnclosingClass(), properties);
    }
}
Also used : MergedAnnotation(org.springframework.core.annotation.MergedAnnotation) Method(java.lang.reflect.Method) MergedAnnotation(org.springframework.core.annotation.MergedAnnotation) Annotation(java.lang.annotation.Annotation)

Example 3 with MergedAnnotation

use of org.springframework.core.annotation.MergedAnnotation in project spring-integration by spring-projects.

the class MethodAnnotationPublisherMetadataSource method getExpressionForPayload.

@Override
public Expression getExpressionForPayload(Method method) {
    return this.payloadExpressions.computeIfAbsent(method, method1 -> {
        Expression payloadExpression = null;
        MergedAnnotation<Payload> payloadMergedAnnotation = MergedAnnotations.from(method, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY).get(Payload.class);
        if (payloadMergedAnnotation.isPresent()) {
            String payloadExpressionString = payloadMergedAnnotation.getString("expression");
            if (!StringUtils.hasText(payloadExpressionString)) {
                payloadExpression = RETURN_VALUE_EXPRESSION;
            } else {
                payloadExpression = EXPRESSION_PARSER.parseExpression(payloadExpressionString);
            }
        }
        Annotation[][] annotationArray = method.getParameterAnnotations();
        for (int i = 0; i < annotationArray.length; i++) {
            Annotation[] parameterAnnotations = annotationArray[i];
            payloadMergedAnnotation = MergedAnnotations.from(parameterAnnotations).get(Payload.class);
            if (payloadMergedAnnotation.isPresent()) {
                Assert.state(payloadExpression == null, "@Payload can be used at most once on a @Publisher method, " + "either at method-level or on a single parameter");
                Assert.state("".equals(payloadMergedAnnotation.getString("expression")), "@Payload on a parameter for a @Publisher method may not contain an 'expression'");
                payloadExpression = EXPRESSION_PARSER.parseExpression("#" + ARGUMENT_MAP_VARIABLE_NAME + "[" + i + "]");
            }
        }
        if (payloadExpression == null || RETURN_VALUE_EXPRESSION.getExpressionString().equals(payloadExpression.getExpressionString())) {
            Assert.isTrue(!void.class.equals(method.getReturnType()), "When defining @Publisher on a void-returning method, an explicit payload " + "expression that does not rely upon a #return value is required.");
        }
        return payloadExpression;
    });
}
Also used : Expression(org.springframework.expression.Expression) Payload(org.springframework.messaging.handler.annotation.Payload) MergedAnnotation(org.springframework.core.annotation.MergedAnnotation) Annotation(java.lang.annotation.Annotation)

Aggregations

MergedAnnotation (org.springframework.core.annotation.MergedAnnotation)3 Annotation (java.lang.annotation.Annotation)2 Method (java.lang.reflect.Method)2 Predicate (com.querydsl.core.types.Predicate)1 Optional (java.util.Optional)1 MethodParameter (org.springframework.core.MethodParameter)1 ResolvableType (org.springframework.core.ResolvableType)1 MergedAnnotations (org.springframework.core.annotation.MergedAnnotations)1 ConversionService (org.springframework.core.convert.ConversionService)1 QuerydslBinderCustomizer (org.springframework.data.querydsl.binding.QuerydslBinderCustomizer)1 QuerydslBindings (org.springframework.data.querydsl.binding.QuerydslBindings)1 QuerydslBindingsFactory (org.springframework.data.querydsl.binding.QuerydslBindingsFactory)1 QuerydslPredicate (org.springframework.data.querydsl.binding.QuerydslPredicate)1 QuerydslPredicateBuilder (org.springframework.data.querydsl.binding.QuerydslPredicateBuilder)1 CastUtils (org.springframework.data.util.CastUtils)1 ClassTypeInformation (org.springframework.data.util.ClassTypeInformation)1 TypeInformation (org.springframework.data.util.TypeInformation)1 Expression (org.springframework.expression.Expression)1 Nullable (org.springframework.lang.Nullable)1 Payload (org.springframework.messaging.handler.annotation.Payload)1