Skip to content

Commit 520bb88

Browse files
authored
IGNITE-27155 BinaryWriterExImpl#failIfUnregistered can be final (#12528)
1 parent 41affa3 commit 520bb88

File tree

7 files changed

+32
-35
lines changed

7 files changed

+32
-35
lines changed

modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryUtils.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2921,12 +2921,19 @@ static BinaryReaderEx reader(BinaryContext ctx,
29212921

29222922
/**
29232923
* @param ctx Context.
2924+
* @param failIfUnregistered Flag to fail while writing object of unregistered type.
29242925
* @return Writer instance.
29252926
*/
2926-
public static BinaryWriterEx writer(BinaryContext ctx) {
2927+
public static BinaryWriterEx writer(BinaryContext ctx, boolean failIfUnregistered) {
29272928
BinaryThreadLocalContext locCtx = BinaryThreadLocalContext.get();
29282929

2929-
return new BinaryWriterExImpl(ctx, BinaryStreams.outputStream((int)CommonUtils.KB, locCtx.chunk()), locCtx.schemaHolder(), null);
2930+
return new BinaryWriterExImpl(
2931+
ctx,
2932+
BinaryStreams.outputStream((int)CommonUtils.KB, locCtx.chunk()),
2933+
locCtx.schemaHolder(),
2934+
null,
2935+
failIfUnregistered
2936+
);
29302937
}
29312938

29322939
/**
@@ -2935,7 +2942,7 @@ public static BinaryWriterEx writer(BinaryContext ctx) {
29352942
* @return Writer instance.
29362943
*/
29372944
public static BinaryWriterEx writer(BinaryContext ctx, BinaryOutputStream out) {
2938-
return new BinaryWriterExImpl(ctx, out, BinaryThreadLocalContext.get().schemaHolder(), null);
2945+
return new BinaryWriterExImpl(ctx, out, BinaryThreadLocalContext.get().schemaHolder(), null, false);
29392946
}
29402947

29412948
/**
@@ -2944,7 +2951,7 @@ public static BinaryWriterEx writer(BinaryContext ctx, BinaryOutputStream out) {
29442951
* @return Writer instance.
29452952
*/
29462953
public static BinaryWriterEx writer(BinaryContext ctx, BinaryOutputStream out, BinaryWriterSchemaHolder schema) {
2947-
return new BinaryWriterExImpl(ctx, out, schema, null);
2954+
return new BinaryWriterExImpl(ctx, out, schema, null, false);
29482955
}
29492956

29502957
/** @return Instance of caching handler. */

modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterEx.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,6 @@ public interface BinaryWriterEx extends BinaryWriter, BinaryRawWriter, ObjectOut
6666
*/
6767
public boolean failIfUnregistered();
6868

69-
/**
70-
* @param failIfUnregistered Fail if unregistered.
71-
*/
72-
public void failIfUnregistered(boolean failIfUnregistered);
73-
7469
/**
7570
* @param obj Object.
7671
* @throws org.apache.ignite.binary.BinaryObjectException In case of error.

modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,26 @@ class BinaryWriterExImpl implements BinaryWriterEx {
8383
private BinaryInternalMapper mapper;
8484

8585
/** */
86-
private boolean failIfUnregistered;
86+
private final boolean failIfUnregistered;
8787

8888
/**
8989
* @param ctx Context.
9090
* @param out Output stream.
9191
* @param handles Handles.
92+
* @param failIfUnregistered Flag to fail while writing object of unregistered type.
9293
*/
93-
public BinaryWriterExImpl(BinaryContext ctx, BinaryOutputStream out, BinaryWriterSchemaHolder schema, BinaryWriterHandles handles) {
94+
public BinaryWriterExImpl(
95+
BinaryContext ctx,
96+
BinaryOutputStream out,
97+
BinaryWriterSchemaHolder schema,
98+
BinaryWriterHandles handles,
99+
boolean failIfUnregistered
100+
) {
94101
this.ctx = ctx;
95102
this.out = out;
96103
this.schema = schema;
97104
this.handles = handles;
105+
this.failIfUnregistered = failIfUnregistered;
98106

99107
start = out.position();
100108
}
@@ -104,11 +112,6 @@ public BinaryWriterExImpl(BinaryContext ctx, BinaryOutputStream out, BinaryWrite
104112
return failIfUnregistered;
105113
}
106114

107-
/** {@inheritDoc} */
108-
@Override public void failIfUnregistered(boolean failIfUnregistered) {
109-
this.failIfUnregistered = failIfUnregistered;
110-
}
111-
112115
/** {@inheritDoc} */
113116
@Override public void typeId(int typeId) {
114117
this.typeId = typeId;
@@ -844,9 +847,7 @@ void writeBooleanField(@Nullable Boolean val) {
844847
if (obj == null)
845848
out.writeByte(GridBinaryMarshaller.NULL);
846849
else {
847-
BinaryWriterExImpl writer = new BinaryWriterExImpl(ctx, out, schema, handles());
848-
849-
writer.failIfUnregistered(failIfUnregistered);
850+
BinaryWriterExImpl writer = new BinaryWriterExImpl(ctx, out, schema, handles(), failIfUnregistered);
850851

851852
writer.marshal(obj);
852853
}
@@ -857,9 +858,7 @@ void writeBooleanField(@Nullable Boolean val) {
857858
if (obj == null)
858859
out.writeByte(GridBinaryMarshaller.NULL);
859860
else {
860-
BinaryWriterExImpl writer = new BinaryWriterExImpl(ctx, out, schema, null);
861-
862-
writer.failIfUnregistered(failIfUnregistered);
861+
BinaryWriterExImpl writer = new BinaryWriterExImpl(ctx, out, schema, null, failIfUnregistered);
863862

864863
writer.marshal(obj);
865864
}
@@ -1538,9 +1537,7 @@ boolean tryWriteAsHandle(Object obj) {
15381537

15391538
/** {@inheritDoc} */
15401539
@Override public BinaryWriterEx newWriter(int typeId) {
1541-
BinaryWriterExImpl res = new BinaryWriterExImpl(ctx, out, schema, handles());
1542-
1543-
res.failIfUnregistered(failIfUnregistered);
1540+
BinaryWriterExImpl res = new BinaryWriterExImpl(ctx, out, schema, handles(), failIfUnregistered);
15441541

15451542
res.typeId(typeId);
15461543

modules/binary/api/src/main/java/org/apache/ignite/internal/binary/GridBinaryMarshaller.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,7 @@ public byte[] marshal(@Nullable Object obj, boolean failIfUnregistered) throws B
250250
if (obj == null)
251251
return new byte[] { NULL };
252252

253-
try (BinaryWriterEx writer = BinaryUtils.writer(ctx)) {
254-
writer.failIfUnregistered(failIfUnregistered);
255-
253+
try (BinaryWriterEx writer = BinaryUtils.writer(ctx, failIfUnregistered)) {
256254
writer.marshal(obj);
257255

258256
return writer.array();

modules/binary/impl/src/main/java/org/apache/ignite/internal/binary/builder/BinaryObjectBuilderImpl.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,12 @@ public BinaryObjectBuilderImpl(BinaryObjectEx obj) {
172172

173173
/** {@inheritDoc} */
174174
@Override public BinaryObject build() {
175-
try (BinaryWriterEx writer = BinaryUtils.writer(ctx)) {
176-
Thread curThread = Thread.currentThread();
177-
178-
if (curThread instanceof IgniteThread)
179-
writer.failIfUnregistered(((IgniteThread)curThread).isForbiddenToRequestBinaryMetadata());
175+
Thread curThread = Thread.currentThread();
180176

177+
try (BinaryWriterEx writer = BinaryUtils.writer(
178+
ctx,
179+
curThread instanceof IgniteThread && ((IgniteThread)curThread).isForbiddenToRequestBinaryMetadata())
180+
) {
181181
writer.typeId(typeId);
182182

183183
BinaryBuilderSerializer serializationCtx = new BinaryBuilderSerializer();

modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryMarshallerSelfTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3019,7 +3019,7 @@ public void testThreadLocalArrayReleased() throws Exception {
30193019

30203020
BinaryMarshaller marsh = binaryMarshaller();
30213021

3022-
try (BinaryWriterEx writer = BinaryUtils.writer(binaryContext(marsh))) {
3022+
try (BinaryWriterEx writer = BinaryUtils.writer(binaryContext(marsh), false)) {
30233023
assertEquals(true, BinaryStreamsTestUtils.threadLocalIsAcquired());
30243024

30253025
writer.writeString("Thread local test");

modules/core/src/test/java/org/apache/ignite/internal/binary/RawBinaryObjectExtractorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void test() throws Exception {
4949

5050
byte[] serializedTestObjectsBytes;
5151

52-
try (BinaryWriterEx writer = BinaryUtils.writer(ctx)) {
52+
try (BinaryWriterEx writer = BinaryUtils.writer(ctx, false)) {
5353
testObjects.forEach(writer::writeObject);
5454

5555
serializedTestObjectsBytes = writer.array();

0 commit comments

Comments
 (0)