You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@myfaces.apache.org by "Rick Hightower (JIRA)" <my...@incubator.apache.org> on 2005/07/26 20:10:22 UTC

[jira] Commented: (MYFACES-366) _SharedRendererUtils does not create the correct type of array, which causes a bind error

    [ http://issues.apache.org/jira/browse/MYFACES-366?page=comments#action_12316786 ] 

Rick Hightower commented on MYFACES-366:
----------------------------------------

Awesome!

> _SharedRendererUtils does not create the correct type of array, which causes a bind error
> -----------------------------------------------------------------------------------------
>
>          Key: MYFACES-366
>          URL: http://issues.apache.org/jira/browse/MYFACES-366
>      Project: MyFaces
>         Type: Bug
>     Versions: 1.0.9 beta
>     Reporter: Rick Hightower
>      Fix For: Nightly Build

>
> We have selectManyCheckbox bound to a strongly typed array. We were getting a class cast exception after our converter was called.
> The class cast exception (root cause) stated that we could not cast an Object[] to a PlanVO[] array. I traced the problem down to _SharedRendererUtils..getConvertedUISelectManyValue(). Instead of creating a array of type PlanVO, it creates a generic array which will not bind to our PlanVO array. If you change our backing bean to be an Object[], then it will not find our converter which is bound by class.
> Ken found an issue when working with selectManyCheckbox.
> some.jsp as follows:
> <h:outputText value="You have been matched!"/>
>             <h:selectManyCheckbox id="standardProviders" value="#{lreController.form.standardPlans}">
>                 <f:selectItems value="#{lreController.standardPlans}"/>
>             </h:selectManyCheckbox>
> Here is the fix towards then end of the getConvertedUISelectManyValue method.
> _SharedRendererUtils.getConvertedUISelectManyValue().java
> //Object array
>             int len = submittedValue.length;
>             //Object[] convertedValues = new Object[len]; <------------ WAS (BROKEN! 
>             // The above line creates a generic Object array that will not bind to our strongly typed array
>             Object [] convertedValues = (Object []) Array.newInstance(arrayComponentType, len); // <-- HERE IS THE FIX.
>             for (int i = 0; i < len; i++) {
>                 convertedValues[i]
>                     = converter.getAsObject(facesContext, component, submittedValue[i]);
>             }
>             return convertedValues;
> If you still don't understand the above. I wrote this little test to explain the problem very succinctly.
> TestTest.java
> import java.lang.reflect.Array;
> import junit.framework.TestCase;
> public class TestTest extends TestCase {
>     
>     public static class Employee {
>         //
>     }
>     private Object [] convertToObjectArrayWrongWay (Class clazz, int size) throws Exception{
>         Object [] objects = new Object [size];
>         for (int index = 0; index < size; index++){
>             objects[index] = clazz.newInstance();
>         }
>         return objects;
>     }
>     private Object [] convertToObjectArrayRightWay (Class clazz, int size) throws Exception {
>         Object [] objects = (Object []) Array.newInstance(clazz, size);
>         for (int index = 0; index < size; index++){
>             objects[index] = clazz.newInstance();
>         }
>         return objects;
>     }
>     public void testWrongWay() throws Exception {
>         try {
>             Employee [] employees = (Employee[]) convertToObjectArrayWrongWay(Employee.class, 10);
>             fail("It will never get this far b/c it throws a ClassCastException!");
>         } catch (ClassCastException cce) {
>             assertTrue("This is not what we want myfaces to do!", true);
>         }
>     }
>     public void testRightWay() throws Exception {
>         Employee [] employees = (Employee[]) convertToObjectArrayRightWay(Employee.class, 10);
>     }
> }

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira