Bug 45433

Summary: Unstandard <size>-Tag doesn't accept arrays
Product: Taglibs Reporter: Oleksiy Drugobytskyy <o.dr>
Component: Sandbox TaglibsAssignee: Tomcat Developers Mailing List <dev>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: unspecified   
Target Milestone: ---   
Hardware: All   
OS: All   
Attachments: Sample jsp with <size>-Tag

Description Oleksiy Drugobytskyy 2008-07-18 06:07:49 UTC
We are still using J2EE 1.3 and use "unstandard-taglibs".
The Tag <size> doesn't work with arrays - it returns -1.

Error is in the class "org.apache.taglibs.unstandard.SizeTag":

// Error
if(target instanceof Collection) { 
     result = ( (Object[])target ).length;
}

// Bugfix  
if(target.isArray) { 
     result = ( (Object[])target ).length;
}

And the casting (Object[]) doesn't works with primitive arrays (int[], boolean[], etc).
Comment 1 Oleksiy Drugobytskyy 2008-07-18 06:08:59 UTC
Created attachment 22282 [details]
Sample jsp with <size>-Tag
Comment 2 Henri Yandell 2009-05-25 02:06:37 UTC
svn ci -m "Fixing bugzilla entry 45433. SizeTag doesn't accept arrays"
Sending        src/main/java/org/apache/taglibs/unstandard/SizeTag.java
Transmitting file data .
Committed revision 778359.


Index: src/main/java/org/apache/taglibs/unstandard/SizeTag.java
===================================================================
--- src/main/java/org/apache/taglibs/unstandard/SizeTag.java    (revision 775710)
+++ src/main/java/org/apache/taglibs/unstandard/SizeTag.java    (working copy)
@@ -75,8 +75,15 @@
             if(target instanceof String) {
                 result = ( (String)target ).length();
             } else
-            if(target instanceof Collection) {
-                result = ( (Object[])target ).length;
+            if(target.getClass().isArray()) {
+                try {
+                    Field lengthField = target.getClass().getField("length");
+                    result = lengthField.getInt(target);
+                } catch(NoSuchFieldException nsfe) {
+                    throw new JspException("Array found without a length field", nsfe);
+                } catch(IllegalAccessException iae) {
+                    throw new JspException("Array found with a non-accessible length field", iae);
+                }
             }
         }
         if(var == null && result != -1) {