commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From erbell...@airfrance.fr
Subject Réf. : Re: Problem using beanutils: MethodUtils.invokeMethod throw a null pointer exception
Date Fri, 02 Apr 2004 07:38:43 GMT




Hi,
Thanks for your response.
I know the method you told me but the problem is that I don't know exaclty
the parameter types.

The invokeMethod(Object, Class, Object[]) guess the parameter types from
their values then call the invokeMethod(Object, Class, Object[], Class[])
the type table filled.

My problem is when a value is null: I would like that invokeMethod analyse
the other parameters and try to call an appropriate method.

At this time I've coded my owmn method invoke method which get through this
case. But I would like to use an external **clean** open-source framework
to not have to maintain the horrible dirty piece of code I wrote...
To be honest i didn't wrote it I just found it and adapt it in the existing
application i work on.
Here it is:
- the checkParametersMatch method check parameters match
- the internalFindMethod method look for a adequate method in a type from
its name and parameters value.
It's only heuristic, It doesn't work in all cases.

I think it would be great to have something with the same functionnality in
a framework like beanutils.

Regards,

Eric

      /**
       * Check the validity of the parameters type.
       * The inheritance level has the DEPTH value.
       * @param in_argCount nombre d'arguments
       * @param in_parameters parametres d'entree
       * @param in_isParametersHashmapped
       * @param in_parameterTypes parametres de la methode trouvee
       * @return true si les parametres sont en concordance, false sinon.
       */
      public final static boolean checkParametersMatch(
            int in_argCount,
            Object[] in_parameters,
            Class[] in_parameterTypes) {

            boolean lc_isMatched = true;
            boolean lc_isFound = false;
            Class cla = null;
            int depth = 0;

            // iterate parameters
            for (int j = 0; j < in_argCount; j++) {

                  // if null continue
                  if (in_parameters[j] == null) {
                        continue;
                  }

                  // if parameters aren't the same
                  if (!in_parameterTypes[j]
                        .getName()
                        .equals(in_parameters[j].getClass().getName())) {

                        lc_isFound = false;
                        cla = in_parameters[j].getClass().getSuperclass();
                        depth = 0;

                        while ((cla != null) && (depth++ < DEPTH) &&
!lc_isFound) {

                              if
(cla.getName().equals(in_parameterTypes[j].getName())) {
                                    lc_isFound = true;
                              }
                              cla = cla.getSuperclass();
                        }

                        if (!lc_isFound) {
                              return lc_isFound;
                        }
                  }
            }
            return lc_isMatched;
      }

      /**
       * Look for a method from the type, the method name and the
parameters value
       * @param in_class classe
       * @param in_methodName nom de la methode
       * @param in_parameters parametres
       * @return methode trouvee
       */
      public final static Method internalFindMethod(
            Class in_class,
            String in_methodName,
            Object[] in_parameters) {

            Method[] lc_methods = null;
            Method lc_method = null;
            Class[] lc_param = null;
            boolean paramHok = false;

            int lc_argCount = 0;

            if (in_parameters != null)
                  lc_argCount = in_parameters.length;

            for (Class cl = in_class; cl != null; cl = cl.getSuperclass())
{

                  lc_methods = cl.getMethods();

                  lc_method = null;
                  for (int i = 0; i < lc_methods.length; i++) {
                        lc_method = lc_methods[i];

                        if (lc_method == null)
                              continue;
                        int mods = lc_method.getModifiers();
                        if (Modifier.isStatic(mods))
                              continue;

                        if (lc_method.getName().equals(in_methodName)
                              && lc_method.getParameterTypes().length ==
lc_argCount) {
                              lc_param = lc_method.getParameterTypes();

                              paramHok =
                                    checkParametersMatch(
                                          lc_argCount,
                                          in_parameters,
                                          lc_param);
                              if (paramHok) {
                                    return lc_method;
                              }
                        }
                  }
            }

            // case in the mother interfaces
            Class[] ifcs = in_class.getInterfaces();
            for (int i = 0; i < ifcs.length; i++) {
                  lc_method =
                        internalFindMethod(
                              ifcs[i],
                              in_methodName,
                              in_parameters);
                  if (lc_method != null) {
                        return lc_method;
                  }
            }
            return null;
      }







"matthew.hawthorne" <matth@apache.org> le 01/04/2004 19:34:56

Veuillez répondre à "Jakarta Commons Users List"
       <commons-user@jakarta.apache.org>

Pour : Jakarta Commons Users List <commons-user@jakarta.apache.org>
cc :

Objet :     Re: Problem using beanutils: MethodUtils.invokeMethod throw a
       null pointer exception


Eric BELLARD wrote:
> I got a null point exception when passing as
> parameters a table with a null value inside like this:
>  [, , null, avionCargoFret, , ]
> The following code inside beanutils send me the
> exception when it does getClass the null element of
> the table:
>
>        Class parameterTypes [] = new Class[arguments];
>         for (int i = 0; i < arguments; i++) {
>             parameterTypes[i] = args[i].getClass();
>         }
>
> Is this a misuse of the MethodUtils.invokeMethod?
> Is this a known limitation of the implementation?
> Is  this a bug?
> Is there any solution out there?


I had the same problem a few weeks ago.  There is another version of
invokeMethod that takes an array of Classes which represent parameter
types.  It then uses the array instead of trying to dynamically get the
parameter types from the parameters, which fails when a parameter is null.

It should look like:

invokeMethod(Object, Class, Object[], Class[])

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







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


Mime
View raw message