forked from ibmruntimes/node-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCastingUtils.java
More file actions
36 lines (31 loc) · 1.06 KB
/
CastingUtils.java
File metadata and controls
36 lines (31 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package node;
import java.lang.reflect.Method;
public class CastingUtils {
public static void cast(Method method, Object[] args) throws Throwable {
Class[] methodParameterTypes = method.getParameterTypes();
if (methodParameterTypes.length != args.length) {
throw new Exception("Method argument length mismatch. Expecting " + methodParameterTypes.length + " found " + args.length);
}
for (int i = 0; i < methodParameterTypes.length; i++) {
args[i] = cast(args[i], methodParameterTypes[i]);
}
}
public static Object cast(Object o, Class t) {
if (o == null) {
return null;
}
Class oClass = o.getClass();
if (oClass == Integer.class) {
Integer i = (Integer) o;
if (t == Double.class) {
return i.doubleValue();
}
} else if (oClass == Double.class) {
Double d = (Double) o;
if (t == Integer.class) {
return d.intValue();
}
}
return o;
}
}