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 477003 2006-11-20 01:14:22Z henning $ 56 */ 57 public class Introspector extends IntrospectorBase 58 { 59 /** 60 * define a public string so that it can be looked for 61 * if interested 62 */ 63 64 public final static String CACHEDUMP_MSG = 65 "Introspector: detected classloader change. Dumping cache."; 66 67 /** 68 * @param log A Log object to use for the introspector. 69 */ 70 public Introspector(final Log log) 71 { 72 super(log); 73 } 74 75 /** 76 * @param logger A runtime logger object. 77 * @deprecated RuntimeLogger is deprecated. Use Introspector(Log log). 78 */ 79 public Introspector(final RuntimeLogger logger) 80 { 81 this(new RuntimeLoggerLog(logger)); 82 } 83 84 /** 85 * Gets the method defined by <code>name</code> and 86 * <code>params</code> for the Class <code>c</code>. 87 * 88 * @param c Class in which the method search is taking place 89 * @param name Name of the method being searched for 90 * @param params An array of Objects (not Classes) that describe the 91 * the parameters 92 * 93 * @return The desired Method object. 94 * @throws IllegalArgumentException When the parameters passed in can not be used for introspection. 95 */ 96 public Method getMethod(final Class c, final String name, final Object[] params) 97 throws IllegalArgumentException 98 { 99 try 100 { 101 return super.getMethod(c, name, params); 102 } 103 catch(MethodMap.AmbiguousException ae) 104 { 105 /* 106 * whoops. Ambiguous. Make a nice log message and return null... 107 */ 108 109 StringBuffer msg = new StringBuffer("Introspection Error : Ambiguous method invocation ") 110 .append(name) 111 .append("("); 112 113 for (int i = 0; i < params.length; i++) 114 { 115 if (i > 0) 116 { 117 msg.append(", "); 118 } 119 120 if (params[i] == null) 121 { 122 msg.append("null"); 123 } 124 else 125 { 126 msg.append(params[i].getClass().getName()); 127 } 128 } 129 130 msg.append(") for class ") 131 .append(c); 132 133 log.error(msg.toString()); 134 } 135 136 return null; 137 } 138 139 /** 140 * Logs that the Introspector Cache has been cleared. 141 */ 142 public void triggerClear() 143 { 144 super.triggerClear(); 145 log.info(CACHEDUMP_MSG); 146 } 147 }