|
|||||||||||||||||||
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
MessageAnnotationWorker.java | 100% | 100% | 100% | 100% |
|
1 | // Copyright 2005 The Apache Software Foundation | |
2 | // | |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | // you may not use this file except in compliance with the License. | |
5 | // You may obtain a copy of the License at | |
6 | // | |
7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
8 | // | |
9 | // Unless required by applicable law or agreed to in writing, software | |
10 | // distributed under the License is distributed on an "AS IS" BASIS, | |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | // See the License for the specific language governing permissions and | |
13 | // limitations under the License. | |
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 | * Builds a method that accesses component messages. | |
30 | * | |
31 | * @author Howard Lewis Ship | |
32 | * @since 4.0 | |
33 | */ | |
34 | public class MessageAnnotationWorker implements MethodAnnotationEnhancementWorker | |
35 | { | |
36 | ||
37 | 9 | public void performEnhancement(EnhancementOperation op, IComponentSpecification spec, |
38 | Method method, Location location) | |
39 | { | |
40 | 9 | if (!method.getReturnType().equals(String.class)) |
41 | 1 | throw new ApplicationRuntimeException(AnnotationMessages.returnStringOnly(method |
42 | .getReturnType())); | |
43 | ||
44 | 8 | Message message = method.getAnnotation(Message.class); |
45 | ||
46 | 8 | String keyName = message.value(); |
47 | ||
48 | 8 | if (keyName.equals("")) |
49 | 7 | keyName = convertMethodNameToKeyName(method.getName()); |
50 | ||
51 | 8 | BodyBuilder builder = new BodyBuilder(); |
52 | ||
53 | 8 | builder.begin(); |
54 | ||
55 | 8 | Class[] parameterTypes = method.getParameterTypes(); |
56 | 8 | int paramCount = Tapestry.size(parameterTypes); |
57 | ||
58 | 8 | if (paramCount > 0) |
59 | { | |
60 | 4 | builder.addln("java.lang.Object[] params = new java.lang.Object[{0}];", paramCount); |
61 | 4 | for (int i = 0; i < paramCount; i++) |
62 | { | |
63 | 6 | builder.add("params[{0}] = ", i); |
64 | ||
65 | 6 | if (parameterTypes[i].isPrimitive()) |
66 | 3 | builder.add("($w) "); |
67 | ||
68 | // Parameter $0 is this, so $1 is the first real parameter. | |
69 | ||
70 | 6 | builder.addln("${0};", i + 1); |
71 | } | |
72 | } | |
73 | ||
74 | 8 | builder.add("return getMessages()."); |
75 | ||
76 | 8 | if (paramCount == 0) |
77 | 4 | builder.addln("getMessage(\"{0}\");", keyName); |
78 | else | |
79 | 4 | builder.addln("format(\"{0}\", params);", keyName); |
80 | ||
81 | 8 | builder.end(); |
82 | ||
83 | 8 | op.addMethod(Modifier.PUBLIC, new MethodSignature(method), builder.toString()); |
84 | ||
85 | 8 | if (isGetter(method)) |
86 | 1 | op.claimProperty(AnnotationUtils.getPropertyName(method)); |
87 | } | |
88 | ||
89 | 8 | boolean isGetter(Method method) |
90 | { | |
91 | // We already know the return type is String | |
92 | ||
93 | 8 | return method.getName().startsWith("get") && method.getParameterTypes().length == 0; |
94 | } | |
95 | ||
96 | 11 | String convertMethodNameToKeyName(String methodName) |
97 | { | |
98 | 11 | StringBuffer buffer = new StringBuffer(); |
99 | ||
100 | 11 | int cursorx = methodName.startsWith("get") ? 3 : 0; |
101 | 11 | int length = methodName.length(); |
102 | 11 | boolean atStart = true; |
103 | ||
104 | 11 | while (cursorx < length) |
105 | { | |
106 | 119 | char ch = methodName.charAt(cursorx); |
107 | ||
108 | 119 | if (Character.isUpperCase(ch)) |
109 | { | |
110 | 16 | if (!atStart) |
111 | 13 | buffer.append('-'); |
112 | 16 | buffer.append(Character.toLowerCase(ch)); |
113 | } | |
114 | else | |
115 | 103 | buffer.append(ch); |
116 | ||
117 | 119 | atStart = false; |
118 | ||
119 | 119 | cursorx++; |
120 | } | |
121 | ||
122 | 11 | return buffer.toString(); |
123 | } | |
124 | } |
|