Bug 45433 - Unstandard <size>-Tag doesn't accept arrays
Summary: Unstandard <size>-Tag doesn't accept arrays
Status: RESOLVED FIXED
Alias: None
Product: Taglibs
Classification: Unclassified
Component: Sandbox Taglibs (show other bugs)
Version: unspecified
Hardware: All All
: P2 normal (vote)
Target Milestone: ---
Assignee: Tomcat Developers Mailing List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-07-18 06:07 UTC by Oleksiy Drugobytskyy
Modified: 2009-05-25 02:06 UTC (History)
0 users



Attachments
Sample jsp with <size>-Tag (1.79 KB, text/plain)
2008-07-18 06:08 UTC, Oleksiy Drugobytskyy
Details

Note You need to log in before you can comment on or make changes to this bug.
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) {