MethodHandler out of a non-static method

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Thu Jun 3 09:33:13 UTC 2021


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