Quantcast
Channel: Active questions tagged methodhandle - Stack Overflow
Viewing all articles
Browse latest Browse all 36

What is the proper MethodType for invocation of a default constructor?

$
0
0

Suggest the code below. To me, it looks fine: I do obtain a MethodType, which takes no parameters as input, and produces an instance of A. The default constructor of a matchesthat description, in my opinion, so I would expect that findConstructor(mt) works. However, I get an Exception. Any ideas, what I am doing wrong?

Thanks,

Jochen

    Exception in thread "main" java.lang.NoSuchMethodException: no such constructor: com.github.jochenw.afw.core.rflct.RflctDemo$A.<init>()A/newInvokeSpecial    at java.base/java.lang.invoke.MemberName.makeAccessException(MemberName.java:974)    at java.base/java.lang.invoke.MemberName$Factory.resolveOrFail(MemberName.java:1117)    at java.base/java.lang.invoke.MethodHandles$Lookup.resolveOrFail(MethodHandles.java:3649)    at java.base/java.lang.invoke.MethodHandles$Lookup.findConstructor(MethodHandles.java:2750)    at com.github.jochenw.afw.core.rflct.RflctDemo.main(RflctDemo.java:18)Caused by: java.lang.NoSuchMethodError: 'com.github.jochenw.afw.core.rflct.RflctDemo$A com.github.jochenw.afw.core.rflct.RflctDemo$A.<init>()'    at java.base/java.lang.invoke.MethodHandleNatives.resolve(Native Method)    at java.base/java.lang.invoke.MemberName$Factory.resolve(MemberName.java:1085)    at java.base/java.lang.invoke.MemberName$Factory.resolveOrFail(MemberName.java:1114)    ... 3 more
import java.lang.invoke.MethodHandle;import java.lang.invoke.MethodHandles;import java.lang.invoke.MethodType;import java.lang.invoke.MethodHandles.Lookup;public class RflctDemo {    /** Instance class, that has a no-args constructor.     */    public static class A {        private boolean valid;        /** Creates a new instance with {@link #isValid() valid} == false.         */        public A() { this(false); }        A(boolean pValid) { valid = pValid; }        boolean isValid() { return valid; }        void setValid(boolean pValid) { valid = pValid; }    }    public static void main(String[] pArgs) throws Throwable {        final Lookup lookup = MethodHandles.privateLookupIn(A.class, MethodHandles.lookup());        final MethodType mt = MethodType.methodType(A.class /* No parameters */);        final MethodHandle mh = lookup.findConstructor(A.class, mt);        final A a = (A) mh.invoke(/* No üarameters */);    }}

Viewing all articles
Browse latest Browse all 36

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>