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.reflect.Method; |
18 |
|
|
19 |
|
import org.apache.hivemind.ApplicationRuntimeException; |
20 |
|
import org.apache.hivemind.ClassResolver; |
21 |
|
import org.apache.hivemind.Location; |
22 |
|
import org.apache.hivemind.Resource; |
23 |
|
import org.apache.hivemind.util.ClasspathResource; |
24 |
|
import org.apache.tapestry.enhance.EnhancementOperation; |
25 |
|
import org.apache.tapestry.enhance.EnhancementWorker; |
26 |
|
import org.apache.tapestry.enhance.InjectAssetWorker; |
27 |
|
import org.apache.tapestry.spec.IAssetSpecification; |
28 |
|
import org.apache.tapestry.spec.IComponentSpecification; |
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
public class InjectAssetAnnotationWorker implements EnhancementWorker |
39 |
|
{ |
40 |
|
InjectAssetWorker _delegate; |
41 |
|
|
42 |
|
private ClassResolver _classResolver; |
43 |
|
|
44 |
|
InjectAssetAnnotationWorker(InjectAssetWorker delegate) |
45 |
3 |
{ |
46 |
3 |
_delegate = delegate; |
47 |
3 |
} |
48 |
|
|
49 |
|
public InjectAssetAnnotationWorker() |
50 |
|
{ |
51 |
1 |
this(new InjectAssetWorker()); |
52 |
1 |
} |
53 |
|
|
54 |
|
public void performEnhancement(EnhancementOperation op, IComponentSpecification spec) |
55 |
|
{ |
56 |
0 |
Class clazz = op.getBaseClass(); |
57 |
|
|
58 |
0 |
Resource classResource = newClassResource(clazz); |
59 |
|
|
60 |
0 |
for (Method m : clazz.getMethods()) |
61 |
|
{ |
62 |
0 |
if (m.getAnnotation(InjectAsset.class) != null) { |
63 |
|
|
64 |
0 |
performEnhancement(op, spec, m, |
65 |
|
AnnotationUtils.buildLocationForAnnotation( |
66 |
|
m, |
67 |
|
m.getAnnotation(InjectAsset.class), |
68 |
|
classResource)); |
69 |
|
} |
70 |
|
} |
71 |
0 |
} |
72 |
|
|
73 |
|
private ClasspathResource newClassResource(Class clazz) |
74 |
|
{ |
75 |
0 |
return new ClasspathResource(_classResolver, clazz.getName().replace('.', '/')); |
76 |
|
} |
77 |
|
|
78 |
|
public void performEnhancement(EnhancementOperation op, IComponentSpecification spec, Method method, Location location) |
79 |
|
{ |
80 |
2 |
InjectAsset as = method.getAnnotation(InjectAsset.class); |
81 |
|
|
82 |
2 |
IAssetSpecification asset = spec.getAsset(as.value()); |
83 |
2 |
if (asset == null) { |
84 |
|
|
85 |
1 |
throw new ApplicationRuntimeException(AnnotationMessages.unknownAsset(as.value(), location)); |
86 |
|
} |
87 |
|
|
88 |
1 |
String propertyName = AnnotationUtils.getPropertyName(method); |
89 |
|
|
90 |
1 |
_delegate.injectAsset(op, as.value(), propertyName, location); |
91 |
1 |
} |
92 |
|
|
93 |
|
public void setClassResolver(ClassResolver classResolver) |
94 |
|
{ |
95 |
2 |
_classResolver = classResolver; |
96 |
2 |
} |
97 |
|
} |