You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@royale.apache.org by gr...@apache.org on 2019/09/22 06:01:47 UTC

[royale-asjs] 02/09: Improved reflection utility methods. Adds the ability to request more specific member types via optional bit-flag based parameter, works as before by default.

This is an automated email from the ASF dual-hosted git repository.

gregdove pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/royale-asjs.git

commit e37faca1d370f84424e823bacf4c993738d31383
Author: greg-dove <gr...@gmail.com>
AuthorDate: Sun Sep 22 08:02:43 2019 +1200

    Improved reflection utility methods. Adds the ability to request more specific member types via optional bit-flag based parameter, works as before by default.
---
 .../src/main/royale/ReflectionClasses.as           |  2 ++
 .../royale/reflection/DefinitionWithMetaData.as    |  4 ++-
 .../apache/royale/reflection/utils/MemberTypes.as  | 40 ++++++++++++++++++++++
 .../{getMembersWithMetadata.as => getMembers.as}   | 25 +++++++-------
 .../reflection/utils/getMembersWithMetadata.as     | 18 +++++-----
 5 files changed, 67 insertions(+), 22 deletions(-)

diff --git a/frameworks/projects/Reflection/src/main/royale/ReflectionClasses.as b/frameworks/projects/Reflection/src/main/royale/ReflectionClasses.as
index 640e170..49c7f49 100644
--- a/frameworks/projects/Reflection/src/main/royale/ReflectionClasses.as
+++ b/frameworks/projects/Reflection/src/main/royale/ReflectionClasses.as
@@ -47,6 +47,8 @@ internal class ReflectionClasses
 	import org.apache.royale.reflection.registerClassAlias; registerClassAlias;
 	//utils
 	import org.apache.royale.reflection.utils.getMembersWithMetadata; getMembersWithMetadata;
+	import org.apache.royale.reflection.utils.getMembers; getMembers;
+	import org.apache.royale.reflection.utils.MemberTypes; MemberTypes;
 	import org.apache.royale.reflection.utils.getStaticConstantsByConvention; getStaticConstantsByConvention;
 	import org.apache.royale.reflection.utils.getMembersWithNameMatch; getMembersWithNameMatch;
 	import org.apache.royale.reflection.utils.filterForMetaTags; filterForMetaTags;
diff --git a/frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/DefinitionWithMetaData.as b/frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/DefinitionWithMetaData.as
index 003a9b1..92f43b1 100755
--- a/frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/DefinitionWithMetaData.as
+++ b/frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/DefinitionWithMetaData.as
@@ -102,7 +102,9 @@ package org.apache.royale.reflection
             var source:Array = _metaData || metadata;
             var results:Array = [];
             var i:uint=0, l:uint = source.length;
-            for(;i<l;i++) if (source[i].name == name) results.push(source[i]);
+            if (l != 0) {
+                for(;i<l;i++) if (source[i].name == name) results.push(source[i]);
+            }
             return results;
         }
     }
diff --git a/frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/utils/MemberTypes.as b/frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/utils/MemberTypes.as
new file mode 100644
index 0000000..7f46323
--- /dev/null
+++ b/frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/utils/MemberTypes.as
@@ -0,0 +1,40 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  Licensed to the Apache Software Foundation (ASF) under one or more
+//  contributor license agreements.  See the NOTICE file distributed with
+//  this work for additional information regarding copyright ownership.
+//  The ASF licenses this file to You under the Apache License, Version 2.0
+//  (the "License"); you may not use this file except in compliance with
+//  the License.  You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+//  Unless required by applicable law or agreed to in writing, software
+//  distributed under the License is distributed on an "AS IS" BASIS,
+//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//  See the License for the specific language governing permissions and
+//  limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+package org.apache.royale.reflection.utils {
+    
+    
+    /**
+     * This is purely a convenience class for use of the constants in the utility methods
+     * in this package which support bitwise selection of subsets of member types that they return
+     * it is:
+     * a) not reflectable in javascript output
+     * and
+     * b) using these constants does not create a dependency on the class itself in javascript output.
+     *
+     * @royalesuppressexport
+     */
+    public class MemberTypes {
+        
+        public static const VARIABLES:uint = 1;
+        public static const ACCESSORS:uint = 2;
+        public static const METHODS:uint = 4;
+    }
+    
+    
+}
diff --git a/frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/utils/getMembersWithMetadata.as b/frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/utils/getMembers.as
similarity index 55%
copy from frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/utils/getMembersWithMetadata.as
copy to frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/utils/getMembers.as
index 927cd1c..436bbde 100644
--- a/frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/utils/getMembersWithMetadata.as
+++ b/frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/utils/getMembers.as
@@ -22,32 +22,31 @@ package org.apache.royale.reflection.utils
 	
 	
     /**
-     *  A utility method to retrieve all members with a single metadata tag name (String) 
-	 *  or one of various tag names (an Array of Strings)
-	 *  It will return variables, accessors or methods that have the specified metadata (assuming it is included in the build)
+     *  A utility method to retrieve all members
+	 *  It will return variables, accessors or methods by default
 	 *  
      *  @param fromDefinition the definition to retrieve the member definitions from
-	 *  @param tagsOrTag either a String or and Array of Strings to search for
-	 *  @param includeStatics true if static members should be searched. Defaults to false, so only instance members are searched
+	 *  @param includeStatics true if static members should be returned. Defaults to false, so only instance members are returned
+ 	 *  @param memberTypes a bitwise combination of MemberTypes constants to restrict returned items
+	 *  to VARIABLES, ACCESSORS, METHODS or specific combinations thereof, defaults to VARIABLES | ACCESSORS | METHODS
      *
      *  @langversion 3.0
      *  @playerversion Flash 10.2
      *  @playerversion AIR 2.6
      *  @productversion Royale 0.0
      */
