View Javadoc

1   /*
2    * Copyright 2001-2006 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.jexl.util.introspection;
18  
19  import java.lang.reflect.Method;
20  
21  import org.apache.commons.logging.Log;
22  
23  
24  /***
25   * This basic function of this class is to return a Method
26   * object for a particular class given the name of a method
27   * and the parameters to the method in the form of an Object[]
28   *
29   * The first time the Introspector sees a 
30   * class it creates a class method map for the
31   * class in question. Basically the class method map
32   * is a Hashtable where Method objects are keyed by a
33   * concatenation of the method name and the names of
34   * classes that make up the parameters.
35   *
36   * For example, a method with the following signature:
37   *
38   * public void method(String a, StringBuffer b)
39   *
40   * would be mapped by the key:
41   *
42   * "method" + "java.lang.String" + "java.lang.StringBuffer"
43   *
44   * This mapping is performed for all the methods in a class
45   * and stored for 
46   * @since 1.0
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   * @version $Id: Introspector.java 398171 2006-04-29 14:57:29Z dion $
52   */
53  public class Introspector extends IntrospectorBase {
54      /***
55       *  define a public string so that it can be looked for
56       *  if interested.
57       */
58  
59      public static final String CACHEDUMP_MSG = 
60          "Introspector : detected classloader change. Dumping cache.";
61  
62      /***
63       *  our engine runtime services.
64       */
65      private final Log rlog;
66  
67      /***
68       *  Recieves our RuntimeServices object.
69       *  @param logger a {@link Log}.
70       */
71      public Introspector(Log logger) {
72          this.rlog = logger;
73      }
74     
75      /***
76       * Gets the method defined by <code>name</code> and
77       * <code>params</code> for the Class <code>c</code>.
78       *
79       * @param c Class in which the method search is taking place
80       * @param name Name of the method being searched for
81       * @param params An array of Objects (not Classes) that describe the
82       *               the parameters
83       *
84       * @return The desired Method object.
85       * @throws Exception if the superclass does.
86       */
87      public Method getMethod(Class c, String name, Object[] params) throws Exception {
88          /*
89           *  just delegate to the base class
90           */
91  
92          try {
93              return super.getMethod(c, name, params);
94          } catch (MethodMap.AmbiguousException ae) {
95              /*
96               *  whoops.  Ambiguous.  Make a nice log message and return null...
97               */
98  
99              StringBuffer msg = new StringBuffer("Introspection Error : Ambiguous method invocation ")
100                 .append(name).append("( ");
101 
102             for (int i = 0; i < params.length; i++) {
103                 if (i > 0) {
104                     msg.append(", ");
105                 }
106                 
107                 msg.append(params[i].getClass().getName());
108             }
109             
110             msg.append(") for class ").append(c.getName());
111             
112             rlog.error(msg.toString());
113         }
114 
115         return null;
116     }
117 
118     /***
119      * Clears the classmap and classname
120      * caches, and logs that we did so.
121      */
122     protected void clearCache() {
123         super.clearCache();
124         rlog.info(CACHEDUMP_MSG);
125     }
126 }