[foreign] RFR: Add a toString method to struct impl classes.
Sundararajan Athijegannathan
sundararajan.athijegannathan at oracle.com
Fri Dec 14 15:06:08 UTC 2018
This is primarily a debug utility. So it could be a static utility
method on java.foreign.memory.Struct. i.e., we can walk through getter
methods by reflection and call those to compute toString.
That way:
* we could avoid generating toString in every struct impl. class
* possible issues with calling getters (which are native memory reads)
from toString
* Default toString can distinguish different instances of the same
struct type (with the same field values)
because of identity hash code. That is useful for debugging at times
(involving struct copy/assignment for eg).
-Sundar
On 14/12/18, 7:57 PM, 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