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 2001/11/27 01:40:46 UTC

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

geirm       01/11/26 16:40:46

  Modified:    src/java/org/apache/velocity/util/introspection
                        ClassMap.java MethodMap.java
  Log:
  Backed out MethodMap doing the logging, and letting ClassMap do that.
  We can change later to have it throw an exception if we so desire.
  This should be fine for now...
  
  Revision  Changes    Path
  1.18      +31 -6     jakarta-velocity/src/java/org/apache/velocity/util/introspection/ClassMap.java
  
  Index: ClassMap.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity/src/java/org/apache/velocity/util/introspection/ClassMap.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- ClassMap.java	2001/11/26 23:25:05	1.17
  +++ ClassMap.java	2001/11/27 00:40:46	1.18
  @@ -72,10 +72,12 @@
    * @author <a href="mailto:bob@werken.com">Bob McWhirter</a>
    * @author <a href="mailto:szegedia@freemail.hu">Attila Szegedi</a>
    * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
  - * @version $Id: ClassMap.java,v 1.17 2001/11/26 23:25:05 geirm Exp $
  + * @version $Id: ClassMap.java,v 1.18 2001/11/27 00:40:46 geirm Exp $
    */
   public class ClassMap
   {
  +    private RuntimeServices rsvc = null;
  +
       private static final class CacheMiss { }
       private static final CacheMiss CACHE_MISS = new CacheMiss();
       private static final Object OBJECT = new Object();
  @@ -93,7 +95,7 @@
        */
       private Map methodCache = new Hashtable();
   
  -    private MethodMap methodMap = null;
  +    private MethodMap methodMap = new MethodMap();
   
       /**
        * Standard constructor
  @@ -101,7 +103,7 @@
       public ClassMap( RuntimeServices rsvc, Class clazz)
       {
           this.clazz = clazz;
  -        methodMap = new MethodMap( rsvc, clazz );
  +        this.rsvc = rsvc;
           populateMethodCache();
       }
   
  @@ -141,9 +143,32 @@
   
           if (cacheEntry == null)
           {
  -            cacheEntry = methodMap.find( name,
  -                                         params );
  -            
  +            try
  +            {
  +                cacheEntry = methodMap.find( name,
  +                                             params );
  +            }
  +            catch( MethodMap.AmbiguousException ae )
  +            {
  +
  +                String msg = "Introspection Error : Ambiguous method invocation "
  +                    + name + "( ";
  +
  +                for (int i = 0; i < params.length; i++)
  +                {
  +                    if ( i > 0)
  +                        msg = msg + ", ";
  +                    
  +                    msg = msg + params[i].getClass().getName();
  +                }
  +                
  +                msg = msg + ") for class " + clazz;
  +
  +                rsvc.error( msg );
  +                
  +                cacheEntry = null;
  +            }
  +
               if ( cacheEntry == null )
               {
                   methodCache.put( methodKey,
  
  
  
  1.13      +29 -44    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.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- MethodMap.java	2001/11/26 16:00:19	1.12
  +++ MethodMap.java	2001/11/27 00:40:46	1.13
  @@ -61,38 +61,16 @@
   
   import java.lang.reflect.Method;
   
  -import org.apache.velocity.runtime.RuntimeServices;
  -
   /**
    *
    * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
    * @author <a href="mailto:bob@werken.com">Bob McWhirter</a>
    * @author <a href="mailto:Christoph.Reck@dlr.de">Christoph Reck</a>
    * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
  - * @version $Id: MethodMap.java,v 1.12 2001/11/26 16:00:19 geirm Exp $
  + * @version $Id: MethodMap.java,v 1.13 2001/11/27 00:40:46 geirm Exp $
    */
   public class MethodMap
   {
  -    protected Class clazz = null;
  -    protected RuntimeServices rsvc = null;
  -
  -    /**
  -     *  we need both the class and rsvc for ambiguity
  -     *  reporting
  -     */
  -    public MethodMap( RuntimeServices rsvc, Class c )
  -    {
  -        this.clazz = c;
  -        this.rsvc = rsvc;
  -    }
  -
  -    /**
  -     *  hide this...
  -     */
  -    private MethodMap()
  -    {
  -    }
  -
       /**
        * Keep track of all methods with the same name.
        */
  @@ -150,6 +128,7 @@
        *  @return Method
        */
       public Method find(String methodName, Object[] params)
  +        throws AmbiguousException
       {
           List methodList = (List) methodByNameMap.get(methodName);
           
  @@ -238,31 +217,24 @@
               }
           }
   
  +        /*
  +         *  if ambiguous is true, it means we couldn't decide
  +         *  so inform the caller...
  +         */
  +
           if ( ambiguous )
           {    
  -            String msg = "MethodMap : Ambiguous method invocation "
  -                + methodName + "( ";
  -
  -            for (int i = 0; i < params.length; i++)
  -            {
  -                if ( i > 0)
  -                    msg = msg + ", ";
  -                
  -                msg = msg + params[i].getClass().getName();
  -            }
  -
  -            msg = msg + ") for class " + clazz;
  -
  -            rsvc.error( msg );
  -
  -            System.out.println( msg );
  -
  -            return null;
  +            throw new AmbiguousException();
           }
              
           return bestMethod;
       }
  -    
  +
  +    /**
  +     *  Calculates the distance, expressed as a vector of inheritance
  +     *  steps, between the calling args and the method args.
  +     *  There still is an issue re interfaces...
  +     */
       private Twonk calcDistance( Object[] set, Class[] base )
       {
           if ( set.length != base.length)
  @@ -322,8 +294,21 @@
                   
           return twonk;
       }
  -    
  -    class Twonk
  +
  +    /**
  +     *  simple distinguishable exception, used when 
  +     *  we run across ambiguous overloading
  +     */
  +    public class AmbiguousException extends Exception
  +    {
  +    }
  +
  +    /**
  +     *  little class to hold 'distance' information
  +     *  for calling params, as well as determine
  +     *  specificity
  +     */
  +    private class Twonk
       {
           public int distance;
           public int[] vec;
  
  
  

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