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 |
| public class MessageAnnotationWorker implements MethodAnnotationEnhancementWorker |
35 |
| { |
36 |
| |
37 |
18
| public void performEnhancement(EnhancementOperation op, IComponentSpecification spec,
|
38 |
| Method method, Location location) |
39 |
| { |
40 |
18
| if (!method.getReturnType().equals(String.class))
|
41 |
2
| throw new ApplicationRuntimeException(AnnotationMessages.returnStringOnly(method
|
42 |
| .getReturnType())); |
43 |
| |
44 |
16
| Message message = method.getAnnotation(Message.class);
|
45 |
| |
46 |
16
| String keyName = message.value();
|
47 |
| |
48 |
16
| if (keyName.equals(""))
|
49 |
14
| keyName = convertMethodNameToKeyName(method.getName());
|
50 |
| |
51 |
16
| BodyBuilder builder = new BodyBuilder();
|
52 |
| |
53 |
16
| builder.begin();
|
54 |
| |
55 |
16
| Class[] parameterTypes = method.getParameterTypes();
|
56 |
16
| int paramCount = Tapestry.size(parameterTypes);
|
57 |
| |
58 |
16
| if (paramCount > 0)
|
59 |
| { |
60 |
8
| builder.addln("java.lang.Object[] params = new java.lang.Object[{0}];", paramCount);
|
61 |
8
| for (int i = 0; i < paramCount; i++)
|
62 |
| { |
63 |
12
| builder.add("params[{0}] = ", i);
|
64 |
| |
65 |
12
| if (parameterTypes[i].isPrimitive())
|
66 |
6
| builder.add("($w) ");
|
67 |
| |
68 |
| |
69 |
| |
70 |
12
| builder.addln("${0};", i + 1);
|
71 |
| } |
72 |
| } |
73 |
| |
74 |
16
| builder.add("return getMessages().");
|
75 |
| |
76 |
16
| if (paramCount == 0)
|
77 |
8
| builder.addln("getMessage(\"{0}\");", keyName);
|
78 |
| else |
79 |
8
| builder.addln("format(\"{0}\", params);", keyName);
|
80 |
| |
81 |
16
| builder.end();
|
82 |
| |
83 |
16
| op.addMethod(Modifier.PUBLIC, new MethodSignature(method), builder.toString(), location);
|
84 |
| |
85 |
16
| if (isGetter(method))
|
86 |
2
| op.claimReadonlyProperty(AnnotationUtils.getPropertyName(method));
|
87 |
| } |
88 |
| |
89 |
16
| boolean isGetter(Method method)
|
90 |
| { |
91 |
| |
92 |
| |
93 |
16
| return method.getName().startsWith("get") && method.getParameterTypes().length == 0;
|
94 |
| } |
95 |
| |
96 |
22
| String convertMethodNameToKeyName(String methodName)
|
97 |
| { |
98 |
22
| StringBuffer buffer = new StringBuffer();
|
99 |
| |
100 |
22
| int cursorx = methodName.startsWith("get") ? 3 : 0;
|
101 |
22
| int length = methodName.length();
|
102 |
22
| boolean atStart = true;
|
103 |
| |
104 |
22
| while (cursorx < length)
|
105 |
| { |
106 |
238
| char ch = methodName.charAt(cursorx);
|
107 |
| |
108 |
238
| if (Character.isUpperCase(ch))
|
109 |
| { |
110 |
32
| if (!atStart)
|
111 |
26
| buffer.append('-');
|
112 |
32
| buffer.append(Character.toLowerCase(ch));
|
113 |
| } |
114 |
| else |
115 |
206
| buffer.append(ch);
|
116 |
| |
117 |
238
| atStart = false;
|
118 |
| |
119 |
238
| cursorx++;
|
120 |
| } |
121 |
| |
122 |
22
| return buffer.toString();
|
123 |
| } |
124 |
| } |