MethodHandler out of a non-static method

Giuseppe Barbieri elect86 at gmail.com
Tue Jun 8 07:26:15 UTC 2021


Thanks Maurizio and Remi, that helped

Playing around I also got it working by using `bind`

var displayHandle = MethodHandles.lookup().bind(teapot, "display",
MethodType.methodType(void.class));
var displayStub = CLinker.getInstance().upcallStub(displayHandle,
FunctionDescriptor.ofVoid(), scope);


What's the difference? Because I'd expect for `findVirtual` to be used only
on methods in implementation classes of abstract classes

Another question, I'm trying to setup a test project with Gradle, but I'm
having a lot of issues, mainly because Gradle supports only up to jdk 16.

Is possible to run your application with a separate jdk (toolchain) but
then you need to use the `application` plugin and cannot rely anymore on
the comfortable Idea UI.
Unless you switch and run your code using the IDE instead of Gradle, but
then I get into some other exceptions. Moreover, some imports remain still
unresolved.

So, I'd like to skip this trainwreck all at once and ask directly if
anybody here has a working base scenario with the latest EA jdk 17 and
Gradle


Thanks in advance



Il giorno gio 3 giu 2021 alle ore 11:33 Maurizio Cimadamore <
maurizio.cimadamore at oracle.com> ha scritto:

> Hi Giuseppe
>
> On 02/06/2021 23:52, Giuseppe Barbieri wrote:
> > Hi,
> >
> > excited from the last development, I came back giving a try to the latest
> > jdk build (the partial 17 available on the site) and I'm trying to
> > update/fix the opengl sample
> >
> > First of all, it looks like `jextract` doesn't accept anymore the option
> to
> > produce an output jar. I had to manually compress the resulting folder to
> > zip and rename, before importing in the target project.
>
> We decided to simplify jextract and focus on source code generation,
> which seems to be the mode that most people want to work with, and it's
> also the "more primitive": you can derive classes (and jars) from it.
>
> If you are using it in a project, you might consider directly checking
> in the extracted sources in your project. If you are using an IDE, it is
> going to improve your experience a little bit (the classfiles generated
> by the old jextract were poorly understood by some of the IDEs out
> there, which led to issues in indexing).
>





>
> >
> > It'd be cool if jextract could output directly the jar (and the sources)
> >
> > Coming back to my issue, how can I retrieve the MethodHandler out of a
> > non-static method?
>
> You mean you want to create a MethodHandle for an instance method?
>
> Let's see (I'll use jshell to make sure my code doesn't have any typos
> :-) ):
>
> ```
> jshell> class Foo {
>     ...> void greet(String msg) { System.out.println(msg); }
>     ...> }
> |  created class Foo
>
> jshell> import java.lang.invoke.*;
>
> jshell> MethodHandle greetMH =
> MethodHandles.lookup().findVirtual(Foo.class, "greet",
> MethodType.methodType(void.class, String.class));
> greetMH ==> MethodHandle(Foo,String)void
>
> jshell> greetMH.invoke(new Foo(), "Panama");
> Panama
> ```
>
> Is this what you were looking for?
>
> Cheers
> Maurizio
>
> >
> >
> > Thanks in advance
>


More information about the panama-dev mailing list