1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kcb.util; |
17 | |
|
18 | |
import java.lang.reflect.InvocationHandler; |
19 | |
import java.lang.reflect.Method; |
20 | |
|
21 | |
import org.springframework.beans.factory.BeanFactory; |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
public class BeanFactoryInvocationHandler implements InvocationHandler { |
29 | |
private static final String PREFIX = "get"; |
30 | |
|
31 | |
private BeanFactory beanFactory; |
32 | |
|
33 | 4 | public BeanFactoryInvocationHandler(BeanFactory beanFactory) { |
34 | 4 | this.beanFactory = beanFactory; |
35 | 4 | } |
36 | |
|
37 | |
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
38 | 4 | if (args != null && args.length > 0) { |
39 | 1 | throw new RuntimeException("BeanFactoryInvocationHandler only serves getters"); |
40 | |
} |
41 | 3 | String methodName = method.getName(); |
42 | 3 | if (!methodName.startsWith(PREFIX)) { |
43 | 1 | throw new RuntimeException("BeanFactoryInvocationHandler only serves getters"); |
44 | |
} |
45 | 2 | String beanName = methodName.substring(PREFIX.length()); |
46 | 2 | if (beanName.length() == 0) { |
47 | 1 | throw new RuntimeException("Illegal accessor, no bean name specified: " + methodName); |
48 | |
} |
49 | 1 | char firstChar = beanName.charAt(0); |
50 | 1 | if (!Character.isLowerCase(firstChar)) { |
51 | 1 | beanName = Character.toLowerCase(firstChar) + beanName.substring(1); |
52 | |
} |
53 | 1 | return beanFactory.getBean(beanName); |
54 | |
} |
55 | |
} |