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 | 0 | String name = method.getName(); |
52 | |
|
53 | 0 | if (name.startsWith("is")) |
54 | |
{ |
55 | 0 | checkGetter(method); |
56 | 0 | return Introspector.decapitalize(name.substring(2)); |
57 | |
} |
58 | |
|
59 | 0 | if (name.startsWith("get")) |
60 | |
{ |
61 | 0 | checkGetter(method); |
62 | 0 | return Introspector.decapitalize(name.substring(3)); |
63 | |
} |
64 | |
|
65 | 0 | if (name.startsWith("set")) |
66 | |
{ |
67 | 0 | checkSetter(method); |
68 | 0 | return Introspector.decapitalize(name.substring(3)); |
69 | |
} |
70 | |
|
71 | 0 | 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 | 0 | StringBuffer buffer = new StringBuffer(); |
88 | |
|
89 | 0 | int cursorx = methodName.startsWith("get") ? 3 : 0; |
90 | 0 | int length = methodName.length(); |
91 | 0 | boolean atStart = true; |
92 | |
|
93 | 0 | while (cursorx < length) |
94 | |
{ |
95 | 0 | char ch = methodName.charAt(cursorx); |
96 | |
|
97 | 0 | if (Character.isUpperCase(ch)) |
98 | |
{ |
99 | 0 | if (!atStart) |
100 | 0 | buffer.append('-'); |
101 | 0 | buffer.append(Character.toLowerCase(ch)); |
102 | |
} |
103 | |
else |
104 | 0 | buffer.append(ch); |
105 | |
|
106 | 0 | atStart = false; |
107 | |
|
108 | 0 | cursorx++; |
109 | 0 | } |
110 | |
|
111 | 0 | return buffer.toString(); |
112 | |
} |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
public static void copyBindings(IContainedComponent source, IContainedComponent target) |
122 | |
{ |
123 | 0 | Iterator i = source.getBindingNames().iterator(); |
124 | 0 | while (i.hasNext()) |
125 | |
{ |
126 | 0 | String bindingName = (String) i.next(); |
127 | 0 | IBindingSpecification binding = source.getBinding(bindingName); |
128 | 0 | target.setBinding(bindingName, binding); |
129 | 0 | } |
130 | |
|
131 | 0 | target.setType(source.getType()); |
132 | 0 | } |
133 | |
|
134 | |
private static void checkGetter(Method method) |
135 | |
{ |
136 | 0 | if (method.getParameterTypes().length > 0) |
137 | 0 | throw new ApplicationRuntimeException(AnnotationMessages.noParametersExpected(method)); |
138 | |
|
139 | 0 | if (method.getReturnType().equals(void.class)) |
140 | 0 | throw new ApplicationRuntimeException(AnnotationMessages.voidAccessor(method)); |
141 | |
|
142 | 0 | } |
143 | |
|
144 | |
private static void checkSetter(Method method) |
145 | |
{ |
146 | 0 | if (!method.getReturnType().equals(void.class)) |
147 | 0 | throw new ApplicationRuntimeException(AnnotationMessages.nonVoidMutator(method)); |
148 | |
|
149 | 0 | if (method.getParameterTypes().length != 1) |
150 | 0 | throw new ApplicationRuntimeException(AnnotationMessages.wrongParameterCount(method)); |
151 | 0 | } |
152 | |
|
153 | |
public static Location buildLocationForAnnotation(Method method, Annotation annotation, |
154 | |
Resource classResource) |
155 | |
{ |
156 | 0 | return new DescribedLocation(classResource, AnnotationMessages.methodAnnotation( |
157 | |
annotation, |
158 | |
method)); |
159 | |
} |
160 | |
} |