1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
package org.apache.tapestry.annotations; |
15 |
|
|
16 |
|
import java.beans.BeanInfo; |
17 |
|
import java.beans.Introspector; |
18 |
|
import java.beans.PropertyDescriptor; |
19 |
|
import java.lang.annotation.Annotation; |
20 |
|
import java.lang.reflect.Method; |
21 |
|
import java.lang.reflect.ParameterizedType; |
22 |
|
import java.lang.reflect.Type; |
23 |
|
import java.lang.reflect.TypeVariable; |
24 |
|
import java.util.List; |
25 |
|
|
26 |
|
import org.apache.hivemind.ApplicationRuntimeException; |
27 |
|
import org.apache.tapestry.enhance.EnhanceUtils; |
28 |
|
import org.apache.tapestry.enhance.EnhancementOperation; |
29 |
|
import org.apache.tapestry.enhance.EnhancementWorker; |
30 |
|
import org.apache.tapestry.spec.IComponentSpecification; |
31 |
|
import org.apache.tapestry.spec.IPropertySpecification; |
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
7 |
public class ComponentPropertyProxyWorker implements EnhancementWorker { |
40 |
|
|
41 |
|
private List<String> _excludedPackages; |
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
public void performEnhancement(EnhancementOperation op, IComponentSpecification spec) { |
47 |
6 |
for (Object o : spec.getPropertySpecificationNames()) { |
48 |
6 |
String name = (String) o; |
49 |
6 |
IPropertySpecification ps = spec.getPropertySpecification(name); |
50 |
|
|
51 |
6 |
checkProxy(op, ps); |
52 |
6 |
} |
53 |
6 |
} |
54 |
|
|
55 |
|
public Class extractPropertyType(Class type, String propertyName, IPropertySpecification ps) { |
56 |
|
|
57 |
|
try { |
58 |
8 |
BeanInfo info = Introspector.getBeanInfo(type); |
59 |
8 |
PropertyDescriptor[] props = info.getPropertyDescriptors(); |
60 |
|
|
61 |
275 |
for (PropertyDescriptor prop : props) { |
62 |
|
|
63 |
275 |
if (!propertyName.equals(prop.getName())) { |
64 |
267 |
continue; |
65 |
|
} |
66 |
|
|
67 |
8 |
Method m = prop.getReadMethod(); |
68 |
8 |
if (m != null && !m.getGenericReturnType().getClass().getName().equals("java.lang.Class") |
69 |
|
&& TypeVariable.class.isAssignableFrom(m.getGenericReturnType().getClass())) { |
70 |
|
|
71 |
2 |
ps.setGeneric(true); |
72 |
2 |
TypeVariable tvar = (TypeVariable)m.getGenericReturnType(); |
73 |
|
|
74 |
|
|
75 |
2 |
if (type.getGenericSuperclass() != null |
76 |
|
&& ParameterizedType.class.isInstance(type.getGenericSuperclass())) { |
77 |
|
|
78 |
1 |
ParameterizedType ptype = (ParameterizedType)type.getGenericSuperclass(); |
79 |
1 |
if (ptype.getActualTypeArguments().length > 0) { |
80 |
|
|
81 |
1 |
ps.setCanProxy(canProxyType((Class)ptype.getActualTypeArguments()[0])); |
82 |
1 |
ps.setType(((Class)tvar.getBounds()[0]).getName()); |
83 |
|
|
84 |
1 |
return (Class)tvar.getBounds()[0]; |
85 |
|
} |
86 |
|
} |
87 |
|
|
88 |
1 |
return null; |
89 |
6 |
} else if (m != null) { |
90 |
|
|
91 |
3 |
ps.setCanProxy(canProxyType(m.getReturnType())); |
92 |
3 |
ps.setType(m.getReturnType().getName()); |
93 |
|
|
94 |
3 |
return m.getReturnType(); |
95 |
|
} |
96 |
|
|
97 |
|
|
98 |
|
|
99 |
3 |
if (m == null && prop.getWriteMethod() == null) |
100 |
0 |
return null; |
101 |
|
|
102 |
3 |
m = prop.getWriteMethod(); |
103 |
3 |
if (m.getParameterTypes().length != 1) |
104 |
0 |
return null; |
105 |
|
|
106 |
3 |
Type genParam = m.getGenericParameterTypes()[0]; |
107 |
3 |
Class param = m.getParameterTypes()[0]; |
108 |
|
|
109 |
3 |
if (!genParam.getClass().getName().equals("java.lang.Class") |
110 |
|
&& TypeVariable.class.isAssignableFrom(genParam.getClass())) { |
111 |
|
|
112 |
2 |
TypeVariable tvar = (TypeVariable)genParam; |
113 |
2 |
ps.setGeneric(true); |
114 |
|
|
115 |
2 |
if (type.getGenericSuperclass() != null) { |
116 |
|
|
117 |
2 |
ParameterizedType ptype = (ParameterizedType)type.getGenericSuperclass(); |
118 |
2 |
if (ptype.getActualTypeArguments().length > 0) { |
119 |
|
|
120 |
2 |
ps.setCanProxy(canProxyType((Class)ptype.getActualTypeArguments()[0])); |
121 |
2 |
ps.setType(((Class)tvar.getBounds()[0]).getName()); |
122 |
|
|
123 |
2 |
return (Class)tvar.getBounds()[0]; |
124 |
|
} |
125 |
|
} |
126 |
|
} |
127 |
|
|
128 |
1 |
ps.setCanProxy(canProxyType(param)); |
129 |
1 |
ps.setType(param.getName()); |
130 |
1 |
return param; |
131 |
|
} |
132 |
|
|
133 |
0 |
} catch (Throwable t) { |
134 |
|
|
135 |
0 |
throw new ApplicationRuntimeException("Error reading property " + propertyName + " from base component class : " + type, t); |
136 |
0 |
} |
137 |
|
|
138 |
0 |
return null; |
139 |
|
} |
140 |
|
|
141 |
|
boolean canProxyType(Class type) |
142 |
|
{ |
143 |
7 |
if (type == null) |
144 |
0 |
return false; |
145 |
|
|
146 |
7 |
if (!EnhanceUtils.canProxyPropertyType(type)) |
147 |
0 |
return false; |
148 |
|
|
149 |
7 |
for (Annotation an : type.getAnnotations()) { |
150 |
5 |
if (isExcluded(an)) { |
151 |
5 |
return false; |
152 |
|
} |
153 |
|
} |
154 |
|
|
155 |
2 |
return true; |
156 |
|
} |
157 |
|
|
158 |
|
void checkProxy(EnhancementOperation op, IPropertySpecification ps) { |
159 |
6 |
ps.setProxyChecked(true); |
160 |
|
|
161 |
6 |
if (!ps.isPersistent()) { |
162 |
0 |
return; |
163 |
|
} |
164 |
|
|
165 |
6 |
extractPropertyType(op.getBaseClass(), ps.getName(), ps); |
166 |
6 |
} |
167 |
|
|
168 |
|
boolean isExcluded(Annotation annotation) { |
169 |
5 |
for (String match : _excludedPackages) { |
170 |
|
|
171 |
5 |
if (annotation.annotationType().getName().indexOf(match) > -1) { |
172 |
5 |
return true; |
173 |
|
} |
174 |
|
} |
175 |
|
|
176 |
0 |
return false; |
177 |
|
} |
178 |
|
|
179 |
|
public void setExcludedPackages(List<String> packages) { |
180 |
7 |
_excludedPackages = packages; |
181 |
7 |
} |
182 |
|
} |
183 |
|
|