Java Reflection Method Parameter getMethodHandle(final Lookup lookup, final Class receiver, final String methodName, final Class... parameterTypes)

Here you can find the source of getMethodHandle(final Lookup lookup, final Class receiver, final String methodName, final Class... parameterTypes)

Description

get Method Handle

License

Apache License

Declaration

public static <T> MethodHandle getMethodHandle(final Lookup lookup,
            final Class<T> receiver, final String methodName,
            final Class<?>... parameterTypes) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodHandles.Lookup;
import java.lang.reflect.Method;

public class Main {
    public static <T> MethodHandle getMethodHandle(final Lookup lookup,
            final Class<T> receiver, final String methodName,
            final Class<?>... parameterTypes) {
        return unreflect(lookup,
                getMethod(receiver, methodName, parameterTypes));
    }/*w  w w.  ja  v a 2  s.  c  o  m*/

    public static <T> MethodHandle getMethodHandle(final Class<T> receiver,
            final String methodName, final Class<?>... parameterTypes) {
        return unreflect(MethodHandles.publicLookup(),
                getMethod(receiver, methodName, parameterTypes));
    }

    public static MethodHandle unreflect(final Method m) {
        return unreflect(MethodHandles.publicLookup(), m);
    }

    public static MethodHandle unreflect(final Lookup lookup, final Method m) {
        try {
            return lookup.unreflect(m);
        } catch (final IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static MethodHandle unreflect(final Lookup lookup,
            final Class<?> klass, final String methodName,
            final Class<?>... args) {
        try {
            return unreflect(lookup,
                    klass.getDeclaredMethod(methodName, args));
        } catch (NoSuchMethodException | SecurityException e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> Method getMethod(final Class<T> receiver,
            final String methodName, final Class<?>... parameterTypes) {
        try {
            return receiver.getMethod(methodName, parameterTypes);
        } catch (NoSuchMethodException | SecurityException e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> Method getDeclaredMethod(final Class<T> receiver,
            final String methodName, final Class<?>... parameterTypes) {
        try {
            return receiver.getDeclaredMethod(methodName, parameterTypes);
        } catch (NoSuchMethodException | SecurityException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. getMethodByParametersWithAnnotation(final Class source, final Class[] params, final Class annotation)
  2. getMethodFromClass(String className, String methodName, Class... parameterTypes)
  3. getMethodFromClassName(String classAndMethodName, Class... parameterTypes)
  4. getMethodFullName(Method m, boolean includeClassPackage, boolean includeParametersType, boolean includeTypesPackage)
  5. getMethodGenericParameterTypes(Method method, int index)
  6. getMethodName(Method method, Class[] parameterClasses, String rightCode)
  7. getMethodParameterAnnotations(Method method, int index, Class annotationClass)
  8. getMethodParameterIndexes(final Method m)
  9. getMethodParameters(final Method method, final Map generics)