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