Giter Club home page Giter Club logo

Comments (3)

danieldietrich avatar danieldietrich commented on August 23, 2024

This: http://cr.openjdk.java.net/~briangoetz/lambda/lambda-translation.html

from vavr.

danieldietrich avatar danieldietrich commented on August 23, 2024

getImplMethodSignature() does only work in simple cases (see javaslang.lambda.Lambdas)

from vavr.

danieldietrich avatar danieldietrich commented on August 23, 2024

Constraint: The lambda has to be Serializable. Here is the test code that shows, that the generic types are erased:

public class Test {

    public static void main(String[] args) throws Throwable {

        final Function<Integer, String> f = i -> String.valueOf(i + 1);

        System.out.println("f.class: " + f.getClass());
        System.out.println("f.class.methods: " + Strings.toString(f.getClass().getMethods()));

        final SerializableFunction<Integer, String> lambda = t -> f.apply(t);
        final SerializedLambda serializedLambda = Lambdas.getSerializedLambda(lambda).get();

        final Class<?> refc = Class.forName(serializedLambda.getFunctionalInterfaceClass().replaceAll("/", "\\."));
        final String name = serializedLambda.getFunctionalInterfaceMethodName();
        final MethodType methodType = MethodType.fromMethodDescriptorString(
                serializedLambda.getFunctionalInterfaceMethodSignature(), lambda.getClass().getClassLoader());

        final MethodHandles.Lookup lookup = MethodHandles.lookup();
        final MethodHandle methodHandle = lookup.findVirtual(refc, name, methodType);

        System.out.println("SERIALIZED LAMBDA: " + serializedLambda);
        System.out.println("METHOD TYPE: " + methodType);
        System.out.println("METHOD HANDLE: " + methodHandle);

        //      final MethodHandle bound = methodHandle.bindTo(lambda);
        //      System.out.println("BOUND METHOD HANDLE: " + bound);
        //
        //      final Object o = bound.invoke(1);
        //      System.out.println("f(1): " + o);

        final MethodHandleInfo info = lookup.revealDirect(methodHandle);
        System.out.println("METHOD HANDLE INFO: " + info);
    }
}

The main problem with respect to this bug is, that captured variables are part of the signature of the generated runtime lambda. I.e. the signature of the following lambda is (Function, Object) -> Object instead of (Integer) -> String:

final SerializableFunction<Integer, String> lambda = t -> f.apply(t);

I will investigate, how to create a SerializedLambda instead to keep the correct signature of the underlying Function f.

from vavr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.