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   * This basic function of this class is to return a Method
28   * object for a particular class given the name of a method
29   * and the parameters to the method in the form of an Object[]
30   *
31   * The first time the Introspector sees a
32   * class it creates a class method map for the
33   * class in question. Basically the class method map
34   * is a Hastable where Method objects are keyed by a
35   * concatenation of the method name and the names of
36   * classes that make up the parameters.
37   *
38   * For example, a method with the following signature:
39   *
40   * public void method(String a, StringBuffer b)
41   *
42   * would be mapped by the key:
43   *
44   * "method" + "java.lang.String" + "java.lang.StringBuffer"
45   *
46   * This mapping is performed for all the methods in a class
47   * and stored for
48   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
49   * @author <a href="mailto:bob@werken.com">Bob McWhirter</a>
50   * @author <a href="mailto:szegedia@freemail.hu">Attila Szegedi</a>
51   * @author <a href="mailto:paulo.gaspar@krankikom.de">Paulo Gaspar</a>
52   * @author <a href="mailto:henning@apache.org">Henning P. Schmiedehausen</a>
53   * @version $Id: Introspector.java 741214 2009-02-05 18:13:47Z nbubna $
54   */
55  public class Introspector extends IntrospectorBase
56  {
57      /**
58       * @param log A Log object to use for the introspector.
59       * @since 1.5
60       */
61      public Introspector(final Log log)
62      {
63          super(log);
64      }
65  
66      /**
67       * Gets the method defined by <code>name</code> and
68       * <code>params</code> for the Class <code>c</code>.
69       *
70       * @param c Class in which the method search is taking place
71       * @param name Name of the method being searched for
72       * @param params An array of Objects (not Classes) that describe the
73       *               the parameters
74       *
75       * @return The desired Method object.
76       * @throws IllegalArgumentException When the parameters passed in can not be used for introspection.
77       */
78      public Method getMethod(final Class c, final String name, final Object[] params)
79          throws IllegalArgumentException
80      {
81          try
82          {
83              return super.getMethod(c, name, params);
84          }
85          catch(MethodMap.AmbiguousException ae)
86          {
87              /*
88               *  whoops.  Ambiguous.  Make a nice log message and return null...
89               */
90  
91              StringBuffer msg = new StringBuffer("Introspection Error : Ambiguous method invocation ")
92                      .append(name)
93                      .append("(");
94  
95              for (int i = 0; i < params.length; i++)
96              {
97                  if (i > 0)
98                  {
99                      msg.append(", ");
100                 }
101 
102                 if (params[i] == null)
103                 {
104                     msg.append("null");
105                 }
106                 else
107                 {
108                     msg.append(params[i].getClass().getName());
109                 }
110             }
111 
112             msg.append(") for class ")
113                     .append(c);
114 
115             log.debug(msg.toString());
116         }
117 
118         return null;
119     }
120 
121 }