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 2020/09/08 03:04:27 UTC

[royale-asjs] branch develop updated: Adding two new utility methods for reflection: 'isDerivedType' function permits checking whether a class or interface TypeDefinition is a 'descendant' of another TypeDefinition 'isSameType' function is a trivial check to see if two TypeDefinitions are the same (works also if TypeDefinition.useCache is not enabled, when there can be more than one instance describing the same type). Additional Utils tests added for isDerivedType

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


The following commit(s) were added to refs/heads/develop by this push:
     new 1a372d2  Adding two new utility methods for reflection: 'isDerivedType' function permits checking whether a class or interface TypeDefinition is a 'descendant' of another TypeDefinition 'isSameType' function is a trivial check to see if two TypeDefinitions are the same (works also if TypeDefinition.useCache is not enabled, when there can be more than one instance describing the same type). Additional Utils tests added for isDerivedType
1a372d2 is described below

commit 1a372d29666810bc4fdaa94fe7c5048a771a8d9e
Author: greg-dove <gr...@gmail.com>
AuthorDate: Tue Sep 8 15:04:04 2020 +1200

    Adding two new utility methods for reflection:
    'isDerivedType' function permits checking whether a class or interface TypeDefinition is a 'descendant' of another TypeDefinition
    'isSameType' function is a trivial check to see if two TypeDefinitions are the same (works also if TypeDefinition.useCache is not enabled, when there can be more than one instance describing the same type).
    Additional Utils tests added for isDerivedType
---
 .../src/main/royale/ReflectionClasses.as           |   2 +
 .../org/apache/royale/reflection/ExtraData.as      |  13 ++-
 .../apache/royale/reflection/nativejs/AS3Object.as |  51 +++++++++
 .../royale/reflection/utils/isDerivedType.as       |  70 +++++++++++++
 .../apache/royale/reflection/utils/isSameType.as   |  50 +++++++++
 .../test/royale/flexUnitTests/ReflectionTester.as  |   5 +-
 .../reflection/ReflectionTesterNativeTypes.as      |   4 +-
 .../reflection/ReflectionTesterTestUtils.as        | 114 +++++++++++++++++++++
 .../support/TestClass3ex.as}                       |  32 +-----
 9 files changed, 310 insertions(+), 31 deletions(-)

diff --git a/frameworks/projects/Reflection/src/main/royale/ReflectionClasses.as b/frameworks/projects/Reflection/src/main/royale/ReflectionClasses.as
index b04df58..754c3a1 100644
--- a/frameworks/projects/Reflection/src/main/royale/ReflectionClasses.as
+++ b/frameworks/projects/Reflection/src/main/royale/ReflectionClasses.as
@@ -53,6 +53,8 @@ internal class ReflectionClasses
 	import org.apache.royale.reflection.utils.getMembersWithNameMatch; getMembersWithNameMatch;
 	import org.apache.royale.reflection.utils.getMembersWithQNameMatch; getMembersWithQNameMatch;
 	import org.apache.royale.reflection.utils.filterForMetaTags; filterForMetaTags;
+	import org.apache.royale.reflection.utils.isDerivedType; isDerivedType;
+	import org.apache.royale.reflection.utils.isSameType; isSameType;
 	
 	import org.apache.royale.reflection.ExtraData; ExtraData;
 	import org.apache.royale.reflection.AccessorDefinition; AccessorDefinition;
