KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > annotations > AnnotationUtils


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.beans.Introspector JavaDoc;
18 import java.lang.reflect.Method JavaDoc;
19
20 import org.apache.hivemind.ApplicationRuntimeException;
21
22 /**
23  * @author Howard M. Lewis Ship
24  * @since 4.0
25  */

26
27 public class AnnotationUtils
28 {
29     public static String JavaDoc getPropertyName(Method JavaDoc method)
30     {
31         String JavaDoc name = method.getName();
32
33         if (name.startsWith("is"))
34         {
35             checkGetter(method);
36             return Introspector.decapitalize(name.substring(2));
37         }
38
39         if (name.startsWith("get"))
40         {
41             checkGetter(method);
42             return Introspector.decapitalize(name.substring(3));
43         }
44
45         if (name.startsWith("set"))
46         {
47             checkSetter(method);
48             return Introspector.decapitalize(name.substring(3));
49         }
50
51         throw new ApplicationRuntimeException(AnnotationMessages.notAccessor(method));
52     }
53
54     private static void checkGetter(Method JavaDoc method)
55     {
56         if (method.getParameterTypes().length > 0)
57             throw new ApplicationRuntimeException(AnnotationMessages.noParametersExpected(method));
58
59         if (method.getReturnType().equals(void.class))
60             throw new ApplicationRuntimeException(AnnotationMessages.voidAccessor(method));
61
62     }
63
64     private static void checkSetter(Method JavaDoc method)
65     {
66         if (!method.getReturnType().equals(void.class))
67             throw new ApplicationRuntimeException(AnnotationMessages.nonVoidMutator(method));
68
69         if (method.getParameterTypes().length != 1)
70             throw new ApplicationRuntimeException(AnnotationMessages.wrongParameterCount(method));
71     }
72 }
73
Popular Tags