-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJNIEx.java
95 lines (86 loc) · 3.2 KB
/
JNIEx.java
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
@copyright Russell Standish 2000-2013
@author Russell Standish
This file is part of Classdesc
Open source licensed under the MIT license. See LICENSE for details.
*/
import java.lang.reflect.*;
public class JNIEx
{
public static Object call(Object obj, String method, Object... args) throws Throwable
{
System.out.printf("in call %s, %d args\n",method,(args!=null)? args.length: 0);
System.out.println(obj.getClass().getName());
System.out.println(obj.getClass().getMethod(method,Object[].class).getName());
if (args==null) args=new Object[0];
return obj.getClass().getMethod(method,Object[].class).invoke(args);
}
public static void main(String[] arg)
{
JNIInterface loader=new JNIInterface("JNIEx");
Class<?> JNIEx;
try
{
JNIEx = loader.loadClass("ExampleIntf");
}
catch (ClassNotFoundException e1)
{
System.out.println("Class not found exception thrown");
return;
}
Method[] methods = JNIEx.getDeclaredMethods();
for (Method m: methods)
{
System.out.printf("%s %s\n",m.getName(), m.getReturnType().getName());
}
System.out.println("-----------------");
{
Class<?> Ex = Proxy.getProxyClass(loader, new Class<?>[]{JNIEx});
Object ex = Proxy.newProxyInstance(loader, new Class<?>[]{JNIEx},
new JNIProxyInvocation());
for (Method m: Ex.getDeclaredMethods())
{
try
{
ex.getClass().getMethod(m.getName(),m.getParameterTypes());
}
catch (Throwable e2)
{
System.out.printf("Error in getting method %s: %s\n",
m.getName(), e2.getMessage());
}
}
try
{
JNIInterface.call("ex.printFoo",0);
JNIInterface.call("ex.foo",2);
JNIInterface.call("ex.printFoo",0);
JNIInterface.call("ex.setFoo",3);
JNIInterface.call("ex.printFoo",0);
System.out.println(JNIInterface.call("ex.getFoo",0));
JNIInterface.call("ex.printBar",0);
JNIInterface.call("ex.setBar",2.0);
JNIInterface.call("ex.printBar",0);
System.out.println(JNIInterface.call("ex.getBar",2));
}
catch (Throwable e1)
{
System.out.println("Error:"+e1.getMessage());
}
}
// //Now try direct call
// {
// Example ex = new Example();
// try
// {
// System.out.println("b4 ex. calls");
// ex.printFoo();
// ex.printBar();
// }
// catch (Throwable e1)
// {
// System.out.println(e1.getMessage());
// }
// }
}
}