3

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.

4

1 回答 1

2

有一种这样的方法。诀窍是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
}
于 2018-09-11T18:47:18.853 回答