1 /*
2  * Copyright 2015-2020 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.data.projection;
17
18 import java.beans.PropertyDescriptor;
19 import java.lang.reflect.Method;
20 import java.util.Map;
21 import java.util.concurrent.ConcurrentHashMap;
22
23 import org.aopalliance.intercept.MethodInterceptor;
24 import org.springframework.beans.BeansException;
25 import org.springframework.beans.factory.BeanFactory;
26 import org.springframework.beans.factory.BeanFactoryAware;
27 import org.springframework.beans.factory.annotation.Value;
28 import org.springframework.core.annotation.AnnotationUtils;
29 import org.springframework.data.util.AnnotationDetectionMethodCallback;
30 import org.springframework.expression.spel.standard.SpelExpressionParser;
31 import org.springframework.lang.Nullable;
32 import org.springframework.util.Assert;
33 import org.springframework.util.ReflectionUtils;
34
35 /**
36  * A {@link ProxyProjectionFactory} that adds support to use {@link Value}-annotated methods on a projection interface
37  * to evaluate the contained SpEL expression to define the outcome of the method call.
38  *
39  * @author Oliver Gierke
40  * @author Thomas Darimont
41  * @author Mark Paluch
42  * @author Jens Schauder
43  * @since 1.10
44  */

45 public class SpelAwareProxyProjectionFactory extends ProxyProjectionFactory implements BeanFactoryAware {
46
47     private final Map<Class<?>, Boolean> typeCache = new ConcurrentHashMap<>();
48     private final SpelExpressionParser parser = new SpelExpressionParser();
49
50     private @Nullable BeanFactory beanFactory;
51
52     /*
53      * (non-Javadoc)
54      * @see org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org.springframework.beans.factory.BeanFactory)
55      */

56     @Override
57     public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
58         this.beanFactory = beanFactory;
59     }
60
61     /*
62      * (non-Javadoc)
63      * @see org.springframework.data.projection.ProxyProjectionFactory#createProjectionInformation(java.lang.Class)
64      */

65     @Override
66     protected ProjectionInformation createProjectionInformation(Class<?> projectionType) {
67         return new SpelAwareProjectionInformation(projectionType);
68     }
69
70     /**
71      * Inspects the given target type for methods with {@link Value} annotations and caches the result. Will create a
72      * {@link SpelEvaluatingMethodInterceptor} if an annotation was found or return the delegate as is if not.
73      *
74      * @param interceptor the root {@link MethodInterceptor}.
75      * @param source The backing source object.
76      * @param projectionType the proxy target type.
77      * @return
78      */

79     @Override
80     protected MethodInterceptor postProcessAccessorInterceptor(MethodInterceptor interceptor, Object source,
81             Class<?> projectionType) {
82
83         return typeCache.computeIfAbsent(projectionType, SpelAwareProxyProjectionFactory::hasMethodWithValueAnnotation)
84                 ? new SpelEvaluatingMethodInterceptor(interceptor, source, beanFactory, parser, projectionType)
85                 : interceptor;
86     }
87
88     /**
89      * Returns whether the given type as a method annotated with {@link Value}.
90      *
91      * @param type must not be {@literal null}.
92      * @return
93      */

94     private static boolean hasMethodWithValueAnnotation(Class<?> type) {
95
96         Assert.notNull(type, "Type must not be null!");
97
98         AnnotationDetectionMethodCallback<Value> callback = new AnnotationDetectionMethodCallback<>(Value.class);
99         ReflectionUtils.doWithMethods(type, callback);
100
101         return callback.hasFoundAnnotation();
102     }
103
104     protected static class SpelAwareProjectionInformation extends DefaultProjectionInformation {
105
106         protected SpelAwareProjectionInformation(Class<?> projectionType) {
107             super(projectionType);
108         }
109
110         /*
111          * (non-Javadoc)
112          * @see org.springframework.data.projection.DefaultProjectionInformation#isInputProperty(java.beans.PropertyDescriptor)
113          */

114         @Override
115         protected boolean isInputProperty(PropertyDescriptor descriptor) {
116
117             if (!super.isInputProperty(descriptor)) {
118                 return false;
119             }
120
121             Method readMethod = descriptor.getReadMethod();
122
123             if (readMethod == null) {
124                 return false;
125             }
126
127             return AnnotationUtils.findAnnotation(readMethod, Value.class) == null;
128         }
129     }
130 }
131