1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.spring.annotation;
21  
22  import com.liferay.portal.kernel.annotation.BeanReference;
23  
24  import java.beans.PropertyDescriptor;
25  
26  import java.lang.reflect.Field;
27  import java.lang.reflect.Method;
28  import java.lang.reflect.Modifier;
29  
30  import org.springframework.beans.BeanUtils;
31  import org.springframework.beans.factory.BeanFactory;
32  import org.springframework.beans.factory.annotation.InjectionMetadata;
33  import org.springframework.util.ClassUtils;
34  import org.springframework.util.ReflectionUtils;
35  
36  /**
37   * <a href="BeanReferenceCallback.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Michael Young
40   *
41   */
42  public class BeanReferenceCallback
43      implements ReflectionUtils.FieldCallback, ReflectionUtils.MethodCallback {
44  
45      public BeanReferenceCallback(
46          BeanFactory beanFactory, InjectionMetadata injectionMetadata,
47          Class<?> clazz) {
48  
49          _beanFactory = beanFactory;
50          _injectionMetadata = injectionMetadata;
51          _class = clazz;
52      }
53  
54      public void doWith(Field field) {
55          if (!field.isAnnotationPresent(BeanReference.class)) {
56              return;
57          }
58  
59          if (Modifier.isStatic(field.getModifiers())) {
60              throw new IllegalStateException(
61                  "@BeanReference annotation is not supported on static fields");
62          }
63  
64          _injectionMetadata.addInjectedField(
65              new BeanReferenceElement(_beanFactory, field, null));
66      }
67  
68      public void doWith(Method method) {
69          if (!method.isAnnotationPresent(BeanReference.class) ||
70              !method.equals(ClassUtils.getMostSpecificMethod(method, _class))) {
71  
72              return;
73          }
74  
75          if (Modifier.isStatic(method.getModifiers())) {
76              throw new IllegalStateException(
77                  "@BeanReference annotation is not supported on static methods");
78          }
79  
80          Class<?>[] parameterTypes = method.getParameterTypes();
81  
82          if (parameterTypes.length != 1) {
83              throw new IllegalStateException(
84                  "@BeanReference annotation requires a single argument method " +
85                      method);
86          }
87  
88          PropertyDescriptor propertyDescriptor = BeanUtils.findPropertyForMethod(
89              method);
90  
91          _injectionMetadata.addInjectedMethod(
92              new BeanReferenceElement(_beanFactory, method, propertyDescriptor));
93      }
94  
95      private BeanFactory _beanFactory;
96      private InjectionMetadata _injectionMetadata;
97      private Class<?> _class;
98  
99  }