001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.util.internal.cache;
020:
021: import java.lang.reflect.Method;
022:
023: import org.apache.beehive.netui.util.internal.concurrent.InternalConcurrentHashMap;
024: import org.apache.beehive.netui.util.logging.Logger;
025:
026: /**
027: * @exclude
028: */
029: public class MethodCache {
030:
031: private static final Logger LOGGER = Logger
032: .getInstance(MethodCache.class);
033:
034: private final InternalConcurrentHashMap _methodCache;
035:
036: /**
037: *
038: */
039: public MethodCache() {
040: _methodCache = new InternalConcurrentHashMap();
041: }
042:
043: /**
044: *
045: */
046: public final Method[] getMethods(Class type) {
047: Object obj = _methodCache.get(type);
048:
049: if (obj == null) {
050: obj = type.getMethods();
051: _methodCache.put(type, obj);
052: }
053:
054: return (Method[]) obj;
055: }
056:
057: /**
058: *
059: */
060: public final Method getMethod(Class type, String methodName,
061: int argCount) {
062: if (LOGGER.isDebugEnabled())
063: LOGGER.debug("Get method \"" + methodName
064: + "\" from type \"" + type + "\" with " + argCount
065: + " params");
066:
067: if (methodName == null)
068: return null;
069:
070: Method[] methods = getMethods(type);
071:
072: for (int i = 0; i < methods.length; i++) {
073: if (methods[i].getName().equals(methodName)
074: && (argCount == methods[i].getParameterTypes().length))
075: return methods[i];
076: }
077:
078: return null;
079: }
080:
081: /**
082: *
083: */
084: public final Method getMethod(Class type, String methodName,
085: String[] argTypes) {
086: if (methodName == null)
087: return null;
088:
089: Method[] methods = getMethods(type);
090: Class[] parameterTypes = null;
091:
092: for (int i = 0; i < methods.length; i++) {
093: // method names don't match
094: if (!methods[i].getName().equals(methodName))
095: continue;
096:
097: // never null...
098: parameterTypes = methods[i].getParameterTypes();
099:
100: /* zero arg method */
101: if ((argTypes == null || argTypes.length == 0)
102: && parameterTypes.length == 0)
103: return methods[i];
104: /* looking for zero arg method; found multi arg method */
105: else if ((argTypes == null || argTypes.length == 0)
106: && !(parameterTypes.length == 0))
107: continue;
108: /* method matching arg count; check argument types */
109: else if (parameterTypes != null
110: && parameterTypes.length == argTypes.length) {
111: boolean match = true;
112: for (int j = 0; j < parameterTypes.length; j++) {
113: String formalTypeName = parameterTypes[j].getName();
114: String actualTypeName = argTypes[j];
115:
116: if (!formalTypeName.equals(actualTypeName)) {
117: match = false;
118: break;
119: }
120: }
121: if (match)
122: return methods[i];
123: }
124: }
125:
126: return null;
127: }
128:
129: /**
130: *
131: */
132: public final Method getMethod(Class type, String methodName,
133: Class[] argTypes) {
134: if (argTypes == null)
135: return getMethod(type, methodName, (String[]) null);
136:
137: String[] typeStrs = new String[argTypes.length];
138: for (int i = 0; i < argTypes.length; i++) {
139: typeStrs[i] = argTypes[i].getName();
140: }
141:
142: return getMethod(type, methodName, typeStrs);
143: }
144: }
|