1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.bean; |
16 |
| |
17 |
| import java.lang.reflect.Field; |
18 |
| |
19 |
| import org.apache.hivemind.ApplicationRuntimeException; |
20 |
| import org.apache.hivemind.ClassResolver; |
21 |
| import org.apache.tapestry.IBeanProvider; |
22 |
| import org.apache.tapestry.Tapestry; |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| public class FieldBeanInitializer extends AbstractBeanInitializer |
33 |
| { |
34 |
| protected String _fieldName; |
35 |
| protected Object _fieldValue; |
36 |
| private boolean _fieldResolved = false; |
37 |
| |
38 |
0
| public synchronized void setBeanProperty(IBeanProvider provider, Object bean)
|
39 |
| { |
40 |
0
| ClassResolver resolver = provider.getClassResolver();
|
41 |
| |
42 |
0
| if (!_fieldResolved)
|
43 |
0
| resolveField(resolver);
|
44 |
| |
45 |
0
| setBeanProperty(bean, _fieldValue);
|
46 |
| } |
47 |
| |
48 |
0
| private void resolveField(ClassResolver resolver)
|
49 |
| { |
50 |
0
| if (_fieldResolved)
|
51 |
0
| return;
|
52 |
| |
53 |
| |
54 |
| |
55 |
0
| int dotx = _fieldName.lastIndexOf('.');
|
56 |
| |
57 |
0
| if (dotx < 0)
|
58 |
0
| throw new ApplicationRuntimeException(
|
59 |
| Tapestry.format("invalid-field-name", _fieldName)); |
60 |
| |
61 |
0
| String className = _fieldName.substring(0, dotx);
|
62 |
0
| String simpleFieldName = _fieldName.substring(dotx + 1);
|
63 |
| |
64 |
| |
65 |
| |
66 |
0
| if (className.indexOf('.') < 0)
|
67 |
0
| className = "java.lang." + className;
|
68 |
| |
69 |
0
| Class targetClass = null;
|
70 |
| |
71 |
0
| try
|
72 |
| { |
73 |
0
| targetClass = resolver.findClass(className);
|
74 |
| } |
75 |
| catch (Throwable t) |
76 |
| { |
77 |
0
| throw new ApplicationRuntimeException(
|
78 |
| Tapestry.format("unable-to-resolve-class", className), |
79 |
| t); |
80 |
| } |
81 |
| |
82 |
0
| Field field = null;
|
83 |
| |
84 |
0
| try
|
85 |
| { |
86 |
0
| field = targetClass.getField(simpleFieldName);
|
87 |
| } |
88 |
| catch (NoSuchFieldException ex) |
89 |
| { |
90 |
0
| throw new ApplicationRuntimeException(
|
91 |
| Tapestry.format("field-not-defined", _fieldName), |
92 |
| ex); |
93 |
| } |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
0
| try
|
99 |
| { |
100 |
0
| _fieldValue = field.get(null);
|
101 |
| } |
102 |
| catch (IllegalAccessException ex) |
103 |
| { |
104 |
0
| throw new ApplicationRuntimeException(
|
105 |
| Tapestry.format("illegal-field-access", _fieldName), |
106 |
| ex); |
107 |
| } |
108 |
| catch (NullPointerException ex) |
109 |
| { |
110 |
0
| throw new ApplicationRuntimeException(
|
111 |
| Tapestry.format("field-is-instance", _fieldName), |
112 |
| ex); |
113 |
| } |
114 |
| |
115 |
0
| _fieldResolved = true;
|
116 |
| } |
117 |
| |
118 |
| } |