001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.util;
028:
029: import java.lang.reflect.Constructor;
030: import java.lang.reflect.Method;
031: import java.util.Map;
032:
033: /**
034: * Some utilities for java reflection that are more efficient and/or slightly
035: * different functionality then the jdk equivalents.
036: **/
037:
038: public final class Reflect {
039: /** map of class->methods **/
040: private static final Map methodsCache = new LRUCache(256);
041:
042: /** memoize class.getMethods(); **/
043: public static Method[] getMethods(Class cl) {
044: synchronized (methodsCache) {
045: Method[] ms = (Method[]) methodsCache.get(cl);
046: if (ms != null)
047: return ms;
048: ms = cl.getMethods();
049: methodsCache.put(cl, ms);
050: return ms;
051: }
052: }
053:
054: /** Like class.getMethod(String, Class[]) except cheaper and returns
055: * null instead of throwing an exception when nothing found.
056: **/
057: public static Method getMethod(Class cl, String name, Class[] params) {
058: Method[] ms = getMethods(cl);
059: int l = ms.length;
060: for (int i = 0; i < l; i++) {
061: Method m = ms[i];
062: Class[] p = m.getParameterTypes();
063: if (name.equals(m.getName())) {
064: if (equalParameterTypes(params, p))
065: return m;
066: }
067: }
068: return null;
069: }
070:
071: private static final Map constructorCache = new LRUCache(256);
072:
073: /** memoize class.getConstructors(); **/
074: public static Constructor[] getConstructors(Class cl) {
075: synchronized (constructorCache) {
076: Constructor[] cs = (Constructor[]) constructorCache.get(cl);
077: if (cs != null)
078: return cs;
079: cs = cl.getConstructors();
080: constructorCache.put(cl, cs);
081: return cs;
082: }
083: }
084:
085: /** Like class.getConstructor(Class[]) except cheaper and returns
086: * null instead of throwing an exception when nothing found.
087: **/
088: public static Constructor getConstructor(Class cl, Class[] params) {
089: Constructor[] cs = getConstructors(cl);
090: int l = cs.length;
091: for (int i = 0; i < l; i++) {
092: Constructor c = cs[i];
093: Class[] p = c.getParameterTypes();
094: if (equalParameterTypes(params, p))
095: return c;
096: }
097: return null;
098: }
099:
100: private static boolean equalParameterTypes(Class[] p1, Class[] p2) {
101: if (p1 == null) {
102: if (p2 == null) {
103: return true;
104: } else {
105: return (p2.length == 0);
106: }
107: } else {
108: if (p2 == null) {
109: return (p1.length == 0);
110: } else {
111: int l = p1.length;
112: if (l != p2.length)
113: return false;
114: for (int i = 0; i < l; i++) {
115: if (p1[i] != p2[i])
116: return false;
117: }
118: return true;
119: }
120: }
121: }
122: }
|