-
Notifications
You must be signed in to change notification settings - Fork 2
/
ClassUtils.as
461 lines (407 loc) · 15.5 KB
/
ClassUtils.as
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
* Copyright (c) 2007-2009 the original author or authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.as3commons.lang {
import flash.events.TimerEvent;
import flash.system.ApplicationDomain;
import flash.utils.Timer;
import flash.utils.describeType;
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;
import flash.utils.getQualifiedSuperclassName;
/**
* Provides utilities for working with <code>Class</code> objects.
*
* @author Christophe Herreman
* @author Erik Westra
*/
public class ClassUtils {
private static const PACKAGE_CLASS_SEPARATOR:String = "::";
/**
* Returns a <code>Class</code> object that corresponds with the given
* instance. If no correspoding class was found, a
* <code>ClassNotFoundError</code> will be thrown.
*
* @param instance the instance from which to return the class
* @param applicationDomain the optional applicationdomain where the instance's class resides
*
* @return the <code>Class</code> that corresponds with the given instance
*
* @see org.springextensions.actionscript.errors.ClassNotFoundError
*/
public static function forInstance(instance:*, applicationDomain:ApplicationDomain = null):Class {
var className:String = getQualifiedClassName(instance);
return forName(className, applicationDomain);
}
/**
* Returns a <code>Class</code> object that corresponds with the given
* name. If no correspoding class was found in the applicationdomain tree, a
* <code>ClassNotFoundError</code> will be thrown.
*
* @param name the name from which to return the class
* @param applicationDomain the optional applicationdomain where the instance's class resides
*
* @return the <code>Class</code> that corresponds with the given name
*
* @see org.springextensions.actionscript.errors.ClassNotFoundError
*/
public static function forName(name:String, applicationDomain:ApplicationDomain = null):Class {
var result:Class;
if (!applicationDomain) {
applicationDomain = ApplicationDomain.currentDomain;
}
while (!applicationDomain.hasDefinition(name)) {
if (applicationDomain.parentDomain) {
applicationDomain = applicationDomain.parentDomain;
} else {
break;
}
}
try {
result = applicationDomain.getDefinition(name) as Class;
} catch (e:ReferenceError) {
throw new ClassNotFoundError("A class with the name '" + name + "' could not be found.");
}
return result;
}
/**
* Returns the name of the given class.
*
* @param clazz the class to get the name from
*
* @return the name of the class
*/
public static function getName(clazz:Class):String {
return getNameFromFullyQualifiedName(getFullyQualifiedName(clazz));
}
/**
* Returns the name of the class or interface, based on the given fully
* qualified class or interface name.
*
* @param fullyQualifiedName the fully qualified name of the class or interface
*
* @return the name of the class or interface
*/
public static function getNameFromFullyQualifiedName(fullyQualifiedName:String):String {
var result:String = "";
var startIndex:int = fullyQualifiedName.indexOf(PACKAGE_CLASS_SEPARATOR);
if (startIndex == -1) {
result = fullyQualifiedName;
} else {
result = fullyQualifiedName.substring(startIndex + PACKAGE_CLASS_SEPARATOR.length, fullyQualifiedName.length);
}
return result;
}
/**
* Returns the fully qualified name of the given class.
*
* @param clazz the class to get the name from
* @param replaceColons whether the double colons "::" should be replaced by a dot "."
* the default is false
*
* @return the fully qualified name of the class
*/
public static function getFullyQualifiedName(clazz:Class, replaceColons:Boolean = false):String {
var result:String = getQualifiedClassName(clazz);
if (replaceColons) {
result = convertFullyQualifiedName(result);
}
return result;
}
/**
* Determines if the class or interface represented by the clazz1 parameter is either the same as, or is
* a superclass or superinterface of the clazz2 parameter. It returns true if so; otherwise it returns false.
*
* @return the boolean value indicating whether objects of the type clazz2 can be assigned to objects of clazz1
*/
public static function isAssignableFrom(clazz1:Class, clazz2:Class):Boolean {
return (clazz1 == clazz2) || isSubclassOf(clazz2, clazz1) || isImplementationOf(clazz2, clazz1);
}
/**
* Returns whether the passed in Class object is a subclass of the
* passed in parent Class. To check if an interface extends another interface, use the isImplementationOf()
* method instead.
*/
public static function isSubclassOf(clazz:Class, parentClass:Class):Boolean {
var classDescription:XML = getFromObject(clazz);
var parentName:String = getQualifiedClassName(parentClass);
return (classDescription.factory.extendsClass.(@type == parentName).length() != 0);
}
/**
* Returns the class that the passed in clazz extends. If no super class
* was found, in case of Object, null is returned.
*
* @param clazz the class to get the super class from
*
* @returns the super class or null if no parent class was found
*/
public static function getSuperClass(clazz:Class):Class {
var result:Class;
var classDescription:XML = getFromObject(clazz);
var superClasses:XMLList = classDescription.factory.extendsClass;
if (superClasses.length() > 0) {
result = ClassUtils.forName(superClasses[0].@type);
}
return result;
}
/**
* Returns the name of the given class' superclass.
*
* @param clazz the class to get the name of its superclass' from
*
* @return the name of the class' superclass
*/
public static function getSuperClassName(clazz:Class):String {
var fullyQualifiedName:String = getFullyQualifiedSuperClassName(clazz);
var index:int = fullyQualifiedName.indexOf(PACKAGE_CLASS_SEPARATOR) + PACKAGE_CLASS_SEPARATOR.length;
return fullyQualifiedName.substring(index, fullyQualifiedName.length);
}
/**
* Returns the fully qualified name of the given class' superclass.
*
* @param clazz the class to get its superclass' name from
* @param replaceColons whether the double colons "::" should be replaced by a dot "."
* the default is false
*
* @return the fully qualified name of the class' superclass
*/
public static function getFullyQualifiedSuperClassName(clazz:Class, replaceColons:Boolean = false):String {
var result:String = getQualifiedSuperclassName(clazz);
if (replaceColons) {
result = convertFullyQualifiedName(result);
}
return result;
}
/**
* Returns whether the passed in <code>Class</code> object implements
* the given interface.
*
* @param clazz the class to check for an implemented interface
* @param interfaze the interface that the clazz argument should implement
*
* @return true if the clazz object implements the given interface; false if not
*/
public static function isImplementationOf(clazz:Class, interfaze:Class):Boolean {
var result:Boolean;
if (clazz == null) {
result = false;
} else {
var classDescription:XML = getFromObject(clazz);
result = (classDescription.factory.implementsInterface.(@type == getQualifiedClassName(interfaze)).length() != 0);
}
return result;
}
/**
* Returns whether the passed in Class object is an interface.
*
* @param clazz the class to check
* @return true if the clazz is an interface; false if not
*/
public static function isInterface(clazz:Class):Boolean {
var classDescription:XML = describeType(clazz);
return (classDescription.factory.extendsClass.(@type == "Object").length() == 0);
}
/**
* Returns an array of all interface names that the given class implements.
*/
public static function getImplementedInterfaceNames(clazz:Class):Array {
var result:Array = getFullyQualifiedImplementedInterfaceNames(clazz);
for (var i:int = 0; i < result.length; i++) {
result[i] = getNameFromFullyQualifiedName(result[i]);
}
return result;
}
/**
* Returns an array of all fully qualified interface names that the
* given class implements.
*/
public static function getFullyQualifiedImplementedInterfaceNames(clazz:Class, replaceColons:Boolean = false):Array {
var result:Array = [];
var classDescription:XML = getFromObject(clazz);
var interfacesDescription:XMLList = classDescription.factory.implementsInterface;
if (interfacesDescription) {
var numInterfaces:int = interfacesDescription.length();
for (var i:int = 0; i < numInterfaces; i++) {
var fullyQualifiedInterfaceName:String = interfacesDescription[i][email protected]();
if (replaceColons) {
fullyQualifiedInterfaceName = convertFullyQualifiedName(fullyQualifiedInterfaceName);
}
result.push(fullyQualifiedInterfaceName);
}
}
return result;
}
/**
* Returns an array of all interface names that the given class implements.
*/
public static function getImplementedInterfaces(clazz:Class):Array {
var result:Array = getFullyQualifiedImplementedInterfaceNames(clazz);
for (var i:int = 0; i < result.length; i++) {
result[i] = getDefinitionByName(result[i]);
}
return result;
}
/**
* Creates an instance of the given class and passes the arguments to
* the constructor.
*
* TODO find a generic solution for this. Currently we support constructors
* with a maximum of 10 arguments.
*
* @param clazz the class from which an instance will be created
* @param args the arguments that need to be passed to the constructor
*/
public static function newInstance(clazz:Class, args:Array = null):* {
var result:*;
var a:Array = (args == null) ? [] : args;
switch (a.length) {
case 1:
result = new clazz(a[0]);
break;
case 2:
result = new clazz(a[0], a[1]);
break;
case 3:
result = new clazz(a[0], a[1], a[2]);
break;
case 4:
result = new clazz(a[0], a[1], a[2], a[3]);
break;
case 5:
result = new clazz(a[0], a[1], a[2], a[3], a[4]);
break;
case 6:
result = new clazz(a[0], a[1], a[2], a[3], a[4], a[5]);
break;
case 7:
result = new clazz(a[0], a[1], a[2], a[3], a[4], a[5], a[6]);
break;
case 8:
result = new clazz(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7]);
break;
case 9:
result = new clazz(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8]);
break;
case 10:
result = new clazz(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9]);
break;
default:
result = new clazz();
}
return result;
}
/**
* Converts the double colon (::) in a fully qualified class name to a dot (.)
*/
public static function convertFullyQualifiedName(className:String):String {
return className.replace(PACKAGE_CLASS_SEPARATOR, ".");
}
// --------------------------------------------------------------------
//
// describeType cache implementation
//
// --------------------------------------------------------------------
/**
* The default value for the interval to clear the describe type cache.
*/
public static const CLEAR_CACHE_INTERVAL:uint = 60000;
/**
* The interval (in miliseconds) at which the cache will be cleared. Note that this value is only used
* on the first call to getFromObject.
*
* @default 60000 (one minute)
*/
public static var clearCacheInterval:uint = CLEAR_CACHE_INTERVAL;
private static var _describeTypeCache:Object = {};
private static var _timer:Timer;
/**
* Clears the describe type cache. This method is called automatically at regular intervals
* defined by the clearCacheInterval property.
*/
public static function clearDescribeTypeCache():void {
_describeTypeCache = {};
if (_timer && _timer.running) {
_timer.stop();
}
}
/**
* Timer handler. Clear the cache.
*/
private static function timerHandler(e:TimerEvent):void {
clearDescribeTypeCache();
}
/**
* Will return the metadata for the given object or class. If metadata has already been requested for
* this type, it will be retrieved from cache. Note that the metadata will allways be that of the class,
* even if you pass an instance.
* <p />
* In order to get instance specific metadata, use the 'factory' property.
* <p />
* The reason we do not allow retrieval of instance metadata is because then we would need to cache the
* metadata double. Metadata takes up a significant amount of memory.
*
* @param object The object from which you want to grab the metadata
*
* @return The class metadata of the given object.
*/
private static function getFromObject(object:Object):XML {
var className:String = getQualifiedClassName(object);
var metadata:XML;
if (_describeTypeCache.hasOwnProperty(className)) {
metadata = _describeTypeCache[className];
} else {
if (!_timer) {
// Only run the timer once to prevent unneeded overhead. This also prevents
// this class from falling for the bug described here:
// http://www.gskinner.com/blog/archives/2008/04/failure_to_unlo.html
_timer = new Timer(clearCacheInterval, 1);
_timer.addEventListener(TimerEvent.TIMER, timerHandler);
}
if (!(object is Class)) {
object = object.constructor;
}
metadata = describeType(object);
_describeTypeCache[className] = metadata;
// Only run the timer if it is not already running.
if (!_timer.running) {
_timer.start();
}
}
return metadata;
}
/**
* Will retrieve the metadata for the given class. Note that in order to access properties and
* methods you need to grab the 'factory' part of the metadata.
*
* @param className The name of the class that you want to retrieve metadata from. The className
* may be in the following forms: package.Class or package::Class
*/
private static function getFromString(className:String):XML {
var classDefinition:Class = getDefinitionByName(className) as Class;
// Calling getFromObject seems double, as it results in the getObjectMethod getting
// the class name using getQualifiedClassName. It however saves us a check on the
// given className which might be in two forms.
// getQualifiedClassName(getDefinitionByName(className)) is faster then converting the
// string using conventional methods.
return getFromObject(classDefinition);
}
}
}