01: /**
02: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
03: *
04: * Permission is hereby granted, free of charge, to any person obtaining a copy
05: * of this software and associated documentation files (the "Software"), to deal
06: * in the Software without restriction, including without limitation the rights
07: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
08: * copies of the Software, and to permit persons to whom the Software is
09: * furnished to do so, subject to the following conditions:
10: *
11: * The above copyright notice and this permission notice shall be included in
12: * all copies or substantial portions of the Software.
13: *
14: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20: * SOFTWARE.
21: */package com.liferay.portal.kernel.util;
22:
23: import java.lang.reflect.Method;
24:
25: import java.util.HashMap;
26: import java.util.Map;
27:
28: /**
29: * <a href="MethodCache.java.html"><b><i>View Source</i></b></a>
30: *
31: * @author Michael C. Han
32: *
33: */
34: public class MethodCache {
35:
36: public static Method get(String className, String methodName)
37: throws ClassNotFoundException, NoSuchMethodException {
38:
39: return get(className, methodName, new Class[] {});
40: }
41:
42: public static Method get(String className, String methodName,
43: Class[] parameterTypes) throws ClassNotFoundException,
44: NoSuchMethodException {
45:
46: MethodKey methodKey = new MethodKey(className, methodName,
47: parameterTypes);
48:
49: return get(methodKey);
50: }
51:
52: public static Method get(MethodKey methodKey)
53: throws ClassNotFoundException, NoSuchMethodException {
54:
55: return _instance._get(methodKey);
56: }
57:
58: private MethodCache() {
59: _classes = new HashMap();
60: _methods = new HashMap();
61: }
62:
63: private Method _get(MethodKey methodKey)
64: throws ClassNotFoundException, NoSuchMethodException {
65:
66: Method method = (Method) _methods.get(methodKey);
67:
68: if (method == null) {
69: String className = methodKey.getClassName();
70: String methodName = methodKey.getMethodName();
71: Class[] types = methodKey.getTypes();
72:
73: Class classObj = (Class) _classes.get(className);
74:
75: if (classObj == null) {
76: ClassLoader contextClassLoader = Thread.currentThread()
77: .getContextClassLoader();
78:
79: classObj = contextClassLoader.loadClass(className);
80:
81: _classes.put(className, classObj);
82: }
83:
84: method = classObj.getMethod(methodName, types);
85:
86: _methods.put(methodKey, method);
87: }
88:
89: return method;
90: }
91:
92: private static MethodCache _instance = new MethodCache();
93:
94: private Map _classes;
95: private Map _methods;
96:
97: }
|