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