Skip to content

Commit ae88331

Browse files
committed
refactoring, improved result of source conversion
1 parent 0964989 commit ae88331

File tree

10 files changed

+92
-18
lines changed

10 files changed

+92
-18
lines changed

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

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@
3535
import java.io.EOFException;
3636
import java.io.IOException;
3737
import java.io.InputStream;
38-
import java.util.ArrayList;
39-
import java.util.List;
38+
import java.util.*;
4039

4140
/**
4241
* the Main class allows a user to parse a binary stream or block for predefined
@@ -639,18 +638,23 @@ public JBBPCompiledBlock getCompiledBlock() {
639638
*
640639
* @param target target to generate sources, must not be null
641640
* @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
641+
* @return list of source items generated during operation, must not be null and must not be empty
643642
* @throws IllegalArgumentException if target is unsupported
644643
* @see JBBPToJava6Converter
645644
* @see JBBPToJava6Converter.Builder
646645
* @since 1.3.0
647646
*/
648-
public String[] convertToSrc(final TargetSources target, final String name) {
647+
public List<ResultSrcItem> convertToSrc(final TargetSources target, final String name) {
649648
JBBPUtils.assertNotNull(name, "Name must not be null");
650-
final String[] result;
651649

652650
switch (target) {
653651
case JAVA_1_6: {
652+
final Properties metadata = new Properties();
653+
metadata.setProperty("source", this.compiledBlock.getSource());
654+
metadata.setProperty("name", name);
655+
metadata.setProperty("target", target.name());
656+
metadata.setProperty("converter", JBBPToJava6Converter.class.getCanonicalName());
657+
654658
final int nameStart = name.lastIndexOf('.');
655659
final String packageName;
656660
final String className;
@@ -661,16 +665,26 @@ public String[] convertToSrc(final TargetSources target, final String name) {
661665
packageName = name.substring(0, nameStart);
662666
className = name.substring(nameStart + 1);
663667
}
664-
result = new String[]{
665-
JBBPToJava6Converter.makeBuilder(this).setClassPackage(packageName).setClassName(className).build().convert()
666-
};
668+
669+
final String resultSources = JBBPToJava6Converter.makeBuilder(this).setClassPackage(packageName).setClassName(className).build().convert();
670+
671+
final Map<String, String> resultMap = Collections.<String, String>singletonMap(name, resultSources);
672+
673+
return Collections.<ResultSrcItem>singletonList(new ResultSrcItem() {
674+
@Override
675+
public Properties getMetadata() {
676+
return metadata;
677+
}
678+
679+
@Override
680+
public Map<String, String> getResult() {
681+
return resultMap;
682+
}
683+
});
667684
}
668-
break;
669685
default: {
670686
throw new IllegalArgumentException("Unsupported target : " + target);
671687
}
672688
}
673-
674-
return result;
675689
}
676690
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2017 Igor Maznitsa.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.igormaznitsa.jbbp;
18+
19+
import java.util.Map;
20+
import java.util.Properties;
21+
22+
/**
23+
* Contains result of conversion of JBBPParser into source.
24+
*
25+
* @since 1.3.0
26+
*/
27+
public interface ResultSrcItem {
28+
/**
29+
* Get metadata generated during operation for the item, depends on converter.
30+
*
31+
* @return the metadata container as properties, must not be null
32+
*/
33+
Properties getMetadata();
34+
35+
/**
36+
* Get generated sources mapped by some key which defined by converter.
37+
*
38+
* @return map containing result of conversion, must not be null and can't be empty.
39+
*/
40+
Map<String, String> getResult();
41+
}

src/main/java/com/igormaznitsa/jbbp/compiler/varlen/JBBPEvaluatorFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*/
1616
package com.igormaznitsa.jbbp.compiler.varlen;
1717

18-
import com.igormaznitsa.jbbp.compiler.JBBPNamedFieldInfo;
1918
import com.igormaznitsa.jbbp.compiler.JBBPCompilerUtils;
19+
import com.igormaznitsa.jbbp.compiler.JBBPNamedFieldInfo;
2020

2121
import java.util.List;
2222

src/main/java/com/igormaznitsa/jbbp/compiler/varlen/JBBPExpressionEvaluator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717

1818
import com.igormaznitsa.jbbp.JBBPNamedNumericFieldMap;
1919
import com.igormaznitsa.jbbp.compiler.JBBPCompiledBlock;
20+
import com.igormaznitsa.jbbp.compiler.JBBPCompilerUtils;
2021
import com.igormaznitsa.jbbp.compiler.JBBPNamedFieldInfo;
2122
import com.igormaznitsa.jbbp.compiler.conversion.ExpressionEvaluatorVisitor;
22-
import com.igormaznitsa.jbbp.compiler.JBBPCompilerUtils;
2323
import com.igormaznitsa.jbbp.exceptions.JBBPCompilationException;
2424
import com.igormaznitsa.jbbp.exceptions.JBBPEvalException;
2525
import com.igormaznitsa.jbbp.io.JBBPBitInputStream;

src/main/java/com/igormaznitsa/jbbp/exceptions/JBBPFinderException.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,16 @@ public JBBPFinderException(final String message, final String nameOrPath, final
5050

5151
/**
5252
* Do not use the metod, it will be removed in future. It just presented for back compatibility.
53+
*
5354
* @return the name or the path used for search, it can be null
55+
* @see #getNameOrPath()
5456
* @deprecated
55-
* @see #getNameOrPath()
5657
*/
5758
@Deprecated
5859
public String getNamrOrPath() {
5960
return this.getNameOrPath();
6061
}
61-
62+
6263
/**
6364
* Get the name or the path used for search.
6465
*

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public JavaSrcTextBuffer() {
3535

3636
/**
3737
* Constructor with capacity value.
38+
*
3839
* @param capacity capacity of the created buffer
3940
*/
4041
public JavaSrcTextBuffer(final int capacity) {
@@ -43,6 +44,7 @@ public JavaSrcTextBuffer(final int capacity) {
4344

4445
/**
4546
* Increase indent
47+
*
4648
* @return this instance
4749
*/
4850
public JavaSrcTextBuffer incIndent() {
@@ -52,6 +54,7 @@ public JavaSrcTextBuffer incIndent() {
5254

5355
/**
5456
* Decrease indent
57+
*
5558
* @return this instance
5659
*/
5760
public JavaSrcTextBuffer decIndent() {
@@ -61,6 +64,7 @@ public JavaSrcTextBuffer decIndent() {
6164

6265
/**
6366
* Print integer value
67+
*
6468
* @param value integer value
6569
* @return this instance
6670
*/
@@ -71,6 +75,7 @@ public JavaSrcTextBuffer print(final int value) {
7175

7276
/**
7377
* Print boolean value
78+
*
7479
* @param value boolean value
7580
* @return this instance
7681
*/
@@ -81,6 +86,7 @@ public JavaSrcTextBuffer print(final boolean value) {
8186

8287
/**
8388
* Foratted print.
89+
*
8490
* @param text format string
8591
* @param args arguments for formatted string
8692
* @return this instance
@@ -93,6 +99,7 @@ public JavaSrcTextBuffer printf(final String text, final Object... args) {
9399

94100
/**
95101
* Print string
102+
*
96103
* @param text string
97104
* @return this instance
98105
*/
@@ -103,6 +110,7 @@ public JavaSrcTextBuffer print(final String text) {
103110

104111
/**
105112
* Clean buffer
113+
*
106114
* @return this instance
107115
*/
108116
public JavaSrcTextBuffer clean() {
@@ -113,6 +121,7 @@ public JavaSrcTextBuffer clean() {
113121

114122
/**
115123
* Print tab char
124+
*
116125
* @return this instance
117126
*/
118127
public JavaSrcTextBuffer tab() {
@@ -122,6 +131,7 @@ public JavaSrcTextBuffer tab() {
122131

123132
/**
124133
* Print tabs for current indent number
134+
*
125135
* @return this instance
126136
*/
127137
public JavaSrcTextBuffer indent() {
@@ -131,6 +141,7 @@ public JavaSrcTextBuffer indent() {
131141

132142
/**
133143
* Print next line char
144+
*
134145
* @return this instance
135146
*/
136147
public JavaSrcTextBuffer println() {
@@ -140,6 +151,7 @@ public JavaSrcTextBuffer println() {
140151

141152
/**
142153
* Check that the buffer is empty
154+
*
143155
* @return true if the buffer is empty, false otherwise
144156
*/
145157
public boolean isEmpty() {
@@ -148,6 +160,7 @@ public boolean isEmpty() {
148160

149161
/**
150162
* Print text and next line char in the end
163+
*
151164
* @param text the text to be printed
152165
* @return this instance
153166
*/
@@ -157,6 +170,7 @@ public JavaSrcTextBuffer println(final String text) {
157170

158171
/**
159172
* Parse string to lines and print each line with current indent
173+
*
160174
* @param text the text to be printed
161175
* @return this instance
162176
*/
@@ -172,6 +186,7 @@ public JavaSrcTextBuffer printLinesWithIndent(final String text) {
172186

173187
/**
174188
* Parse string to lines and print each line with '//' comment
189+
*
175190
* @param text text to be printed
176191
* @return this instance
177192
*/
@@ -187,6 +202,7 @@ public JavaSrcTextBuffer printCommentLinesWithIndent(final String text) {
187202

188203
/**
189204
* Print string as multiline java comment started with '/*'
205+
*
190206
* @param text text to be printed as multiline comment
191207
* @return this instance
192208
*/
@@ -204,6 +220,7 @@ public JavaSrcTextBuffer printCommentMultiLinesWithIndent(final String text) {
204220

205221
/**
206222
* Print string as multiline java comment started with '/**'
223+
*
207224
* @param text text to be printed as multiline comment
208225
* @return this instance
209226
*/

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
/**
2020
* Allowed sourcws for source generation in JBBPParser
21+
*
2122
* @see com.igormaznitsa.jbbp.JBBPParser#convertToSrc(TargetSources, String)
2223
* @since 1.3.0
2324
*/

src/test/java/com/igormaznitsa/jbbp/JBBPParserTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1707,7 +1707,7 @@ public void testParse_NoErrorForIgnoreRemainingFieldsFlag() throws Exception {
17071707
@Test
17081708
public void testConvertToSrc_Java16() throws Exception {
17091709
final JBBPParser parser = JBBPParser.prepare("byte a;");
1710-
assertTrue(parser.convertToSrc(TargetSources.JAVA_1_6, "someclass").length != 0);
1710+
assertTrue(parser.convertToSrc(TargetSources.JAVA_1_6, "someclass").get(0).getResult().values().iterator().next().length() > 128);
17111711
}
17121712

17131713
}

src/test/java/com/igormaznitsa/jbbp/compiler/conversion/JBBPToJBBPToJava6ConverterExpressionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public int read() throws IOException {
4646
final int detectedlength = getField(obj, "data", byte[].class).length;
4747

4848
if (etalonValue != detectedlength) {
49-
System.err.println(JBBPParser.prepare(String.format("byte [%s] data;", expression)).convertToSrc(TargetSources.JAVA_1_6,PACKAGE_NAME+"."+CLASS_NAME)[0]);
49+
System.err.println(JBBPParser.prepare(String.format("byte [%s] data;", expression)).convertToSrc(TargetSources.JAVA_1_6,PACKAGE_NAME+"."+CLASS_NAME).get(0).getResult().values().iterator().next());
5050
fail(etalonValue + "!=" + detectedlength);
5151
}
5252
}

src/test/java/com/igormaznitsa/jbbp/testaux/AbstractJBBPToJava6ConverterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ protected Object compileAndMakeInstance(final String instanceClassName, final St
112112

113113
protected Object compileAndMakeInstance(final String instanceClassName, final String script, final int parserFlags, final JBBPCustomFieldTypeProcessor customFieldProcessor, final JavaClassContent... extraClasses) throws Exception {
114114
final List<JavaClassContent> klazzes = new ArrayList<JavaClassContent>(Arrays.asList(extraClasses));
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]));
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).get(0).getResult().values().iterator().next()));
116116
final ClassLoader cloader = saveAndCompile(klazzes.toArray(new JavaClassContent[klazzes.size()]));
117117
return cloader.loadClass(instanceClassName).newInstance();
118118
}

0 commit comments

Comments
 (0)