Skip to content

Commit 44e7dfe

Browse files
authored
Merge pull request #8 from cordisvictor/v1.4.2
v1.4.2: SerializableStrategy GetFields.readFields fix
2 parents 244e95d + da8764f commit 44e7dfe

File tree

3 files changed

+59
-51
lines changed

3 files changed

+59
-51
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ EasyML
99
(http://sourceforge.net/projects/kxml/files/kxml2/2.3.0/kxml2-min-2.3.0.jar/download)
1010

1111

12+
!Release 1.4.2
13+
- bugfix: n.s.e.m.j.i.SerializableStrategy GetFieldImpl readFields fix.
14+
15+
1216
!Release 1.4.1
1317
- bugfix: XMLWriter text driver empty line when pretty printing.
1418

easyml/src/net/sourceforge/easyml/marshalling/java/io/SerializableStrategy.java

Lines changed: 44 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
*
5555
* @author Victor Cordis ( cordis.victor at gmail.com)
5656
* @since 1.0
57-
* @version 1.3.8
57+
* @version 1.4.2
5858
*/
5959
public class SerializableStrategy extends AbstractStrategy<Serializable>
6060
implements CompositeStrategy<Serializable> {
@@ -530,7 +530,7 @@ public void writeDouble(double val) throws IOException {
530530
public void writeFields() throws IOException {
531531
this.writer.startElement(SerializableStrategy.ELEMENT_FIELDS);
532532
if (this.lazyPutFieldImpl != null) {
533-
for (Map.Entry<String, Object> field : this.lazyPutFieldImpl.content.entrySet()) {
533+
for (Map.Entry<String, Object> field : this.lazyPutFieldImpl.fields.entrySet()) {
534534
final String key = field.getKey();
535535
final Object val = field.getValue();
536536
this.writer.startElement(key);
@@ -587,51 +587,51 @@ public void writeUnshared(Object obj) throws IOException {
587587

588588
private static final class PutFieldImpl extends ObjectOutputStream.PutField {
589589

590-
private final Map<String, Object> content = new HashMap<>();
590+
private final Map<String, Object> fields = new HashMap<>();
591591

592592
@Override
593593
public void put(String name, boolean val) {
594-
this.content.put(name, val);
594+
this.fields.put(name, val);
595595
}
596596

597597
@Override
598598
public void put(String name, byte val) {
599-
this.content.put(name, val);
599+
this.fields.put(name, val);
600600
}
601601

602602
@Override
603603
public void put(String name, char val) {
604-
this.content.put(name, val);
604+
this.fields.put(name, val);
605605
}
606606

607607
@Override
608608
public void put(String name, short val) {
609-
this.content.put(name, val);
609+
this.fields.put(name, val);
610610
}
611611

612612
@Override
613613
public void put(String name, int val) {
614-
this.content.put(name, val);
614+
this.fields.put(name, val);
615615
}
616616

617617
@Override
618618
public void put(String name, long val) {
619-
this.content.put(name, val);
619+
this.fields.put(name, val);
620620
}
621621

622622
@Override
623623
public void put(String name, float val) {
624-
this.content.put(name, val);
624+
this.fields.put(name, val);
625625
}
626626

627627
@Override
628628
public void put(String name, double val) {
629-
this.content.put(name, val);
629+
this.fields.put(name, val);
630630
}
631631

632632
@Override
633633
public void put(String name, Object val) {
634-
this.content.put(name, val);
634+
this.fields.put(name, val);
635635
}
636636

637637
@Deprecated
@@ -718,25 +718,26 @@ public GetField readFields() throws IOException, ClassNotFoundException {
718718
fields.put(localPartName, null);
719719
} else {
720720
// check for an alias in case readFields reads XML written by defaultMarshalObject:
721+
ValueType keyVT;
721722
try {
722723
final Field aliasedF = this.context.fieldFor(this.level, localPartName);
723-
final ValueType keyVT = SerializableStrategy.valueTypeFor(aliasedF);
724-
if (keyVT != null) {
725-
try {
726-
fields.put(localPartName, keyVT.parseValue(this.reader.readValue()));
727-
} catch (NumberFormatException nfx) {
728-
throw new InvalidFormatException(this.context.readerPositionDescriptor(), nfx);
729-
} catch (IllegalArgumentException iax) {
730-
throw new InvalidFormatException(this.context.readerPositionDescriptor(), iax);
731-
}
732-
} else { // move down and read object:
733-
if (!this.reader.next() || !this.reader.atElementStart()) {
734-
throw new InvalidFormatException(this.context.readerPositionDescriptor(), "expected element start");
735-
}
736-
fields.put(localPartName, this.reader.read());
724+
keyVT = SerializableStrategy.valueTypeFor(aliasedF);
725+
} catch (NoSuchFieldException customFieldKey) {
726+
// must be a non-source field name.
727+
// we will read type from XML.
728+
keyVT = null;
729+
}
730+
if (keyVT != null) {
731+
try {
732+
fields.put(localPartName, keyVT.parseValue(this.reader.readValue()));
733+
} catch (IllegalArgumentException iax) {
734+
throw new InvalidFormatException(this.context.readerPositionDescriptor(), iax);
737735
}
738-
} catch (NoSuchFieldException invalidLocalPartNameX) {
739-
throw new IOException(invalidLocalPartNameX);
736+
} else { // move down and read object:
737+
if (!this.reader.next() || !this.reader.atElementStart()) {
738+
throw new InvalidFormatException(this.context.readerPositionDescriptor(), "expected element start");
739+
}
740+
fields.put(localPartName, this.reader.read());
740741
}
741742
}
742743
} else if (this.reader.atElementEnd() && this.reader.elementName().equals(SerializableStrategy.ELEMENT_FIELDS)) {
@@ -836,10 +837,10 @@ public long skip(long n) throws IOException {
836837

837838
private static final class GetFieldImpl extends ObjectInputStream.GetField {
838839

839-
private final Map content;
840+
private final Map fields;
840841

841-
public GetFieldImpl(Map content) {
842-
this.content = content;
842+
public GetFieldImpl(Map fields) {
843+
this.fields = fields;
843844
}
844845

845846
@Override
@@ -852,68 +853,60 @@ public boolean defaulted(String name) throws IOException {
852853
return false;
853854
}
854855

855-
public <T> T get0(String name, Class<T> c) {
856-
final Object value = this.content.get(name);
856+
private <T> T get0(String name, Class<T> c, T defValue) {
857+
final Object value = this.fields.get(name);
857858
if (value != null) {
858859
if (value.getClass() == c) {
859860
return (T) value;
860861
}
861862
throw new IllegalArgumentException("field name does not map to required type: " + name + ", " + c.getName());
862863
}
863-
return null;
864+
return defValue;
864865
}
865866

866867
@Override
867868
public boolean get(String name, boolean val) throws IOException {
868-
final Boolean value = this.get0(name, Boolean.class);
869-
return value != null ? value : val;
869+
return this.get0(name, Boolean.class, val);
870870
}
871871

872872
@Override
873873
public byte get(String name, byte val) throws IOException {
874-
final Byte value = this.get0(name, Byte.class);
875-
return value != null ? value : val;
874+
return this.get0(name, Byte.class, val);
876875
}
877876

878877
@Override
879878
public char get(String name, char val) throws IOException {
880-
final Character value = this.get0(name, Character.class);
881-
return value != null ? value : val;
879+
return this.get0(name, Character.class, val);
882880
}
883881

884882
@Override
885883
public short get(String name, short val) throws IOException {
886-
final Short value = this.get0(name, Short.class);
887-
return value != null ? value : val;
884+
return this.get0(name, Short.class, val);
888885
}
889886

890887
@Override
891888
public int get(String name, int val) throws IOException {
892-
final Integer value = this.get0(name, Integer.class);
893-
return value != null ? value : val;
889+
return this.get0(name, Integer.class, val);
894890
}
895891

896892
@Override
897893
public long get(String name, long val) throws IOException {
898-
final Long value = this.get0(name, Long.class);
899-
return value != null ? value : val;
894+
return this.get0(name, Long.class, val);
900895
}
901896

902897
@Override
903898
public float get(String name, float val) throws IOException {
904-
final Float value = this.get0(name, Float.class);
905-
return value != null ? value : val;
899+
return this.get0(name, Float.class, val);
906900
}
907901

908902
@Override
909903
public double get(String name, double val) throws IOException {
910-
final Double value = this.get0(name, Double.class);
911-
return value != null ? value : val;
904+
return this.get0(name, Double.class, val);
912905
}
913906

914907
@Override
915908
public Object get(String name, Object val) throws IOException {
916-
final Object value = this.content.get(name);
909+
final Object value = this.fields.get(name);
917910
return value != null ? value : val;
918911
}
919912
}//(+)class GetFieldImpl.

easyml/test/net/sourceforge/easyml/BugsTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.sourceforge.easyml;
22

3+
import java.util.BitSet;
34
import net.sourceforge.easyml.marshalling.java.lang.ObjectStrategy;
45
import net.sourceforge.easyml.marshalling.java.lang.ObjectStrategyV1_3_4;
56
import static org.junit.Assert.*;
@@ -50,6 +51,16 @@ public void testBugObjectX_defValFieldHidesSuperDefValField() throws Exception {
5051
assertEquals(expected, easyml.deserialize(easyml.serialize(expected)));
5152
}
5253

54+
@Test
55+
public void testBugSerial_PutGetFieldsCustomKeys() throws Exception {
56+
final BitSet expected = new BitSet();
57+
expected.set(1);
58+
expected.set(3);
59+
expected.set(5);
60+
61+
assertEquals(expected, easyml.deserialize(easyml.serialize(expected)));
62+
}
63+
5364
private static class Person {
5465

5566
private final String name;

0 commit comments

Comments
 (0)