diff --git a/frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/ExtraData.as b/frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/ExtraData.as
index afd2d65..5d399b8 100644
--- a/frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/ExtraData.as
+++ b/frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/ExtraData.as
@@ -38,6 +38,9 @@ package org.apache.royale.reflection {
 		public static function get isRelevant():Boolean{
 			return false;
 		}
+
+		public static function reset():void{
+		}
 		
 		public static function hasData(key:*):Boolean{
 			return false
@@ -82,6 +85,13 @@ package org.apache.royale.reflection {
 		public static function get isRelevant():Boolean{
 			return true;
 		}
+
+		/**
+		 * this is non-reflectable
+		 */
+		public static function reset():void{
+			reflectionData = null;
+		}
 		
 		/**
 		 * this is non-reflectable
@@ -137,7 +147,8 @@ package org.apache.royale.reflection {
 				AS3Boolean(),
 				AS3int(),
 				AS3uint(),
-				AS3Vector()
+				AS3Vector(),
+				AS3Object()
 			];
 			while(items.length) addExternDefintion(items.pop());
 			
diff --git a/frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/nativejs/AS3Object.as b/frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/nativejs/AS3Object.as
new file mode 100644
index 0000000..12b7e22
--- /dev/null
+++ b/frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/nativejs/AS3Object.as
@@ -0,0 +1,51 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  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.nativejs {
+    
+    /**
+     * Provides data for a stand-in TypeDefinition of the native as3 type for javascript
+     */
+    COMPILE::JS
+    public function AS3Object():Object{
+        var ret:Object= {};
+        ret['classRef'] = Object;
+        ret['name'] = 'Object';
+        ret['NATIVE_TYPE'] = true;
+    
+        ret['ROYALE_CLASS_INFO'] = { names: [{ name: 'Object', qName: 'Object', kind: 'class', isDynamic: true }] };
+    
+        ret['ROYALE_REFLECTION_INFO'] = function():Object {
+            return {
+                'accessors':function():Object {
+                    return {
+                        'length':{'access':'readwrite','type':'uint','declaredBy':'Object'}
+                    };
+                },
+                'methods':function():Object {
+                    return {
+                        'Object':{'declaredBy':'Object','type':''}
+                    };
+                }
+            };
+        };
+        
+        return ret;
+    }
+    
+}
diff --git a/frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/utils/isDerivedType.as b/frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/utils/isDerivedType.as
new file mode 100644
index 0000000..7a6b9ff
--- /dev/null
+++ b/frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/utils/isDerivedType.as
@@ -0,0 +1,70 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  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
+{
+	import org.apache.royale.reflection.TypeDefinition;
+	
+	
+    /**
+     *  A utility method to check whether one definition is derived from another
+	 *
+	 *  This could be to check whether an interface extends another interface, or a class is a subclass of another class
+	 *  or whether a class implements an interface.
+	 *  
+     *  @param targetType TypeDefinition the definition to verify
+	 *  @param derivedFrom TypeDefinition the definition to verify against
+	 *  @return true if targetType parameter represents some form of 'descendant' of derivedFrom parameter
+     *
+     *  @langversion 3.0
+     *  @playerversion Flash 10.2
+     *  @playerversion AIR 2.6
+     *  @productversion Royale 0.0
+	 *
+	 *  @royaleignorecoercion org.apache.royale.reflection.TypeDefinition
+	 *  @royalesuppressexport
+     */
+    public function isDerivedType(targetType:TypeDefinition, derivedFrom:TypeDefinition):Boolean
+	{
+		if (!targetType || !derivedFrom) throw new Error('both arguments must be non-null');
+
+		//if this is the same definition, then it cannot be a derived type
+		if (targetType == derivedFrom || targetType.qualifiedName == derivedFrom.qualifiedName) return false;
+
+		var i:int, l:int;
+		var checks:Array;
+
+		if (derivedFrom.kind == 'class') {
+			if (targetType.kind == 'interface') return false; //an interface cannot be derived from (be a descendent of) a class
+			if (derivedFrom.qualifiedName == 'Object') return true; //always true
+			checks = targetType.baseClasses;
+		} else {
+			//derivedFrom is an interface
+			checks = targetType.interfaces;
+		}
+		l = checks.length;
+		var dervivedName:String = derivedFrom.qualifiedName;
+		for (i=0; i<l; i++) {
+			if (derivedFrom == checks[i] || dervivedName == TypeDefinition(checks[i]).qualifiedName) return true;
+		}
+
+		return false;
+    }
+	
+	
+}
diff --git a/frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/utils/isSameType.as b/frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/utils/isSameType.as
new file mode 100644
index 0000000..3586b10
--- /dev/null
+++ b/frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/utils/isSameType.as
@@ -0,0 +1,50 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  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
+{
+	import org.apache.royale.reflection.TypeDefinition;
+	
+	
+    /**
+     *  A utility method to check whether one definition represents the same type (Class or Interface) as another
+	 *
+	 *  This check is useful in situations where TypeDefinition.useCache is false
+	 *  
+     *  @param type1 TypeDefinition the first definition
+	 *  @param type2 TypeDefinition the second definition
+     *
+     *  @langversion 3.0
+     *  @playerversion Flash 10.2
+     *  @playerversion AIR 2.6
+     *  @productversion Royale 0.0
+	 *
+	 *  @royaleignorecoercion org.apache.royale.reflection.TypeDefinition
+	 *  @royalesuppressexport
+     */
+    public function isSameType(type1:TypeDefinition, type2:TypeDefinition):Boolean
+	{
+		if (!type1 || !type2) throw new Error('both arguments must be non-null');
+
+		//if caching is not enabled, then it is possible that there could be two separate instances representing the same type
+		return (type1 == type2 || type1.qualifiedName == type2.qualifiedName);
+
+    }
+	
+	
+}
diff --git a/frameworks/projects/Reflection/src/test/royale/flexUnitTests/ReflectionTester.as b/frameworks/projects/Reflection/src/test/royale/flexUnitTests/ReflectionTester.as
index b9282b9..dc248b6 100644
--- a/frameworks/projects/Reflection/src/test/royale/flexUnitTests/ReflectionTester.as
+++ b/frameworks/projects/Reflection/src/test/royale/flexUnitTests/ReflectionTester.as
@@ -36,7 +36,8 @@ package flexUnitTests
                 ReflectionTesterTestAlias,
                 ReflectionTesterTestDynamic,
                 ReflectionTesterNativeTypes,
-                ReflectionTesterTestEdgeCases
+                ReflectionTesterTestEdgeCases,
+                ReflectionTesterTestUtils
             ];
         }
         
