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