1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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   */
22  
23  package com.liferay.portal.kernel.util;
24  
25  import java.lang.reflect.Method;
26  
27  import java.util.HashMap;
28  import java.util.Map;
29  
30  /**
31   * <a href="MethodCache.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Michael C. Han
34   */
35  public class MethodCache {
36  
37      public static Method get(String className, String methodName)
38          throws ClassNotFoundException, NoSuchMethodException {
39  
40          return get(null, null, className, methodName);
41      }
42  
43      public static Method get(
44              String className, String methodName, Class<?>[] parameterTypes)
45          throws ClassNotFoundException, NoSuchMethodException {
46  
47          return get(null, null, className, methodName, parameterTypes);
48      }
49  
50      public static Method get(
51              Map<String, Class<?>> classesMap, Map<MethodKey, Method> methodsMap,
52              String className, String methodName)
53          throws ClassNotFoundException, NoSuchMethodException {
54  
55          return get(className, methodName, new Class[0]);
56      }
57  
58      public static Method get(
59              Map<String, Class<?>> classesMap, Map<MethodKey, Method> methodsMap,
60              String className, String methodName, Class<?>[] parameterTypes)
61          throws ClassNotFoundException, NoSuchMethodException {
62  
63          MethodKey methodKey = new MethodKey(
64              classesMap, methodsMap, className, methodName, parameterTypes);
65  
66          return get(methodKey);
67      }
68  
69      public static Method get(MethodKey methodKey)
70          throws ClassNotFoundException, NoSuchMethodException {
71  
72          return _instance._get(methodKey);
73      }
74  
75      private MethodCache() {
76          _classesMap = new HashMap<String, Class<?>>();
77          _methodsMap = new HashMap<MethodKey, Method>();
78      }
79  
80      private Method _get(MethodKey methodKey)
81          throws ClassNotFoundException, NoSuchMethodException {
82  
83          Map<String, Class<?>> classesMap = methodKey.getClassesMap();
84  
85          if (classesMap == null) {
86              classesMap = _classesMap;
87          }
88  
89          Map<MethodKey, Method> methodsMap = methodKey.getMethodsMap();
90  
91          if (methodsMap == null) {
92              methodsMap = _methodsMap;
93          }
94  
95          Method method = methodsMap.get(methodKey);
96  
97          if (method == null) {
98              String className = methodKey.getClassName();
99              String methodName = methodKey.getMethodName();
100             Class<?>[] types = methodKey.getTypes();
101 
102             Class<?> classObj = classesMap.get(className);
103 
104             if (classObj == null) {
105                 Thread currentThread = Thread.currentThread();
106 
107                 ClassLoader contextClassLoader =
108                     currentThread.getContextClassLoader();
109 
110                 classObj = contextClassLoader.loadClass(className);
111 
112                 classesMap.put(className, classObj);
113             }
114 
115             method = classObj.getMethod(methodName, types);
116 
117             methodsMap.put(methodKey, method);
118         }
119 
120         return method;
121     }
122 
123     private static MethodCache _instance = new MethodCache();
124 
125     private Map<String, Class<?>> _classesMap;
126     private Map<MethodKey, Method> _methodsMap;
127 
128 }