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 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| public class AnnotationEnhancementWorker implements EnhancementWorker |
40 |
| { |
41 |
| private ClassResolver _classResolver; |
42 |
| |
43 |
| private ErrorLog _errorLog; |
44 |
| |
45 |
| private Map _methodWorkers; |
46 |
| |
47 |
| private Map _classWorkers; |
48 |
| |
49 |
6
| public void setClassWorkers(Map classWorkers)
|
50 |
| { |
51 |
6
| _classWorkers = classWorkers;
|
52 |
| } |
53 |
| |
54 |
14
| public void performEnhancement(EnhancementOperation op, IComponentSpecification spec)
|
55 |
| { |
56 |
14
| Class clazz = op.getBaseClass();
|
57 |
| |
58 |
14
| Resource classResource = newClassResource(clazz);
|
59 |
| |
60 |
14
| for (Annotation a : clazz.getAnnotations())
|
61 |
| { |
62 |
6
| performClassEnhancement(op, spec, clazz, a, classResource);
|
63 |
| } |
64 |
| |
65 |
14
| for (Method m : clazz.getMethods())
|
66 |
| { |
67 |
982
| performMethodEnhancement(op, spec, m, classResource);
|
68 |
| } |
69 |
| } |
70 |
| |
71 |
14
| private ClasspathResource newClassResource(Class clazz)
|
72 |
| { |
73 |
14
| return new ClasspathResource(_classResolver, clazz.getName().replace('.', '/'));
|
74 |
| } |
75 |
| |
76 |
6
| void performClassEnhancement(EnhancementOperation op, IComponentSpecification spec,
|
77 |
| Class clazz, Annotation annotation, Resource classResource) |
78 |
| { |
79 |
6
| ClassAnnotationEnhancementWorker worker = (ClassAnnotationEnhancementWorker) _classWorkers
|
80 |
| .get(annotation.annotationType()); |
81 |
| |
82 |
6
| if (worker == null)
|
83 |
2
| return;
|
84 |
| |
85 |
4
| try
|
86 |
| { |
87 |
4
| Location location = new AnnotationLocation(classResource, AnnotationMessages
|
88 |
| .classAnnotation(annotation, clazz)); |
89 |
| |
90 |
4
| worker.performEnhancement(op, spec, clazz, location);
|
91 |
| } |
92 |
| catch (Exception ex) |
93 |
| { |
94 |
2
| _errorLog.error(AnnotationMessages.failureProcessingClassAnnotation(
|
95 |
| annotation, |
96 |
| clazz, |
97 |
| ex), null, ex); |
98 |
| } |
99 |
| |
100 |
| } |
101 |
| |
102 |
982
| void performMethodEnhancement(EnhancementOperation op, IComponentSpecification spec,
|
103 |
| Method method, Resource classResource) |
104 |
| { |
105 |
982
| for (Annotation a : method.getAnnotations())
|
106 |
| { |
107 |
264
| performMethodEnhancement(op, spec, method, a, classResource);
|
108 |
| } |
109 |
| } |
110 |
| |
111 |
264
| void performMethodEnhancement(EnhancementOperation op, IComponentSpecification spec,
|
112 |
| Method method, Annotation annotation, Resource classResource) |
113 |
| { |
114 |
264
| MethodAnnotationEnhancementWorker worker = (MethodAnnotationEnhancementWorker) _methodWorkers
|
115 |
| .get(annotation.annotationType()); |
116 |
| |
117 |
264
| if (worker == null)
|
118 |
258
| return;
|
119 |
| |
120 |
6
| try
|
121 |
| { |
122 |
6
| Location location = new AnnotationLocation(classResource, AnnotationMessages
|
123 |
| .methodAnnotation(annotation, method)); |
124 |
6
| worker.performEnhancement(op, spec, method, location);
|
125 |
| } |
126 |
| catch (Exception ex) |
127 |
| { |
128 |
2
| _errorLog.error(
|
129 |
| AnnotationMessages.failureProcessingAnnotation(annotation, method, ex), |
130 |
| null, |
131 |
| ex); |
132 |
| } |
133 |
| |
134 |
| } |
135 |
| |
136 |
8
| public void setMethodWorkers(Map methodWorkers)
|
137 |
| { |
138 |
8
| _methodWorkers = methodWorkers;
|
139 |
| } |
140 |
| |
141 |
4
| public void setErrorLog(ErrorLog errorLog)
|
142 |
| { |
143 |
4
| _errorLog = errorLog;
|
144 |
| } |
145 |
| |
146 |
10
| public void setClassResolver(ClassResolver classResolver)
|
147 |
| { |
148 |
10
| _classResolver = classResolver;
|
149 |
| } |
150 |
| } |