Java Docs: use instanceof with name binding (#2087)

* Java Docs: use instanceof with name binding

* remove changes in generated code

* Update docs/src/test/java/jdocs/ddata/protobuf/TwoPhaseSetSerializer2.java

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* merge issue

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
PJ Fanning 2025-08-25 13:45:13 +01:00 committed by GitHub
parent 18d7ff815a
commit 56470a4569
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 60 additions and 76 deletions

View file

@ -48,8 +48,8 @@ public class TwoPhaseSetSerializer extends AbstractSerializationSupport {
@Override
public byte[] toBinary(Object obj) {
if (obj instanceof TwoPhaseSet) {
return twoPhaseSetToProto((TwoPhaseSet) obj).toByteArray();
if (obj instanceof TwoPhaseSet tps) {
return twoPhaseSetToProto(tps).toByteArray();
} else {
throw new IllegalArgumentException("Can't serialize object of type " + obj.getClass());
}

View file

@ -49,8 +49,8 @@ public class TwoPhaseSetSerializer2 extends AbstractSerializationSupport {
@Override
public byte[] toBinary(Object obj) {
if (obj instanceof TwoPhaseSet) {
return twoPhaseSetToProto((TwoPhaseSet) obj).toByteArray();
if (obj instanceof TwoPhaseSet tps) {
return twoPhaseSetToProto(tps).toByteArray();
} else {
throw new IllegalArgumentException("Can't serialize object of type " + obj.getClass());
}

View file

@ -24,8 +24,8 @@ public class TwoPhaseSetSerializerWithCompression extends TwoPhaseSetSerializer
// #compression
@Override
public byte[] toBinary(Object obj) {
if (obj instanceof TwoPhaseSet) {
return compress(twoPhaseSetToProto((TwoPhaseSet) obj));
if (obj instanceof TwoPhaseSet tps) {
return compress(twoPhaseSetToProto(tps));
} else {
throw new IllegalArgumentException("Can't serialize object of type " + obj.getClass());
}

View file

@ -67,8 +67,8 @@ public class EchoManager extends AbstractActor {
.match(
Tcp.CommandFailed.class,
failed -> {
if (failed.cmd() instanceof Bind) {
log.warning("cannot bind to [{}]", ((Bind) failed.cmd()).localAddress());
if (failed.cmd() instanceof Bind bind) {
log.warning("cannot bind to [{}]", bind.localAddress());
getContext().stop(getSelf());
} else {
log.warning("unknown command failed [{}]", failed.cmd());

View file

@ -236,11 +236,9 @@ public class LambdaPersistenceDocTest {
}
void updateState(Object event) {
if (event instanceof MsgSent) {
final MsgSent evt = (MsgSent) event;
if (event instanceof MsgSent evt) {
deliver(destination, deliveryId -> new Msg(deliveryId, evt.s));
} else if (event instanceof MsgConfirmed) {
final MsgConfirmed evt = (MsgConfirmed) event;
} else if (event instanceof MsgConfirmed evt) {
confirmDelivery(evt.deliveryId);
}
}

View file

@ -117,8 +117,7 @@ public class PersistenceQueryDocTest {
*/
@Override
public Source<EventEnvelope, NotUsed> eventsByTag(String tag, Offset offset) {
if (offset instanceof Sequence) {
Sequence sequenceOffset = (Sequence) offset;
if (offset instanceof Sequence sequenceOffset) {
return Source.fromGraph(
new MyEventsByTagSource(conn, tag, sequenceOffset.value(), refreshInterval));
} else if (offset == NoOffset.getInstance())

View file

@ -101,8 +101,7 @@ public class PersistenceSchemaEvolutionDocTest {
@Override
public byte[] toBinary(Object o) {
if (o instanceof SeatReserved) {
SeatReserved s = (SeatReserved) o;
if (o instanceof SeatReserved s) {
return FlightAppModels.SeatReserved.newBuilder()
.setRow(s.row)
.setLetter(s.letter)
@ -153,8 +152,7 @@ public class PersistenceSchemaEvolutionDocTest {
@Override
public EventSeq fromJournal(Object event, String manifest) {
if (event instanceof JsObject) {
JsObject json = (JsObject) event;
if (event instanceof JsObject json) {
if (V1.equals(manifest)) json = rename(json, "code", "seatNr");
return EventSeq.single(json);
} else {
@ -217,8 +215,7 @@ public class PersistenceSchemaEvolutionDocTest {
// serialize the object
@Override
public byte[] toBinary(Object obj) {
if (obj instanceof Person) {
Person p = (Person) obj;
if (obj instanceof Person p) {
return (p.name + "|" + p.surname).getBytes(utf8);
} else {
throw new IllegalArgumentException(
@ -312,8 +309,7 @@ public class PersistenceSchemaEvolutionDocTest {
@Override
public EventSeq fromJournal(Object event, String manifest) {
if (event instanceof UserDetailsChanged) {
UserDetailsChanged c = (UserDetailsChanged) event;
if (event instanceof UserDetailsChanged c) {
if (c.name == null) return EventSeq.single(new UserAddressChanged(c.address));
else if (c.address == null) return EventSeq.single(new UserNameChanged(c.name));
else return EventSeq.create(new UserNameChanged(c.name), new UserAddressChanged(c.address));
@ -426,8 +422,7 @@ public class PersistenceSchemaEvolutionDocTest {
@Override
public byte[] toBinary(Object o) {
if (o instanceof SamplePayload) {
SamplePayload s = (SamplePayload) o;
if (o instanceof SamplePayload s) {
return s.payload.toString().getBytes(utf8);
} else {
// previously also handled "old" events here.
@ -511,8 +506,7 @@ public class PersistenceSchemaEvolutionDocTest {
@Override
public Object toJournal(Object event) {
if (event instanceof SeatBooked) {
SeatBooked s = (SeatBooked) event;
if (event instanceof SeatBooked s) {
return new SeatBookedData(s.code, s.customer.name);
} else {
throw new IllegalArgumentException("Unsupported: " + event.getClass());
@ -521,8 +515,7 @@ public class PersistenceSchemaEvolutionDocTest {
@Override
public EventSeq fromJournal(Object event, String manifest) {
if (event instanceof SeatBookedData) {
SeatBookedData d = (SeatBookedData) event;
if (event instanceof SeatBookedData d) {
return EventSeq.single(new SeatBooked(d.code, new Customer(d.customerName)));
} else {
throw new IllegalArgumentException("Unsupported: " + event.getClass());
@ -551,8 +544,7 @@ public class PersistenceSchemaEvolutionDocTest {
@Override
public EventSeq fromJournal(Object event, String manifest) {
if (event instanceof JsObject) {
JsObject json = (JsObject) event;
if (event instanceof JsObject json) {
return EventSeq.single(marshaller.fromJson(json));
} else {
throw new IllegalArgumentException(

View file

@ -74,8 +74,7 @@ public class LeveldbPersistenceQueryDocTest {
@Override
public Object toJournal(Object event) {
if (event instanceof String) {
String s = (String) event;
if (event instanceof String s) {
Set<String> tags = new HashSet<String>();
if (s.contains("green")) tags.add("green");
if (s.contains("black")) tags.add("black");

View file

@ -43,8 +43,7 @@ public class TestKitExamples {
// if you return ProcessingSuccess the operation will be performed, otherwise not.
if (count < 10) {
count += 1;
if (processingUnit instanceof ReadEvents) {
ReadEvents read = (ReadEvents) processingUnit;
if (processingUnit instanceof ReadEvents read) {
if (read.batch().nonEmpty()) {
ProcessingSuccess.getInstance();
} else {
@ -79,8 +78,7 @@ public class TestKitExamples {
// if you return ProcessingSuccess the operation will be performed, otherwise not.
if (count < 10) {
count += 1;
if (processingUnit instanceof ReadSnapshot) {
ReadSnapshot read = (ReadSnapshot) processingUnit;
if (processingUnit instanceof ReadSnapshot read) {
if (read.getSnapshot().isPresent()) {
ProcessingSuccess.getInstance();
} else {

View file

@ -135,8 +135,8 @@ public class ConsistentHashingRouterDocTest extends AbstractJavaTest {
new ConsistentHashMapper() {
@Override
public Object hashKey(Object message) {
if (message instanceof Evict) {
return ((Evict) message).key;
if (message instanceof Evict evict) {
return evict.key;
} else {
return null;
}

View file

@ -91,7 +91,7 @@ public class CustomRouterDocTest extends AbstractJavaTest {
@Override
public boolean equals(Object obj) {
return (obj instanceof TestRoutee) && n == ((TestRoutee) obj).n;
return (obj instanceof TestRoutee routee) && n == routee.n;
}
}

View file

@ -116,8 +116,8 @@ public class SerializationDocTest {
@Override
public byte[] toBinary(Object obj) {
// Put the real code that serializes the object here
if (obj instanceof Customer) return ((Customer) obj).name.getBytes(UTF_8);
else if (obj instanceof User) return ((User) obj).name.getBytes(UTF_8);
if (obj instanceof Customer customer) return customer.name.getBytes(UTF_8);
else if (obj instanceof User user) return user.name.getBytes(UTF_8);
else throw new IllegalArgumentException("Unknown type: " + obj);
}

View file

@ -54,28 +54,28 @@ public class ClusterShardingTest {
@Override
public String entityId(Object message) {
if (message instanceof Counter.EntityEnvelope)
return String.valueOf(((Counter.EntityEnvelope) message).id);
else if (message instanceof Counter.Get)
return String.valueOf(((Counter.Get) message).counterId);
if (message instanceof Counter.EntityEnvelope envelope)
return String.valueOf(envelope.id);
else if (message instanceof Counter.Get get)
return String.valueOf(get.counterId);
else return null;
}
@Override
public Object entityMessage(Object message) {
if (message instanceof Counter.EntityEnvelope)
return ((Counter.EntityEnvelope) message).payload;
if (message instanceof Counter.EntityEnvelope envelope)
return envelope.payload;
else return message;
}
@Override
public String shardId(Object message) {
int numberOfShards = 100;
if (message instanceof Counter.EntityEnvelope) {
long id = ((Counter.EntityEnvelope) message).id;
if (message instanceof Counter.EntityEnvelope envelope) {
long id = envelope.id;
return String.valueOf(id % numberOfShards);
} else if (message instanceof Counter.Get) {
long id = ((Counter.Get) message).counterId;
} else if (message instanceof Counter.Get get) {
long id = get.counterId;
return String.valueOf(id % numberOfShards);
} else {
return null;
@ -123,17 +123,17 @@ public class ClusterShardingTest {
@Override
public String entityId(Object message) {
if (message instanceof Counter.EntityEnvelope)
return String.valueOf(((Counter.EntityEnvelope) message).id);
else if (message instanceof Counter.Get)
return String.valueOf(((Counter.Get) message).counterId);
if (message instanceof Counter.EntityEnvelope envelope)
return String.valueOf(envelope.id);
else if (message instanceof Counter.Get get)
return String.valueOf(get.counterId);
else return null;
}
@Override
public Object entityMessage(Object message) {
if (message instanceof Counter.EntityEnvelope)
return ((Counter.EntityEnvelope) message).payload;
if (message instanceof Counter.EntityEnvelope envelope)
return envelope.payload;
else return message;
}
@ -141,14 +141,14 @@ public class ClusterShardingTest {
@Override
public String shardId(Object message) {
int numberOfShards = 100;
if (message instanceof Counter.EntityEnvelope) {
long id = ((Counter.EntityEnvelope) message).id;
if (message instanceof Counter.EntityEnvelope envelope) {
long id = envelope.id;
return String.valueOf(id % numberOfShards);
} else if (message instanceof Counter.Get) {
long id = ((Counter.Get) message).counterId;
} else if (message instanceof Counter.Get get) {
long id = get.counterId;
return String.valueOf(id % numberOfShards);
} else if (message instanceof ShardRegion.StartEntity) {
long id = Long.valueOf(((ShardRegion.StartEntity) message).entityId());
} else if (message instanceof ShardRegion.StartEntity entity) {
long id = Long.valueOf(entity.entityId());
return String.valueOf(id % numberOfShards);
} else {
return null;

View file

@ -64,8 +64,8 @@ public class BidiFlowDocTest extends AbstractJavaTest {
@Override
public boolean equals(Object o) {
if (o instanceof Ping) {
return ((Ping) o).id == id;
if (o instanceof Ping ping) {
return ping.id == id;
} else return false;
}
@ -84,8 +84,8 @@ public class BidiFlowDocTest extends AbstractJavaTest {
@Override
public boolean equals(Object o) {
if (o instanceof Pong) {
return ((Pong) o).id == id;
if (o instanceof Pong pong) {
return pong.id == id;
} else return false;
}
@ -98,8 +98,8 @@ public class BidiFlowDocTest extends AbstractJavaTest {
// #codec-impl
public static ByteString toBytes(Message msg) {
// #implementation-details-elided
if (msg instanceof Ping) {
final int id = ((Ping) msg).id;
if (msg instanceof Ping ping) {
final int id = ping.id;
return new ByteStringBuilder().putByte((byte) 1).putInt(id, ByteOrder.LITTLE_ENDIAN).result();
} else {
final int id = ((Pong) msg).id;

View file

@ -803,8 +803,7 @@ public class IntegrationDocTest extends AbstractJavaTest {
System.out.println("enqueued " + x);
} else if (result == QueueOfferResult.dropped()) {
System.out.println("dropped " + x);
} else if (result instanceof QueueOfferResult.Failure) {
QueueOfferResult.Failure failure = (QueueOfferResult.Failure) result;
} else if (result instanceof QueueOfferResult.Failure failure) {
System.out.println("Offer failed " + failure.cause().getMessage());
} else if (result instanceof QueueOfferResult.QueueClosed$) {
System.out.println("Bounded Source Queue closed");

View file

@ -34,11 +34,10 @@ public class Monitor {
private static <T> void printMonitorState(FlowMonitorState.StreamState<T> state) {
if (state == FlowMonitorState.finished()) {
System.out.println("Stream is initialized but hasn't processed any element");
} else if (state instanceof FlowMonitorState.Received) {
FlowMonitorState.Received msg = (FlowMonitorState.Received) state;
System.out.println("Last message received: " + msg.msg());
} else if (state instanceof FlowMonitorState.Failed) {
Throwable cause = ((FlowMonitorState.Failed) state).cause();
} else if (state instanceof FlowMonitorState.Received received) {
System.out.println("Last message received: " + received.msg());
} else if (state instanceof FlowMonitorState.Failed failed) {
Throwable cause = failed.cause();
System.out.println("Stream failed with cause: " + cause.getMessage());
} else {
System.out.println("Stream completed already");