MethodHandler out of a non-static method

Remi Forax forax at univ-mlv.fr
Thu Jun 3 09:47:51 UTC 2021


----- Mail original -----
> De: "Maurizio Cimadamore" <maurizio.cimadamore at oracle.com>
> À: "Giuseppe Barbieri" <elect86 at gmail.com>, "panama-dev at openjdk.java.net'" <panama-dev at openjdk.java.net>
> Envoyé: Jeudi 3 Juin 2021 11:33:13
> Objet: Re: MethodHandler out of a non-static method

> 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
> ```
> 

I will add that intelliJ is able to do completion inside Lookup.findStatic, LookupfindVirtual, etc
at the point when you write the string corresponding to the name, using Ctrl+Space complete the rest.

> Is this what you were looking for?
> 
> Cheers
> Maurizio

Regards,
Rémi


More information about the panama-dev mailing list