Skip to content

Commit 1b34711

Browse files
committed
refactoring
1 parent 0a93096 commit 1b34711

File tree

2 files changed

+34
-43
lines changed

2 files changed

+34
-43
lines changed

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

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package com.igormaznitsa.jbbp.utils;
1818

19-
import com.igormaznitsa.jbbp.exceptions.JBBPException;
2019
import com.igormaznitsa.jbbp.mapper.Bin;
2120
import java.io.IOException;
2221
import java.lang.reflect.Field;
@@ -86,23 +85,4 @@ public void onReachedMaxValueNumberForLine(final JBBPTextWriter context) throws
8685

8786
}
8887

89-
/**
90-
* Auxiliary method to extract field value.
91-
*
92-
* @param instance object instance, can be null
93-
* @param field the filed which value should be extracted, must not be null
94-
* @return the field value
95-
*/
96-
public Object extractFieldValue(final Object instance, final Field field) {
97-
JBBPUtils.assertNotNull(field, "Field must not be null");
98-
try {
99-
if (ReflectUtils.isPotentiallyAccessibleField(field)) {
100-
return field.get(instance);
101-
} else {
102-
return ReflectUtils.makeAccessible(field).get(instance);
103-
}
104-
} catch (Exception ex) {
105-
throw new JBBPException("Can't extract value from field for exception", ex);
106-
}
107-
}
10888
}

jbbp/src/test/java/com/igormaznitsa/jbbp/it/Z80_v1_ParsingTest.java

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616

1717
package com.igormaznitsa.jbbp.it;
1818

19+
import static com.igormaznitsa.jbbp.io.JBBPByteOrder.LITTLE_ENDIAN;
20+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.junit.jupiter.api.Assertions.assertTrue;
23+
import static org.junit.jupiter.api.Assertions.fail;
24+
25+
1926
import com.igormaznitsa.jbbp.JBBPParser;
2027
import com.igormaznitsa.jbbp.io.JBBPBitNumber;
2128
import com.igormaznitsa.jbbp.io.JBBPBitOrder;
@@ -29,19 +36,15 @@
2936
import com.igormaznitsa.jbbp.model.JBBPFieldArrayByte;
3037
import com.igormaznitsa.jbbp.model.JBBPFieldBit;
3138
import com.igormaznitsa.jbbp.model.JBBPFieldStruct;
39+
import com.igormaznitsa.jbbp.utils.Function;
3240
import com.igormaznitsa.jbbp.utils.JBBPTextWriter;
3341
import com.igormaznitsa.jbbp.utils.JBBPTextWriterExtraAdapter;
3442
import com.igormaznitsa.jbbp.utils.JBBPUtils;
35-
import org.junit.jupiter.api.Test;
36-
3743
import java.io.ByteArrayOutputStream;
3844
import java.io.IOException;
3945
import java.io.InputStream;
4046
import java.lang.reflect.Field;
41-
42-
import static com.igormaznitsa.jbbp.io.JBBPByteOrder.LITTLE_ENDIAN;
43-
import com.igormaznitsa.jbbp.utils.Function;
44-
import static org.junit.jupiter.api.Assertions.*;
47+
import org.junit.jupiter.api.Test;
4548

4649
/**
4750
* Test to parse RLE encoded snapshots in well-known Z80 format (v.1) for
@@ -65,12 +68,30 @@ public void testRLEEncoding() throws Exception {
6568
assertArrayEquals(new byte[] {(byte) 0xED, (byte) 0xED, 8, 5, 1, 2, 3, 0x00, (byte) 0xED, (byte) 0xED, 0x00}, JBBPOut.BeginBin().Var(new RLEDataEncoder(), 1, new byte[] {5, 5, 5, 5, 5, 5, 5, 5, 1, 2, 3}).End().toByteArray());
6669
}
6770

71+
private static final Function<Class<?>, Object> INSTANTIATOR = aClass -> {
72+
if (aClass == Flags.class) {
73+
return new Flags();
74+
}
75+
if (aClass == EmulFlags.class) {
76+
return new EmulFlags();
77+
}
78+
return null;
79+
};
80+
81+
@Test
82+
public void testParseAndWriteTestZ80WithoutCheckOfFields() throws Exception {
83+
assertParseAndPackBack("test1.z80", 16059);
84+
assertParseAndPackBack("test2.z80", 29330);
85+
assertParseAndPackBack("test3.z80", 5711);
86+
assertParseAndPackBack("test4.z80", 9946);
87+
}
88+
6889
private Z80Snapshot assertParseAndPackBack(final String name, final long etalonLen) throws Exception {
6990
final Z80Snapshot z80sn;
7091

7192
final InputStream resource = getResourceAsInputStream(name);
7293
try {
73-
z80sn = z80Parser.parse(resource).mapTo(new Z80Snapshot(), new DataProcessor(),INSTANTIATOR);
94+
z80sn = z80Parser.parse(resource).mapTo(new Z80Snapshot(), new DataProcessor(), INSTANTIATOR);
7495
assertEquals(etalonLen, z80Parser.getFinalStreamByteCounter());
7596
} finally {
7697
JBBPUtils.closeQuietly(resource);
@@ -103,14 +124,6 @@ private Z80Snapshot assertParseAndPackBack(final String name, final long etalonL
103124
return z80sn;
104125
}
105126

106-
@Test
107-
public void testParseAndWriteTestZ80WithoutCheckOfFields() throws Exception {
108-
assertParseAndPackBack("test1.z80", 16059);
109-
assertParseAndPackBack("test2.z80", 29330);
110-
assertParseAndPackBack("test3.z80", 5711);
111-
assertParseAndPackBack("test4.z80", 9946);
112-
}
113-
114127
@Test
115128
public void testParseAndWriteTestZ80WithCheckOfFields() throws Exception {
116129
final Z80Snapshot z80sn = assertParseAndPackBack("test.z80", 12429);
@@ -119,8 +132,12 @@ public void testParseAndWriteTestZ80WithCheckOfFields() throws Exception {
119132

120133
@Override
121134
public String doConvertCustomField(JBBPTextWriter context, Object obj, Field field, Bin annotation) throws IOException {
122-
final byte[] data = (byte[]) extractFieldValue(obj, field);
123-
return "byte array length [" + data.length + ']';
135+
try {
136+
final byte[] data = (byte[]) field.get(obj);
137+
return "byte array length [" + data.length + ']';
138+
} catch (Exception ex) {
139+
throw new RuntimeException(ex);
140+
}
124141
}
125142

126143
}).Bin(z80sn).Close().toString();
@@ -170,12 +187,6 @@ public String doConvertCustomField(JBBPTextWriter context, Object obj, Field fie
170187
assertTrue(summ > 0);
171188
}
172189

173-
private static final Function<Class<?>,Object> INSTANTIATOR = aClass -> {
174-
if (aClass == Flags.class) return new Flags();
175-
if (aClass == EmulFlags.class) return new EmulFlags();
176-
return null;
177-
};
178-
179190
@Test
180191
public void testParseAndPackThrowMapping() throws Exception {
181192
final InputStream in = getResourceAsInputStream("test.z80");

0 commit comments

Comments
 (0)