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