@@ -51,5 +52,7 @@ package flexUnitTests
         public var reflectionTesterNativeTypes:ReflectionTesterNativeTypes;
     
         public var reflectionTesterTestEdgeCases:ReflectionTesterTestEdgeCases;
+
+        public var reflectionTesterTestUtils:ReflectionTesterTestUtils;
     }
 }
diff --git a/frameworks/projects/Reflection/src/test/royale/flexUnitTests/reflection/ReflectionTesterNativeTypes.as b/frameworks/projects/Reflection/src/test/royale/flexUnitTests/reflection/ReflectionTesterNativeTypes.as
index d831e45..3d4668a 100644
--- a/frameworks/projects/Reflection/src/test/royale/flexUnitTests/reflection/ReflectionTesterNativeTypes.as
+++ b/frameworks/projects/Reflection/src/test/royale/flexUnitTests/reflection/ReflectionTesterNativeTypes.as
@@ -44,17 +44,19 @@ package flexUnitTests.reflection
         [BeforeClass]
         public static function setUpBeforeClass():void
         {
+            ExtraData.addAll();
         }
         
         [AfterClass]
         public static function tearDownAfterClass():void
         {
+            ExtraData.reset();
         }
         
         [Before]
         public function setUp():void
         {
-            ExtraData.addAll();
+
         }
         
         [After]
