简体   繁体   English

强制Java不可变为Throwable类生成toString()

[英]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? 是否可以强制Java Immutables注释处理库为已经继承非默认toString()方法的抽象类生成toString方法?

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. RuntimeException从Throwables继承toString()和我到目前为止看到的Immutables库跳过生成toString()的原因。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM