1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.annotations; |
16 |
|
|
17 |
|
import org.apache.hivemind.ClassResolver; |
18 |
|
import org.apache.hivemind.ErrorLog; |
19 |
|
import org.apache.hivemind.Location; |
20 |
|
import org.apache.hivemind.Resource; |
21 |
|
import org.apache.hivemind.util.ClasspathResource; |
22 |
|
import org.apache.tapestry.enhance.EnhancementOperation; |
23 |
|
import org.apache.tapestry.enhance.EnhancementWorker; |
24 |
|
import org.apache.tapestry.spec.IComponentSpecification; |
25 |
|
import org.apache.tapestry.util.DescribedLocation; |
26 |
|
|
27 |
|
import java.lang.annotation.Annotation; |
28 |
|
import java.lang.reflect.Method; |
29 |
|
import java.util.List; |
30 |
|
import java.util.Map; |
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
18 |
public class AnnotationEnhancementWorker implements EnhancementWorker |
42 |
|
{ |
43 |
|
private ClassResolver _classResolver; |
44 |
|
|
45 |
|
private ErrorLog _errorLog; |
46 |
|
|
47 |
|
private Map<Class, MethodAnnotationEnhancementWorker> _methodWorkers; |
48 |
|
|
49 |
|
private Map<Class, ClassAnnotationEnhancementWorker> _classWorkers; |
50 |
|
|
51 |
|
private List<SecondaryAnnotationWorker> _secondaryAnnotationWorkers; |
52 |
|
|
53 |
|
public void setClassWorkers(Map<Class, ClassAnnotationEnhancementWorker> classWorkers) |
54 |
|
{ |
55 |
6 |
_classWorkers = classWorkers; |
56 |
6 |
} |
57 |
|
|
58 |
|
public void performEnhancement(EnhancementOperation op, IComponentSpecification spec) |
59 |
|
{ |
60 |
14 |
Class clazz = op.getBaseClass(); |
61 |
|
|
62 |
14 |
Resource classResource = newClassResource(clazz); |
63 |
|
|
64 |
20 |
for (Annotation a : clazz.getAnnotations()) |
65 |
|
{ |
66 |
6 |
performClassEnhancement(op, spec, clazz, a, classResource); |
67 |
|
} |
68 |
|
|
69 |
1212 |
for (Method m : clazz.getMethods()) |
70 |
|
{ |
71 |
1198 |
performMethodEnhancement(op, spec, m, classResource); |
72 |
|
} |
73 |
14 |
} |
74 |
|
|
75 |
|
private ClasspathResource newClassResource(Class clazz) |
76 |
|
{ |
77 |
14 |
return new ClasspathResource(_classResolver, clazz.getName().replace('.', '/')); |
78 |
|
} |
79 |
|
|
80 |
|
void performClassEnhancement(EnhancementOperation op, IComponentSpecification spec, |
81 |
|
Class clazz, Annotation annotation, Resource classResource) |
82 |
|
{ |
83 |
6 |
ClassAnnotationEnhancementWorker worker = _classWorkers.get(annotation.annotationType()); |
84 |
|
|
85 |
6 |
if (worker == null) |
86 |
2 |
return; |
87 |
|
|
88 |
|
try |
89 |
|
{ |
90 |
4 |
Location location = new DescribedLocation(classResource, AnnotationMessages.classAnnotation(annotation, clazz)); |
91 |
|
|
92 |
4 |
worker.performEnhancement(op, spec, clazz, location); |
93 |
|
} |
94 |
2 |
catch (Exception ex) |
95 |
|
{ |
96 |
2 |
_errorLog.error(AnnotationMessages.failureProcessingClassAnnotation( |
97 |
|
annotation, |
98 |
|
clazz, |
99 |
|
ex), null, ex); |
100 |
2 |
} |
101 |
|
|
102 |
4 |
} |
103 |
|
|
104 |
|
void performMethodEnhancement(EnhancementOperation op, IComponentSpecification spec, |
105 |
|
Method method, Resource classResource) |
106 |
|
{ |
107 |
1590 |
for (Annotation a : method.getAnnotations()) |
108 |
|
{ |
109 |
388 |
performMethodEnhancement(op, spec, method, a, classResource); |
110 |
|
} |
111 |
|
|
112 |
|
try |
113 |
|
{ |
114 |
|
|
115 |
1202 |
for (SecondaryAnnotationWorker worker : _secondaryAnnotationWorkers) |
116 |
4 |
if (worker.canEnhance(method)) |
117 |
2 |
worker.peformEnhancement(op, spec, method, classResource); |
118 |
|
|
119 |
|
} |
120 |
2 |
catch (Exception ex) |
121 |
|
{ |
122 |
2 |
_errorLog.error(AnnotationMessages.failureEnhancingMethod(method, ex), null, ex); |
123 |
1200 |
} |
124 |
1202 |
} |
125 |
|
|
126 |
|
void performMethodEnhancement(EnhancementOperation op, IComponentSpecification spec, |
127 |
|
Method method, Annotation annotation, Resource classResource) |
128 |
|
{ |
129 |
388 |
MethodAnnotationEnhancementWorker worker = _methodWorkers.get(annotation.annotationType()); |
130 |
|
|
131 |
388 |
if (worker == null) |
132 |
382 |
return; |
133 |
|
|
134 |
|
try |
135 |
|
{ |
136 |
6 |
Location location = AnnotationUtils.buildLocationForAnnotation( |
137 |
|
method, |
138 |
|
annotation, |
139 |
|
classResource); |
140 |
6 |
worker.performEnhancement(op, spec, method, location); |
141 |
|
} |
142 |
2 |
catch (Exception ex) |
143 |
|
{ |
144 |
2 |
_errorLog.error( |
145 |
|
AnnotationMessages.failureProcessingAnnotation(annotation, method, ex), |
146 |
|
null, |
147 |
|
ex); |
148 |
4 |
} |
149 |
|
|
150 |
6 |
} |
151 |
|
|
152 |
|
public void setMethodWorkers(Map<Class, MethodAnnotationEnhancementWorker> methodWorkers) |
153 |
|
{ |
154 |
12 |
_methodWorkers = methodWorkers; |
155 |
12 |
} |
156 |
|
|
157 |
|
public void setErrorLog(ErrorLog errorLog) |
158 |
|
{ |
159 |
6 |
_errorLog = errorLog; |
160 |
6 |
} |
161 |
|
|
162 |
|
public void setClassResolver(ClassResolver classResolver) |
163 |
|
{ |
164 |
10 |
_classResolver = classResolver; |
165 |
10 |
} |
166 |
|
|
167 |
|
public void setSecondaryAnnotationWorkers(List<SecondaryAnnotationWorker> workers) |
168 |
|
{ |
169 |
18 |
_secondaryAnnotationWorkers = workers; |
170 |
18 |
} |
171 |
|
} |