get Deserialized Lambda Object using reflection - Java Lambda Stream

Java examples for Lambda Stream:Optional

Description

get Deserialized Lambda Object using reflection

Demo Code


//package com.java2s;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Optional;

public class Main {
    public static Optional<Object> getDeserializedLambdaObject(
            SerializedLambda sl) {
        Method readReplace = null;
        Object obj = null;//from  ww  w .j a v  a 2  s  .c om
        try {
            readReplace = SerializedLambda.class
                    .getDeclaredMethod("readResolve");
            readReplace.setAccessible(true);
            obj = readReplace.invoke(sl);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } finally {
            return Optional.of(obj);
        }
    }
}

Related Tutorials