BaseMessage: make bitcoinSerializeToStream() abstract

Rather than log an error at runtime, the compiler should fail with an error
when subclasses of `BaseMessage` don't implement `bitcoinSerializeToStream()`.
This commit is contained in:
Sean Gilligan 2023-04-15 13:27:47 -07:00 committed by Andreas Schildbach
parent 792183dfef
commit 97b88fffdf
2 changed files with 5 additions and 3 deletions

View file

@ -62,9 +62,7 @@ public abstract class BaseMessage implements Message {
/**
* Serializes this message to the provided stream. If you just want the raw bytes use bitcoinSerialize().
*/
protected void bitcoinSerializeToStream(OutputStream stream) throws IOException {
log.error("Error: {} class has not implemented bitcoinSerializeToStream method. Generating message with no payload", getClass());
}
protected abstract void bitcoinSerializeToStream(OutputStream stream) throws IOException;
/** @deprecated use {@link Transaction#getTxId()}, {@link Block#getHash()}, {@link FilteredBlock#getHash()} or {@link TransactionOutPoint#hash()} */
@Deprecated

View file

@ -25,6 +25,8 @@ import org.bitcoinj.params.TestNet3Params;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;
import java.net.InetAddress;
import java.nio.BufferUnderflowException;
@ -234,6 +236,8 @@ public class BitcoinSerializerTest {
MessageSerializer serializer = MAINNET.getDefaultSerializer();
Message unknownMessage = new BaseMessage() {
@Override
protected void bitcoinSerializeToStream(OutputStream stream) {}
};
ByteArrayOutputStream bos = new ByteArrayOutputStream(ADDRESS_MESSAGE_BYTES.length);
serializer.serialize(unknownMessage, bos);