Example usage for java.lang.invoke SerializedLambda getCapturedArgCount

List of usage examples for java.lang.invoke SerializedLambda getCapturedArgCount

Introduction

In this page you can find the example usage for java.lang.invoke SerializedLambda getCapturedArgCount.

Prototype

public int getCapturedArgCount() 

Source Link

Document

Get the count of dynamic arguments to the lambda capture site.

Usage

From source file:org.lambdamatic.analyzer.LambdaExpressionAnalyzer.java

/**
 * Returns the {@link SerializedLambdaInfo} for the given {@code expression}
 * /*from   w ww.  j  a v a  2 s .c  o  m*/
 * @param expression the expression to analyze.
 * @return the corresponding {@link SerializedLambda}
 * @throws AnalyzeException if something wrong happened (a {@link NoSuchMethodException},
 *         {@link IllegalArgumentException} or {@link InvocationTargetException} exception
 *         occurred).
 * 
 * @see http ://docs.oracle.com/javase/8/docs/api/java/lang/invoke/SerializedLambda.html
 * @see http ://stackoverflow.com/questions/21860875/printing-debug-info-on-errors
 *      -with-java-8-lambda-expressions/21879031 #21879031
 */
private static SerializedLambdaInfo getSerializedLambdaInfo(final Object expression) {
    final Class<?> cl = expression.getClass();
    try {
        final Method m = cl.getDeclaredMethod("writeReplace");
        m.setAccessible(true);
        final Object result = m.invoke(expression);
        if (result instanceof SerializedLambda) {
            final SerializedLambda serializedLambda = (SerializedLambda) result;
            LOGGER.debug(" Lambda FunctionalInterface: {}.{} ({})",
                    serializedLambda.getFunctionalInterfaceClass(),
                    serializedLambda.getFunctionalInterfaceMethodName(),
                    serializedLambda.getFunctionalInterfaceMethodSignature());
            LOGGER.debug(" Lambda Implementation: {}.{} ({})", serializedLambda.getImplClass(),
                    serializedLambda.getImplMethodName(), serializedLambda.getImplMethodSignature());
            IntStream.range(0, serializedLambda.getCapturedArgCount())
                    .forEach(i -> LOGGER.debug("  with Captured Arg(" + i + "): '"
                            + serializedLambda.getCapturedArg(i)
                            + ((serializedLambda.getCapturedArg(i) != null)
                                    ? "' (" + serializedLambda.getCapturedArg(i).getClass().getName() + ")"
                                    : "")));
            return new SerializedLambdaInfo(serializedLambda);
        }
    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
        throw new AnalyzeException("Failed to find the Serialized form for the given Lambda Expression", e);
    }
    return null;
}