View Javadoc

1   package org.apache.velocity.util.introspection;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
20   */
21  
22  import java.lang.reflect.Method;
23  
24  import org.apache.velocity.runtime.log.Log;
25  
26  /**
27   * Lookup a a Method object for a particular class given the name of a method
28   * and its parameters.
29   *
30   * The first time the Introspector sees a
31   * class it creates a class method map for the
32   * class in question. Basically the class method map
33   * is a Hashtable where Method objects are keyed by a
34   * concatenation of the method name and the names of
35   * classes that make up the parameters.
36   *
37   * For example, a method with the following signature:
38   *
39   * public void method(String a, StringBuffer b)
40   *
41   * would be mapped by the key:
42   *
43   * "method" + "java.lang.String" + "java.lang.StringBuffer"
44   *
45   * This mapping is performed for all the methods in a class
46   * and stored for.
47   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
48   * @author <a href="mailto:bob@werken.com">Bob McWhirter</a>
49   * @author <a href="mailto:szegedia@freemail.hu">Attila Szegedi</a>
50   * @author <a href="mailto:paulo.gaspar@krankikom.de">Paulo Gaspar</a>
51   * @author <a href="mailto:henning@apache.org">Henning P. Schmiedehausen</a>
52   * @version $Id: IntrospectorBase.java 685685 2008-08-13 21:43:27Z nbubna $
53   */
54  public abstract class IntrospectorBase
55  {
56      /** Class logger */
57      protected final Log log;
58  
59      /** The Introspector Cache */
60      private final IntrospectorCache introspectorCache;
61      
62      /**
63       * C'tor.
64       */
65      protected IntrospectorBase(final Log log)
66      {
67          this.log = log;
68          introspectorCache = new IntrospectorCacheImpl(log); // TODO: Load that from properties.
69      }
70      
71      /**
72       * Gets the method defined by <code>name</code> and
73       * <code>params</code> for the Class <code>c</code>.
74       *
75       * @param c Class in which the method search is taking place
76       * @param name Name of the method being searched for
77       * @param params An array of Objects (not Classes) that describe the
78       *               the parameters
79       *
80       * @return The desired Method object.
81       * @throws IllegalArgumentException When the parameters passed in can not be used for introspection.
82       * @throws MethodMap.AmbiguousException When the method map contains more than one match for the requested signature.
83       */
84      public Method getMethod(final Class c, final String name, final Object[] params)
85              throws IllegalArgumentException,MethodMap.AmbiguousException
86      {
87          if (c == null)
88          {
89              throw new IllegalArgumentException ("class object is null!");
90          }
91          
92          if (params == null)
93          {
94              throw new IllegalArgumentException("params object is null!");
95          }
96  
97          IntrospectorCache ic = getIntrospectorCache();
98  
99          ClassMap classMap = ic.get(c);
100         if (classMap == null)
101         {
102             classMap = ic.put(c);
103         }
104 
105         return classMap.findMethod(name, params);
106     }
107 
108     /**
109      * Return the internal IntrospectorCache object.
110      * 
111      * @return The internal IntrospectorCache object.
112      * @since 1.5
113      */
114     protected IntrospectorCache getIntrospectorCache()
115     {
116 	    return introspectorCache;
117     }
118 
119 }