Skip to content

Commit f0e547a

Browse files
committed
added convertToSrc method into JBBPParser
1 parent 9f7b21a commit f0e547a

File tree

7 files changed

+93
-64
lines changed

7 files changed

+93
-64
lines changed

src/main/java/com/igormaznitsa/jbbp/JBBPParser.java

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.igormaznitsa.jbbp.model.*;
3030
import com.igormaznitsa.jbbp.utils.JBBPIntCounter;
3131
import com.igormaznitsa.jbbp.utils.JBBPUtils;
32+
import com.igormaznitsa.jbbp.utils.TargetSources;
3233

3334
import java.io.ByteArrayInputStream;
3435
import java.io.EOFException;
@@ -634,30 +635,42 @@ public JBBPCompiledBlock getCompiledBlock() {
634635
}
635636

636637
/**
637-
* Generate java class sources for the parser (Java 1.6+).
638+
* Convert the prepared parser into sources. It doesn't provide way to define different flag for conversion, it uses default flags for converters and provided for short fast way.
638639
*
639-
* @param classPackage package for the new generated class, can be null
640-
* @param className class name of the new generated class, must not be null
641-
* @param nullableClassHeaderComment text to be added as comment into class header, it can be null
642-
* @param makeGettersSetters generate getters setters for fields if true
643-
* @return generated sources of class file
644-
* @since 1.3.0
640+
* @param target target to generate sources, must not be null
641+
* @param name name of result, depends on target, must not be null, for instance class name (example 'com.test.jbbp.Parser')
642+
* @return array of generated sources, must not be null
643+
* @throws IllegalArgumentException if target is unsupported
645644
* @see JBBPToJava6Converter
646-
*/
647-
public String makeJavaSources(final String classPackage, final String className, final String nullableClassHeaderComment, final boolean makeGettersSetters) {
648-
return JBBPToJava6Converter.makeBuilder(this).setClassPackage(classPackage).setDoGettersSetters(makeGettersSetters).setClassName(className).setClassHeadComments(nullableClassHeaderComment).build().convert();
649-
}
650-
651-
/**
652-
* Generate java class sources for the parser.
653-
*
654-
* @param classPackage package for the new generated class, must not be null
655-
* @param className class name of the new generated class, must not be null
656-
* @param makeGettersSetters generate getters setters for fields if true
657-
* @return generated sources of class file
645+
* @see JBBPToJava6Converter.Builder
658646
* @since 1.3.0
659647
*/
660-
public String makeJavaSources(final String classPackage, final String className, final boolean makeGettersSetters) {
661-
return this.makeJavaSources(classPackage, className, null, makeGettersSetters);
648+
public String[] convertToSrc(final TargetSources target, final String name) {
649+
JBBPUtils.assertNotNull(name, "Name must not be null");
650+
final String[] result;
651+
652+
switch (target) {
653+
case JAVA_1_6: {
654+
final int nameStart = name.lastIndexOf('.');
655+
final String packageName;
656+
final String className;
657+
if (nameStart < 0) {
658+
packageName = null;
659+
className = name;
660+
} else {
661+
packageName = name.substring(0, nameStart);
662+
className = name.substring(nameStart + 1);
663+
}
664+
result = new String[]{
665+
JBBPToJava6Converter.makeBuilder(this).setClassPackage(packageName).setClassName(className).build().convert()
666+
};
667+
}
668+
break;
669+
default: {
670+
throw new IllegalArgumentException("Unsupported target : " + target);
671+
}
672+
}
673+
674+
return result;
662675
}
663676
}

