Uploaded image for project: 'Commons OGNL (Dormant)'
  1. Commons OGNL (Dormant)
  2. OGNL-162

ognl for Generic Type

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 2.7.2
    • None
    • Core Runtime
    • None
    • jdk1.5 ,struts2 2.0.11.1,spring 2.5.3
    • Important

    Description

      in struts2, if i define a class like

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

      public class Faq extents Entity<K>{}

      public class Entity<K>{

      private K id;

      public Entity(){}

      public Entity(K id)

      { this.id = id; }

      public K getId()

      { return id; }

      }

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

      than the value of this 'id' will change to a array in the action as well as id=String[1] or id = Long[1]. that is my opinion

      in the abstract class OgnlRuntime.java modify this method

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

      public static Method getAppropriateMethod( OgnlContext context, Object source, Object target, String methodName, String propertyName, List methods, Object[] args, Object[] actualArgs )

      {

      Method result = null;

      Class[] resultParameterTypes = null;

      if (methods != null) {

      for (int i = 0, icount = methods.size(); i < icount; i++) {

      Method m = (Method)methods.get;

      Class[] mParameterTypes = getParameterTypes(m);

      //Generic converted Type

      Class[] mGenericparameterTypeString =

      {java.lang.String.class}

      ;

      if ( areArgsCompatible(args, mParameterTypes) && ((result == null) || isMoreSpecific(mParameterTypes, resultParameterTypes)) ) {

      if(areArgsCompatibleGeneric(args,mGenericparameterTypeString))

      { if(getConvertedTypesGeneric( context, target, m, propertyName, args, actualArgs)) result = m; }

      else{

      result = m;

      resultParameterTypes = mParameterTypes;

      System.arraycopy(args, 0, actualArgs, 0, args.length);

      for (int j = 0; j < mParameterTypes.length; j++) {

      Class type = mParameterTypes[j];

      if (type.isPrimitive() && (actualArgs[j] == null))

      { actualArgs[j] = getConvertedType(context, source, result, propertyName, null, type); }

      }

      }

      }

      }

      }

      if ( result == null )

      { result = getConvertedMethodAndArgs( context, target, propertyName, methods, args, actualArgs ); }

      return result;

      }

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

      in order to getting the Generic Type , we will add a new method like this

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

      public static boolean getConvertedTypesGeneric( OgnlContext context, Object target, Member member, String propertyName, Object[] args, Object[] newArgs)

      {

      boolean result = false;

      // get Generic Type

      Class[] parameterTypes =

      {(Class) ((ParameterizedType) target.getClass().getGenericSuperclass()).getActualTypeArguments()[0]}

      ;

      if (parameterTypes.length == args.length) {

      result = true;

      for (int i = 0, ilast = parameterTypes.length - 1; result && (i <= ilast); i++) {

      Object arg = args[i];

      Class type = parameterTypes[i];

      if (isTypeCompatible(arg, type))

      { newArgs[i] = arg; }

      else {

      Object v = getConvertedType(context, target, member, propertyName, arg, type);

      if (v == OgnlRuntime.NoConversionPossible)

      { result = false; }

      else

      { newArgs[i] = v; }

      }

      }

      }

      return result;

      }

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

      and add a method for dealing with Generic Object

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

      public static final boolean areArgsCompatibleGeneric(Object[] args, Class[] classes)

      {

      boolean result = true;

      Object[] newArgs = {};

      if ( classes.length != args.length)

      { result = false; }

      else {

      for ( int i=0; ilast = args.length-1; result && (i <= ilast) ; i++){

      Object arg = args[i];

      if(arg.getClass().isArray())

      { newArgs = (Object[]) arg; result = isTypeCompatible(newArgs[i] , classes[i]); }

      else

      { result = false; }

      }

      }

      }

      return result;

      }

      Attachments

        Activity

          People

            jkuhnert Jesse Kuhnert
            towne towne
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated: