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