1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.spring.annotation;
24  
25  import com.liferay.portal.kernel.annotation.BeanReference;
26  
27  import java.beans.PropertyDescriptor;
28  
29  import java.lang.reflect.Field;
30  import java.lang.reflect.Method;
31  import java.lang.reflect.Modifier;
32  
33  import org.springframework.beans.BeanUtils;
34  import org.springframework.beans.factory.BeanFactory;
35  import org.springframework.beans.factory.annotation.InjectionMetadata;
36  import org.springframework.util.ClassUtils;
37  import org.springframework.util.ReflectionUtils;
38  
39  /**
40   * <a href="BeanReferenceCallback.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Michael Young
43   *
44   */
45  public class BeanReferenceCallback
46      implements ReflectionUtils.FieldCallback, ReflectionUtils.MethodCallback {
47  
48      public BeanReferenceCallback(
49          BeanFactory beanFactory, InjectionMetadata injectionMetadata,
50          Class<?> clazz) {
51  
52          _beanFactory = beanFactory;
53          _injectionMetadata = injectionMetadata;
54          _class = clazz;
55      }
56  
57      public void doWith(Field field) {
58          if (!field.isAnnotationPresent(BeanReference.class)) {
59              return;
60          }
61  
62          if (Modifier.isStatic(field.getModifiers())) {
63              throw new IllegalStateException(
64                  "@BeanReference annotation is not supported on static fields");
65          }
66  
67          _injectionMetadata.addInjectedField(
68              new BeanReferenceElement(_beanFactory, field, null));
69      }
70  
71      public void doWith(Method method) {
72          if (!method.isAnnotationPresent(BeanReference.class) ||
73              !method.equals(ClassUtils.getMostSpecificMethod(method, _class))) {
74  
75              return;
76          }
77  
78          if (Modifier.isStatic(method.getModifiers())) {
79              throw new IllegalStateException(
80                  "@BeanReference annotation is not supported on static methods");
81          }
82  
83          Class<?>[] parameterTypes = method.getParameterTypes();
84  
85          if (parameterTypes.length != 1) {
86              throw new IllegalStateException(
87                  "@BeanReference annotation requires a single argument method " +
88                      method);
89          }
90  
91          PropertyDescriptor propertyDescriptor = BeanUtils.findPropertyForMethod(
92              method);
93  
94          _injectionMetadata.addInjectedMethod(
95              new BeanReferenceElement(_beanFactory, method, propertyDescriptor));
96      }
97  
98      private BeanFactory _beanFactory;
99      private InjectionMetadata _injectionMetadata;
100     private Class<?> _class;
101 
102 }