You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Nathan Ewing <Na...@cbhk.com> on 2004/04/22 22:56:09 UTC

DynaForms with array properties

As anyone who's probably tried to put an array of objects as part of a
DynaForm knows, if you try to submit a form with the array on it, struts
in it's infinite wisdom does not create the array correctly, but instead
throws an ArrayOutOfBounds exception.  I've extended DynaValidatorForm
and made it so that when struts tries to call get(name, index), if the
array doesn't exists it creates it, and if the array is too small it
expands it and instantiates the objects.  I'm posting this in case
someone finds it useful.  I haven't tested it a lot so there is no
implied warranty if you use this code.

	Nathan

--------------------------------------------------------------

/*
 * ImprovedDynaValidatorForm.java
 *
 * Created on April 22, 2004, 11:26 AM
 */

package org.struts.util;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import org.apache.struts.validator.DynaValidatorForm;

/**
 *
*/
public class ImprovedDynaValidatorForm extends DynaValidatorForm {
	
	/** Creates a new instance of ImprovedDynaValidatorForm */
	public ImprovedDynaValidatorForm() {
	}
	
	public Object get(String name, int index) {
		
		Object value = dynaValues.get(name);
		
		if (value == null){
			Class type = getDynaProperty(name).getType();
			if (type.isArray()){
				value =
Array.newInstance(type.getComponentType(), 0);
				set(name, value);
			}
			else {
				try{
					value = type.newInstance();
				}
				catch (java.lang.InstantiationException
e){
					throw new
NullPointerException(e.getMessage());
				} catch
(java.lang.IllegalAccessException e){
					throw new
NullPointerException(e.getMessage());
				}
				
			}
		}
		
		Object array;
		if (value.getClass().isArray()){
			if ( ((Object[])value).length <= index ){
				//Create a new array of size index
				array =
Array.newInstance(value.getClass().getComponentType(), index + 1);
				String valueClass =
value.getClass().toString();
				String arrayClass =
array.getClass().toString();
				//Fill in any values that were already
in the array
				for (int
i=0;i<((Object[])value).length;i++){
					Array.set(array,
i,((Object[])value)[i]);
				}
				//Instantiate any null objects in the
array
				for (int
i=0;i<((Object[])array).length;i++){
					if (((Object[])array)[i] ==
null){
						try{
							Array.set(array,
i, value.getClass().getComponentType().newInstance());
						}
						catch
(java.lang.InstantiationException e){
							throw new
NullPointerException(e.getMessage());
						} catch
(java.lang.IllegalAccessException e){
							throw new
NullPointerException(e.getMessage());
						}
						
					}
				}
				//Put new larger array in
				value = array;
				set(name, value);
			}
			return (Array.get(value,index));
		} else if (value instanceof List) {
			return ((List) value).get(index);
		} else {
			throw new IllegalArgumentException
			("Non-indexed property for '" + name + "[" +
index + "]'");
		}
		
		
	}
	

}


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org