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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
/*
+----------------------------------------------------------------------+
| PHP version 4.0 |
+----------------------------------------------------------------------+
| Copyright (c) 1997, 1998, 1999 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://www.php.net/license.html. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Sam Ruby (rubys@us.ibm.com) |
+----------------------------------------------------------------------+
*/
package net.php;
import java.lang.reflect.*;
import java.beans.*;
class reflect {
static { System.loadLibrary("php_java"); }
//
// Native methods
//
private static native void setResultFromString(long result, String value);
private static native void setResultFromLong(long result, long value);
private static native void setResultFromDouble(long result, double value);
private static native void setResultFromBoolean(long result, boolean value);
private static native void setResultFromObject(long result, Object value);
private static native void setException(long result, String value);
//
// Helper routines which encapsulate the native methods
//
static void setResult(long result, Object value) {
if (value == null) return;
if (value instanceof java.lang.String) {
setResultFromString(result, (String)value);
} else if (value instanceof java.lang.Number) {
if (value instanceof java.lang.Integer ||
value instanceof java.lang.Short ||
value instanceof java.lang.Byte) {
setResultFromLong(result, ((Number)value).longValue());
} else {
/* Float, Double, BigDecimal, BigInteger, Double, Long, ... */
setResultFromDouble(result, ((Number)value).doubleValue());
}
} else if (value instanceof java.lang.Boolean) {
setResultFromBoolean(result, ((Boolean)value).booleanValue());
} else {
setResultFromObject(result, value);
}
}
static void setException(long result, Throwable e) {
if (e instanceof InvocationTargetException) {
Throwable t = ((InvocationTargetException)e).getTargetException();
if (t!=null) e=t;
}
setException(result, e.toString());
}
//
// Create an new instance of a given class
//
public static void CreateObject(String name, Object args[], long result) {
try {
Constructor cons[] = Class.forName(name).getConstructors();
for (int i=0; i<cons.length; i++) {
if (cons[i].getParameterTypes().length == args.length) {
setResult(result, cons[i].newInstance(args));
return;
}
}
// for classes which have no visible constructor, return the class
// useful for classes like java.lang.System and java.util.Calendar.
if (args.length == 0) {
setResult(result, Class.forName(name));
return;
}
throw new InstantiationException("No matching constructor found");
} catch (Exception e) {
setException(result, e);
}
}
//
// Invoke a method on a given object
//
public static void Invoke
(Object object, String method, Object args[], long result)
{
try {
for (Class jclass = object.getClass();;jclass=(Class)object) {
Method methods[] = jclass.getMethods();
for (int i=0; i<methods.length; i++) {
if (methods[i].getName().equalsIgnoreCase(method) &&
methods[i].getParameterTypes().length == args.length) {
setResult(result, methods[i].invoke(object, args));
return;
}
}
// try a second time with the object itself, if it is of type Class
if (!(jclass instanceof Class) || (jclass==object)) break;
}
throw new NoSuchMethodException(method);
} catch (Exception e) {
setException(result, e);
}
}
//
// Get or Set a property
//
public static void GetSetProp
(Object object, String prop, Object args[], long result)
{
try {
for (Class jclass = object.getClass();;jclass=(Class)object) {
BeanInfo beanInfo = Introspector.getBeanInfo(jclass);
PropertyDescriptor props[] = beanInfo.getPropertyDescriptors();
for (int i=0; i<props.length; i++) {
if (props[i].getName().equalsIgnoreCase(prop)) {
Method method;
if (args!=null && args.length>0) {
method=props[i].getWriteMethod();
} else {
method=props[i].getReadMethod();
}
setResult(result, method.invoke(object, args));
return;
}
}
Field jfields[] = jclass.getFields();
for (int i=0; i<jfields.length; i++) {
if (jfields[i].getName().equalsIgnoreCase(prop)) {
if (args!=null && args.length>0) {
jfields[i].set(object, args[0]);
} else {
setResult(result, jfields[i].get(object));
}
return;
}
}
// try a second time with the object itself, if it is of type Class
if (!(jclass instanceof Class) || (jclass==object)) break;
}
} catch (Exception e) {
setException(result, e);
}
}
//
// Helper routines for the C implementation
//
public static Object MakeArg(boolean b) { return new Boolean(b); }
public static Object MakeArg(long l) { return new Long(l); }
public static Object MakeArg(double d) { return new Double(d); }
}
|