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.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.MethodParameter;
020    import com.liferay.portal.kernel.util.MethodParametersResolver;
021    
022    import java.lang.reflect.AccessibleObject;
023    import java.lang.reflect.Method;
024    
025    import java.util.HashMap;
026    import java.util.Map;
027    
028    import jodd.paramo.Paramo;
029    
030    /**
031     * @author Igor Spasic
032     */
033    public class MethodParametersResolverImpl implements MethodParametersResolver {
034    
035            @Override
036            public MethodParameter[] resolveMethodParameters(Method method) {
037                    MethodParameter[] methodParameters = _methodParameters.get(method);
038    
039                    if (methodParameters != null) {
040                            return methodParameters;
041                    }
042    
043                    try {
044                            Class<?>[] methodParameterTypes = method.getParameterTypes();
045    
046                            jodd.paramo.MethodParameter[] joddMethodParameters =
047                                    Paramo.resolveParameters(method);
048    
049                            methodParameters = new MethodParameter[joddMethodParameters.length];
050    
051                            for (int i = 0; i < joddMethodParameters.length; i++) {
052                                    methodParameters[i] = new MethodParameter(
053                                            joddMethodParameters[i].getName(),
054                                            joddMethodParameters[i].getSignature(),
055                                            methodParameterTypes[i]);
056                            }
057                    }
058                    catch (Exception e) {
059                            _log.error(e, e);
060    
061                            return null;
062                    }
063    
064                    _methodParameters.put(method, methodParameters);
065    
066                    return methodParameters;
067            }
068    
069            private static Log _log = LogFactoryUtil.getLog(
070                    MethodParametersResolverImpl.class);
071    
072            private Map<AccessibleObject, MethodParameter[]> _methodParameters =
073                    new HashMap<AccessibleObject, MethodParameter[]>();
074    
075    }