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 2022/01/07 02:23:35 UTC

[royale-asjs] branch develop updated: Add a simple utility method to check whether a name is defined (before requesting its definition). The implementation can be optimized later.

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 423dea8  Add a simple utility method to check whether a name is defined (before requesting its definition). The implementation can be optimized later.
423dea8 is described below

commit 423dea8ab380c4a904d458e83ca6f8ef5f837d86
Author: greg-dove <gr...@gmail.com>
AuthorDate: Fri Jan 7 15:23:23 2022 +1300

    Add a simple utility method to check whether a name is defined (before requesting its definition). The implementation can be optimized later.
---
 .../src/main/royale/ReflectionClasses.as           |  1 +
 .../royale/reflection/hasDefinitionWithName.as     | 43 ++++++++++++++++++++++
 .../reflection/ReflectionTesterTestEdgeCases.as    | 13 +++++++
 3 files changed, 57 insertions(+)

diff --git a/frameworks/projects/Reflection/src/main/royale/ReflectionClasses.as b/frameworks/projects/Reflection/src/main/royale/ReflectionClasses.as
index e498a79..294e69e 100644
--- a/frameworks/projects/Reflection/src/main/royale/ReflectionClasses.as
+++ b/frameworks/projects/Reflection/src/main/royale/ReflectionClasses.as
@@ -40,6 +40,7 @@ internal class ReflectionClasses
 	import org.apache.royale.reflection.getAliasByClass; getAliasByClass;
 	import org.apache.royale.reflection.getClassByAlias; getClassByAlias;
 	import org.apache.royale.reflection.getDefinitionByName; getDefinitionByName;
+	import org.apache.royale.reflection.hasDefinitionWithName; hasDefinitionWithName;
 	import org.apache.royale.reflection.getDynamicFields; getDynamicFields;
 	import org.apache.royale.reflection.isDynamicObject; isDynamicObject;
 	import org.apache.royale.reflection.getQualifiedClassName; getQualifiedClassName;
diff --git a/frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/hasDefinitionWithName.as b/frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/hasDefinitionWithName.as
new file mode 100644
index 0000000..e846983
--- /dev/null
+++ b/frameworks/projects/Reflection/src/main/royale/org/apache/royale/reflection/hasDefinitionWithName.as
@@ -0,0 +1,43 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  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
+{
+
+    
+    /**
+     *  A simple way to check if a name is defined before requesting its definition (which will throw a RTE if it is invalid)
+     * 
+     *  @langversion 3.0
+     *  @playerversion Flash 10.2
+     *  @playerversion AIR 2.6
+     *  @productversion Royale 0.0
+     */
+    public function hasDefinitionWithName(name:String):Boolean
+	{
+        //for now wrapping in try/catch - we may be able to optimize this later.
+        var ret:Boolean;
+        try{
+            var cl:Class = getDefinitionByName(name);
+            ret = true;
+        } catch (e:Error){
+
+        }
+        return ret;
+    }
+}
diff --git a/frameworks/projects/Reflection/src/test/royale/flexUnitTests/reflection/ReflectionTesterTestEdgeCases.as b/frameworks/projects/Reflection/src/test/royale/flexUnitTests/reflection/ReflectionTesterTestEdgeCases.as
index 557ef2c..65e9320 100644
--- a/frameworks/projects/Reflection/src/test/royale/flexUnitTests/reflection/ReflectionTesterTestEdgeCases.as
+++ b/frameworks/projects/Reflection/src/test/royale/flexUnitTests/reflection/ReflectionTesterTestEdgeCases.as
@@ -74,6 +74,19 @@ package flexUnitTests.reflection
         public function testNull():void
         {
             var err:Boolean;
+
+            var noClass:Boolean = hasDefinitionWithName('nothing_to_see');
+            var hasClass:Boolean = hasDefinitionWithName('flexUnitTests.reflection.support.TestClass1');
+            assertFalse(noClass, 'hasDefinitionWithName for nothing_to_see should be false');
+
+            assertTrue(hasClass, 'hasDefinitionWithName for flexUnitTests.reflection.support.TestClass1 should be true')
+        }
+
+
+        [Test]
+        public function testHasDefinition():void
+        {
+            var err:Boolean;
             try{
                 var test:* = getDefinitionByName('null');
             } catch(e:Error) {