arrow-core icon indicating copy to clipboard operation
arrow-core copied to clipboard

Arrow tries (and fails) to set final fields via reflection

Open hcoles opened this issue 5 years ago • 1 comments

I maintain http://pitest.org, a mutation testing system for java, which is also being increasingly used with kotlin.

Pitest includes a block coverage system, the implementation of which adds a synthetic static final field to each class.

This causes a runtime error with arrow as it attempts to write to the final field via reflection.

The problem can be reproduced in the following repo by running gradlew pitest

https://github.com/Octogonapus/reproduce-pitest-problem

Unfortunately I don't speak kotlin, and have only a hazy idea of what arrow is trying to achieve, but it seems likely that it should avoid writing to synthetic fields (and probably also final static ones).

If it does need to write to final fields then

https://github.com/arrow-kt/arrow/blob/master/modules/core/arrow-core-data/src/main/kotlin/arrow/typeclasses/ContinuationUtils.kt

should first change the field's modifier as well as making it accessible. e.g.

it.getModifiers() & ~Modifier.FINAL

Arrow seems to work ok with jacoco as the probe field it adds to classes is non final. There may be issues with other coverage systems however (and would be with jacoco for interfaces, as the jvm spec requires final fields in this case, but I don't know enough about arrow to know if this is a valid scenario).

hcoles avatar Dec 04 '19 20:12 hcoles

Thank you for the report. I'll take a look ASAP. We're migrating away from the reflection hack (which I assure you is necessary 🙃) soon-ish, maybe we can have a point release with the fix.

Congratulations on Pitest by the way. I did not know it and it looks POWERFUL 🤩

pakoito avatar Dec 04 '19 21:12 pakoito