001
014
015 package com.liferay.portal.kernel.util;
016
017 import java.lang.reflect.Method;
018
019 import java.util.HashMap;
020 import java.util.Map;
021
022
025 public class MethodCache {
026
027 public static Method get(String className, String methodName)
028 throws ClassNotFoundException, NoSuchMethodException {
029
030 return get(null, null, className, methodName);
031 }
032
033 public static Method get(
034 String className, String methodName, Class<?>[] parameterTypes)
035 throws ClassNotFoundException, NoSuchMethodException {
036
037 return get(null, null, className, methodName, parameterTypes);
038 }
039
040 public static Method get(
041 Map<String, Class<?>> classesMap, Map<MethodKey, Method> methodsMap,
042 String className, String methodName)
043 throws ClassNotFoundException, NoSuchMethodException {
044
045 return get(className, methodName, new Class[0]);
046 }
047
048 public static Method get(
049 Map<String, Class<?>> classesMap, Map<MethodKey, Method> methodsMap,
050 String className, String methodName, Class<?>[] parameterTypes)
051 throws ClassNotFoundException, NoSuchMethodException {
052
053 MethodKey methodKey = new MethodKey(
054 className, methodName, parameterTypes);
055
056 return _instance._get(classesMap, methodsMap, methodKey);
057 }
058
059 public static Method get(MethodKey methodKey)
060 throws ClassNotFoundException, NoSuchMethodException {
061
062 return _instance._get(null, null, methodKey);
063 }
064
065 public static Method put(MethodKey methodKey, Method method) {
066 return _instance._put(methodKey, method);
067 }
068
069 public static void remove(Class<?> clazz) {
070 _instance._remove(clazz);
071 }
072
073 public static void reset() {
074 _instance._reset();
075 }
076
077 private MethodCache() {
078 _classesMap = new HashMap<String, Class<?>>();
079 _methodsMap = new HashMap<MethodKey, Method>();
080 }
081
082 private Method _get(
083 Map<String, Class<?>> classesMap, Map<MethodKey, Method> methodsMap,
084 MethodKey methodKey)
085 throws ClassNotFoundException, NoSuchMethodException {
086
087 if (classesMap == null) {
088 classesMap = _classesMap;
089 }
090
091 if (methodsMap == null) {
092 methodsMap = _methodsMap;
093 }
094
095 Method method = methodsMap.get(methodKey);
096
097 if (method == null) {
098 String className = methodKey.getClassName();
099 String methodName = methodKey.getMethodName();
100 Class<?>[] parameterTypes = methodKey.getParameterTypes();
101
102 Class<?> clazz = classesMap.get(className);
103
104 if (clazz == null) {
105 Thread currentThread = Thread.currentThread();
106
107 ClassLoader contextClassLoader =
108 currentThread.getContextClassLoader();
109
110 clazz = contextClassLoader.loadClass(className);
111
112 classesMap.put(className, clazz);
113 }
114
115 method = clazz.getMethod(methodName, parameterTypes);
116
117 methodsMap.put(methodKey, method);
118 }
119
120 return method;
121 }
122
123 private Method _put(MethodKey methodKey, Method method) {
124 return _methodsMap.put(methodKey, method);
125 }
126
127 private void _remove(Class<?> clazz) {
128 _classesMap.remove(clazz.getName());
129
130 for (Method method : clazz.getMethods()) {
131 _methodsMap.remove(new MethodKey(method));
132 }
133 }
134
135 private void _reset() {
136 _classesMap.clear();
137 _methodsMap.clear();
138 }
139
140 private static MethodCache _instance = new MethodCache();
141
142 private Map<String, Class<?>> _classesMap;
143 private Map<MethodKey, Method> _methodsMap;
144
145 }