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   */
36  public class MethodCache {
37  
38      public static Method get(String className, String methodName)
39          throws ClassNotFoundException, NoSuchMethodException {
40  
41          return get(null, null, className, methodName);
42      }
43  
44      public static Method get(
45              String className, String methodName, Class<?>[] parameterTypes)
46          throws ClassNotFoundException, NoSuchMethodException {
47  
48          return get(null, null, className, methodName, parameterTypes);
49      }
50  
51      public static Method get(
52              Map<String, Class<?>> classesMap, Map<MethodKey, Method> methodsMap,
53              String className, String methodName)
54          throws ClassNotFoundException, NoSuchMethodException {
55  
56          return get(className, methodName, new Class[0]);
57      }
58  
59      public static Method get(
60              Map<String, Class<?>> classesMap, Map<MethodKey, Method> methodsMap,
61              String className, String methodName, Class<?>[] parameterTypes)
62          throws ClassNotFoundException, NoSuchMethodException {
63  
64          MethodKey methodKey = new MethodKey(
65              classesMap, methodsMap, className, methodName, parameterTypes);
66  
67          return get(methodKey);
68      }
69  
70      public static Method get(MethodKey methodKey)
71          throws ClassNotFoundException, NoSuchMethodException {
72  
73          return _instance._get(methodKey);
74      }
75  
76      private MethodCache() {
77          _classesMap = new HashMap<String, Class<?>>();
78          _methodsMap = new HashMap<MethodKey, Method>();
79      }
80  
81      private Method _get(MethodKey methodKey)
82          throws ClassNotFoundException, NoSuchMethodException {
83  
84          Map<String, Class<?>> classesMap = methodKey.getClassesMap();
85  
86          if (classesMap == null) {
87              classesMap = _classesMap;
88          }
89  
90          Map<MethodKey, Method> methodsMap = methodKey.getMethodsMap();
91  
92          if (methodsMap == null) {
93              methodsMap = _methodsMap;
94          }
95  
96          Method method = methodsMap.get(methodKey);
97  
98          if (method == null) {
99              String className = methodKey.getClassName();
100             String methodName = methodKey.getMethodName();
101             Class<?>[] types = methodKey.getTypes();
102 
103             Class<?> classObj = classesMap.get(className);
104 
105             if (classObj == null) {
106                 Thread currentThread = Thread.currentThread();
107 
108                 ClassLoader contextClassLoader =
109                     currentThread.getContextClassLoader();
110 
111                 classObj = contextClassLoader.loadClass(className);
112 
113                 classesMap.put(className, classObj);
114             }
115 
116             method = classObj.getMethod(methodName, types);
117 
118             methodsMap.put(methodKey, method);
119         }
120 
121         return method;
122     }
123 
124     private static MethodCache _instance = new MethodCache();
125 
126     private Map<String, Class<?>> _classesMap;
127     private Map<MethodKey, Method> _methodsMap;
128 
129 }