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