[foreign] RFR: Add a toString method to struct impl classes.

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Fri Dec 14 14:44:41 UTC 2018


Looks good - an alternative would be to do that reflectively, or to have 
our own toString bootstrap (we already have a few of them).

Curious - what happens if you also enable it for unions? Granted, some 
of the values might be garbage, but it should work?

Maurizio

On 14/12/2018 14:27, Jorn Vernee wrote:
> Hi,
>
> During porting the binder to Windows I wrote a bunch of debugging 
> code. I thought this might be useful to have in the future, and I'd 
> like to handle these so I can remove them from my other local changes 
> (less stuff to maintain), so I'm creating this RFR. There are 3 
> subjects that I've split into 3 patches; This is the first one.
>
> 1.) toString method generation for Structs. This adds generation of a 
> toString method to StructImplGenerator, which uses the struct's 
> getters to output the values of the struct e.g.
>
> @NativeStruct("[i32(get=one$get)(set=one$set)i32(get=two$get)(set=two$set)]")
>     public interface MyStruct extends Struct<MyStruct> {
>         int one$get();
>         void one$set(int i);
>
>         int two$get();
>         void two$set(int i);
>     }
>
>     public static void main(String[] args) throws 
> IllegalAccessException {
>         try(var scope = Scope.newNativeScope()) {
>             MyStruct s = scope.allocateStruct(MyStruct.class);
>
>             s.one$set(10);
>             s.two$set(20);
>
>             System.out.println(s);
>         }
>     }
>
> Will output:
>
>     Main$MyStruct{ one$get=10, two$get=20 }
>
> Webrev: 
> http://cr.openjdk.java.net/~jvernee/panama/webrevs/structtostring/webrev.01/
>
> Please let me know what you think and if you want to have this in the 
> repo, otherwise I will just throw it away.
>
> Cheers,
> Jorn


More information about the panama-dev mailing list