reflection_wev8.js
5.23 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
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
187
188
189
190
191
192
193
194
195
196
197
/*
Copyright (c) 2004-2005, The Dojo Foundation
All Rights Reserved.
Licensed under the Academic Free License version 2.1 or above OR the
modified BSD license. For more information on Dojo licensing, see:
http://dojotoolkit.org/community/licensing.shtml
*/
dojo.provide("dojo.reflect");
/*****************************************************************
reflect.js
v.1.5.0
(c) 2003-2004 Thomas R. Trenka, Ph.D.
Derived from the reflection functions of f(m).
http://dojotoolkit.org
http://fm.dept-z.com
There is a dependency on the variable dJ_global, which
should always refer to the global object.
******************************************************************/
if(!dj_global){ var dj_global = this; }
dojo.reflect = {} ;
dojo.reflect.$unknownType = function(){ } ;
dojo.reflect.ParameterInfo = function(name, type){
this.name = name ;
this.type = (type) ? type : dojo.reflect.$unknownType ;
} ;
dojo.reflect.PropertyInfo = function(name, type) {
this.name = name ;
this.type = (type) ? type : dojo.reflect.$unknownType ;
} ;
dojo.reflect.MethodInfo = function(name, fn){
var parse = function(f) {
var o = {} ;
var s = f.toString() ;
var param = ((s.substring(s.indexOf('(')+1, s.indexOf(')'))).replace(/\s+/g, "")).split(",") ;
o.parameters = [] ;
for (var i = 0; i < param.length; i++) {
o.parameters.push(new dojo.reflect.ParameterInfo(param[i])) ;
}
o.body = (s.substring(s.indexOf('{')+1, s.lastIndexOf('}'))).replace(/(^\s*)|(\s*$)/g, "") ;
return o ;
} ;
var tmp = parse(fn) ;
var p = tmp.parameters ;
var body = tmp.body ;
this.name = (name) ? name : "anonymous" ;
this.getParameters = function(){ return p ; } ;
this.getNullArgumentsObject = function() {
var a = [] ;
for (var i = 0; i < p.length; i++){
a.push(null);
}
return a ;
} ;
this.getBody = function() { return body ; } ;
this.type = Function ;
this.invoke = function(src, args){ return fn.apply(src, args) ; } ;
} ;
// Static object that can activate instances of the passed type.
dojo.reflect.Activator = new (function(){
this.createInstance = function(type, args) {
switch (typeof(type)) {
case "function" : {
var o = {} ;
type.apply(o, args) ;
return o ;
} ;
case "string" : {
var o = {} ;
(dojo.reflect.Reflector.getTypeFromString(type)).apply(o, args) ;
return o ;
} ;
}
throw new Error("dojo.reflect.Activator.createInstance(): no such type exists.");
}
})() ;
dojo.reflect.Reflector = new (function(){
this.getTypeFromString = function(s) {
var parts = s.split("."), i = 0, obj = dj_global ;
do { obj = obj[parts[i++]] ; } while (i < parts.length && obj) ;
return (obj != dj_global) ? obj : null ;
};
this.typeExists = function(s) {
var parts = s.split("."), i = 0, obj = dj_global ;
do { obj = obj[parts[i++]] ; } while (i < parts.length && obj) ;
return (obj && obj != dj_global) ;
};
this.getFieldsFromType = function(s) {
var type = s ;
if (typeof(s) == "string") {
type = this.getTypeFromString(s) ;
}
var nullArgs = (new dojo.reflect.MethodInfo(type)).getNullArgumentsObject() ;
return this.getFields(dojo.reflect.Activator.createInstance(s, nullArgs)) ;
};
this.getPropertiesFromType = function(s) {
var type = s ;
if (typeof(s) == "string") {
type = this.getTypeFromString(s);
}
var nullArgs = (new dojo.reflect.MethodInfo(type)).getNullArgumentsObject() ;
return this.getProperties(dojo.reflect.Activator.createInstance(s, nullArgs)) ;
};
this.getMethodsFromType = function(s) {
var type = s ;
if (typeof(s) == "string") {
type = this.getTypeFromString(s) ;
}
var nullArgs = (new dojo.reflect.MethodInfo(type)).getNullArgumentsObject() ;
return this.getMethods(dojo.reflect.Activator.createInstance(s, nullArgs)) ;
};
this.getType = function(o) { return o.constructor ; } ;
this.getFields = function(obj) {
var arr = [] ;
for (var p in obj) {
if(this.getType(obj[p]) != Function){
arr.push(new dojo.reflect.PropertyInfo(p, this.getType(obj[p]))) ;
}else{
arr.push(new dojo.reflect.MethodInfo(p, obj[p]));
}
}
return arr ;
};
this.getProperties = function(obj) {
var arr = [] ;
var fi = this.getFields(obj) ;
for (var i = 0; i < fi.length; i++){
if (this.isInstanceOf(fi[i], dojo.reflect.PropertyInfo)){
arr.push(fi[i]) ;
}
}
return arr ;
};
this.getMethods = function(obj) {
var arr = [] ;
var fi = this.getFields(obj) ;
for (var i = 0; i < fi.length; i++){
if (this.isInstanceOf(fi[i], dojo.reflect.MethodInfo)){
arr.push(fi[i]) ;
}
}
return arr ;
};
/*
this.implements = function(o, type) {
if (this.isSubTypeOf(o, type)) return false ;
var f = this.getFieldsFromType(type) ;
for (var i = 0; i < f.length; i++) {
if (typeof(o[(f[i].name)]) == "undefined"){
return false;
}
}
return true ;
};
*/
this.getBaseClass = function(o) {
if (o.getType().prototype.prototype.constructor){
return (o.getType()).prototype.prototype.constructor ;
}
return Object ;
} ;
this.isInstanceOf = function(o, type) {
return (this.getType(o) == type) ;
};
this.isSubTypeOf = function(o, type) {
return (o instanceof type) ;
};
this.isBaseTypeOf = function(o, type) {
return (type instanceof o);
};
})();
// back-compat
dojo.provide("dojo.reflect.reflection");