toString/equals/hashCode implemented using method handles

mandy chung mandy.chung at
Wed Jun 20 22:00:39 UTC 2018

Hi Remi,

Thanks for this. I will look into it and gladly update the 
implementation (the current implementation was just an initial version 
to enable javac to generate the indy).


On 6/20/18 2:00 PM, Remi Forax wrote:
> As discussed during our today meeting, here is an implementation of
> toString, equals and hashCode that can be used as default
> implementation of these methods for value types.
> toString use the ConcatMetafactory so the code is simple, for
> equals(), it first sort the fields to have the primitive type at the
> end of the array and the reference type at the beginning, given that
> the tree is constructed by adding equals tests in front of the
> previous equals tests, the constructed method handle tree will test
> the fields that stores primitive types first, for hashCode(), it's
> just a reduction using foldArguments, it creates big mh trees but the
> code should be fast (not loop combinator).
> The code tries to cache every primitive method handles and doesn't
> cache method handles that come from methods in user-defined classes.
> Currently the code retrieves the fields using the reflection, making
> the code hard to test because the order of the fields returned by
> getDeclaredFields() is not specified, perhaps the list of the fields
> should be stored in a Constant Dynamic as an array of MethodHandle
> (we can use MethodHandleInfo if we want the name of a method) and
> pass as a boostrap argument of the indy call inside toString, equals
> and hashCode.
> cheers, Rémi

More information about the valhalla-spec-experts mailing list