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