Force Java Immutables to generate toString() for Throwable classes
Force Java Immutables to generate toString() for Throwable classes
Is it possible to force Java Immutables annotation processing library to generate toString method for an abstract class which already inherits non-default toString() method?
For example:
@Value.Immutables
public abstract class MyRuntimeException extends RuntimeException
@Value.Default
public abstract long timestamp();
@Value.Redacted
public abstract long secretTimestamp();
And result to be:
MyRuntimeException e = ImmutableMyRuntimeException.builder().timestamp(111)
.secretTimestamp(222).build();
assert e.toString().contains("111");
assert !e.toString().contains("222");
RuntimeException inherits toString() from Throwables and what I have seen so far Immutables library skips generating toString() because of that.
1 Answer
1
There is one such way. The trick is to declare abstract toString
method signature in abstract value type.
toString
@Value.Immutable
public abstract class MyRuntimeException extends RuntimeException
public abstract long timestamp();
public abstract long secretTimestamp();
@Override public abstract String toString(); //<-- forces toString impl
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.