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
|
/**
* Copyright (c) Rich Hickey. All rights reserved.
* The use and distribution terms for this software are covered by the
* Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
* which can be found in the file CPL.TXT at the root of this distribution.
* By using this software in any fashion, you are agreeing to be bound by
* the terms of this license.
* You must not remove this notice, or any other, from this software.
**/
using System;
using System.Reflection;
using System.Collections;
namespace org.clojure.runtime
{
public class Reflector{
public static Object invokeInstanceMethod(String name, Object target, Object[] args) //throws Exception
{
Type t = target.GetType();
IList methods = getMethods(t, args.Length, name,false);
return invokeMatchingMethod(name, target, args, t, methods,false);
}
private static object invokeMatchingMethod(String name, Object target, Object[] args, Type t, IList methods, bool statics)
{
if (methods.Count == 0)
{
throw new InvalidOperationException("No matching field or method found");
}
else if (methods.Count == 1)
{
MethodInfo m = (MethodInfo)methods[0];
return prepRet(m.Invoke(target, boxArgs(m.GetParameters(), args)));
}
else //overloaded w/same arity, let reflection choose most specific match
{
return prepRet(t.InvokeMember(name, BindingFlags.Public | (statics ? BindingFlags.Static : BindingFlags.Instance)
| BindingFlags.FlattenHierarchy | BindingFlags.InvokeMethod,
null, target, args));
}
}
public static Object invokeConstructor(Type t, Object[] args) //throws Exception
{
ConstructorInfo[] allctors = t.GetConstructors();
ArrayList ctors = new ArrayList();
foreach (ConstructorInfo ctor in allctors)
{
if (ctor.GetParameters().Length == args.Length)
ctors.Add(ctor);
}
if (ctors.Count == 0)
{
throw new InvalidOperationException("No matching ctor found");
}
else if (ctors.Count == 1)
{
ConstructorInfo ctor = (ConstructorInfo)ctors[0];
return ctor.Invoke(boxArgs(ctor.GetParameters(), args));
}
else //overloaded w/same arity, let reflection choose most specific match
{
return t.InvokeMember(null, BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance,
null, null, args);
}
}
public static Object invokeStaticMethod(String name, String className, Object[] args) //throws Exception
{
Type t = Type.GetType(className);
if (name.Equals("new"))
return invokeConstructor(t, args);
IList methods = getMethods(t, args.Length, name, true);
return invokeMatchingMethod(name, null, args, t, methods,true);
}
public static Object getStaticField(String name, String className) //throws Exception
{
//check for field first
Type t = Type.GetType(className);
FieldInfo f = getField(t, name, true);
if (f != null) //field get
{
return prepRet(f.GetValue(null));
}
PropertyInfo p = getProperty(t, name, true);
if (p != null)
{
return prepRet(p.GetValue(null, null));
}
throw new InvalidOperationException("No matching field or property found");
}
public static Object setStaticField(String name, String className, Object arg1) //throws Exception
{
//check for field first
Type t = Type.GetType(className);
FieldInfo f = getField(t, name, true);
if (f != null) //field get
{
f.SetValue(null, boxArg(f.FieldType, arg1));
return arg1;
}
PropertyInfo p = getProperty(t, name, true);
if (p != null)
{
p.SetValue(null, boxArg(p.PropertyType, arg1), null);
return arg1;
}
throw new InvalidOperationException("No matching field or property found");
}
public static Object invokeInstanceMember(String name, Object target) //throws Exception
{
//check for field first
Type t = target.GetType();
FieldInfo f = getField(t, name,false);
if(f != null) //field get
{
return prepRet(f.GetValue(target));
}
PropertyInfo p = getProperty(t, name,false);
if (p != null)
{
return prepRet(p.GetValue(target, null));
}
return invokeInstanceMethod(name, target, RT.EMPTY_ARRAY);
}
public static Object invokeInstanceMember(String name, Object target, Object arg1) //throws Exception
{
//check for field first
Type t = target.GetType();
FieldInfo f = getField(t, name,false);
if (f != null) //field get
{
f.SetValue(target,boxArg(f.FieldType,arg1));
return arg1;
}
PropertyInfo p = getProperty(t, name,false);
if (p != null)
{
//could be indexed property, which we otherwise aren't dealing with yet
if(p.GetIndexParameters() != null && p.GetIndexParameters().Length == 1)
return prepRet(p.GetValue(target, new Object[]{boxArg(p.GetIndexParameters()[0].ParameterType,arg1)}));
p.SetValue(target,boxArg(p.PropertyType,arg1),null);
return arg1;
}
return invokeInstanceMethod(name, target, new Object[]{arg1});
}
public static Object invokeInstanceMember(String name, Object target, Object arg1, Object arg2) //throws Exception
{
return invokeInstanceMethod(name, target, new Object[]{arg1, arg2});
}
public static Object invokeInstanceMember(String name, Object target, Object arg1, Object arg2, Object arg3)
//throws Exception
{
return invokeInstanceMethod(name, target, new Object[]{arg1, arg2, arg3});
}
public static Object invokeInstanceMember(String name, Object target, Object arg1, Object arg2, Object arg3,
Object arg4)
//throws Exception
{
return invokeInstanceMethod(name, target, new Object[]{arg1, arg2, arg3, arg4});
}
public static Object invokeInstanceMember(String name, Object target, Object arg1, Object arg2, Object arg3,
Object arg4,
Cons arglist)
//throws Exception
{
Object[] args = new Object[4 + RT.length(arglist)];
args[0] = arg1;
args[1] = arg2;
args[2] = arg3;
args[3] = arg4;
for(int i = 4; arglist != null; i++, arglist = arglist.rest)
args[i] = arglist.first;
return invokeInstanceMethod(name, target, args);
}
public static FieldInfo getField(Type t, string name,bool statics)
{
return t.GetField(name, BindingFlags.Public | (statics ? BindingFlags.Static : BindingFlags.Instance) | BindingFlags.FlattenHierarchy);
}
public static PropertyInfo getProperty(Type t, string name, bool statics)
{
return t.GetProperty(name, BindingFlags.Public | (statics ? BindingFlags.Static : BindingFlags.Instance) | BindingFlags.FlattenHierarchy);
}
static public IList getMethods(Type t, int arity, String name, bool getStatics)
{
MethodInfo[] allmethods = t.GetMethods(BindingFlags.Public | (getStatics?BindingFlags.Static : BindingFlags.Instance)
| BindingFlags.FlattenHierarchy);
ArrayList methods = new ArrayList();
for (int i = 0; i < allmethods.Length; i++)
{
if (name.Equals(allmethods[i].Name)
&& allmethods[i].GetParameters().Length == arity)
{
methods.
|