KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > util > internal > cache > MethodCache


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.util.internal.cache;
19
20 import java.lang.reflect.Method JavaDoc;
21 import org.apache.beehive.netui.util.internal.concurrent.InternalConcurrentHashMap;
22
23 import org.apache.beehive.netui.util.logging.Logger;
24
25 /**
26  * @exclude
27  */

28 public class MethodCache {
29
30     private static final Logger LOGGER = Logger.getInstance(MethodCache.class);
31
32     private final InternalConcurrentHashMap _methodCache;
33
34     /**
35      *
36      */

37     public MethodCache() {
38         _methodCache = new InternalConcurrentHashMap();
39     }
40
41     /**
42      *
43      */

44     public final Method JavaDoc[] getMethods(Class JavaDoc type) {
45         if(LOGGER.isDebugEnabled()) LOGGER.debug("type: " + type + " hash code: " + type.hashCode());
46
47         Object JavaDoc obj = _methodCache.get(type);
48
49         if(obj == null) {
50             obj = type.getMethods();
51             _methodCache.put(type, obj);
52         }
53
54         return (Method JavaDoc[])obj;
55     }
56
57     /**
58      *
59      */

60     public final Method JavaDoc getMethod(Class JavaDoc type, String JavaDoc methodName, int argCount) {
61         if(LOGGER.isDebugEnabled()) LOGGER.debug("Get method \"" + methodName + "\" from type \"" + type + "\" with " + argCount + " params");
62
63         if(methodName == null)
64             return null;
65
66         Method JavaDoc[] methods = getMethods(type);
67
68         for(int i = 0; i < methods.length; i++) {
69             if(methods[i].getName().equals(methodName) && (argCount == methods[i].getParameterTypes().length))
70                 return methods[i];
71         }
72
73         return null;
74     }
75
76     /**
77      *
78      */

79     public final Method JavaDoc getMethod(Class JavaDoc type, String JavaDoc methodName, String JavaDoc[] argTypes) {
80         if(methodName == null)
81             return null;
82
83         Method JavaDoc[] methods = getMethods(type);
84         Class JavaDoc[] parameterTypes = null;
85
86         for(int i = 0; i < methods.length; i++) {
87             // method names don't match
88
if(!methods[i].getName().equals(methodName))
89                 continue;
90
91             // never null...
92
parameterTypes = methods[i].getParameterTypes();
93             
94             // zero arg method
95
if((argTypes == null || argTypes.length == 0) && parameterTypes.length == 0)
96                 return methods[i];
97             // looking for zero arg method; found multi arg method
98
else if((argTypes == null || argTypes.length == 0) && !(parameterTypes.length == 0))
99                 continue;
100             // method matching arg count; check argument types
101
else if(parameterTypes != null && parameterTypes.length == argTypes.length) {
102                 boolean match = true;
103                 for(int j = 0; j < parameterTypes.length; j++) {
104                     if(!parameterTypes[j].getName().equals(argTypes[j])) {
105                         match = false;
106                         break;
107                     }
108                 }
109                 if(match) return methods[i];
110             }
111         }
112
113         return null;
114     }
115
116     /**
117      *
118      */

119     public final Method JavaDoc getMethod(Class JavaDoc type, String JavaDoc methodName, Class JavaDoc[] argTypes) {
120         if(argTypes == null)
121             return getMethod(type, methodName, (String JavaDoc[])null);
122
123         String JavaDoc[] typeStrs = new String JavaDoc[argTypes.length];
124         for(int i = 0; i < argTypes.length; i++) {
125             typeStrs[i] = argTypes[i].getName();
126         }
127
128         return getMethod(type, methodName, typeStrs);
129     }
130 }
131
Popular Tags