1
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
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 }