How to wrap a java.lang.Appendable into a java.io.Writer?

前端 未结 3 882
没有蜡笔的小新
没有蜡笔的小新 2020-12-21 08:37

UPDATE2: My own version of the adapter class, that only calls instanceof in the constructor and uses a (Java 1.5) delta in the flush() and cl

3条回答
  •  生来不讨喜
    2020-12-21 09:17

    You can accept any Appendable and then check if it is a Writer through instanceof. Then do a downcast and call that function that accepts only Writer.

    example:

    public void myMethod(Appendable app) throws InvalidAppendableException {
    
       if (app instanceof Writer) {
          someObj.thatMethod((Writer) app);
       } else {
          throw new InvalidAppendableException();
       }
    }
    

提交回复
热议问题