src/main/java/com/igormaznitsa/jbbp/io/AbstractMappedClassFieldObserver.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,21 @@ protected void processObject(final Object obj, final Field field, final Object c
103103
listOfClassHierarchy.add(current);
104104
current = current.getSuperclass();
105105
}
106+
106107
for (int i = listOfClassHierarchy.size() - 1; i >= 0; i--) {
107108
final Class<?> clazzToProcess = listOfClassHierarchy.get(i);
108109
final Bin clazzAnno = clazzToProcess.getAnnotation(Bin.class);
110+
109111
for (final Field f : clazzToProcess.getDeclaredFields()) {
110112
if (!f.isAccessible()) {
111113
JBBPUtils.makeAccessible(f);
112114
}
115+
113116
final int modifiers = f.getModifiers();
114117
if (Modifier.isTransient(modifiers) || Modifier.isStatic(modifiers) || f.getName().indexOf('$') >= 0) {
115118
continue;
116119
}
120+
117121
Bin fieldAnno = f.getAnnotation(Bin.class);
118122
fieldAnno = fieldAnno == null ? clazzAnno : fieldAnno;
119123
if (fieldAnno == null) {
@@ -630,7 +634,13 @@ public int hashCode() {
630634

631635
@Override
632636
public int compareTo(final OrderedField o) {
633-
return this.order < o.order ? -1 : 1;
637+
final int result;
638+
if (this.order == o.order) {
639+
result = this.field.getName().compareTo(o.field.getName());
640+
} else {
641+
result = this.order < o.order ? -1 : 1;
642+
}
643+
return result;
634644
}
635645
}
636646

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,18 @@
2222
import com.igormaznitsa.jbbp.io.JBBPBitInputStream;
2323
import com.igormaznitsa.jbbp.io.JBBPBitOrder;
2424
import com.igormaznitsa.jbbp.model.JBBPAbstractField;
25-
import com.igormaznitsa.jbbp.testaux.AbstractJavaClassCompilerTest;
25+
import com.igormaznitsa.jbbp.testaux.AbstractJBBPToJava6ConverterTest;
2626
import org.junit.Test;
2727

2828
import java.io.IOException;
2929

3030
import static org.junit.Assert.*;
3131

32-
public class JBBPToJava6ConverterCompilationTest extends AbstractJavaClassCompilerTest {
32+
public class JBBPToJBBPToJava6ConverterCompilationTest extends AbstractJBBPToJava6ConverterTest {
33+
34+
private static String makeSources(final JBBPParser parser, final String classComment, final boolean useSetterGetter) {
35+
return JBBPToJava6Converter.makeBuilder(parser).setClassPackage(PACKAGE_NAME).setClassName(CLASS_NAME).setClassHeadComments(classComment).setDoGettersSetters(useSetterGetter).build().convert();
36+
}
3337

3438
private void assertCompilation(final String classSrc) throws Exception {
3539
System.out.println(classSrc);
@@ -39,8 +43,8 @@ private void assertCompilation(final String classSrc) throws Exception {
3943
@Test
4044
public void testExpression() throws Exception {
4145
final JBBPParser parser = JBBPParser.prepare("bit:8 bitf; var somevar; bool bbb; long aaa; ubyte kkk; {{int lrn; {int [(lrn/aaa*1*(2*somevar-4)&$joomla)/(100%9>>bitf)&56|~kkk^78&bbb];}}}");
42-
assertCompilation(parser.makeJavaSources(PACKAGE_NAME, CLASS_NAME, "some multiline text\nto be added into header", false));
43-
assertCompilation(parser.makeJavaSources(PACKAGE_NAME, CLASS_NAME, "some multiline text\nto be added into header", true));
46+
assertCompilation(makeSources(parser,"some multiline text\nto be added into header", false));
47+
assertCompilation(makeSources(parser,"some multiline text\nto be added into header", true));
4448
}
4549

4650
@Test
@@ -110,57 +114,57 @@ public void testZ80snap1() throws Exception {
110114
+ "<short reg_de; <short reg_bc_alt; <short reg_de_alt; <short reg_hl_alt; byte reg_a_alt; byte reg_f_alt; <short reg_iy; <short reg_ix; byte iff; byte iff2;"
111115
+ "emulFlags{bit:2 interruptmode; bit:1 issue2emulation; bit:1 doubleintfreq; bit:2 videosync; bit:2 inputdevice;}"
112116
+ "byte [_] data;");
113-
assertCompilation(parser.makeJavaSources(PACKAGE_NAME, CLASS_NAME, false));
114-
assertCompilation(parser.makeJavaSources(PACKAGE_NAME, CLASS_NAME, true));
117+
assertCompilation(makeSources(parser,null,false));
118+
assertCompilation(makeSources(parser,null,true));
115119
}
116120

117121
@Test
118122
public void testSinglePrimitiveNamedFields() throws Exception {
119123
final JBBPParser parser = JBBPParser.prepare("bit a;byte b;ubyte c;short d;ushort e;bool f;int g;long h;");
120-
assertCompilation(parser.makeJavaSources(PACKAGE_NAME, CLASS_NAME, false));
121-
assertCompilation(parser.makeJavaSources(PACKAGE_NAME, CLASS_NAME, true));
124+
assertCompilation(makeSources(parser,null,false));
125+
assertCompilation(makeSources(parser,null,true));
122126
}
123127

124128
@Test
125129
public void testSinglePrimitiveAnonymousFields() throws Exception {
126130
final JBBPParser parser = JBBPParser.prepare("bit;byte;ubyte;short;ushort;bool;int;long;");
127-
assertCompilation(parser.makeJavaSources(PACKAGE_NAME, CLASS_NAME, false));
128-
assertCompilation(parser.makeJavaSources(PACKAGE_NAME, CLASS_NAME, true));
131+
assertCompilation(makeSources(parser,null,false));
132+
assertCompilation(makeSources(parser,null,true));
129133
}
130134

131135
@Test
132136
public void testSinglePrimitiveAnonymousArrayFields() throws Exception {
133137
final JBBPParser parser = JBBPParser.prepare("bit[1];byte[2];ubyte[3];short[4];ushort[5];bool[6];int[7];long[8];");
134-
assertCompilation(parser.makeJavaSources(PACKAGE_NAME, CLASS_NAME, false));
135-
assertCompilation(parser.makeJavaSources(PACKAGE_NAME, CLASS_NAME, true));
138+
assertCompilation(makeSources(parser,null,false));
139+
assertCompilation(makeSources(parser,null,true));
136140
}
137141

138142
@Test
139143
public void testActions() throws Exception {
140144
final JBBPParser parser = JBBPParser.prepare("reset$$;skip:8;align:22;");
141-
assertCompilation(parser.makeJavaSources(PACKAGE_NAME, CLASS_NAME, false));
142-
assertCompilation(parser.makeJavaSources(PACKAGE_NAME, CLASS_NAME, true));
145+
assertCompilation(makeSources(parser,null,false));
146+
assertCompilation(makeSources(parser,null,true));
143147
}
144148

145149
@Test
146150
public void testStruct() throws Exception {
147151
final JBBPParser parser = JBBPParser.prepare("int;{byte;ubyte;{long;}}");
148-
assertCompilation(parser.makeJavaSources(PACKAGE_NAME, CLASS_NAME, false));
149-
assertCompilation(parser.makeJavaSources(PACKAGE_NAME, CLASS_NAME, true));
152+
assertCompilation(makeSources(parser,null,false));
153+
assertCompilation(makeSources(parser,null,true));
150154
}
151155

152156
@Test
153157
public void testPrimitiveArrayInsideStructArray() throws Exception {
154158
final JBBPParser parser = JBBPParser.prepare("ubyte len; {ubyte[len];} ubyte [_] rest;");
155-
assertCompilation(parser.makeJavaSources(PACKAGE_NAME, CLASS_NAME, false));
156-
assertCompilation(parser.makeJavaSources(PACKAGE_NAME, CLASS_NAME, true));
159+
assertCompilation(makeSources(parser,null,false));
160+
assertCompilation(makeSources(parser,null,true));
157161
}
158162

159163
@Test
160164
public void testExternalValueInExpression() throws Exception {
161165
final JBBPParser parser = JBBPParser.prepare("ubyte len; <int [len*2+$ex] hello;");
162-
assertCompilation(parser.makeJavaSources(PACKAGE_NAME, CLASS_NAME, false));
163-
assertCompilation(parser.makeJavaSources(PACKAGE_NAME, CLASS_NAME, true));
166+
assertCompilation(makeSources(parser,null,false));
167+
assertCompilation(makeSources(parser,null,true));
164168
}
165169

166170
@Test
@@ -182,29 +186,29 @@ public JBBPAbstractField readCustomFieldType(JBBPBitInputStream in, JBBPBitOrder
182186
}
183187
});
184188

185-
assertCompilation(parser.makeJavaSources(PACKAGE_NAME, CLASS_NAME, false));
186-
assertCompilation(parser.makeJavaSources(PACKAGE_NAME, CLASS_NAME, true));
189+
assertCompilation(makeSources(parser,null,false));
190+
assertCompilation(makeSources(parser,null,true));
187191
}
188192

189193
@Test
190194
public void testVarType() throws Exception {
191195
final JBBPParser parser = JBBPParser.prepare("var alpha; var [$$] beta;");
192-
assertCompilation(parser.makeJavaSources(PACKAGE_NAME, CLASS_NAME, false));
193-
assertCompilation(parser.makeJavaSources(PACKAGE_NAME, CLASS_NAME, true));
196+
assertCompilation(makeSources(parser,null,false));
197+
assertCompilation(makeSources(parser,null,true));
194198
}
195199

196200
@Test
197201
public void testAllVariantsWithLinksToExternalStructures() throws Exception {
198202
final JBBPParser parser = JBBPParser.prepare("bit:1 bit1; bit:2 bit2; bit:3 bit3; bit:4 bit4; bit:5 bit5; bit:6 bit6; bit:7 bit7; bit:8 bit8;"
199203
+ "byte alpha; ubyte beta; short gamma; ushort delta; bool epsilon; int teta; long longField; var varField;"
200204
+ "struct1 { byte someByte; struct2 {bit:3 [34*someByte<<1+$ext] data;} }");
201-
assertCompilation(parser.makeJavaSources(PACKAGE_NAME, CLASS_NAME, false));
202-
assertCompilation(parser.makeJavaSources(PACKAGE_NAME, CLASS_NAME, true));
205+
assertCompilation(makeSources(parser,null,false));
206+
assertCompilation(makeSources(parser,null,true));
203207
}
204208

205209
@Test
206210
public void testPngParsing() throws Exception {
207-
final JBBPParser pngParser = JBBPParser.prepare(
211+
final JBBPParser parser = JBBPParser.prepare(
208212
"long header;"
209213
+ "// chunks\n"
210214
+ "chunk [_]{"
@@ -214,15 +218,15 @@ public void testPngParsing() throws Exception {
214218
+ " int crc;"
215219
+ "}"
216220
);
217-
assertCompilation(pngParser.makeJavaSources(PACKAGE_NAME, CLASS_NAME, false));
218-
assertCompilation(pngParser.makeJavaSources(PACKAGE_NAME, CLASS_NAME, true));
221+
assertCompilation(makeSources(parser,null,false));
222+
assertCompilation(makeSources(parser,null,true));
219223
}
220224

221225
@Test
222226
public void testPrimitiveFieldsInExpression() throws Exception {
223227
final JBBPParser parser = JBBPParser.prepare("long lfield; int ifield; byte bfield; ggg {ubyte ubfield; short shfield;} ushort ushfield; bit:4 bitfield; byte [bfield*ggg.shfield<<bitfield-ggg.ubfield&ushfield%lfield/ifield] array;");
224-
assertCompilation(parser.makeJavaSources(PACKAGE_NAME, CLASS_NAME, false));
225-
assertCompilation(parser.makeJavaSources(PACKAGE_NAME, CLASS_NAME, true));
228+
assertCompilation(makeSources(parser,null,false));
229+
assertCompilation(makeSources(parser,null,true));
226230
}
227231

228232
@Test
@@ -243,8 +247,8 @@ public JBBPAbstractField readCustomFieldType(JBBPBitInputStream in, JBBPBitOrder
243247
return null;
244248
}
245249
});
246-
assertCompilation(parser.makeJavaSources(PACKAGE_NAME, CLASS_NAME, false));
247-
assertCompilation(parser.makeJavaSources(PACKAGE_NAME, CLASS_NAME, true));
250+
assertCompilation(makeSources(parser,null,false));
251+
assertCompilation(makeSources(parser,null,true));
248252
}
249253

250254
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717

1818
import com.igormaznitsa.jbbp.JBBPParser;
1919
import com.igormaznitsa.jbbp.io.JBBPBitInputStream;
20-
import com.igormaznitsa.jbbp.testaux.AbstractJavaClassCompilerTest;
20+
import com.igormaznitsa.jbbp.testaux.AbstractJBBPToJava6ConverterTest;
21+
import com.igormaznitsa.jbbp.utils.TargetSources;
2122
import org.junit.Test;
2223

2324
import java.io.IOException;
@@ -27,7 +28,7 @@
2728
import static org.junit.Assert.assertTrue;
2829
import static org.junit.Assert.fail;
2930

30-
public class JBBPToJava6ConverterExpressionTest extends AbstractJavaClassCompilerTest {
31+
public class JBBPToJBBPToJava6ConverterExpressionTest extends AbstractJBBPToJava6ConverterTest {
3132

3233
private void assertExpression(final int etalonValue, final String expression) throws Exception {
3334
assertTrue("Etalon value must not be zero or egative one : " + etalonValue, etalonValue > 0);
@@ -45,7 +46,7 @@ public int read() throws IOException {
4546
final int detectedlength = getField(obj, "data", byte[].class).length;
4647

4748
if (etalonValue != detectedlength) {
48-
System.err.println(JBBPParser.prepare(String.format("byte [%s] data;", expression)).makeJavaSources(PACKAGE_NAME, CLASS_NAME, false));
49+
System.err.println(JBBPParser.prepare(String.format("byte [%s] data;", expression)).convertToSrc(TargetSources.JAVA_1_6,PACKAGE_NAME+"."+CLASS_NAME)[0]);
4950
fail(etalonValue + "!=" + detectedlength);
5051
}
5152
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import com.igormaznitsa.jbbp.model.JBBPAbstractField;
2626
import com.igormaznitsa.jbbp.model.JBBPFieldArrayInt;
2727
import com.igormaznitsa.jbbp.model.JBBPFieldInt;
28-
import com.igormaznitsa.jbbp.testaux.AbstractJavaClassCompilerTest;
28+
import com.igormaznitsa.jbbp.testaux.AbstractJBBPToJava6ConverterTest;
2929
import org.apache.commons.io.IOUtils;
3030
import org.junit.Test;
3131

@@ -42,7 +42,7 @@
4242
/**
4343
* Test reading writing with converted classes from parser.
4444
*/
45-
public class JBBPToJava6ConverterReadWriteTest extends AbstractJavaClassCompilerTest {
45+
public class JBBPToJBBPToJava6ConverterReadWriteTest extends AbstractJBBPToJava6ConverterTest {
4646

4747
public interface ByteTestInterface {
4848
byte getA();
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package com.igormaznitsa.jbbp.compiler.conversion;
1717

1818
import com.igormaznitsa.jbbp.JBBPParser;
19-
import com.igormaznitsa.jbbp.testaux.AbstractJavaClassCompilerTest;
19+
import com.igormaznitsa.jbbp.testaux.AbstractJBBPToJava6ConverterTest;
2020
import org.junit.Test;
2121

2222
import java.io.EOFException;
@@ -28,7 +28,7 @@
2828
/**
2929
* Test parser flags for converted sources.
3030
*/
31-
public class ParserToJavaClassConverterJBBPFlagsTest extends AbstractJavaClassCompilerTest {
31+
public class ParserToJBBPToJavaClassConverterJBBPFlagsTest extends AbstractJBBPToJava6ConverterTest {
3232

3333
@Test
3434
public void testFlag_SkipRemainingFieldsIfEOF() throws Exception {

src/test/java/com/igormaznitsa/jbbp/testaux/AbstractJavaClassCompilerTest.java renamed to src/test/java/com/igormaznitsa/jbbp/testaux/AbstractJBBPToJava6ConverterTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.igormaznitsa.jbbp.io.JBBPBitInputStream;
2121
import com.igormaznitsa.jbbp.io.JBBPBitOrder;
2222
import com.igormaznitsa.jbbp.io.JBBPBitOutputStream;
23+
import com.igormaznitsa.jbbp.utils.TargetSources;
2324
import org.apache.commons.io.FileUtils;
2425
import org.junit.Rule;
2526
import org.junit.rules.TemporaryFolder;
@@ -34,7 +35,7 @@
3435
import java.net.URLClassLoader;
3536
import java.util.*;
3637

37-
public abstract class AbstractJavaClassCompilerTest {
38+
public abstract class AbstractJBBPToJava6ConverterTest {
3839

3940
protected static final String PACKAGE_NAME = "com.igormaznitsa.test";
4041
protected static final String CLASS_NAME = "TestClass";
@@ -111,7 +112,7 @@ protected Object compileAndMakeInstance(final String instanceClassName, final St
111112

112113
protected Object compileAndMakeInstance(final String instanceClassName, final String script, final int parserFlags, final JBBPCustomFieldTypeProcessor customFieldProcessor, final JavaClassContent... extraClasses) throws Exception {
113114
final List<JavaClassContent> klazzes = new ArrayList<JavaClassContent>(Arrays.asList(extraClasses));
114-
klazzes.add(0, new JavaClassContent(PACKAGE_NAME + '.' + CLASS_NAME, JBBPParser.prepare(script, JBBPBitOrder.LSB0, customFieldProcessor, parserFlags).makeJavaSources(PACKAGE_NAME, CLASS_NAME, false)));
115+
klazzes.add(0, new JavaClassContent(PACKAGE_NAME + '.' + CLASS_NAME, JBBPParser.prepare(script, JBBPBitOrder.LSB0, customFieldProcessor, parserFlags).convertToSrc(TargetSources.JAVA_1_6, PACKAGE_NAME+"."+CLASS_NAME)[0]));
115116
final ClassLoader cloader = saveAndCompile(klazzes.toArray(new JavaClassContent[klazzes.size()]));
116117
return cloader.loadClass(instanceClassName).newInstance();
117118
}

0 commit comments

Comments
 (0)