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  public class BeanReferenceCallback
45      implements ReflectionUtils.FieldCallback, ReflectionUtils.MethodCallback {
46  
47      public BeanReferenceCallback(
48          BeanFactory beanFactory, InjectionMetadata injectionMetadata,
49          Class<?> clazz) {
50  
51          _beanFactory = beanFactory;
52          _injectionMetadata = injectionMetadata;
53          _class = clazz;
54      }
55  
56      public void doWith(Field field) {
57          if (!field.isAnnotationPresent(BeanReference.class)) {
58              return;
59          }
60  
61          if (Modifier.isStatic(field.getModifiers())) {
62              throw new IllegalStateException(
63                  "@BeanReference annotation is not supported on static fields");
64          }
65  
66          _injectionMetadata.addInjectedField(
67              new BeanReferenceElement(_beanFactory, field, null));
68      }
69  
70      public void doWith(Method method) {
71          if (!method.isAnnotationPresent(BeanReference.class) ||
72              !method.equals(ClassUtils.getMostSpecificMethod(method, _class))) {
73  
74              return;
75          }
76  
77          if (Modifier.isStatic(method.getModifiers())) {
78              throw new IllegalStateException(
79                  "@BeanReference annotation is not supported on static methods");
80          }
81  
82          Class<?>[] parameterTypes = method.getParameterTypes();
83  
84          if (parameterTypes.length != 1) {
85              throw new IllegalStateException(
86                  "@BeanReference annotation requires a single argument method " +
87                      method);
88          }
89  
90          PropertyDescriptor propertyDescriptor = BeanUtils.findPropertyForMethod(
91              method);
92  
93          _injectionMetadata.addInjectedMethod(
94              new BeanReferenceElement(_beanFactory, method, propertyDescriptor));
95      }
96  
97      private BeanFactory _beanFactory;
98      private InjectionMetadata _injectionMetadata;
99      private Class<?> _class;
100 
101 }