Skip to content

Commit d68be99

Browse files
committed
fix for issue #2 "NullPointerException in JBBPTextWriter for String field mapped to byte array"
1 parent 8ab9751 commit d68be99

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

src/main/java/com/igormaznitsa/jbbp/utils/JBBPTextWriter.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ protected void onArrayEnd(final Object obj, final Field field, final com.igormaz
115115
try {
116116
IndentDec();
117117
HR();
118-
Comment("END ARRAY : " + makeArrayDescription(field, annotation));
118+
if (field.getType() == String.class){
119+
Comment("END STRING : " + makeArrayDescription(field, annotation));
120+
}else{
121+
Comment("END ARRAY : " + makeArrayDescription(field, annotation));
122+
}
119123
HR();
120124
}
121125
catch (IOException ex) {
@@ -130,7 +134,11 @@ protected void onArrayEnd(final Object obj, final Field field, final com.igormaz
130134
protected void onArrayStart(final Object obj, final Field field, final com.igormaznitsa.jbbp.mapper.Bin annotation, final int length) {
131135
try {
132136
HR();
133-
Comment("START ARRAY : " + makeArrayDescription(field, annotation) + " OF " + field.getType().getComponentType().getSimpleName() + " [" + length + ']');
137+
if (field.getType() == String.class){
138+
Comment("STRING: " + makeFieldDescription(field, annotation));
139+
}else{
140+
Comment("START ARRAY : " + makeArrayDescription(field, annotation) + " OF " + field.getType().getComponentType().getSimpleName() + " [" + length + ']');
141+
}
134142
HR();
135143
IndentInc();
136144
}

src/test/java/com/igormaznitsa/jbbp/utils/JBBPTextWriterTest.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.igormaznitsa.jbbp.io.JBBPByteOrder;
2020
import com.igormaznitsa.jbbp.it.AbstractParserIntegrationTest;
2121
import com.igormaznitsa.jbbp.mapper.Bin;
22+
import com.igormaznitsa.jbbp.mapper.BinType;
2223
import com.igormaznitsa.jbbp.utils.JBBPTextWriter.Extra;
2324
import java.io.*;
2425
import java.lang.reflect.Field;
@@ -637,7 +638,7 @@ class SomeClass {
637638

638639
final String text = writer.SetCommentPrefix("; ").Bin(cl).Close().toString();
639640
System.out.println(text);
640-
assertFile("testwriterbin1.txt", text);;
641+
assertFile("testwriterbin1.txt", text);
641642
}
642643

643644
@Test
@@ -682,13 +683,28 @@ class Png {
682683

683684
System.out.println(text);
684685

685-
assertFile("testwriterbin2.txt", text);;
686+
assertFile("testwriterbin2.txt", text);
686687
}
687688
finally {
688689
JBBPUtils.closeQuietly(pngStream);
689690
}
690691
}
691692

693+
@Test
694+
public void testBin_ByteArrayMappedToString() throws Exception {
695+
class Parsed {
696+
@Bin (type = BinType.BYTE_ARRAY) String str1;
697+
@Bin (type = BinType.UBYTE_ARRAY) String str2;
698+
}
699+
700+
final Parsed parsed = JBBPParser.prepare("byte [5] str1; ubyte [4] str2;").parse(new byte[]{49,50,51,52,53,54,55,56,57}).mapTo(Parsed.class);
701+
final String text = writer.Bin(parsed).Close().toString();
702+
703+
System.out.println(text);
704+
705+
assertFile("testwriterbin5.txt", text);
706+
}
707+
692708
@Test
693709
public void testCustomFieldInMappedClass() throws Exception {
694710
class TestClass {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
~--------------------------------------------------------------------------------
2+
;START : Parsed
3+
~--------------------------------------------------------------------------------
4+
~--------------------------------------------------------------------------------
5+
;STRING: str1
6+
~--------------------------------------------------------------------------------
7+
.0x31,0x32,0x33,0x34,0x35
8+
~--------------------------------------------------------------------------------
9+
;END STRING : str1
10+
~--------------------------------------------------------------------------------
11+
~--------------------------------------------------------------------------------
12+
;STRING: str2
13+
~--------------------------------------------------------------------------------
14+
.0x36,0x37,0x38,0x39
15+
~--------------------------------------------------------------------------------
16+
;END STRING : str2
17+
~--------------------------------------------------------------------------------
18+
~--------------------------------------------------------------------------------
19+
;END : Parsed
20+
~--------------------------------------------------------------------------------

0 commit comments

Comments
 (0)