1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.annotations; |
16 |
|
|
17 |
|
import java.beans.Introspector; |
18 |
|
import java.lang.annotation.Annotation; |
19 |
|
import java.lang.reflect.Method; |
20 |
|
import java.util.Iterator; |
21 |
|
|
22 |
|
import org.apache.hivemind.ApplicationRuntimeException; |
23 |
|
import org.apache.hivemind.Location; |
24 |
|
import org.apache.hivemind.Resource; |
25 |
|
import org.apache.tapestry.spec.IBindingSpecification; |
26 |
|
import org.apache.tapestry.spec.IContainedComponent; |
27 |
|
import org.apache.tapestry.util.DescribedLocation; |
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
public final class AnnotationUtils |
35 |
|
{ |
36 |
|
|
37 |
0 |
private AnnotationUtils() { } |
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
public static String getPropertyName(Method method) |
50 |
|
{ |
51 |
46 |
String name = method.getName(); |
52 |
|
|
53 |
46 |
if (name.startsWith("is")) |
54 |
|
{ |
55 |
2 |
checkGetter(method); |
56 |
1 |
return Introspector.decapitalize(name.substring(2)); |
57 |
|
} |
58 |
|
|
59 |
44 |
if (name.startsWith("get")) |
60 |
|
{ |
61 |
40 |
checkGetter(method); |
62 |
39 |
return Introspector.decapitalize(name.substring(3)); |
63 |
|
} |
64 |
|
|
65 |
4 |
if (name.startsWith("set")) |
66 |
|
{ |
67 |
3 |
checkSetter(method); |
68 |
1 |
return Introspector.decapitalize(name.substring(3)); |
69 |
|
} |
70 |
|
|
71 |
1 |
throw new ApplicationRuntimeException(AnnotationMessages.notAccessor(method)); |
72 |
|
} |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
public static String convertMethodNameToKeyName(String methodName) |
86 |
|
{ |
87 |
14 |
StringBuffer buffer = new StringBuffer(); |
88 |
|
|
89 |
14 |
int cursorx = methodName.startsWith("get") ? 3 : 0; |
90 |
14 |
int length = methodName.length(); |
91 |
14 |
boolean atStart = true; |
92 |
|
|
93 |
162 |
while (cursorx < length) |
94 |
|
{ |
95 |
148 |
char ch = methodName.charAt(cursorx); |
96 |
|
|
97 |
148 |
if (Character.isUpperCase(ch)) |
98 |
|
{ |
99 |
23 |
if (!atStart) |
100 |
17 |
buffer.append('-'); |
101 |
23 |
buffer.append(Character.toLowerCase(ch)); |
102 |
|
} |
103 |
|
else |
104 |
125 |
buffer.append(ch); |
105 |
|
|
106 |
148 |
atStart = false; |
107 |
|
|
108 |
148 |
cursorx++; |
109 |
148 |
} |
110 |
|
|
111 |
14 |
return buffer.toString(); |
112 |
|
} |
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
public static void copyBindings(IContainedComponent source, IContainedComponent target) |
122 |
|
{ |
123 |
2 |
Iterator i = source.getBindingNames().iterator(); |
124 |
5 |
while (i.hasNext()) |
125 |
|
{ |
126 |
3 |
String bindingName = (String) i.next(); |
127 |
3 |
IBindingSpecification binding = source.getBinding(bindingName); |
128 |
3 |
target.setBinding(bindingName, binding); |
129 |
3 |
} |
130 |
|
|
131 |
2 |
target.setType(source.getType()); |
132 |
2 |
} |
133 |
|
|
134 |
|
private static void checkGetter(Method method) |
135 |
|
{ |
136 |
42 |
if (method.getParameterTypes().length > 0) |
137 |
1 |
throw new ApplicationRuntimeException(AnnotationMessages.noParametersExpected(method)); |
138 |
|
|
139 |
41 |
if (method.getReturnType().equals(void.class)) |
140 |
1 |
throw new ApplicationRuntimeException(AnnotationMessages.voidAccessor(method)); |
141 |
|
|
142 |
40 |
} |
143 |
|
|
144 |
|
private static void checkSetter(Method method) |
145 |
|
{ |
146 |
3 |
if (!method.getReturnType().equals(void.class)) |
147 |
1 |
throw new ApplicationRuntimeException(AnnotationMessages.nonVoidMutator(method)); |
148 |
|
|
149 |
2 |
if (method.getParameterTypes().length != 1) |
150 |
1 |
throw new ApplicationRuntimeException(AnnotationMessages.wrongParameterCount(method)); |
151 |
1 |
} |
152 |
|
|
153 |
|
public static Location buildLocationForAnnotation(Method method, Annotation annotation, |
154 |
|
Resource classResource) |
155 |
|
{ |
156 |
14 |
return new DescribedLocation(classResource, AnnotationMessages.methodAnnotation( |
157 |
|
annotation, |
158 |
|
method)); |
159 |
|
} |
160 |
|
} |