You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by ge...@apache.org on 2002/07/14 23:35:05 UTC

cvs commit: jakarta-velocity/src/java/org/apache/velocity/util/introspection MethodMap.java

geirm       2002/07/14 14:35:05

  Modified:    src/java/org/apache/velocity/util/introspection Tag:
                        VEL_1_3_BRANCH MethodMap.java
  Log:
  update - fix to the (List, int, int) problem (and many like it...)
  
  This problem should't be in 1.4, but will move the testcase there as well
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.13.2.1  +110 -30   jakarta-velocity/src/java/org/apache/velocity/util/introspection/MethodMap.java
  
  Index: MethodMap.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity/src/java/org/apache/velocity/util/introspection/MethodMap.java,v
  retrieving revision 1.13
  retrieving revision 1.13.2.1
  diff -u -r1.13 -r1.13.2.1
  --- MethodMap.java	27 Nov 2001 00:40:46 -0000	1.13
  +++ MethodMap.java	14 Jul 2002 21:35:05 -0000	1.13.2.1
  @@ -85,9 +85,9 @@
       {
           String methodName = method.getName();
   
  -        List l = (List) methodByNameMap.get( methodName );
  +        List l = (List) methodByNameMap.get(methodName);
   
  -        if ( l == null)
  +        if (l == null)
           {
               l = new ArrayList();
               methodByNameMap.put(methodName, l);
  @@ -166,15 +166,15 @@
                    *  determining specificity
                    */
                    
  -                Twonk twonk = calcDistance( params, parameterTypes );
  +                Twonk twonk = calcDistance(params, parameterTypes);
                   
  -                if (twonk != null )
  +                if (twonk != null)
                   {
                       /*
                        *  if we don't have anything yet, take it
                        */
                        
  -                    if ( bestTwonk == null )
  +                    if (bestTwonk == null)
                       {
                           bestTwonk = twonk;
                           bestMethod = method;
  @@ -186,11 +186,11 @@
                            * versus what we think of as the best candidate
                            */
                            
  -                        int val = twonk.moreSpecific( bestTwonk );
  +                        int val = twonk.moreSpecific(bestTwonk);
                            
                           //System.out.println("Val = " + val + " for " + method + " vs " + bestMethod );
                               
  -                        if( val == 0)
  +                        if (val == 0)
                           {
                               /*
                                * this means that the parameters 'crossed'
  @@ -199,7 +199,7 @@
                                */
                               ambiguous = true;
                           }
  -                        else if ( val == 1)
  +                        else if (val == 1)
                           {
                               /*
                                *  the current method is clearly more
  @@ -222,7 +222,7 @@
            *  so inform the caller...
            */
   
  -        if ( ambiguous )
  +        if (ambiguous)
           {    
               throw new AmbiguousException();
           }
  @@ -235,12 +235,12 @@
        *  steps, between the calling args and the method args.
        *  There still is an issue re interfaces...
        */
  -    private Twonk calcDistance( Object[] set, Class[] base )
  +    private Twonk calcDistance(Object[] set, Class[] base)
       {
           if ( set.length != base.length)
               return null;
               
  -        Twonk twonk = new Twonk( set.length );
  +        Twonk twonk = new Twonk(set.length);
           
           int distance = 0;
           
  @@ -252,9 +252,21 @@
                
               Class setclass = set[i].getClass();
                
  -            if ( !base[i].isAssignableFrom( set[i].getClass() ))
  -                return null;
  -    
  +            if (!base[i].isAssignableFrom(setclass))
  +            {
  +                /*
  +                 * check to see if we are dealing with primitives
  +                 */
  +                if (checkPrimitive(base[i], setclass))
  +                {
  +                    break;
  +                }
  +                else
  +                {
  +                    return null;
  +                }
  +            }
  +
               /*
                * ok, I can.  How many steps?
                */
  @@ -267,7 +279,7 @@
                    * is this a valid step?
                    */
                    
  -                if ( !base[i].isAssignableFrom( c ) )
  +                if (!base[i].isAssignableFrom(c))
                   {      
                       /*
                        *  it stopped being assignable - therefore we are looking at
  @@ -277,7 +289,7 @@
                       break;
                   }
                   
  -                if(  base[i].equals( c ) )
  +                if (base[i].equals(c))
                   {
                       /*
                        *  we are equal, so no need to move forward
  @@ -296,6 +308,69 @@
       }
   
       /**
  +     *  check for primitive and widening.  Take from the 1.4 code
  +     */
  +    private boolean checkPrimitive(Class formal, Class arg)
  +    {
  +
  +        if(formal.isPrimitive())
  +        {
  +            if(formal == Boolean.TYPE && arg == Boolean.class)
  +            {
  +                return true;
  +            }
  +
  +            if(formal == Character.TYPE && arg == Character.class)
  +            {
  +                return true;
  +            }
  +
  +            if(formal == Byte.TYPE && arg == Byte.class)
  +            {
  +                return true;
  +            }
  +
  +            if(formal == Short.TYPE &&
  +                    (arg == Short.class || arg == Byte.class))
  +            {
  +                return true;
  +            }
  +
  +            if(formal == Integer.TYPE &&
  +               (arg == Integer.class || arg == Short.class ||
  +                arg == Byte.class))
  +            {
  +                return true;
  +            }
  +
  +            if(formal == Long.TYPE &&
  +               (arg == Long.class || arg == Integer.class ||
  +                arg == Short.class || arg == Byte.class))
  +            {
  +                return true;
  +            }
  +
  +            if(formal == Float.TYPE &&
  +               (arg == Float.class || arg == Long.class ||
  +                arg == Integer.class || arg == Short.class ||
  +                arg == Byte.class))
  +            {
  +                return true;
  +            }
  +
  +            if(formal == Double.TYPE &&
  +               (arg == Double.class || arg == Float.class ||
  +                arg == Long.class || arg == Integer.class ||
  +                arg == Short.class || arg == Byte.class))
  +            {
  +                return true;
  +            }
  +        }
  +
  +        return false;
  +    }
  +
  +    /**
        *  simple distinguishable exception, used when 
        *  we run across ambiguous overloading
        */
  @@ -313,16 +388,18 @@
           public int distance;
           public int[] vec;
           
  -        public Twonk( int size )
  +        public Twonk(int size)
           {
               vec = new int[size];
           }
           
  -        public int moreSpecific( Twonk other )
  +        public int moreSpecific(Twonk other)
           {
  -            if (other.vec.length != vec.length )
  +            if (other.vec.length != vec.length)
  +            {
                   return -1;
  -                
  +            }
  +
               boolean low = false;
               boolean high = false;
               
  @@ -332,7 +409,7 @@
                   {
                       high = true;
                   }
  -                else if (vec[i] < other.vec[i] )
  +                else if (vec[i] < other.vec[i])
                   {
                       low = true;
                   }                    
  @@ -343,27 +420,30 @@
                *  we saw the parameter 'slopes' cross
                *  this means ambiguity
                */
  -             
               if (high && low)
  +            {
                   return 0;
  -               
  +            }
  +
               /*
                *  we saw that all args were 'high', meaning
                *  that the other method is more specific so
                *  we are less
                */
  -             
  -            if( high && !low)
  +            if (high && !low)
  +            {
                   return -1;
  -                
  +            }
  +
               /*
                *  we saw that all points were lower, therefore
                *  we are more specific
                */
  -             
  -            if( !high && low )
  +            if (!high && low)
  +            {
                   return 1;
  -            
  +            }
  +
               /*
                *  the remainder, neither high or low
                *  means we are the same.  This really can't 
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>