001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    }