| 1 | package org.apache.velocity.util.introspection; |
| 2 | |
| 3 | /* |
| 4 | * Copyright 2001,2004 The Apache Software Foundation. |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | import java.util.Map; |
| 20 | import java.util.Set; |
| 21 | import java.util.HashMap; |
| 22 | import java.util.HashSet; |
| 23 | |
| 24 | import java.lang.reflect.Method; |
| 25 | |
| 26 | import org.apache.velocity.runtime.RuntimeServices; |
| 27 | import org.apache.velocity.runtime.RuntimeLogger; |
| 28 | |
| 29 | /** |
| 30 | * This basic function of this class is to return a Method |
| 31 | * object for a particular class given the name of a method |
| 32 | * and the parameters to the method in the form of an Object[] |
| 33 | * |
| 34 | * The first time the Introspector sees a |
| 35 | * class it creates a class method map for the |
| 36 | * class in question. Basically the class method map |
| 37 | * is a Hastable where Method objects are keyed by a |
| 38 | * concatenation of the method name and the names of |
| 39 | * classes that make up the parameters. |
| 40 | * |
| 41 | * For example, a method with the following signature: |
| 42 | * |
| 43 | * public void method(String a, StringBuffer b) |
| 44 | * |
| 45 | * would be mapped by the key: |
| 46 | * |
| 47 | * "method" + "java.lang.String" + "java.lang.StringBuffer" |
| 48 | * |
| 49 | * This mapping is performed for all the methods in a class |
| 50 | * and stored for |
| 51 | * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a> |
| 52 | * @author <a href="mailto:bob@werken.com">Bob McWhirter</a> |
| 53 | * @author <a href="mailto:szegedia@freemail.hu">Attila Szegedi</a> |
| 54 | * @author <a href="mailto:paulo.gaspar@krankikom.de">Paulo Gaspar</a> |
| 55 | * @version $Id: Introspector.java,v 1.21.4.1 2004/03/03 23:23:08 geirm Exp $ |
| 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 | * our engine runtime services |
| 69 | */ |
| 70 | private RuntimeLogger rlog = null; |
| 71 | |
| 72 | /** |
| 73 | * Recieves our RuntimeServices object |
| 74 | */ |
| 75 | public Introspector(RuntimeLogger logger) |
| 76 | { |
| 77 | this.rlog = logger; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Gets the method defined by <code>name</code> and |
| 82 | * <code>params</code> for the Class <code>c</code>. |
| 83 | * |
| 84 | * @param c Class in which the method search is taking place |
| 85 | * @param name Name of the method being searched for |
| 86 | * @param params An array of Objects (not Classes) that describe the |
| 87 | * the parameters |
| 88 | * |
| 89 | * @return The desired Method object. |
| 90 | */ |
| 91 | public Method getMethod(Class c, String name, Object[] params) |
| 92 | throws Exception |
| 93 | { |
| 94 | /* |
| 95 | * just delegate to the base class |
| 96 | */ |
| 97 | |
| 98 | try |
| 99 | { |
| 100 | return super.getMethod( c, name, params ); |
| 101 | } |
| 102 | catch( MethodMap.AmbiguousException ae ) |
| 103 | { |
| 104 | /* |
| 105 | * whoops. Ambiguous. Make a nice log message and return null... |
| 106 | */ |
| 107 | |
| 108 | String msg = "Introspection Error : Ambiguous method invocation " |
| 109 | + name + "( "; |
| 110 | |
| 111 | for (int i = 0; i < params.length; i++) |
| 112 | { |
| 113 | if ( i > 0) |
| 114 | msg = msg + ", "; |
| 115 | |
| 116 | msg = msg + params[i].getClass().getName(); |
| 117 | } |
| 118 | |
| 119 | msg = msg + ") for class " + c; |
| 120 | |
| 121 | rlog.error( msg ); |
| 122 | } |
| 123 | |
| 124 | return null; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Clears the classmap and classname |
| 129 | * caches, and logs that we did so |
| 130 | */ |
| 131 | protected void clearCache() |
| 132 | { |
| 133 | super.clearCache(); |
| 134 | rlog.info( CACHEDUMP_MSG ); |
| 135 | } |
| 136 | } |