View Javadoc
1 /*** 2 * Redistribution and use of this software and associated documentation 3 * ("Software"), with or without modification, are permitted provided 4 * that the following conditions are met: 5 * 6 * 1. Redistributions of source code must retain copyright 7 * statements and notices. Redistributions must also contain a 8 * copy of this document. 9 * 10 * 2. Redistributions in binary form must reproduce the 11 * above copyright notice, this list of conditions and the 12 * following disclaimer in the documentation and/or other 13 * materials provided with the distribution. 14 * 15 * 3. The name "Exolab" must not be used to endorse or promote 16 * products derived from this Software without prior written 17 * permission of Exoffice Technologies. For written permission, 18 * please contact tma@netspace.net.au. 19 * 20 * 4. Products derived from this Software may not be called "Exolab" 21 * nor may "Exolab" appear in their names without prior written 22 * permission of Exoffice Technologies. Exolab is a registered 23 * trademark of Exoffice Technologies. 24 * 25 * 5. Due credit should be given to the Exolab Project 26 * (http://www.exolab.org/). 27 * 28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS 29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 39 * OF THE POSSIBILITY OF SUCH DAMAGE. 40 * 41 * Copyright 2001-2004 (C) Exoffice Technologies Inc. All Rights Reserved. 42 * 43 * $Id: MethodCache.java,v 1.2 2004/01/31 13:44:24 tanderson Exp $ 44 */ 45 package org.exolab.jmscts.core; 46 47 import java.lang.reflect.Method; 48 49 import java.util.ArrayList; 50 import java.util.HashMap; 51 import java.util.Iterator; 52 53 54 /*** 55 * Helper for caching the methods of a class to avoid expensive 56 * {@link Class#getDeclaredMethods} and {@link Class#getMethod} calls 57 * 58 * @version $Revision: 1.2 $ $Date: 2004/01/31 13:44:24 $ 59 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a> 60 */ 61 public class MethodCache { 62 63 /*** 64 * The class for which the methods are being cached 65 */ 66 private final Class _type; 67 68 /*** 69 * A cache of methods, keyed by name. Where multiple instances exist 70 * with the same name, a list is used 71 */ 72 private final HashMap _cache = new HashMap(); 73 74 75 /*** 76 * Cache all public methods for the specified class type 77 * 78 * @param type the class type to cache methods for 79 */ 80 public MethodCache(Class type) { 81 _type = type; 82 Method[] methods = _type.getMethods(); 83 for (int i = 0; i < methods.length; ++i) { 84 Method method = methods[i]; 85 String name = method.getName(); 86 Object value = _cache.get(name); 87 if (value == null) { 88 _cache.put(name, method); 89 } else { 90 if (value instanceof ArrayList) { 91 ((ArrayList) value).add(method); 92 } else { 93 ArrayList list = new ArrayList(); 94 list.add(value); // add the existing method 95 list.add(method); 96 _cache.put(name, list); 97 } 98 } 99 } 100 } 101 102 /*** 103 * Get the named method. If more than one method exists with the same 104 * name, the first will be returned 105 * 106 * @param name the name of the method 107 * @return the first method matching name 108 * @throws NoSuchMethodException if the method doesn't exist 109 */ 110 public Method getMethod(String name) throws NoSuchMethodException { 111 Object value = _cache.get(name); 112 if (value == null) { 113 throw new NoSuchMethodException( 114 "Method=" + name + " not found for class=" + _type.getName()); 115 } 116 Method result = null; 117 if (value instanceof Method) { 118 result = (Method) value; 119 } else { 120 result = (Method) ((ArrayList) value).get(0); 121 } 122 return result; 123 } 124 125 /*** 126 * Get the named method, with the specified number of arguments. 127 * If more than one method exists the first will be returned 128 * 129 * @param name the name of the method 130 * @param args the number of arguments 131 * @return the first method matching name 132 * @throws NoSuchMethodException if the method doesn't exist 133 */ 134 public Method getMethod(String name, int args) 135 throws NoSuchMethodException { 136 137 Object value = _cache.get(name); 138 if (value == null) { 139 throw new NoSuchMethodException( 140 "Method=" + name + " not found for class=" + _type.getName()); 141 } 142 Method result = null; 143 if (value instanceof Method) { 144 result = (Method) value; 145 if (result.getParameterTypes().length != args) { 146 throw new NoSuchMethodException( 147 "Method=" + name + " taking parameters=" + args 148 + " not found for class=" + _type.getName()); 149 } 150 } else { 151 ArrayList list = (ArrayList) value; 152 Iterator iter = list.iterator(); 153 Method next = null; 154 while (iter.hasNext()) { 155 next = (Method) iter.next(); 156 if (next.getParameterTypes().length == args) { 157 result = next; 158 break; 159 } 160 } 161 if (result == null) { 162 throw new NoSuchMethodException( 163 "Method=" + name + " taking parameters=" + args 164 + " not found for class=" + _type.getName()); 165 } 166 } 167 return result; 168 } 169 170 }

This page was automatically generated by Maven