The day after Black Friday and most folks are still at the WalMart so I'll take 
a stab at some testcases

private E createElementByConstructor() {
                try {
                        Constructor<E>[] constrArray = (Constructor<E>[]) 
elementClass
                                        .getConstructors(); 
                        Constructor<E> constructor = null;
                        Class[] paramTypes = null;
 
                        for (Constructor<E> constr : constrArray) {
                                if (constructor == null) {
/*this is executed first which is fine */
                                        constructor = constr;
                                        paramTypes = constr.getParameterTypes();
/*constr ParameterType (Class) count (e.g. 0) is less than number of Classes 
stored in local var paramTypes (e.g. 1) */
                                } else if (constr.getParameterTypes().length < 
paramTypes.length) {
                                        constructor = constr;
                                        paramTypes = constr.getParameterTypes();
                                }
                        }/* Testcase  execution 1st pass Class is Class1 with 1 
parameter named fu*/
Class=Class1 parameter=fu                    
Constructor is now Class1(fu)
paramTypes.length is now 1

/* Testcase execution 2nd pass Class is Class2 with 0 parameters*/
Class=Class2   0 parameters                      so                  
constr.getParameterTypes().length()==0 < paramTypes 1
Constructor is now Class2();
paramTypes.length is now 0

/* Testcase execution 3rd pass Class is Class2 with 2 parameters parameter=fu 
and parameter=bar */
Class3  parameter fu, parameter bar    so 
constr.getParameterTypes().length()==2 < paramTypes 0            FAILS!!!!
assignments to variables of constructor and paramTypes are bypassed

Constructor is STILL Class2();

paramTypes.length is STILL 0



is this what you intend?
Martin 
______________________________________________ 
Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité
 
Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger 
sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung 
oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem 
Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. 
Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung 
fuer den Inhalt uebernehmen.
Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le 
destinataire prévu, nous te demandons avec bonté que pour satisfaire informez 
l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est 
interdite. Ce message sert à l'information seulement et n'aura pas n'importe 
quel effet légalement obligatoire. Étant donné que les email peuvent facilement 
être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité 
pour le contenu fourni.




> Date: Sat, 27 Nov 2010 18:26:11 +0900
> Subject: Re: javax.servlet.ServletException: BeanUtils.populate
> From: liying.cn.2...@gmail.com
> To: user@struts.apache.org
> 
> I have dug it out from my old project.
> 
> The code is not very beautiful, you can modify it if you wish,
> but please don't change the author information.
> 
> Here is the code:
> 
> ========================================
> 
> package com.ewsoft.common.utils;
> 
> import java.lang.reflect.Constructor;
> import java.util.ArrayList;
> import java.util.List;
> 
> /**
>  * A List class which can create items as need.<br />
>  * When method get(index) and add(index, element) is called,<br />
>  * and if there is no item in this index, item instances will be created
>  * automatically to avoid a NullPointerException.
>  *
>  * @author Li Ying(liying.cn.2...@gmail.com)
>  */
> public class AutoList<E extends Object> extends ArrayList<E> {
>       private Class<E> elementClass;
> 
>       public AutoList(final Class<E> elementClass) {
>               super();
>               this.elementClass = elementClass;
>       }
> 
>       private void ensureSize(final int size) {
>               synchronized (this) {
>                       while (this.size() < size) {
>                               add(createNewElement());
>                       }
>               }
>       }
> 
>       private E createElementByConstructor() {
>               try {
>                       Constructor<E>[] constrArray = (Constructor<E>[]) 
> elementClass
>                                       .getConstructors();
> 
>                       Constructor<E> constructor = null;
>                       Class[] paramTypes = null;
> 
>                       for (Constructor<E> constr : constrArray) {
>                               if (constructor == null) {
>                                       constructor = constr;
>                                       paramTypes = constr.getParameterTypes();
>                               } else if (constr.getParameterTypes().length < 
> paramTypes.length) {
>                                       constructor = constr;
>                                       paramTypes = constr.getParameterTypes();
>                               }
>                       }
> 
>                       List paramList = new ArrayList();
>                       for (int i = 0; i < paramTypes.length; i++) {
>                               paramList.add(null);
>                       }
> 
>                       return constructor.newInstance(paramList.toArray());
>               } catch (Exception e) {
>                       return null;
>               }
>       }
> 
>       private E createNewElement() {
>               return createElementByConstructor();
>       }
> 
>       /*
>        * (non-Javadoc)
>        *
>        * @see java.util.ArrayList#get(int)
>        */
>       @Override
>       public final E get(final int index) {
>               ensureSize(index + 1);
> 
>               return super.get(index);
>       }
> 
>       /*
>        * (non-Javadoc)
>        *
>        * @see java.util.ArrayList#add(int, java.lang.Object)
>        */
>       @Override
>       public final void add(final int index, final E element) {
>               ensureSize(index);
> 
>               super.add(index, element);
>       }
> }
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
> For additional commands, e-mail: user-h...@struts.apache.org
> 
                                          

Reply via email to