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