PageRenderTime 829ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/source/core/aw/utils/propertyinspector/PropertyInspectorItem.as

http://jsinterface.googlecode.com/
ActionScript | 757 lines | 167 code | 52 blank | 538 comment | 16 complexity | 50d2950e12635670d6a82d6aca94148c MD5 | raw file
  1. package aw.utils.propertyinspector{
  2. import aw.data.BasicValue;
  3. import flash.utils.describeType;
  4. import flash.utils.getDefinitionByName;
  5. /**
  6. * Objects of this class contain information on the classes located in the system.
  7. * Objects of this class can be instantiated directly with the constructor, but
  8. * itÂ’s recommended to do this with methods of PropertyInspector class where an
  9. * information cashing mechanism is present.
  10. * @example ?????? ????????? ?????? ?? ?????? PropertyInspectorItem
  11. <listing version="3.0">
  12. package {
  13. import aw.utils.PropertyInspector;
  14. import aw.utils.propertyinspector.PropertyInspectorItem;
  15. import flash.display.MovieClip;
  16. import flash.display.Sprite;
  17. import flash.geom.Rectangle;
  18. public class Test extends Sprite{
  19. public function Test():void{
  20. super();
  21. var pii:PropertyInspectorItem = PropertyInspector.getInfo(MovieClip);
  22. // Is object dynamic
  23. trace(pii.isDynamic);
  24. // All methods that returns value of Rectangle type
  25. trace(pii.getMethodsByType(Rectangle));
  26. // Get object PropertyItem, containing information about the properties of the object
  27. pii.getByName('width');
  28. }
  29. }
  30. }
  31. </listing>
  32. * For information about static member of class, use methods in the static_ns namespace
  33. *
  34. * @public
  35. * @author Galaburda a_[w] Oleg http://www.actualwave.com
  36. * @see aw.utils.PropertyInspector
  37. * @see aw.utils.propertyinspector.static_ns
  38. */
  39. public class PropertyInspectorItem extends Object{
  40. /**
  41. *
  42. *
  43. *
  44. * @public (constant)
  45. * @langversion ActionScript 3.0
  46. * @playerversion Flash 9.0.28.0
  47. */
  48. static public const VAR_NODE_NAME:String = 'variable';
  49. /**
  50. *
  51. *
  52. *
  53. * @public (constant)
  54. * @langversion ActionScript 3.0
  55. * @playerversion Flash 9.0.28.0
  56. */
  57. static public const CONST_NODE_NAME:String = 'constant';
  58. /**
  59. *
  60. *
  61. *
  62. * @public (constant)
  63. * @langversion ActionScript 3.0
  64. * @playerversion Flash 9.0.28.0
  65. */
  66. static public const GETSET_NODE_NAME:String = 'accessor';
  67. /**
  68. *
  69. *
  70. *
  71. * @public (constant)
  72. * @langversion ActionScript 3.0
  73. * @playerversion Flash 9.0.28.0
  74. */
  75. static public const METHOD_NODE_NAME:String = 'method';
  76. /**
  77. *
  78. *
  79. *
  80. * @public (constant)
  81. * @langversion ActionScript 3.0
  82. * @playerversion Flash 9.0.28.0
  83. */
  84. static public const PROTOTYPE_NODE_NAME:String = 'factory';
  85. /**
  86. *
  87. *
  88. *
  89. * @public (constant)
  90. * @langversion ActionScript 3.0
  91. * @playerversion Flash 9.0.28.0
  92. */
  93. static public const NAME_ATTR_NAME:String = 'name';
  94. /**
  95. *
  96. *
  97. *
  98. * @public (constant)
  99. * @langversion ActionScript 3.0
  100. * @playerversion Flash 9.0.28.0
  101. */
  102. static public const ACCESS_ATTR_NAME:String = 'access';
  103. /**
  104. *
  105. *
  106. *
  107. * @public (constant)
  108. * @langversion ActionScript 3.0
  109. * @playerversion Flash 9.0.28.0
  110. */
  111. static public const TYPE_ATTR_NAME:String = 'type';
  112. /**
  113. *
  114. *
  115. *
  116. * @public (constant)
  117. * @langversion ActionScript 3.0
  118. * @playerversion Flash 9.0.28.0
  119. */
  120. static public const URI_ATTR_NAME:String = 'uri';
  121. /**
  122. *
  123. *
  124. *
  125. * @public (constant)
  126. * @langversion ActionScript 3.0
  127. * @playerversion Flash 9.0.28.0
  128. */
  129. static public const RETURNTYPE_ATTR_NAME:String = 'returnType';
  130. /**
  131. *
  132. *
  133. *
  134. * @public (constant)
  135. * @langversion ActionScript 3.0
  136. * @playerversion Flash 9.0.28.0
  137. */
  138. static public const VOID_RETURN_TYPE_NAME:String = 'void';
  139. /**
  140. *
  141. *
  142. *
  143. * @public (constant)
  144. * @langversion ActionScript 3.0
  145. * @playerversion Flash 9.0.28.0
  146. */
  147. static public const ANY_RETURN_TYPE_NAME:String = '*';
  148. /**
  149. *
  150. *
  151. *
  152. * @public (constant)
  153. * @langversion ActionScript 3.0
  154. * @playerversion Flash 9.0.28.0
  155. */
  156. static public const READ_VALUE:String = 'readonly';
  157. /**
  158. *
  159. *
  160. *
  161. * @public (constant)
  162. * @langversion ActionScript 3.0
  163. * @playerversion Flash 9.0.28.0
  164. */
  165. static public const WRITE_VALUE:String = 'writeonly';
  166. /**
  167. *
  168. *
  169. *
  170. * @public (constant)
  171. * @langversion ActionScript 3.0
  172. * @playerversion Flash 9.0.28.0
  173. */
  174. static public const READWRITE_VALUE:String = 'readwrite';
  175. /**
  176. *
  177. *
  178. *
  179. * @private (protected)
  180. * @langversion ActionScript 3.0
  181. * @playerversion Flash 9.0.28.0
  182. */
  183. protected var _isDynamic:Boolean;
  184. /**
  185. * Contains the names of all members of the class
  186. *
  187. *
  188. * @private (protected)
  189. * @langversion ActionScript 3.0
  190. * @playerversion Flash 9.0.28.0
  191. */
  192. protected var _names:Object;
  193. /**
  194. * An array of objects PropertyItem, describing the properties
  195. *
  196. *
  197. * @private (protected)
  198. * @langversion ActionScript 3.0
  199. * @playerversion Flash 9.0.28.0
  200. * @see aw.utils.propertyinspector.PropertyItem
  201. */
  202. protected var _properties:Array;
  203. /**
  204. * An array of objects PropertyItem, describing the methods
  205. *
  206. *
  207. * @private (protected)
  208. * @langversion ActionScript 3.0
  209. * @playerversion Flash 9.0.28.0
  210. * @see aw.utils.propertyinspector.PropertyItem
  211. */
  212. protected var _methods:Array;
  213. /**
  214. * Contains the names of all static members of the class
  215. *
  216. *
  217. * @private (protected)
  218. * @langversion ActionScript 3.0
  219. * @playerversion Flash 9.0.28.0
  220. */
  221. protected var _staticNames:Object;
  222. /**
  223. * An array of objects PropertyItem, describing the static properties
  224. *
  225. *
  226. * @private (protected)
  227. * @langversion ActionScript 3.0
  228. * @playerversion Flash 9.0.28.0
  229. * @see aw.utils.propertyinspector.PropertyItem
  230. */
  231. protected var _staticProperties:Array;
  232. /**
  233. * An array of objects PropertyItem, describing the static methods
  234. *
  235. *
  236. * @private (protected)
  237. * @langversion ActionScript 3.0
  238. * @playerversion Flash 9.0.28.0
  239. * @see aw.utils.propertyinspector.PropertyItem
  240. */
  241. protected var _staticMethods:Array;
  242. /**
  243. * Creates an object containing all the information about the class and its member
  244. *
  245. *
  246. * @public
  247. * @param o
  248. * @return void
  249. * @langversion ActionScript 3.0
  250. * @playerversion Flash 9.0.28.0
  251. */
  252. public function PropertyInspectorItem(o:Class):void{
  253. super();
  254. _names = new Object();
  255. _properties = new Array();
  256. _methods = new Array();
  257. _staticNames = new Object();
  258. _staticProperties = new Array();
  259. _staticMethods = new Array();
  260. saveInfo(o);
  261. }
  262. /**
  263. * Is class dynamic
  264. *
  265. * @public (getter)
  266. * @return Boolean
  267. * @langversion ActionScript 3.0
  268. * @playerversion Flash 9.0.28.0
  269. */
  270. public function get isDynamic():Boolean{
  271. return this._isDynamic;
  272. }
  273. /**
  274. * An array of objects PropertyItem, describing the properties
  275. *
  276. *
  277. * @public (getter)
  278. * @return Array
  279. * @langversion ActionScript 3.0
  280. * @playerversion Flash 9.0.28.0
  281. */
  282. public function get properties():Array{
  283. return this._properties.concat();
  284. }
  285. /**
  286. * An array of objects PropertyItem, describing the static properties
  287. *
  288. * @copy #_staticProperties
  289. * @public (static_ns,getter)
  290. * @return Array
  291. * @langversion ActionScript 3.0
  292. * @playerversion Flash 9.0.28.0
  293. */
  294. static_ns function get properties():Array{
  295. return this._staticProperties.concat();
  296. }
  297. /**
  298. * Retrieve object PropertyItem, by name of a member of the class
  299. *
  300. *
  301. * @public
  302. * @param name
  303. * @return PropertyItem
  304. * @see aw.utils.propertyinspector.PropertyItem
  305. * @langversion ActionScript 3.0
  306. * @playerversion Flash 9.0.28.0
  307. */
  308. public function getByName(name:*):PropertyItem{
  309. return this._names[String(name)];
  310. }
  311. /**
  312. * Retrieve object PropertyItem, by name of a static member of the class
  313. *
  314. *
  315. * @public (static_ns)
  316. * @param name
  317. * @return PropertyItem
  318. * @see aw.utils.propertyinspector.PropertyItem
  319. * @langversion ActionScript 3.0
  320. * @playerversion Flash 9.0.28.0
  321. */
  322. static_ns function getByName(name:*):PropertyItem{
  323. return this._staticNames[String(name)];
  324. }
  325. /**
  326. * Verifies the existence of a member of the class, by its name.
  327. *
  328. *
  329. * @public
  330. * @param name
  331. * @return Boolean
  332. * @langversion ActionScript 3.0
  333. * @playerversion Flash 9.0.28.0
  334. */
  335. public function isExists(name:*):Boolean{
  336. return (String(name) in this._names);
  337. }
  338. /**
  339. * Verifies the existence of a static member of the class, by its name.
  340. *
  341. *
  342. * @public (static_ns)
  343. * @param name
  344. * @return Boolean
  345. * @langversion ActionScript 3.0
  346. * @playerversion Flash 9.0.28.0
  347. */
  348. static_ns function isExists(name:*):Boolean{
  349. return (String(name) in this._staticNames);
  350. }
  351. /**
  352. * Returns TRUE, if passed the name of the property and FALSE - if the name of the method.
  353. *
  354. *
  355. * @public
  356. * @param name
  357. * @return Boolean
  358. * @langversion ActionScript 3.0
  359. * @playerversion Flash 9.0.28.0
  360. */
  361. public function isProperty(name:*):Boolean{
  362. var prop:PropertyItem = this._names[String(name)];
  363. return (prop && prop.access!=PropertyAccessType.METHOD);
  364. }
  365. /**
  366. * Returns TRUE, if passed the name of the property and FALSE - if the name of the method. For static members only.
  367. *
  368. *
  369. * @public (static_ns)
  370. * @param name
  371. * @return Boolean
  372. * @langversion ActionScript 3.0
  373. * @playerversion Flash 9.0.28.0
  374. */
  375. static_ns function isProperty(name:*):Boolean{
  376. var prop:PropertyItem = this._staticNames[String(name)];
  377. return (prop && prop.access!=PropertyAccessType.METHOD);
  378. }
  379. /**
  380. * Separates properties by level of access
  381. *
  382. *
  383. * @public
  384. * @param acc
  385. * @return Array
  386. * @langversion ActionScript 3.0
  387. * @playerversion Flash 9.0.28.0
  388. */
  389. public function getProperties(acc:int=-1):Array{
  390. return this.propertiesByAccess(this._properties, acc);
  391. }
  392. /**
  393. * Separates static properties by level of access
  394. *
  395. *
  396. * @public (static_ns)
  397. * @param acc
  398. * @return Array
  399. * @langversion ActionScript 3.0
  400. * @playerversion Flash 9.0.28.0
  401. */
  402. static_ns function getProperties(acc:int=-1):Array{
  403. return this.propertiesByAccess(this._staticProperties, acc);
  404. }
  405. /**
  406. * Separates properties by level of access from list
  407. *
  408. *
  409. * @private (protected)
  410. * @param arr
  411. * @param acc
  412. * @return Array
  413. * @langversion ActionScript 3.0
  414. * @playerversion Flash 9.0.28.0
  415. */
  416. protected function propertiesByAccess(arr:Array, acc:int=-1):Array{
  417. var ret:Array;
  418. if(acc<0){
  419. ret = arr.concat();
  420. }else{
  421. var len:int = arr.length;
  422. ret = new Array();
  423. var item:PropertyItem;
  424. for(var i:int=0; i<len; i++){
  425. item = arr[i] as PropertyItem;
  426. if(item.access==acc) ret.push(item);
  427. }
  428. }
  429. return ret;
  430. }
  431. /**
  432. * Separates properties by the type of class member
  433. *
  434. *
  435. * @public
  436. * @param cont
  437. * @return Array
  438. * @langversion ActionScript 3.0
  439. * @playerversion Flash 9.0.28.0
  440. */
  441. public function getPropertiesByContainer(cont:int=-1):Array{
  442. return this.propertiesByContainer(this._properties, cont);
  443. }
  444. /**
  445. * Separates static properties by the type of class member
  446. *
  447. *
  448. * @public (static_ns)
  449. * @param cont
  450. * @return Array
  451. * @langversion ActionScript 3.0
  452. * @playerversion Flash 9.0.28.0
  453. */
  454. static_ns function getPropertiesByContainer(cont:int=-1):Array{
  455. return this.propertiesByContainer(this._staticProperties, cont);
  456. }
  457. /**
  458. * Separates properties by the type of class member from list
  459. *
  460. *
  461. * @private (protected)
  462. * @param arr
  463. * @param cont
  464. * @return Array
  465. * @langversion ActionScript 3.0
  466. * @playerversion Flash 9.0.28.0
  467. */
  468. protected function propertiesByContainer(arr:Array, cont:int=-1):Array{
  469. var ret:Array;
  470. if(cont<0){
  471. ret = arr.concat();
  472. }else{
  473. var len:int = arr.length;
  474. ret = new Array();
  475. var item:PropertyItem;
  476. for(var i:int=0; i<len; i++){
  477. item = arr[i] as PropertyItem;
  478. if(item.container==cont) ret.push(item);
  479. }
  480. }
  481. return ret;
  482. }
  483. /**
  484. * Separates the properties by value type
  485. *
  486. *
  487. * @public
  488. * @param cls
  489. * @return Array
  490. * @langversion ActionScript 3.0
  491. * @playerversion Flash 9.0.28.0
  492. */
  493. public function getPropertiesByType(cls:Class):Array{
  494. return this.propertiesByType(this._properties, cls);
  495. }
  496. /**
  497. * Separates the static properties by value type
  498. *
  499. *
  500. * @public (static_ns)
  501. * @param cls
  502. * @return Array
  503. * @langversion ActionScript 3.0
  504. * @playerversion Flash 9.0.28.0
  505. */
  506. static_ns function getPropertiesByType(cls:Class):Array{
  507. return this.propertiesByType(this._staticProperties, cls);
  508. }
  509. /**
  510. * Separates the properties by value type from list
  511. *
  512. *
  513. * @private (protected)
  514. * @param arr
  515. * @param cls
  516. * @return Array
  517. * @langversion ActionScript 3.0
  518. * @playerversion Flash 9.0.28.0
  519. */
  520. protected function propertiesByType(arr:Array, cls:Class):Array{
  521. var len:int = arr.length;
  522. var ret:Array = new Array();
  523. var item:PropertyItem;
  524. for(var i:int=0; i<len; i++){
  525. item = arr[i] as PropertyItem;
  526. if(item.type==cls) ret.push(item);
  527. }
  528. return ret;
  529. }
  530. /**
  531. * Separates the methods by return type
  532. *
  533. *
  534. * @public
  535. * @param cls
  536. * @return Array
  537. * @langversion ActionScript 3.0
  538. * @playerversion Flash 9.0.28.0
  539. */
  540. public function getMethodsByType(cls:Class):Array{
  541. return this.methodsByType(this._methods, cls);
  542. }
  543. /**
  544. * Separates the static methods by return type
  545. *
  546. *
  547. * @public (static_ns)
  548. * @param cls
  549. * @return Array
  550. * @langversion ActionScript 3.0
  551. * @playerversion Flash 9.0.28.0
  552. */
  553. static_ns function getMethodsByType(cls:Class):Array{
  554. return this.methodsByType(this._staticMethods, cls);
  555. }
  556. /**
  557. * Separates the methods by return type from list
  558. *
  559. *
  560. * @private (protected)
  561. * @param arr
  562. * @param cls
  563. * @return Array
  564. * @langversion ActionScript 3.0
  565. * @playerversion Flash 9.0.28.0
  566. */
  567. protected function methodsByType(arr:Array, cls:Class):Array{
  568. var len:int = arr.length;
  569. var ret:Array = new Array();
  570. var item:PropertyItem;
  571. for(var i:int=0; i<len; i++){
  572. item = arr[i] as PropertyItem;
  573. if(item.type==cls) ret.push(item);
  574. }
  575. return ret;
  576. }
  577. /**
  578. *
  579. *
  580. * @copy #_methods
  581. * @public (getter)
  582. * @return Array
  583. * @langversion ActionScript 3.0
  584. * @playerversion Flash 9.0.28.0
  585. */
  586. public function get methods():Array{
  587. return this._methods.concat();
  588. }
  589. /**
  590. *
  591. *
  592. * @copy #_staticMethods
  593. * @public (static_ns,getter)
  594. * @return Array
  595. * @langversion ActionScript 3.0
  596. * @playerversion Flash 9.0.28.0
  597. */
  598. static_ns function get methods():Array{
  599. return this._staticMethods.concat();
  600. }
  601. /**
  602. * Processes and stores information about object class
  603. *
  604. *
  605. * @private (protected)
  606. * @param o
  607. * @return void
  608. * @langversion ActionScript 3.0
  609. * @playerversion Flash 9.0.28.0
  610. */
  611. protected function saveInfo(o:Class):void{
  612. var x:XML = describeType(o);
  613. this._isDynamic = BasicValue.expectsBoolean(x.@isDynamic);
  614. saveInfoTo(x, this._staticNames, this._staticProperties, this._staticMethods);
  615. saveInfoTo(x.factory[0], this._names, this._properties, this._methods);
  616. }
  617. /**
  618. * Parses the received XML and creates objects PropertyItem to find data about the members of the class
  619. *
  620. *
  621. * @private (protected)
  622. * @param x
  623. * @param names
  624. * @param properties
  625. * @param methods
  626. * @return void
  627. * @langversion ActionScript 3.0
  628. * @playerversion Flash 9.0.28.0
  629. */
  630. static protected function saveInfoTo(x:XML, names:Object, properties:Array, methods:Array):void{
  631. var types:Object = PropertyAccessType.TYPES;
  632. var item:PropertyItem;
  633. x.variable.(item = new PropertyItem(attribute(NAME_ATTR_NAME), getDefinition(attribute(TYPE_ATTR_NAME)), types[attribute(ACCESS_ATTR_NAME)], PropertyContainerType.VARIABLE, String(attribute(URI_ATTR_NAME))), names[String(item.name)] = item, properties.push(item));
  634. x.accessor.(item = new PropertyItem(attribute(NAME_ATTR_NAME), getDefinition(attribute(TYPE_ATTR_NAME)), types[attribute(ACCESS_ATTR_NAME)], PropertyContainerType.ACCESSOR, String(attribute(URI_ATTR_NAME))), names[String(item.name)] = item, properties.push(item));
  635. x.constant.(item = new PropertyItem(attribute(NAME_ATTR_NAME), getDefinition(attribute(TYPE_ATTR_NAME)), PropertyAccessType.READONLY, PropertyContainerType.CONSTANT, String(attribute(URI_ATTR_NAME))), names[String(item.name)] = item, properties.push(item));
  636. x.method.(item = new PropertyItem(attribute(NAME_ATTR_NAME), getDefinition(attribute(RETURNTYPE_ATTR_NAME)), PropertyAccessType.METHOD, PropertyContainerType.METHOD, String(attribute(URI_ATTR_NAME))), names[String(item.name)] = item, methods.push(item));
  637. /*
  638. var types:Object = PropertyAccessType.TYPES;
  639. var i:int;
  640. var node:XML;
  641. var name:String;
  642. var item:PropertyItem;
  643. var xlist:XMLList = x.variable;
  644. var prop:int = PropertyContainerType.VARIABLE;
  645. var len:int = xlist.length();
  646. for(i=0; i<len; i++){
  647. node = xlist[i];
  648. name = node.@name;
  649. item = new PropertyItem(name, getDefinition(node.@type), types[node.@access], prop, String(node.@uri));
  650. names[String(item.name)] = item;
  651. properties.push(item);
  652. }
  653. xlist = x.accessor;
  654. prop = PropertyContainerType.ACCESSOR;
  655. len = xlist.length();
  656. for(i=0; i<len; i++){
  657. node = xlist[i];
  658. name = node.@name;
  659. item = new PropertyItem(name, getDefinition(node.@type), types[node.@access], prop, String(node.@uri));
  660. names[String(item.name)] = item;
  661. properties.push(item);
  662. }
  663. xlist = x.constant;
  664. prop = PropertyContainerType.CONSTANT;
  665. len = xlist.length();
  666. for(i=0; i<len; i++){
  667. node = xlist[i];
  668. name = node.@name;
  669. item = new PropertyItem(name, getDefinition(node.@type), PropertyAccessType.READONLY, prop, String(node.@uri));
  670. names[String(item.name)] = item;
  671. properties.push(item);
  672. }
  673. xlist = x.method;
  674. prop = PropertyContainerType.METHOD;
  675. len = xlist.length();
  676. var type:String;
  677. for(i=0; i<len; i++){
  678. node = xlist[i];
  679. name = node.@name;
  680. type = node.@returnType;
  681. item = new PropertyItem(name, getDefinition(type), PropertyAccessType.METHOD, prop, String(node.@uri));
  682. names[String(item.name)] = item;
  683. methods.push(item);
  684. }
  685. */
  686. }
  687. /**
  688. *
  689. *
  690. *
  691. * @private (protected)
  692. * @param str
  693. * @return Class
  694. * @langversion ActionScript 3.0
  695. * @playerversion Flash 9.0.28.0
  696. */
  697. static protected function getDefinition(str:String):Class{
  698. if(str==VOID_RETURN_TYPE_NAME) return undefined;
  699. else if(str==ANY_RETURN_TYPE_NAME) return Object;
  700. else return getDefinitionByName(str) as Class;
  701. }
  702. }
  703. }