2
* This program is free software; you can redistribute it and/or modify it under
3
* the terms of the GNU General Public License as published by the Free Software
4
* Foundation; either version 2 of the License, or (at your option) any later
5
* version. This program is distributed in the hope that it will be useful, but
6
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
8
* details. You should have received a copy of the GNU General Public License
9
* along with this program; if not, write to the Free Software Foundation, Inc.,
10
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
13
package games.strategy.engine.message;
15
import games.strategy.util.Tuple;
17
import java.lang.reflect.Method;
18
import java.util.Arrays;
19
import java.util.Comparator;
20
import java.util.logging.Level;
21
import java.util.logging.Logger;
23
public class RemoteInterfaceHelper
25
private static final Logger s_logger = Logger.getLogger(RemoteInterfaceHelper.class.getName());
28
public static int getNumber(String methodName, Class<?>[] argTypes, Class<?> remoteInterface)
30
Method[] methods = remoteInterface.getMethods();
31
Arrays.sort(methods, methodComparator);
33
if(s_logger.isLoggable(Level.FINEST)) {
34
s_logger.fine("Sorted methods:" + Arrays.asList(methods));
37
for(int i =0; i < methods.length; i++)
39
if(methods[i].getName().equals(methodName))
41
Class<?>[] types = methods[i].getParameterTypes();
47
else if(types != null && argTypes != null && types.length == argTypes.length)
50
for(int j = 0; j < argTypes.length; j++)
52
if(!argTypes[j].equals(types[j]))
63
throw new IllegalStateException("Method not found");
66
public static Tuple<String, Class<?>[]> getMethodInfo(int methodNumber, Class<?> remoteInterface)
68
Method[] methods = remoteInterface.getMethods();
69
Arrays.sort(methods, methodComparator);
71
if(s_logger.isLoggable(Level.FINEST)) {
72
s_logger.fine("Sorted methods:" + Arrays.asList(methods));
74
return new Tuple<String, Class<?>[]>(methods[methodNumber].getName(), methods[methodNumber].getParameterTypes());
79
* get methods does not guarantee an order, so sort.
81
private static Comparator<Method> methodComparator = new Comparator<Method>()
84
public int compare(Method o1, Method o2)
89
if(!o1.getName().equals(o2.getName()))
90
return o1.getName().compareTo(o2.getName());
93
Class<?>[] t1 = o1.getParameterTypes();
94
Class<?>[] t2 = o2.getParameterTypes();
104
if(t1.length != t2.length)
105
return t1.length - t2.length;
106
for(int i =0; i < t1.length; i++)
108
if(!t1[i].getName().equals(t2[i].getName()))
110
return t1[i].getName().compareTo(t2[i].getName());