~ubuntu-branches/ubuntu/precise/triplea/precise

« back to all changes in this revision

Viewing changes to src/games/strategy/engine/message/RemoteInterfaceHelper.java

  • Committer: Package Import Robot
  • Author(s): Scott Howard
  • Date: 2011-11-11 21:40:11 UTC
  • Revision ID: package-import@ubuntu.com-20111111214011-sehf2rwat36o2xqf
Tags: upstream-1.3.2.2
ImportĀ upstreamĀ versionĀ 1.3.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
 
11
 */
 
12
 
 
13
package games.strategy.engine.message;
 
14
 
 
15
import games.strategy.util.Tuple;
 
16
 
 
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;
 
22
 
 
23
public class RemoteInterfaceHelper
 
24
{
 
25
    private static final Logger s_logger = Logger.getLogger(RemoteInterfaceHelper.class.getName());
 
26
    
 
27
 
 
28
    public static int getNumber(String methodName, Class<?>[] argTypes, Class<?> remoteInterface)
 
29
    {
 
30
        Method[] methods = remoteInterface.getMethods();
 
31
        Arrays.sort(methods, methodComparator);
 
32
 
 
33
         if(s_logger.isLoggable(Level.FINEST)) {
 
34
            s_logger.fine("Sorted methods:" + Arrays.asList(methods));
 
35
        } 
 
36
    
 
37
        for(int i =0; i < methods.length; i++)
 
38
        {
 
39
            if(methods[i].getName().equals(methodName))
 
40
            {
 
41
                Class<?>[] types = methods[i].getParameterTypes();
 
42
                //both null
 
43
                if(types == argTypes)
 
44
                {
 
45
                    return i;
 
46
                }
 
47
                else if(types != null && argTypes != null && types.length == argTypes.length)
 
48
                {
 
49
                    boolean match = true;
 
50
                    for(int j = 0; j < argTypes.length; j++)
 
51
                    {
 
52
                        if(!argTypes[j].equals(types[j]))
 
53
                        {
 
54
                            match= false;
 
55
                            break;
 
56
                        }
 
57
                    }
 
58
                    if(match)
 
59
                        return i;
 
60
                }
 
61
            }
 
62
        }
 
63
        throw new IllegalStateException("Method not found");
 
64
    }
 
65
    
 
66
    public static Tuple<String, Class<?>[]> getMethodInfo(int methodNumber, Class<?> remoteInterface)
 
67
    {
 
68
        Method[] methods = remoteInterface.getMethods();
 
69
        Arrays.sort(methods, methodComparator);
 
70
 
 
71
         if(s_logger.isLoggable(Level.FINEST)) {
 
72
            s_logger.fine("Sorted methods:" + Arrays.asList(methods));
 
73
        } 
 
74
        return new Tuple<String, Class<?>[]>(methods[methodNumber].getName(), methods[methodNumber].getParameterTypes());
 
75
    }
 
76
    
 
77
    
 
78
    /**
 
79
     * get methods does not guarantee an order, so sort.
 
80
     */
 
81
    private static Comparator<Method> methodComparator = new Comparator<Method>()
 
82
    {
 
83
 
 
84
        public int compare(Method o1, Method o2)
 
85
        {
 
86
            if(o1 == o2)
 
87
                return 0;
 
88
            
 
89
            if(!o1.getName().equals(o2.getName()))
 
90
                return o1.getName().compareTo(o2.getName());
 
91
            
 
92
            
 
93
            Class<?>[] t1 = o1.getParameterTypes();
 
94
            Class<?>[] t2 = o2.getParameterTypes();
 
95
            
 
96
            //both null
 
97
            if(t1 == t2)
 
98
                return 0;
 
99
            if(t1 == null)
 
100
                return -1;
 
101
            if(t2 == null)
 
102
                return 1;
 
103
            
 
104
            if(t1.length != t2.length)
 
105
                return t1.length - t2.length;
 
106
            for(int i =0; i < t1.length; i++)
 
107
            {
 
108
                if(!t1[i].getName().equals(t2[i].getName()))
 
109
                {
 
110
                    return t1[i].getName().compareTo(t2[i].getName());
 
111
                }
 
112
            }
 
113
            return 0;
 
114
        }
 
115
        
 
116
    };
 
117
    
 
118
}