001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.lang.reflect.Method;
018    
019    import java.util.Map;
020    import java.util.concurrent.ConcurrentHashMap;
021    
022    /**
023     * @author Michael C. Han
024     * @author Shuyang Zhou
025     */
026    public class MethodCache {
027    
028            public static void reset() {
029                    _methods.clear();
030            }
031    
032            /**
033             * @see MethodKey
034             */
035            protected static Method get(MethodKey methodKey)
036                    throws NoSuchMethodException {
037    
038                    Method method = _methods.get(methodKey);
039    
040                    if (method == null) {
041                            Class<?> declaringClass = methodKey.getDeclaringClass();
042    
043                            method = declaringClass.getDeclaredMethod(
044                                    methodKey.getMethodName(), methodKey.getParameterTypes());
045    
046                            if (!method.isAccessible()) {
047                                    method.setAccessible(true);
048                            }
049    
050                            _methods.put(methodKey, method);
051                    }
052    
053                    return method;
054            }
055    
056            private static Map<MethodKey, Method> _methods =
057                    new ConcurrentHashMap<MethodKey, Method>();
058    
059    }