1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.kernel.util;
21  
22  import java.lang.reflect.Method;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  /**
28   * <a href="MethodCache.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Michael C. Han
31   *
32   */
33  public class MethodCache {
34  
35      public static Method get(String className, String methodName)
36          throws ClassNotFoundException, NoSuchMethodException {
37  
38          return get(null, null, className, methodName);
39      }
40  
41      public static Method get(
42              String className, String methodName, Class<?>[] parameterTypes)
43          throws ClassNotFoundException, NoSuchMethodException {
44  
45          return get(null, null, className, methodName, parameterTypes);
46      }
47  
48      public static Method get(
49              Map<String, Class<?>> classesMap, Map<MethodKey, Method> methodsMap,
50              String className, String methodName)
51          throws ClassNotFoundException, NoSuchMethodException {
52  
53          return get(className, methodName, new Class[0]);
54      }
55  
56      public static Method get(
57              Map<String, Class<?>> classesMap, Map<MethodKey, Method> methodsMap,
58              String className, String methodName, Class<?>[] parameterTypes)
59          throws ClassNotFoundException, NoSuchMethodException {
60  
61          MethodKey methodKey = new MethodKey(
62              classesMap, methodsMap, className, methodName, parameterTypes);
63  
64          return get(methodKey);
65      }
66  
67      public static Method get(MethodKey methodKey)
68          throws ClassNotFoundException, NoSuchMethodException {
69  
70          return _instance._get(methodKey);
71      }
72  
73      private MethodCache() {
74          _classesMap = new HashMap<String, Class<?>>();
75          _methodsMap = new HashMap<MethodKey, Method>();
76      }
77  
78      private Method _get(MethodKey methodKey)
79          throws ClassNotFoundException, NoSuchMethodException {
80  
81          Map<String, Class<?>> classesMap = methodKey.getClassesMap();
82  
83          if (classesMap == null) {
84              classesMap = _classesMap;
85          }
86  
87          Map<MethodKey, Method> methodsMap = methodKey.getMethodsMap();
88  
89          if (methodsMap == null) {
90              methodsMap = _methodsMap;
91          }
92  
93          Method method = methodsMap.get(methodKey);
94  
95          if (method == null) {
96              String className = methodKey.getClassName();
97              String methodName = methodKey.getMethodName();
98              Class<?>[] types = methodKey.getTypes();
99  
100             Class<?> classObj = classesMap.get(className);
101 
102             if (classObj == null) {
103                 Thread currentThread = Thread.currentThread();
104 
105                 ClassLoader contextClassLoader =
106                     currentThread.getContextClassLoader();
107 
108                 classObj = contextClassLoader.loadClass(className);
109 
110                 classesMap.put(className, classObj);
111             }
112 
113             method = classObj.getMethod(methodName, types);
114 
115             methodsMap.put(methodKey, method);
116         }
117 
118         return method;
119     }
120 
121     private static MethodCache _instance = new MethodCache();
122 
123     private Map<String, Class<?>> _classesMap;
124     private Map<MethodKey, Method> _methodsMap;
125 
126 }