markhobson / hamcrest-compose

Hamcrest matchers for composition.
Apache License 2.0
30 stars 6 forks source link

Infer feature name for method references in hasFeature #6

Open markhobson opened 9 years ago

markhobson commented 9 years ago

When using ComposeMatchers.hasFeature(Function, Matcher) the feature name defaults to the string representation of the supplied function. For method references this results in something unwieldy like the following:

org.hobsoft.hamcrest.compose.demo.Main$$Lambda$1/424058530@31befd9f

Ideally we would infer the method name from the method reference and use that instead. Furthermore we could translate method names to English property names, e.g. getFirstName to first name.

markhobson commented 9 years ago

Java currently has no official mechanism of obtaining the method name from a method reference. I'm aware of two possible workarounds:

  1. Invoke the method reference function with a spy proxy to obtain the function name. The proxy can be created with CGLib and Objenesis but the problem is determining the proxy's superclass from the method reference. This would either need to be supplied to hasFeature (making this method less convenient) or be obtained in a non-portable way from the bytecode by using a library such as TypeTools.
  2. Use SerializedLambda to obtain the method reference name. This is reliant on lambda's being serializable which they aren't by default. The type intersection required to make a serializable lambda would again make this approach less convenient.

Neither of these approaches seem suitable but I'm open to ideas.

markhobson commented 1 year ago

21 addresses inferring the method reference name.

Remaining task is to derive a friendly name from the method name, e.g. first name from getFirstName.