Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill another out of date script
[simgrid.git] / contrib / psg / src / peersim / vector / GetterSetterFinder.java
1 /*
2  * Copyright (c) 2003-2005 The BISON Project
3  *
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.
7  *
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.
12  *
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.
16  *
17  */
18
19 package peersim.vector;
20
21 import java.lang.reflect.*;
22 import java.util.*;
23
24 /**
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. 
30  */
31 class GetterSetterFinder
32 {
33
34 //--------------------------------------------------------------------------
35
36 /**
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
40  * getter methods. 
41  * 
42  * @param clazz
43  *          the class where to find get/set method
44  * @param methodName
45  *          the method to be searched
46  * @return the requested method
47  */
48 public static Method getGetterMethod(Class clazz, String methodName)
49                 throws NoSuchMethodException
50 {
51         // Search methods
52         Method[] methods = clazz.getMethods();
53         ArrayList<Method> list = new ArrayList<Method>();
54         for (Method m: methods) {
55                 if (m.getName().equals(methodName)) {
56                         list.add(m);
57                 }
58         }
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());
65         }
66         
67         // Found a single method with the right name; check if
68         // it is a gettter.
69         Method method = list.get(0);
70         Class[] pars = method.getParameterTypes();
71         if (pars.length > 0) {
72                 throw new NoSuchMethodException(method.getName() + " of class "
73                 + clazz.getName()
74                 + " is not a valid getter method: "+
75                 "its argument list is not empty");
76         }
77         
78         Class ret = method.getReturnType();
79         if (    !( ret==int.class || ret==long.class ||
80                 ret==double.class || ret==float.class || ret==boolean.class)
81         ) {
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");
85         }
86         
87         return method;
88 }
89
90 //--------------------------------------------------------------------------
91
92 /**
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
97  * setter methods. 
98  * @param clazz
99  *          the class where to find get/set method
100  * @param methodName
101  *          the method to be searched
102  * @return the requested method, if it fully conforms to the definition of
103  * the setter methods.
104  */
105 public static Method getSetterMethod(Class clazz, String methodName)
106                 throws NoSuchMethodException
107 {
108         // Search methods
109         Method[] methods = clazz.getMethods();
110         ArrayList<Method> list = new ArrayList<Method>();
111         for (Method m: methods) {
112                 if (m.getName().equals(methodName)) {
113                         list.add(m);
114                 }
115         }
116         
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());
123         }
124         
125         // Found a single method with the right name; check if
126         // it is a setter.
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 )
132         ) {
133                 throw new NoSuchMethodException(method.getName() + " of class "
134                 + clazz.getName()
135                 + " is not a valid setter method: "+
136                 "it should have exactly one argument of type "+
137                 "int, long, short or double");
138         }
139         
140         Class ret = method.getReturnType();
141         if (!ret.equals(void.class)) {
142                 throw new NoSuchMethodException(method.getName() + "  of class "
143                 + clazz.getName() +
144                 " is not a valid setter method; it returns a value");
145         }
146         
147         return method;
148 }
149
150 //--------------------------------------------------------------------------
151
152
153 /**
154  * Returns the field type for the specified getter.
155  */
156 public static Class getGetterType(Method m)
157 {
158         return m.getReturnType();
159 }
160
161 //--------------------------------------------------------------------------
162
163 /**
164  * Returns the field type for the specified setter.
165  */
166 public static Class getSetterType(Method m)
167 {
168         Class[] pars = m.getParameterTypes();
169         return pars[0];
170 }
171
172 //--------------------------------------------------------------------------
173
174 }