diff --git a/frameworks/projects/Reflection/src/test/royale/flexUnitTests/reflection/ReflectionTesterTestUtils.as b/frameworks/projects/Reflection/src/test/royale/flexUnitTests/reflection/ReflectionTesterTestUtils.as
new file mode 100644
index 0000000..d89d238
--- /dev/null
+++ b/frameworks/projects/Reflection/src/test/royale/flexUnitTests/reflection/ReflectionTesterTestUtils.as
@@ -0,0 +1,114 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  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 flexUnitTests.reflection
+{
+    import org.apache.royale.test.asserts.*;
+    
+    import flexUnitTests.reflection.support.*;
+
+    import org.apache.royale.reflection.*
+    import org.apache.royale.reflection.utils.*;
+    
+    /**
+     * @royalesuppresspublicvarwarning
+     */
+    public class ReflectionTesterTestUtils
+    {
+        
+        public static var isJS:Boolean ;
+        
+        [BeforeClass]
+        public static function setUpBeforeClass():void
+        {
+            isJS = COMPILE::JS;
+            ExtraData.addAll();
+        }
+        
+        [AfterClass]
+        public static function tearDownAfterClass():void
+        {
+            ExtraData.reset();
+        }
+        
+        [Before]
+        public function setUp():void
+        {
+        
+        }
+        
+        [After]
+        public function tearDown():void
+        {
+        
+        }
+        
+        
+        [Test]
+        public function testIsDerivedType():void
+        {
+            var check:Boolean;
+            var targetCandidate:TypeDefinition;
+            var ancestry:TypeDefinition;
+            //various interface checks
+            ancestry = describeType(ITestInterface);
+            targetCandidate = describeType(ITestInterface2)
+            check = isDerivedType(targetCandidate, ancestry);
+            assertTrue(check, 'Unexpected ancestry check for '+targetCandidate.qualifiedName +"<--"+ancestry.qualifiedName );
+
+            targetCandidate = describeType(ITestInterface4)
+            check = isDerivedType(targetCandidate, ancestry);
+            assertTrue(check, 'Unexpected ancestry check for '+targetCandidate.qualifiedName +"<--"+ancestry.qualifiedName );
+
+            targetCandidate = describeType(ITestInterface)
+            check = isDerivedType(targetCandidate, ancestry);
+            assertFalse(check, 'Unexpected ancestry check for '+targetCandidate.qualifiedName +"<--"+ancestry.qualifiedName );
+
+            targetCandidate = describeType(TestClass3);
+            check = isDerivedType(targetCandidate, ancestry);
+            assertTrue(check, 'Unexpected ancestry check for '+targetCandidate.qualifiedName +"<--"+ancestry.qualifiedName );
+
+            targetCandidate = describeType(TestClass3ex);
+            check = isDerivedType(targetCandidate, ancestry);
+            assertTrue(check, 'Unexpected ancestry check for '+targetCandidate.qualifiedName +"<--"+ancestry.qualifiedName );
+
+            ancestry = describeType(TestClass1);
+            check = isDerivedType(targetCandidate, ancestry);
+            assertTrue(check, 'Unexpected ancestry check for '+targetCandidate.qualifiedName +"<--"+ancestry.qualifiedName );
+
+            targetCandidate = describeType(TestClass1);
+            check = isDerivedType(targetCandidate, ancestry);
+            //A class cannot be a derived type of itself
+            assertFalse(check, 'Unexpected ancestry check for '+targetCandidate.qualifiedName +"<--"+ancestry.qualifiedName );
+
+            //an interface is not considered to be derived from 'Object'
+            targetCandidate = describeType(ITestInterface)
+            ancestry = describeType(Object);
+            check = isDerivedType(targetCandidate, ancestry);
+            assertFalse(check, 'Unexpected ancestry check for '+targetCandidate.qualifiedName +"<--"+ancestry.qualifiedName );
+
+            //an class is considered to be derived from 'Object'
+            targetCandidate = describeType(TestClass1);
+            check = isDerivedType(targetCandidate, ancestry);
+            assertTrue(check, 'Unexpected ancestry check for '+targetCandidate.qualifiedName +"<--"+ancestry.qualifiedName );
+
+
+        }
+
+    }
+}
diff --git a/frameworks/projects/Reflection/src/test/royale/flexUnitTests/ReflectionTester.as b/frameworks/projects/Reflection/src/test/royale/flexUnitTests/reflection/support/TestClass3ex.as
similarity index 50%
copy from frameworks/projects/Reflection/src/test/royale/flexUnitTests/ReflectionTester.as
copy to frameworks/projects/Reflection/src/test/royale/flexUnitTests/reflection/support/TestClass3ex.as
index b9282b9..3a450ec 100644
--- a/frameworks/projects/Reflection/src/test/royale/flexUnitTests/ReflectionTester.as
+++ b/frameworks/projects/Reflection/src/test/royale/flexUnitTests/reflection/support/TestClass3ex.as
@@ -16,40 +16,16 @@
 //  limitations under the License.
 //
 ////////////////////////////////////////////////////////////////////////////////
-package flexUnitTests
+package flexUnitTests.reflection.support
 {
-    import flexUnitTests.reflection.*;
     
-    [Suite]
-    [RunWith("org.apache.royale.test.runners.SuiteRunner")]
+    
     /**
      * @royalesuppresspublicvarwarning
      */
-    public class ReflectionTester
+    public class TestClass3ex extends TestClass3
     {
-        public function ReflectionTester()
-        {
-            // see notes in CoreTester
-            var arr:Array = [
-                ReflectionTesterTest,
-                ReflectionTesterTestUseCache,
-                ReflectionTesterTestAlias,
-                ReflectionTesterTestDynamic,
-                ReflectionTesterNativeTypes,
-                ReflectionTesterTestEdgeCases
-            ];
-        }
-        
-        public var reflectionTesterCacheTest:ReflectionTesterTestUseCache;
-        public var reflectionTesterTest:ReflectionTesterTest;
-        
-        public var reflectionTesterAliasTest:ReflectionTesterTestAlias;
         
-        
-        public var reflectionTesterDynamicTest:ReflectionTesterTestDynamic;
-    
-        public var reflectionTesterNativeTypes:ReflectionTesterNativeTypes;
-    
-        public var reflectionTesterTestEdgeCases:ReflectionTesterTestEdgeCases;
+
     }
 }