1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.jexl.util.introspection;
19
20 import java.lang.reflect.Method;
21
22 import org.apache.commons.logging.Log;
23
24
25 /**
26 * This basic function of this class is to return a Method
27 * object for a particular class given the name of a method
28 * and the parameters to the method in the form of an Object[]
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 * @since 1.0
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 * @version $Id: Introspector.java 584046 2007-10-12 05:14:37Z proyal $
53 */
54 public class Introspector extends IntrospectorBase {
55 /**
56 * define a public string so that it can be looked for
57 * if interested.
58 */
59
60 public static final String CACHEDUMP_MSG =
61 "Introspector : detected classloader change. Dumping cache.";
62
63 /**
64 * our engine runtime services.
65 */
66 private final Log rlog;
67
68 /**
69 * Recieves our RuntimeServices object.
70 * @param logger a {@link Log}.
71 */
72 public Introspector(Log logger) {
73 super(logger);
74 this.rlog = 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(Class c, String name, Object[] params) throws IllegalArgumentException {
90 /*
91 * just delegate to the base class
92 */
93
94 try {
95 return super.getMethod(c, name, params);
96 } catch (MethodMap.AmbiguousException ae) {
97 /*
98 * whoops. Ambiguous. Make a nice log message and return null...
99 */
100
101 StringBuffer msg = new StringBuffer("Introspection Error : Ambiguous method invocation ")
102 .append(name).append("( ");
103
104 for (int i = 0; i < params.length; i++) {
105 if (i > 0) {
106 msg.append(", ");
107 }
108
109 msg.append(null == params[i] ? "null" : params[i].getClass().getName());
110 }
111
112 msg.append(") for class ").append(c.getName());
113
114 rlog.error(msg.toString());
115 }
116
117 return null;
118 }
119
120 /**
121 * Clears the classmap and classname
122 * caches, and logs that we did so.
123 */
124 protected void clearCache() {
125 super.clearCache();
126 rlog.info(CACHEDUMP_MSG);
127 }
128 }