Revision: 447
          http://svn.sourceforge.net/stripes/?rev=447&view=rev
Author:   tfenne
Date:     2006-10-24 17:28:43 -0700 (Tue, 24 Oct 2006)

Log Message:
-----------
Fix for STS-291: InputHiddenTag (and potentially other places) cast all arrays 
to Object[] causing class cast exceptions for primitive arrays

Modified Paths:
--------------
    trunk/stripes/src/net/sourceforge/stripes/ajax/JavaScriptBuilder.java
    trunk/stripes/src/net/sourceforge/stripes/tag/InputHiddenTag.java
    trunk/stripes/src/net/sourceforge/stripes/tag/InputTagSupport.java
    trunk/stripes/src/net/sourceforge/stripes/util/CollectionUtil.java
    trunk/stripes/src/net/sourceforge/stripes/util/Log.java
    trunk/stripes/src/net/sourceforge/stripes/util/UrlBuilder.java

Modified: trunk/stripes/src/net/sourceforge/stripes/ajax/JavaScriptBuilder.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/ajax/JavaScriptBuilder.java       
2006-10-24 12:06:07 UTC (rev 446)
+++ trunk/stripes/src/net/sourceforge/stripes/ajax/JavaScriptBuilder.java       
2006-10-25 00:28:43 UTC (rev 447)
@@ -29,6 +29,7 @@
 import java.util.Random;
 import java.util.Set;
 import java.lang.reflect.Method;
+import java.lang.reflect.Array;
 
 /**
  * <p>Builds a set of JavaScript statements that will re-construct the value 
of a Java object,
@@ -301,7 +302,7 @@
                 buildCollectionNode(targetName, (Collection) in);
             }
             else if (in.getClass().isArray()) {
-                buildArrayNode(targetName, (Object[]) in);
+                buildArrayNode(targetName, in);
             }
             else if (Map.class.isAssignableFrom(in.getClass())) {
                 buildMapNode(targetName, (Map) in);
@@ -418,20 +419,23 @@
      * @param targetName The generated name of the array node being translated.
      * @param in The Array being translated.
      */
