r9549 - in helma-ng/trunk/src/org/helma: javascript util

[email protected]
Newsgroups gmane.comp.java.helma.cvs
Message-ID <20090320132816.A5F603D0D6@mia>
Author: hannes
Date: 2009-03-20 14:28:16 +0100 (Fri, 20 Mar 2009)
New Revision: 9549

Modified:
   helma-ng/trunk/src/org/helma/javascript/StaticMethods.java
   helma-ng/trunk/src/org/helma/util/ScriptUtils.java
Log:
Integrate null check into ScriptUtils.get*Argument() methods to simplify code and improve error messages.

Details at http://dev.helma.org/trac/helma/changeset/9549

Modified: helma-ng/trunk/src/org/helma/javascript/StaticMethods.java
===================================================================
--- helma-ng/trunk/src/org/helma/javascript/StaticMethods.java	2009-03-20 13:28:14 UTC (rev 9548)
+++ helma-ng/trunk/src/org/helma/javascript/StaticMethods.java	2009-03-20 13:28:16 UTC (rev 9549)
@@ -49,12 +49,10 @@
     public static void defineProperty(Context cx, Scriptable thisObj, Object[] args, Function funObj)
             throws JavaScriptException {
         ScriptUtils.checkArguments(args, 3, 3);
-        ScriptableObject obj = ScriptUtils.getScriptableArgument(args, 0);
-        String propname = ScriptUtils.getStringArgument(args, 1);
-        Scriptable desc = ScriptUtils.getScriptableArgument(args, 2);
-        if (obj == null || propname == null || desc == null) {
-            throw new IllegalArgumentException();
-        }
+        ScriptableObject obj = ScriptUtils.getScriptableArgument(args, 0, false);
+        String propname = ScriptUtils.getStringArgument(args, 1, false);
+        Scriptable desc = ScriptUtils.getScriptableArgument(args, 2, false);
+
         PropertyDescriptor propDesc = new PropertyDescriptor(desc);
         propDesc.defineProperty(obj, propname);
     }

Modified: helma-ng/trunk/src/org/helma/util/ScriptUtils.java
===================================================================
--- helma-ng/trunk/src/org/helma/util/ScriptUtils.java	2009-03-20 13:28:14 UTC (rev 9548)
+++ helma-ng/trunk/src/org/helma/util/ScriptUtils.java	2009-03-20 13:28:16 UTC (rev 9549)
@@ -74,7 +74,6 @@
             throw new IllegalArgumentException();
     }
 
-
     /**
      * Get an argument as ScriptableObject
      * @param args the argument array
@@ -82,10 +81,11 @@
      * @return the argument as ScriptableObject
      * @throws IllegalArgumentException if the argument can't be converted to a map
      */
-    public static ScriptableObject getScriptableArgument(Object[] args, int pos)
+    public static ScriptableObject getScriptableArgument(Object[] args, int pos, boolean allowNull)
             throws IllegalArgumentException {
         if (pos >= args.length || args[pos] == null || args[pos] == Undefined.instance) {
-            return null;
+            if (allowNull) return null;
+            throw new IllegalArgumentException("Argument " + (pos + 1) + " must not be null");
         } if (args[pos] instanceof ScriptableObject) {
             return (ScriptableObject) args[pos];
         }
@@ -98,9 +98,11 @@
      * @param pos the position of the requested argument
      * @return the argument as string
      */
-    public static String getStringArgument(Object[] args, int pos) {
-        if (pos >= args.length || args[pos] == null || args[pos] == Undefined.instance)
-            return null;
+    public static String getStringArgument(Object[] args, int pos, boolean allowNull) {
+        if (pos >= args.length || args[pos] == null || args[pos] == Undefined.instance) {
+            if (allowNull) return null;
+            throw new IllegalArgumentException("Argument " + (pos + 1) + " must not be null");
+        }
         return ScriptRuntime.toString(args[pos].toString());
     }
 
@@ -111,10 +113,11 @@
      * @return the argument as map
      * @throws IllegalArgumentException if the argument can't be converted to a map
      */
-    public static Map getMapArgument(Object[] args, int pos)
+    public static Map getMapArgument(Object[] args, int pos, boolean allowNull)
             throws IllegalArgumentException {
         if (pos >= args.length || args[pos] == null || args[pos] == Undefined.instance) {
-            return null;
+            if (allowNull) return null;
+            throw new IllegalArgumentException("Argument " + (pos + 1) + " must not be null");
         } if (args[pos] instanceof Map) {
             return (Map) args[pos];
         }
@@ -127,9 +130,11 @@
      * @param pos the position of the requested argument
      * @return the argument as object
      */
-    public static Object getObjectArgument(Object[] args, int pos) {
-        if (pos >= args.length || args[pos] == null || args[pos] == Undefined.instance)
-            return null;
+    public static Object getObjectArgument(Object[] args, int pos, boolean allowNull) {
+        if (pos >= args.length || args[pos] == null || args[pos] == Undefined.instance) {
+            if (allowNull) return null;
+            throw new IllegalArgumentException("Argument " + (pos + 1) + " must not be null");
+        }
         return Context.jsToJava(args[pos], Object.class);
     }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.