Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use LambdaMetafactory.metafactory() for plain non-static getter

I have a simple Person class with a getName() that returns a String:

public class Person {

    public String getName() {...}

}

How do I use LambdaMetafactory to create a lambda for that non-static method getName() at runtime?

Here's what I got this far:

public class MyMain {

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

        MethodHandles.Lookup lookup = MethodHandles.lookup();
        String invokedMethodName = "getName";
        MethodType invokedType = MethodType.methodType(GetterFunction.class);
        MethodType methodType = MethodType.methodType(Object.class);
        MethodHandle virtual = lookup.findVirtual(Person.class, "getName", MethodType.methodType(String.class));
        CallSite site = LambdaMetafactory.metafactory(lookup,
                invokedMethodName,
                invokedType,
                methodType,
                virtual,
                methodType);
        getterFunction = (GetterFunction) site.getTarget().invokeExact();
        System.out.println(getterFunction.getName(new Person("Ann")));
    }

    @FunctionalInterface
    private interface GetterFunction {

        String getName(Person person);

    }

}

Which throws:

java.lang.invoke.LambdaConversionException: Incorrect number of parameters for instance method invokeVirtual foo.Person.getName:()String; 0 captured parameters, 0 functional interface method parameters, 0 implementation parameters
    at java.lang.invoke.AbstractValidatingLambdaMetafactory.validateMetafactoryArgs(AbstractValidatingLambdaMetafactory.java:193)
    at java.lang.invoke.LambdaMetafactory.metafactory(LambdaMetafactory.java:303)
like image 367
Geoffrey De Smet Avatar asked Oct 18 '25 05:10

Geoffrey De Smet


1 Answers

Just extending Geoffrey De Smet's answer, to show what it would look like with Function, as there is a minor adjustment needed:

public class MyMain {

    public static void main(String[] args) throws Throwable {
        MethodHandles.Lookup lookup = MethodHandles.lookup();
        CallSite site = LambdaMetafactory.metafactory(lookup,
                "apply",
                MethodType.methodType(Function.class),
                MethodType.methodType(Object.class, Object.class), // Function::apply signature
                lookup.findVirtual(Person.class, "getName", MethodType.methodType(String.class)),
                MethodType.methodType(String.class, Person.class) // Person::getName signature
        );

        Function<Person, String> getterFunction = (Function<Person, String>) site.getTarget().invokeExact();

        System.out.println(getterFunction.apply(new Person("Ann")));
    }

    static class Person {
        String name;

        public Person(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
    }

}
like image 160
Lodovik Avatar answered Oct 20 '25 17:10

Lodovik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!