Skip to content

Commit ade661a

Browse files
authored
Merge pull request #2 from JoniSt/allow-final-fields
Enable serialization of final fields
2 parents fca29ab + 068d5c2 commit ade661a

2 files changed

Lines changed: 61 additions & 3 deletions

File tree

src/netserializer/SerDes.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,12 @@ private <T extends Serializable> TypeReaderWriter<T> makeClassReaderWriter(final
662662
final List<FieldInfo> persistentFields = new ArrayList<>();
663663
for (Field f: fields) {
664664
int modifiers = f.getModifiers();
665-
if (!Modifier.isFinal(modifiers) && !Modifier.isStatic(modifiers) && !Modifier.isTransient(modifiers)) {
665+
666+
//v1.1: Allow serialization of final fields.
667+
//Setting final fields via reflection may give weird results if the
668+
//field is a compile-time constant, but if it is, it will never have
669+
//a different value anyway, so this is not an issue.
670+
if (!Modifier.isStatic(modifiers) && !Modifier.isTransient(modifiers)) {
666671

667672
//If the field's compile-time type is primitive, it is possible
668673
//to omit the runtime object type since the runtime type will

unittests/netserializer/SerDesTest.java

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,41 @@ public static TestClassMap getInstance() {
206206
}
207207
}
208208

209+
class TestClassFinalField implements Serializable {
210+
public final int i1;
211+
public final Integer i2;
212+
213+
//Compile-time constant
214+
public final String str = "abc";
215+
216+
public TestClassFinalField() {
217+
i1 = 99;
218+
i2 = 3;
219+
}
220+
221+
public TestClassFinalField(int i1, Integer i2) {
222+
this.i1 = i1;
223+
this.i2 = i2;
224+
}
225+
226+
@Override
227+
public boolean equals(Object obj) {
228+
if (obj == null) {
229+
return false;
230+
}
231+
if (!this.getClass().equals(obj.getClass())) {
232+
return false;
233+
}
234+
TestClassFinalField other = (TestClassFinalField)obj;
235+
return (this.i1 == other.i1) && this.i2.equals(other.i2) && this.str.equals(other.str);
236+
}
237+
238+
@Override
239+
public int hashCode() {
240+
return Integer.hashCode(i1) + this.i2.hashCode() + str.hashCode();
241+
}
242+
}
243+
209244
public class SerDesTest {
210245

211246
private SerDes writer;
@@ -218,7 +253,8 @@ private SerDes makeSerDes() throws NoSuchMethodException, SecurityException {
218253
TestClassComposed.class,
219254
TestClassBoxedPrimitive.class,
220255
TestClassListAndSet.class,
221-
TestClassMap.class));
256+
TestClassMap.class,
257+
TestClassFinalField.class));
222258
}
223259

224260
@Before
@@ -319,14 +355,31 @@ public void testNull() throws Exception {
319355
testWith(null);
320356
}
321357

358+
/**
359+
* Tests serialization of final fields.
360+
*
361+
* @throws Exception
362+
*/
363+
@Test
364+
public void testFinalFields() throws Exception {
365+
String whatItShouldBe = new TestClassFinalField().str;
366+
367+
TestClassFinalField obj = (TestClassFinalField)testWith(new TestClassFinalField());
368+
assertTrue(obj.str.equals(whatItShouldBe));
369+
370+
obj = (TestClassFinalField)testWith(new TestClassFinalField(5, 87));
371+
assertTrue(obj.str.equals(whatItShouldBe));
372+
}
373+
322374

323-
private void testWith(Object obj) throws Exception {
375+
private Object testWith(Object obj) throws Exception {
324376
Object result = this.serializeAndDeserialize(obj, writer, reader);
325377
if (obj != null) {
326378
assertTrue(obj.equals(result));
327379
} else {
328380
assertTrue(result == null);
329381
}
382+
return result;
330383
}
331384

332385
private Object serializeAndDeserialize(Object obj, SerDes writer, SerDes reader) throws Exception {

0 commit comments

Comments
 (0)