1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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 }