2 * Copyright (c) 2003-2005 The BISON Project
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 package peersim.vector;
21 import java.lang.reflect.*;
25 * This utility class can be used to obtain get/set methods from classes. In
26 * particular, it is used in the vector package to locate get/set methods for
27 * observing and modifying protocol fields.
28 * Please refer to package {@link peersim.vector} for a definition of
29 * getter and setter methods.
31 class GetterSetterFinder
34 //--------------------------------------------------------------------------
37 * Search a getter method in the specified class. It succeeds only of there
38 * is exactly one method with the given name that is a getter method.
39 * Please refer to package {@link peersim.vector} for a definition of
43 * the class where to find get/set method
45 * the method to be searched
46 * @return the requested method
48 public static Method getGetterMethod(Class clazz, String methodName)
49 throws NoSuchMethodException
52 Method[] methods = clazz.getMethods();
53 ArrayList<Method> list = new ArrayList<Method>();
54 for (Method m: methods) {
55 if (m.getName().equals(methodName)) {
59 if (list.size() == 0) {
60 throw new NoSuchMethodException("No getter method for method "
61 + methodName + " in class " + clazz.getName());
62 } else if (list.size() > 1) {
63 throw new NoSuchMethodException("Multiple getter for method "
64 + methodName + " in class " + clazz.getName());
67 // Found a single method with the right name; check if
69 Method method = list.get(0);
70 Class[] pars = method.getParameterTypes();
71 if (pars.length > 0) {
72 throw new NoSuchMethodException(method.getName() + " of class "
74 + " is not a valid getter method: "+
75 "its argument list is not empty");
78 Class ret = method.getReturnType();
79 if ( !( ret==int.class || ret==long.class ||
80 ret==double.class || ret==float.class || ret==boolean.class)
82 throw new NoSuchMethodException(method.getName() + " of class "
83 + clazz.getName() + " is not a valid getter method: "+
84 "it should have a return type of int, long, short or double");
90 //--------------------------------------------------------------------------
93 * Search a setter method in the specified class.
94 * It succeeds only of there
95 * is exactly one method with the given name that is a setter method.
96 * Please refer to package {@link peersim.vector} for a definition of
99 * the class where to find get/set method
101 * the method to be searched
102 * @return the requested method, if it fully conforms to the definition of
103 * the setter methods.
105 public static Method getSetterMethod(Class clazz, String methodName)
106 throws NoSuchMethodException
109 Method[] methods = clazz.getMethods();
110 ArrayList<Method> list = new ArrayList<Method>();
111 for (Method m: methods) {
112 if (m.getName().equals(methodName)) {
117 if (list.size() == 0) {
118 throw new NoSuchMethodException("No setter method for method "
119 + methodName + " in class " + clazz.getName());
120 } else if (list.size() > 1) {
121 throw new NoSuchMethodException("Multiple setter for method "
122 + methodName + " in class " + clazz.getName());
125 // Found a single method with the right name; check if
127 Method method = list.get(0);
128 Class[] pars = method.getParameterTypes();
129 if ( pars.length != 1 ||
130 !( pars[0]==int.class || pars[0]==long.class ||
131 pars[0]==double.class || pars[0]==float.class )
133 throw new NoSuchMethodException(method.getName() + " of class "
135 + " is not a valid setter method: "+
136 "it should have exactly one argument of type "+
137 "int, long, short or double");
140 Class ret = method.getReturnType();
141 if (!ret.equals(void.class)) {
142 throw new NoSuchMethodException(method.getName() + " of class "
144 " is not a valid setter method; it returns a value");
150 //--------------------------------------------------------------------------
154 * Returns the field type for the specified getter.
156 public static Class getGetterType(Method m)
158 return m.getReturnType();
161 //--------------------------------------------------------------------------
164 * Returns the field type for the specified setter.
166 public static Class getSetterType(Method m)
168 Class[] pars = m.getParameterTypes();
172 //--------------------------------------------------------------------------