Java Reflection Method Parameter getMethod(Class type, String name, Class... parameterTypes)

Here you can find the source of getMethod(Class type, String name, Class... parameterTypes)

Description

get Method

License

BSD License

Declaration

public static Method getMethod(Class<?> type, String name, Class<?>... parameterTypes) 

Method Source Code

//package com.java2s;
/**/*  w w  w .  j  a  v a2 s .  c  o  m*/
 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
 */

import java.lang.reflect.Method;

public class Main {
    public static final String CLINIT = "<clinit>";
    public static final String INIT = "<init>";

    public static Method getMethod(Class<?> type, String name, Class<?>... parameterTypes) {
        try {
            return myGetMethod(type, name, parameterTypes);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
    }

    private static Method myGetMethod(Class<?> type, String name, Class<?>... parameterTypes)
            throws NoSuchMethodException {
        // Scan the type hierarchy just like Class.getMethod(String, Class[])
        // using Class.getDeclaredMethod(String, Class[])
        // System.out.println("type: " + type);
        // System.out.println("name: " + name);
        // System.out
        // .println("parameterTypes: " + Arrays.toString(parameterTypes));
        try {
            // System.out.println("Checking getDeclaredMethod");
            // for (Method m : type.getDeclaredMethods()) {
            // System.out.println("\t" + m);
            // }
            return type.getDeclaredMethod(name, parameterTypes);
        } catch (NoSuchMethodException e) {
            try {
                // Try the super classes
                if (type.getSuperclass() != null) {
                    // System.out.println("Checking super: "
                    // + type.getSuperclass());
                    return myGetMethod(type.getSuperclass(), name, parameterTypes);
                }
            } catch (NoSuchMethodException e2) {
                // Okay
            }
            // Try the super interfaces
            for (Class<?> superInterface : type.getInterfaces()) {
                try {
                    // System.out.println("Checking super interface: "
                    // + superInterface);
                    return myGetMethod(superInterface, name, parameterTypes);
                } catch (NoSuchMethodException e3) {
                    // Okay
                }
            }
            throw new NoSuchMethodException(type.getName() + '.' + getMethodSignature(name, parameterTypes));
        }
    }

    public static String getMethodSignature(String name, Class<?>... parameterTypes) {
        StringBuilder builder = new StringBuilder(name);
        if (!(name.equals(CLINIT) || name.equals(INIT))) {
            builder.append('(');
            if (parameterTypes != null && parameterTypes.length > 0) {
                builder.append(parameterTypes[0].getName());
                for (int i = 1; i < parameterTypes.length; i++) {
                    builder.append(", ").append(parameterTypes[i].getName());
                }
            }
            builder.append(')');
        }
        return builder.toString();
    }
}

Related

  1. getMethod(Class declaringType, String methodName, Class... parameterTypes)
  2. getMethod(Class instance, String method, Class... parameters)
  3. getMethod(Class sourceClass, boolean isFindDeclaredMethod, boolean isUpwardFind, String methodName, Class... methodParameterTypes)
  4. getMethod(Class target, String methodName, Class... parameterTypes)
  5. getMethod(Class type, String name, Class... parametersType)
  6. getMethod(Class type, String name, Class... parameterTypes)
  7. getMethod(Class clazz, String methodName, Class[] parameterTypes)
  8. getMethod(final Class clazz, final String name, final Class... parameterTypes)
  9. getMethod(final Class javaClass, final String methodName, final Class[] methodParameterTypes, final boolean shouldSetAccessible)