-    public function getMembersWithMetadata(fromDefinition:TypeDefinition, tagsOrTag:Object, includeStatics:Boolean = false):Array
+    public function getMembers(fromDefinition:TypeDefinition, includeStatics:Boolean = false, memberTypes:uint = 7):Array
 	{
         var ret:Array = [];
 		if (!fromDefinition) return ret;
-		var search:Array = tagsOrTag is Array ? tagsOrTag as Array : [tagsOrTag+''];
 		if (includeStatics) {
-			filterForMetaTags(fromDefinition.staticVariables, search, ret);
-			filterForMetaTags(fromDefinition.staticAccessors, search, ret);
-			filterForMetaTags(fromDefinition.staticMethods, search, ret);
+			if ((memberTypes & MemberTypes.VARIABLES) !=0) ret = ret.concat(fromDefinition.staticVariables);
+			if ((memberTypes & MemberTypes.ACCESSORS) !=0) ret = ret.concat(fromDefinition.staticAccessors);
+			if ((memberTypes & MemberTypes.METHODS) !=0) ret = ret.concat(fromDefinition.staticMethods);
 		}
-		filterForMetaTags(fromDefinition.variables, search, ret);
-		filterForMetaTags(fromDefinition.accessors, search, ret);
-		filterForMetaTags(fromDefinition.methods, search, ret);
+		if ((memberTypes & MemberTypes.VARIABLES) !=0) ret = ret.concat(fromDefinition.variables);
+		if ((memberTypes & MemberTypes.ACCESSORS) !=0) ret = ret.concat(fromDefinition.accessors);
+		if ((memberTypes & MemberTypes.METHODS) !=0) ret = ret.concat(fromDefinition.methods);
 		return ret;
     }
 	
diff --git a/frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/utils/getMembersWithMetadata.as b/frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/utils/getMembersWithMetadata.as
index 927cd1c..aadc2f6 100644
--- a/frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/utils/getMembersWithMetadata.as
+++ b/frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/utils/getMembersWithMetadata.as
@@ -24,30 +24,32 @@ package org.apache.royale.reflection.utils
     /**
      *  A utility method to retrieve all members with a single metadata tag name (String) 
 	 *  or one of various tag names (an Array of Strings)
-	 *  It will return variables, accessors or methods that have the specified metadata (assuming it is included in the build)
+	 *  It will return variables, accessors or methods (by default) that have the specified metadata (assuming it is included in the build)
 	 *  
      *  @param fromDefinition the definition to retrieve the member definitions from
 	 *  @param tagsOrTag either a String or and Array of Strings to search for
 	 *  @param includeStatics true if static members should be searched. Defaults to false, so only instance members are searched
+	 *  @param memberTypes a bitwise combination of MemberTypes constants to restrict returned items
+	 *  to VARIABLES, ACCESSORS, METHODS or specific combinations thereof, defaults to VARIABLES | ACCESSORS | METHODS
      *
      *  @langversion 3.0
      *  @playerversion Flash 10.2
      *  @playerversion AIR 2.6
      *  @productversion Royale 0.0
      */
-    public function getMembersWithMetadata(fromDefinition:TypeDefinition, tagsOrTag:Object, includeStatics:Boolean = false):Array
+    public function getMembersWithMetadata(fromDefinition:TypeDefinition, tagsOrTag:Object, includeStatics:Boolean = false, memberTypes:uint = 7):Array
 	{
         var ret:Array = [];
 		if (!fromDefinition) return ret;
 		var search:Array = tagsOrTag is Array ? tagsOrTag as Array : [tagsOrTag+''];
 		if (includeStatics) {
-			filterForMetaTags(fromDefinition.staticVariables, search, ret);
-			filterForMetaTags(fromDefinition.staticAccessors, search, ret);
-			filterForMetaTags(fromDefinition.staticMethods, search, ret);
+			if ((memberTypes & MemberTypes.VARIABLES) !=0) filterForMetaTags(fromDefinition.staticVariables, search, ret);
+			if ((memberTypes & MemberTypes.ACCESSORS) !=0) filterForMetaTags(fromDefinition.staticAccessors, search, ret);
+			if ((memberTypes & MemberTypes.METHODS) !=0) filterForMetaTags(fromDefinition.staticMethods, search, ret);
 		}
-		filterForMetaTags(fromDefinition.variables, search, ret);
-		filterForMetaTags(fromDefinition.accessors, search, ret);
-		filterForMetaTags(fromDefinition.methods, search, ret);
+		if ((memberTypes & MemberTypes.VARIABLES) !=0) filterForMetaTags(fromDefinition.variables, search, ret);
+		if ((memberTypes & MemberTypes.ACCESSORS) !=0) filterForMetaTags(fromDefinition.accessors, search, ret);
+		if ((memberTypes & MemberTypes.METHODS) !=0) filterForMetaTags(fromDefinition.methods, search, ret);
 		return ret;
     }