Example usage for java.lang.reflect Method isDefault

List of usage examples for java.lang.reflect Method isDefault

Introduction

In this page you can find the example usage for java.lang.reflect Method isDefault.

Prototype

public boolean isDefault() 

Source Link

Document

Returns true if this method is a default method; returns false otherwise.

Usage

From source file:net.jodah.typetools.TypeResolver.java

/**
 * Populates the {@code map} with variable/argument pairs for the {@code functionalInterface}.
 *///from   www  .  j ava 2s  .c  o m
private static void populateLambdaArgs(Class<?> functionalInterface, final Class<?> lambdaType,
        Map<TypeVariable<?>, Type> map) {
    if (GET_CONSTANT_POOL != null) {
        try {
            // Find SAM
            for (Method m : functionalInterface.getMethods()) {
                if (!m.isDefault() && !Modifier.isStatic(m.getModifiers()) && !m.isBridge()) {
                    // Skip methods that override Object.class
                    Method objectMethod = OBJECT_METHODS.get(m.getName());
                    if (objectMethod != null
                            && Arrays.equals(m.getTypeParameters(), objectMethod.getTypeParameters()))
                        continue;

                    // Get functional interface's type params
                    Type returnTypeVar = m.getGenericReturnType();
                    Type[] paramTypeVars = m.getGenericParameterTypes();

                    // Get lambda's type arguments
                    ConstantPool constantPool = (ConstantPool) GET_CONSTANT_POOL.invoke(lambdaType);
                    String[] methodRefInfo = constantPool.getMemberRefInfoAt(
                            constantPool.getSize() - resolveMethodRefOffset(constantPool, lambdaType));

                    // Skip auto boxing methods
                    if (methodRefInfo[1].equals("valueOf") && constantPool.getSize() > 22) {
                        try {
                            methodRefInfo = constantPool.getMemberRefInfoAt(constantPool.getSize()
                                    - resolveAutoboxedMethodRefOffset(constantPool, lambdaType));
                        } catch (MethodRefOffsetResolutionFailed ignore) {
                        }
                    }

                    if (returnTypeVar instanceof TypeVariable) {
                        Class<?> returnType = TypeDescriptor.getReturnType(methodRefInfo[2])
                                .getType(lambdaType.getClassLoader());
                        if (!returnType.equals(Void.class))
                            map.put((TypeVariable<?>) returnTypeVar, returnType);
                    }

                    TypeDescriptor[] arguments = TypeDescriptor.getArgumentTypes(methodRefInfo[2]);

                    // Handle arbitrary object instance method references
                    int paramOffset = 0;
                    if (paramTypeVars[0] instanceof TypeVariable
                            && paramTypeVars.length == arguments.length + 1) {
                        Class<?> instanceType = TypeDescriptor.getObjectType(methodRefInfo[0])
                                .getType(lambdaType.getClassLoader());
                        map.put((TypeVariable<?>) paramTypeVars[0], instanceType);
                        paramOffset = 1;
                    }

                    // Handle local final variables from context that are passed as arguments.
                    int argOffset = 0;
                    if (paramTypeVars.length < arguments.length) {
                        argOffset = arguments.length - paramTypeVars.length;
                    }

                    for (int i = 0; i + argOffset < arguments.length; i++) {
                        if (paramTypeVars[i] instanceof TypeVariable) {
                            map.put((TypeVariable<?>) paramTypeVars[i + paramOffset],
                                    arguments[i + argOffset].getType(lambdaType.getClassLoader()));
                        }
                    }
                    break;
                }
            }

        } catch (Exception ignore) {
        }
    }
}