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 | |
import java.lang.reflect.Modifier; |
19 | |
|
20 | |
import org.apache.hivemind.ApplicationRuntimeException; |
21 | |
import org.apache.hivemind.Location; |
22 | |
import org.apache.hivemind.service.BodyBuilder; |
23 | |
import org.apache.hivemind.service.MethodSignature; |
24 | |
import org.apache.tapestry.Tapestry; |
25 | |
import org.apache.tapestry.enhance.EnhancementOperation; |
26 | |
import org.apache.tapestry.spec.IComponentSpecification; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | 0 | public class MessageAnnotationWorker implements MethodAnnotationEnhancementWorker |
35 | |
{ |
36 | |
|
37 | |
public void performEnhancement(EnhancementOperation op, IComponentSpecification spec, |
38 | |
Method method, Location location) |
39 | |
{ |
40 | 0 | if (!method.getReturnType().equals(String.class)) |
41 | 0 | throw new ApplicationRuntimeException(AnnotationMessages.returnStringOnly(method |
42 | |
.getReturnType())); |
43 | |
|
44 | 0 | Message message = method.getAnnotation(Message.class); |
45 | |
|
46 | 0 | String keyName = message.value(); |
47 | |
|
48 | 0 | if (keyName.equals("")) |
49 | 0 | keyName = AnnotationUtils.convertMethodNameToKeyName(method.getName()); |
50 | |
|
51 | 0 | BodyBuilder builder = new BodyBuilder(); |
52 | |
|
53 | 0 | builder.begin(); |
54 | |
|
55 | 0 | Class[] parameterTypes = method.getParameterTypes(); |
56 | 0 | int paramCount = Tapestry.size(parameterTypes); |
57 | |
|
58 | 0 | if (paramCount > 0) |
59 | |
{ |
60 | 0 | builder.addln("java.lang.Object[] params = new java.lang.Object[{0}];", paramCount); |
61 | 0 | for (int i = 0; i < paramCount; i++) |
62 | |
{ |
63 | 0 | builder.add("params[{0}] = ", i); |
64 | |
|
65 | 0 | if (parameterTypes[i].isPrimitive()) |
66 | 0 | builder.add("($w) "); |
67 | |
|
68 | |
|
69 | |
|
70 | 0 | builder.addln("${0};", i + 1); |
71 | |
} |
72 | |
} |
73 | |
|
74 | 0 | builder.add("return getMessages()."); |
75 | |
|
76 | 0 | if (paramCount == 0) |
77 | 0 | builder.addln("getMessage(\"{0}\");", keyName); |
78 | |
else |
79 | 0 | builder.addln("format(\"{0}\", params);", keyName); |
80 | |
|
81 | 0 | builder.end(); |
82 | |
|
83 | 0 | op.addMethod(Modifier.PUBLIC, new MethodSignature(method), builder.toString(), location); |
84 | |
|
85 | 0 | if (isGetter(method)) |
86 | 0 | op.claimReadonlyProperty(AnnotationUtils.getPropertyName(method)); |
87 | 0 | } |
88 | |
|
89 | |
boolean isGetter(Method method) |
90 | |
{ |
91 | |
|
92 | |
|
93 | 0 | return method.getName().startsWith("get") && method.getParameterTypes().length == 0; |
94 | |
} |
95 | |
} |