-    void buildArrayNode(String targetName, Object[] in) throws Exception {
+    void buildArrayNode(String targetName, Object in) throws Exception {
         StringBuilder out = new StringBuilder();
         out.append("[");
 
-        for (int i=0; i<in.length; i++) {
-            if (isScalarType(in[i])) {
-                out.append( getScalarAsString(in[i]) );
+        int length = Array.getLength(in);
+        for (int i=0; i<length; i++) {
+            Object value = Array.get(in, i);
+
+            if (isScalarType(value)) {
+                out.append( getScalarAsString(value) );
             }
             else {
                 out.append("null");
-                buildNode(targetName + "[" + i + "]", in[i]);
+                buildNode(targetName + "[" + i + "]", value);
             }
 
-            if (i != in.length-1) {
+            if (i != length-1) {
                 out.append(", ");
             }
         }

Modified: trunk/stripes/src/net/sourceforge/stripes/tag/InputHiddenTag.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/tag/InputHiddenTag.java   
2006-10-24 12:06:07 UTC (rev 446)
+++ trunk/stripes/src/net/sourceforge/stripes/tag/InputHiddenTag.java   
2006-10-25 00:28:43 UTC (rev 447)
@@ -17,6 +17,7 @@
 import javax.servlet.jsp.JspException;
 import javax.servlet.jsp.tagext.BodyTag;
 import java.util.Collection;
+import java.lang.reflect.Array;
 
 /**
  * <p>Generates one or more [EMAIL PROTECTED] <input type="hidden" ... />} 
HTML tags based on the value
@@ -99,7 +100,9 @@
             writeSingletonTag(getPageContext().getOut(), "input");
         }
         else if (valueOrValues.getClass().isArray()) {
-            for (Object value : (Object[]) valueOrValues) {
+            int len = Array.getLength(valueOrValues);
+            for (int i=0; i<len; ++i) {
+                Object value = Array.get(valueOrValues, i);
                 getAttributes().put("value", format(value));
                 writeSingletonTag(getPageContext().getOut(), "input");
             }

Modified: trunk/stripes/src/net/sourceforge/stripes/tag/InputTagSupport.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/tag/InputTagSupport.java  
2006-10-24 12:06:07 UTC (rev 446)
+++ trunk/stripes/src/net/sourceforge/stripes/tag/InputTagSupport.java  
2006-10-25 00:28:43 UTC (rev 447)
@@ -32,6 +32,7 @@
 import java.util.Locale;
 import java.util.Random;
 import java.lang.reflect.Method;
+import java.lang.reflect.Array;
 import java.io.IOException;
 
 /**
@@ -95,10 +96,9 @@
         Object unknown = getOverrideValueOrValues();
         Object returnValue = null;
 
-        if (unknown != null && unknown instanceof Object[]) {
-            Object[] array = (Object[]) unknown;
-            if (array.length > 0) {
-                returnValue = array[0];
+        if (unknown != null && unknown.getClass().isArray()) {
+            if (Array.getLength(unknown) > 0) {
+                returnValue = Array.get(unknown, 0);
             }
         }
         else if (unknown != null && unknown instanceof Collection) {
@@ -175,9 +175,10 @@
         if (selected != null) {
             String stringValue = (value == null) ? "" : format(value);
 
-            if (selected instanceof Object[]) {
-                Object[] selectedIf = (Object[]) selected;
-                for (Object item : selectedIf) {
+            if (selected.getClass().isArray()) {
+                int length = Array.getLength(selected);
+                for (int i=0; i<length; ++i) {
+                    Object item = Array.get(selected, i);
                     if ( (format(item).equals(stringValue)) ) {
                         return true;
                     }

Modified: trunk/stripes/src/net/sourceforge/stripes/util/CollectionUtil.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/util/CollectionUtil.java  
2006-10-24 12:06:07 UTC (rev 446)
+++ trunk/stripes/src/net/sourceforge/stripes/util/CollectionUtil.java  
2006-10-25 00:28:43 UTC (rev 447)
@@ -14,6 +14,8 @@
  */
 package net.sourceforge.stripes.util;
 
+import java.lang.reflect.Array;
+
 /**
  * Utility methods for working with Collections and Arrays.
  *
@@ -77,4 +79,33 @@
         if (isPositive) return contains(events, event);
         else return !contains(events, "!" + event);
     }
+
+    /**
+     * Converts an Object reference that is known to be an array into an 
Object[]. If the array
+     * is assignable to Object[], the array passed in is simply cast and 
returned. Otherwise a
+     * new Object[] of equal size is constructed and the elements are wrapped 
and inserted into
+     * the new array before being returned.
+     *
+     * @param in an array of Objects or primitives
+     * @return an Object[], either the array passed in, or in the case of 
primitives, a new
+     *         Object[] containing a wrapper for each element in the input 
array
+     * @throws IllegalArgumentException thrown if the in parameter is null or 
not an array
+     */
+    public static Object[] asObjectArray(Object in) {
+        if (in == null || !in.getClass().isArray()) {
+            throw new IllegalArgumentException("Parameter to asObjectArray 
must be a non-null array.");
+        }
+        else if (in instanceof Object[]) {
+            return (Object[]) in;
+        }
+        else {
+            int length = Array.getLength(in);
+            Object[] out = new Object[length];
+            for (int i=0; i<length; ++i) {
+                out[i] = Array.get(in, i);
+            }
+
+            return out;
+        }
+    }
 }

Modified: trunk/stripes/src/net/sourceforge/stripes/util/Log.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/util/Log.java     2006-10-24 
12:06:07 UTC (rev 446)
+++ trunk/stripes/src/net/sourceforge/stripes/util/Log.java     2006-10-25 
00:28:43 UTC (rev 447)
@@ -198,7 +198,7 @@
         StringBuilder builder = new StringBuilder(128);
         for (Object part : messageParts) {
             if (part instanceof Object[]) {
-                builder.append( Arrays.toString((Object[])part) );
+                builder.append( Arrays.toString((Object[]) 
CollectionUtil.asObjectArray(part) ));
             }
             else {
                 builder.append(part);

Modified: trunk/stripes/src/net/sourceforge/stripes/util/UrlBuilder.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/util/UrlBuilder.java      
2006-10-24 12:06:07 UTC (rev 446)
+++ trunk/stripes/src/net/sourceforge/stripes/util/UrlBuilder.java      
2006-10-25 00:28:43 UTC (rev 447)
@@ -114,7 +114,7 @@
                     addParameter(name, ((Collection) v).toArray());
                 }
                 else if (v.getClass().isArray()) {
-                    addParameter(name, (Object[]) v);
+                    addParameter(name, (Object[]) 
CollectionUtil.asObjectArray(v));
                 }
                 else {
                     // Figure out whether we already have params or not


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Stripes-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-development

Reply via email to