1
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
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 }