Debian Codegraph @

native/jpype_module/src/main/java/org/jpype/manager/MethodResolution.java

raw symbols SCIP
1 /* ****************************************************************************
2 Licensed under the Apache License, Version 2.0 (the "License");
3 you may not use this file except in compliance with the License.
4 You may obtain a copy of the License at
5
6 http://www.apache.org/licenses/LICENSE-2.0
7
8 Unless required by applicable law or agreed to in writing, software
9 distributed under the License is distributed on an "AS IS" BASIS,
10 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 See the License for the specific language governing permissions and
12 limitations under the License.
13
14 See NOTICE file for details.
15 **************************************************************************** */
16 package org.jpype.manager;
17
18 import java.lang.reflect.Executable;
19 import java.lang.reflect.Modifier;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.HashMap;
23 import java.util.LinkedList;
24 import java.util.List;
25
26 /**
27 * Sort out which methods hide other methods.
28 * <p>
29 * When resolving method overloads there may be times in which more than one
30 * overload applies. JPype requires that the methods appear in order from most
31 * to least specific. And each method overload requires a list of methods that
32 * are more general. If two or more methods match and one is not more specific
33 * than the other.
34 *
35 * @author nelson85
36 */
37 public class MethodResolution
38 {
39
40 long ptr = 0;
41 boolean covered = false;
42 Executable executable;
43 List<MethodResolution> children = new ArrayList<>();
44
45 MethodResolution(Executable method)
46 {
47 this.executable = method;
48 }
49
50 private boolean isCovered()
51 {
52 for (MethodResolution ov : this.children)
53 {
54 if (!ov.covered)
55 return false;
56 }
57 covered = true;
58 return true;
59 }
60
61 /**
62 * Order methods from least to most specific.
63 *
64 * @param <T>
65 * @param methods
66 * @return
67 */
68 public static <T extends Executable>
69 List<MethodResolution> sortMethods(List<T> methods)
70 {
71 // Create a method resolution for each method
72 LinkedList<MethodResolution> unsorted = new LinkedList<>();
73 for (T m1 : methods)
74 {
75 unsorted.add(new MethodResolution(m1));
76 }
77
78 for (MethodResolution m1 : unsorted)
79 {
80 for (MethodResolution m2 : unsorted)
81 {
82 if (m1 == m2)
83 continue;
84
85 if (isMoreSpecificThan(m1.executable, m2.executable)
86 && !isMoreSpecificThan(m2.executable, m1.executable))
87 {
88 m1.children.add(m2);
89 }
90 }
91 }
92
93 // Execute a graph sort problem so that the most specific are always on the front
94 LinkedList<MethodResolution> out = new LinkedList<>();
95 while (!unsorted.isEmpty())
96 {
97 // Remove the first unsorted element
98 MethodResolution front = unsorted.pop();
99 // Check to see if all dependencies are already ordered
100 boolean good = front.isCovered();
101
102 // If all dependencies are included
103 if (good)
104 {
105 front.covered = true;
106 out.add(front);
107 } else
108 {
109 unsorted.add(front);
110 }
111 }
112 return out;
113 }
114
115 // Table for primitive rules
116 static Class[] of(Class... l)
117 {
118 return l;
119 }
120 static HashMap<Class, Class[]> CONVERSION = new HashMap<>();
121
122
123 {
124 CONVERSION.put(Byte.TYPE,
125 of(Byte.TYPE, Byte.class, Short.TYPE, Short.class,
126 Integer.TYPE, Integer.class, Long.TYPE, Long.class,
127 Float.TYPE, Float.class, Double.TYPE, Double.class));
128 CONVERSION.put(Character.TYPE,
129 of(Character.TYPE, Character.class, Short.TYPE, Short.class,
130 Integer.TYPE, Integer.class, Long.TYPE, Long.class,
131 Float.TYPE, Float.class, Double.TYPE, Double.class));
132 CONVERSION.put(Short.TYPE,
133 of(Short.TYPE, Short.class,
134 Integer.TYPE, Integer.class, Long.TYPE, Long.class,
135 Float.TYPE, Float.class, Double.TYPE, Double.class));
136 CONVERSION.put(Integer.TYPE,
137 of(Integer.TYPE, Integer.class, Long.TYPE, Long.class,
138 Float.TYPE, Float.class, Double.TYPE, Double.class));
139 CONVERSION.put(Long.TYPE,
140 of(Long.TYPE, Long.class,
141 Float.TYPE, Float.class, Double.TYPE, Double.class));
142 CONVERSION.put(Float.TYPE,
143 of(Float.TYPE, Float.class, Double.TYPE, Double.class));
144 CONVERSION.put(Double.TYPE,
145 of(Double.TYPE, Double.class));
146 CONVERSION.put(Boolean.TYPE,
147 of(Boolean.TYPE, Boolean.class));
148 }
149
150 static boolean isAssignableTo(Class c1, Class c2)
151 {
152 if (!c1.isPrimitive())
153 return c2.isAssignableFrom(c1);
154 Class[] cl = CONVERSION.get(c1);
155 if (cl == null)
156 return false;
157 for (Class c3 : cl)
158 if (c2.equals(c3))
159 return true;
160 return false;
161 }
162
163 /**
164 * Determine is a executable is more specific than another.
165 * <p>
166 * This is public so that we can debug from within jpype.
167 *
168 * @param method1
169 * @param method2
170 * @return
171 */
172 public static boolean isMoreSpecificThan(Executable method1, Executable method2)
173 {
174 List<Class<?>> param1 = new ArrayList<>(Arrays.asList(method1.getParameterTypes()));
175 List<Class<?>> param2 = new ArrayList<>(Arrays.asList(method2.getParameterTypes()));
176
177 if (!Modifier.isStatic(method1.getModifiers()))
178 param1.add(0, method1.getDeclaringClass());
179 if (!Modifier.isStatic(method2.getModifiers()))
180 param2.add(0, method2.getDeclaringClass());
181
182 // Special handling is needed for varargs as it may chop or expand.
183 // we have 4 cases for a varargs methods
184 // foo(Arg0, Arg1...) as
185 // foo(Arg0)
186 // foo(Arg0, Arg1)
187 // foo(Arg0, Arg1[])
188 // foo(Arg0, Arg1, Arg1+)
189 if (method1.isVarArgs() && method2.isVarArgs())
190 {
191 // Punt on this as there are too many different cases
192 return isMoreSpecificThan(param1, param2);
193 }
194
195 if (method1.isVarArgs())
196 {
197 int n1 = param1.size();
198 int n2 = param2.size();
199
200 // Last element is an array
201 Class<?> cls = param1.get(n1 - 1);
202 Class<?> cls2 = cls.getComponentType();
203
204 // Less arguments, chop the list
205 if (n1 - 1 == n2)
206 return isMoreSpecificThan(param1.subList(0, n2), param2);
207
208 // Same arguments
209 if (n1 == n2)
210 {
211 List<Class<?>> q = new ArrayList<>(param1);
212 q.set(n1 - 1, cls2);
213
214 // Check both ways
215 boolean isMoreSpecific = isMoreSpecificThan(param1, param2) || isMoreSpecificThan(q, param2);
216
217 // If the varargs array is of the single-variable's type (or they are primitive-equivalent),
218 // the single-variable signature should win specificity
219 Class<?> svCls = param2.get(n2 - 1);
220 return isMoreSpecific && !(isAssignableTo(cls2, svCls) && isAssignableTo(svCls, cls2));
221 }
222
223 // More arguments
224 if (n1 < n2)
225 {
226 // Grow the list
227 List<Class<?>> q = new ArrayList<>(param1);
228 q.set(n1 - 1, cls2);
229 for (int i = n1; i < n2; ++i)
230 q.add(cls2);
231 return isMoreSpecificThan(q, param2);
232 }
233 }
234
235 if (method2.isVarArgs())
236 {
237 int n1 = param1.size();
238 int n2 = param2.size();
239
240 // Last element is an array
241 Class<?> cls = param2.get(n2 - 1);
242 Class<?> cls2 = cls.getComponentType();
243
244 // Less arguments, chop the list
245 if (n2 - 1 == n1)
246 return isMoreSpecificThan(param1, param2.subList(0, n2));
247
248 // Same arguments
249 if (n1 == n2)
250 {
251 List<Class<?>> q = new ArrayList<>(param2);
252 q.set(n2 - 1, cls2);
253
254 // Compare both ways
255 return isMoreSpecificThan(param1, param2) || isMoreSpecificThan(param1, q);
256 }
257
258 // More arguments
259 if (n2 < n1)
260 {
261 // Grow the list
262 List<Class<?>> q = new ArrayList<>(param2);
263 q.set(n2 - 1, cls2);
264 for (int i = n2; i < n1; ++i)
265 q.add(cls2);
266 return isMoreSpecificThan(param1, q);
267 }
268 }
269
270 return isMoreSpecificThan(param1, param2);
271 }
272
273 public static boolean isMoreSpecificThan(List<Class<?>> param1, List<Class<?>> param2)
274 {
275 // FIXME need to consider resolving mixing of static and non-static
276 // Methods here.
277 if (param1.size() != param2.size())
278 return false;
279
280 for (int i = 0; i < param1.size(); ++i)
281 {
282 if (!isAssignableTo(param1.get(i), param2.get(i)))
283 return false;
284 }
285 return true;
286 }
287 }