2
// (c) Software Lab. Alexander Burger
7
import java.lang.reflect.*;
9
// java Reflector fifo/java fifo/rqst fifo/rply
10
public class Reflector {
11
public static void main(String[] args) throws Exception {
12
final InOut io = new InOut(System.in, System.out);
13
final InOut rpc = new InOut(new FileInputStream(args[1]), new FileOutputStream(args[0]));
18
Object x, y, z, lst[];
20
while ((lst = (Object[])io.read()) != null) {
22
switch((Integer)lst[0]) {
23
// (java 'cls 'T ['any ..]) -> obj
24
// (java 'cls 'msg ['any ..]) -> obj
25
// (java 'obj 'msg ['any ..]) -> obj
29
Object[] arg = new Object[i];
30
Class[] par = new Class[i];
33
if (v == InOut.T || v == InOut.Nil) {
34
arg[i] = v == InOut.T;
35
par[i] = Boolean.TYPE;
39
if (v instanceof Byte)
41
else if (v instanceof Character)
42
par[i] = Character.TYPE;
43
else if (v instanceof Short)
45
else if (v instanceof Integer)
46
par[i] = Integer.TYPE;
47
else if (v instanceof Long)
49
else if (v instanceof Double)
52
par[i] = v.getClass();
56
x = javaConstructor(Class.forName(y.toString()), par).newInstance(arg);
59
if (y instanceof String) {
60
m = javaMethod(Class.forName((String)y), z.toString(), par);
61
x = m.invoke(null, arg);
64
m = javaMethod(y.getClass(), z.toString(), par);
67
if (m.getReturnType() == Void.TYPE)
72
// (public 'obj 'any ['any ..]) -> obj
73
// (public 'cls 'any ['any ..]) -> obj
76
if (y instanceof String) {
77
Class cls = Class.forName((String)y);
78
x = cls.getField(z.toString()).get(cls);
81
x = y.getClass().getField(z.toString()).get(y);
82
for (i = 3; i < lst.length; ++i)
83
x = x.getClass().getField(lst[i].toString()).get(x);
86
// (interface 'obj 'cls|lst 'sym 'fun ..) -> obj
90
Class[] c = new Class[y instanceof Object[]? ((Object[])y).length : 1];
91
if (y instanceof Object[])
92
for (i = 0; i < c.length; ++i)
93
c[i] = Class.forName((((Object[])y)[i]).toString());
95
c[0] = Class.forName(y.toString());
96
InvocationHandler h = new InvocationHandler() {
97
public Object invoke(Object o, Method m, Object[] lst) {
98
String nm = m.getName();
99
if (nm.equals("hashCode"))
100
return System.identityHashCode(o);
108
catch (Exception e) {}
112
io.print(Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), c, h));
114
// (reflect 'obj) -> [lst lst]
116
Class cls = y instanceof Class? ((Class)y).getSuperclass() : y.getClass();
117
Field[] fld = cls.getDeclaredFields();
118
io.Out.write(InOut.BEG);
119
io.Out.write(InOut.BEG);
121
io.Out.write(InOut.DOT);
122
io.print(cls.getName());
123
for (i = 0; i < fld.length; ++i) {
124
if (!(y instanceof Class)) {
125
io.Out.write(InOut.BEG);
126
if (fld[i].isAccessible())
127
io.print(fld[i].get(y));
129
fld[i].setAccessible(true);
130
io.print(fld[i].get(y));
131
fld[i].setAccessible(false);
133
io.Out.write(InOut.DOT);
135
io.prSym(fld[i].getName());
137
io.Out.write(InOut.END);
140
System.err.println("## " + lst[0] + ": Undefined");
145
catch (EOFException e) {
150
catch (Exception e) {
151
System.err.println("## " + e);
152
io.Out.write(InOut.NIX);
154
while (io.In.available() > 0)
156
rpc.Out.write(InOut.NIX);
158
while (rpc.In.available() > 0)
164
final static Constructor javaConstructor(Class cls, Class[] par) throws NoSuchMethodException {
166
for (Constructor m : cls.getConstructors()) {
167
Class<?>[] types = m.getParameterTypes();
168
if (types.length == par.length) {
169
for (int i = 0; i < types.length; ++i)
170
if (!(types[i].isAssignableFrom(par[i])))
175
throw new NoSuchMethodException();
178
final static Method javaMethod(Class cls, String nm, Class[] par) throws NoSuchMethodException {
180
for (Method m : cls.getMethods()) {
181
if (m.getName().equals(nm)) {
182
Class<?>[] types = m.getParameterTypes();
183
if (types.length == par.length) {
184
for (int i = 0; i < types.length; ++i)
185
if (!(types[i].isAssignableFrom(par[i])))
191
throw new NoSuchMethodException(nm + "(" + par + ")");