Java Reflection Method Parameter getMethodParameterIndexes(final Method m)

Here you can find the source of getMethodParameterIndexes(final Method m)

Description

Try to determine the names of the method parameters.

License

Apache License

Parameter

Parameter Description
m a parameter

Return

Empty list if no parameters present or names could not be determined. List of parameter names else.

Declaration

public static Map<String, Integer> getMethodParameterIndexes(final Method m) 

Method Source Code

//package com.java2s;
/**/*from  w w w . j a v a 2 s  .c o  m*/
 *  Copyright 2013 Sven Ewald
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import java.util.Collections;
import java.util.HashMap;

import java.util.Locale;
import java.util.Map;

public class Main {
    private final static Method GETPARAMETERS = findMethodByName(Method.class, "getParameters");

    /**
     * Try to determine the names of the method parameters. Does not work on pre Java 8 jdk and
     * given method owner has to be compiled with "-parameters" option. NOTICE: The correct function
     * depends on the JVM implementation.
     *
     * @param m
     * @return Empty list if no parameters present or names could not be determined. List of
     *         parameter names else.
     */
    public static Map<String, Integer> getMethodParameterIndexes(final Method m) {
        if ((GETPARAMETERS == null) || (m == null)) {
            return Collections.emptyMap();
        }
        Map<String, Integer> paramNames = new HashMap<String, Integer>();
        try {
            Object[] params = (Object[]) GETPARAMETERS.invoke(m);
            if (params.length == 0) {
                return Collections.emptyMap();
            }
            Method getName = findMethodByName(params[0].getClass(), "getName");
            if (getName == null) {
                return Collections.emptyMap();
            }
            int i = -1;
            for (Object o : params) {
                ++i;
                String name = (String) getName.invoke(o);
                if (name == null) {
                    continue;
                }
                paramNames.put(name.toUpperCase(Locale.ENGLISH), i);
            }
            return Collections.unmodifiableMap(paramNames);
        } catch (IllegalArgumentException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Non exception throwing shortcut to find the first method with a given name.
     *
     * @param clazz
     * @param name
     * @return method with name "name" or null if it does not exist.
     */
    public static Method findMethodByName(final Class<?> clazz, final String name) {
        for (final Method m : clazz.getMethods()) {
            if (name.equals(m.getName())) {
                return m;
            }
        }
        return null;
    }
}

Related

  1. getMethodFullName(Method m, boolean includeClassPackage, boolean includeParametersType, boolean includeTypesPackage)
  2. getMethodGenericParameterTypes(Method method, int index)
  3. getMethodHandle(final Lookup lookup, final Class receiver, final String methodName, final Class... parameterTypes)
  4. getMethodName(Method method, Class[] parameterClasses, String rightCode)
  5. getMethodParameterAnnotations(Method method, int index, Class annotationClass)
  6. getMethodParameters(final Method method, final Map generics)
  7. getMethodParametersType(Class clazz, String methodName)
  8. getMethodParameterTypes(final Method method)
  9. getMethodQuietly(Class clazz, String methodName, Class... parameterTypes)