enumeratum
enumeratum copied to clipboard
Any suggested way to forbid calls to `toString` on `EnumEntry`ies?
.toString
on case objects is generated by Scala compiler, not overridden by Enumeratum... so when a entryName
-mangling mixin like UpperSnakecase
is involved, .toString
and .entryName
return different things. To avoid bugs (esp. in the data returned via API), I've added a piece of ArchUnit like that:
"never call toString() on EnumEntries but use entryName() instead" in {
noClasses.should
.callMethodWhere {
target {
owner {
assignableTo(classOf[enumeratum.EnumEntry])
}
} and target {
nameMatching("toString")
}
}
.because("entryName should be used instead since toString does not take UPPER_SNAKE_CASE into account")
.checkAll(importedProductionClasses, importedTestClasses)
}
}
But I've just realized this doesn't work, as toString
is always recognized to be owned by java.lang.Object
.
Is there suggested way to deal with this problem? I can't see anything below a scalac plugin could handle that properly TBH 🤔
Yea this is a tough one. Maybe https://github.com/mrdziuban/disable-to-string?
See https://github.com/mblink/disable-toString