Skip to content

Commit 9756e7a

Browse files
committed
added flag to not make internal classes as static ones
1 parent 38903a1 commit 9756e7a

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

jbbp/src/main/java/com/igormaznitsa/jbbp/compiler/conversion/JBBPToJava6Converter.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,15 +270,15 @@ public void visitStructureStart(final int offsetInCompiledBlock, final JBBPNamed
270270
final String structBaseTypeName = structName.toUpperCase(Locale.ENGLISH);
271271
final String arraySizeIn = nullableArraySize == null ? null : evaluatorToString(NAME_INPUT_STREAM, offsetInCompiledBlock, nullableArraySize, this.flagSet, true);
272272
final String arraySizeOut = nullableArraySize == null ? null : evaluatorToString(NAME_OUTPUT_STREAM, offsetInCompiledBlock, nullableArraySize, this.flagSet, true);
273-
final Struct newStruct = new Struct(this.getCurrentStruct(), structBaseTypeName, "public static");
273+
final Struct newStruct = new Struct(this.getCurrentStruct(), structBaseTypeName, "public" + (builder.internalClassesNotStatic ? "" : " static"));
274274

275275
final String fieldModifier = makeModifier(nullableNameFieldInfo);
276276

277277
final String toType;
278278
if (this.builder.generateFields) {
279279
toType = "";
280280
} else {
281-
toType = '('+structBaseTypeName+')';
281+
toType = '(' + structBaseTypeName + ')';
282282
}
283283

284284
final String structType;
@@ -293,8 +293,8 @@ public void visitStructureStart(final int offsetInCompiledBlock, final JBBPNamed
293293

294294
this.getCurrentStruct().getReadFunc().indent()
295295
.printf("if ( this.%1$s == null) { this.%1$s = new %2$s(%3$s);}", structName, structType, this.structStack.size() == 1 ? "this" : "this." + NAME_ROOT_STRUCT)
296-
.printf(" %s.read(%s);%n", toType.length() == 0 ? "this." + structName : '('+toType+ "this." + structName+')', NAME_INPUT_STREAM);
297-
this.getCurrentStruct().getWriteFunc().indent().print(toType.length() == 0 ? structName : '(' +toType + structName + ')').println(".write(Out);");
296+
.printf(" %s.read(%s);%n", toType.length() == 0 ? "this." + structName : '(' + toType + "this." + structName + ')', NAME_INPUT_STREAM);
297+
this.getCurrentStruct().getWriteFunc().indent().print(toType.length() == 0 ? structName : '(' + toType + structName + ')').println(".write(Out);");
298298
} else {
299299
structType = structBaseTypeName + " []";
300300
if (this.builder.generateFields) {
@@ -310,7 +310,7 @@ public void visitStructureStart(final int offsetInCompiledBlock, final JBBPNamed
310310
structBaseTypeName,
311311
(this.structStack.size() == 1 ? "this" : NAME_ROOT_STRUCT),
312312
NAME_INPUT_STREAM);
313-
this.getCurrentStruct().getWriteFunc().indent().printf("for (int I=0;I<this.%1$s.length;I++){ %2$s.write(%3$s); }%n", structName, toType.length() == 0 ? "this."+structName + "[I]" : '(' + toType +"this."+structName + "[I])", NAME_OUTPUT_STREAM);
313+
this.getCurrentStruct().getWriteFunc().indent().printf("for (int I=0;I<this.%1$s.length;I++){ %2$s.write(%3$s); }%n", structName, toType.length() == 0 ? "this." + structName + "[I]" : '(' + toType + "this." + structName + "[I])", NAME_OUTPUT_STREAM);
314314
} else {
315315
this.getCurrentStruct().getReadFunc().indent()
316316
.printf("if (this.%1$s == null || this.%1$s.length != %2$s){ this.%1$s = new %3$s[%2$s]; for(int I=0;I<%2$s;I++){ this.%1$s[I] = new %3$s(%4$s);}}", structName, arraySizeIn, structBaseTypeName, (this.structStack.size() == 1 ? "this" : "this." + NAME_ROOT_STRUCT))
@@ -971,6 +971,11 @@ public static final class Builder {
971971
* Superclasses to be extended by generated subclasses.
972972
*/
973973
private final Map<String, String> mapSubClassesSuperclasses = new HashMap<String, String>();
974+
975+
/**
976+
* Imternal classes must not be static.
977+
*/
978+
public boolean internalClassesNotStatic;
974979
/**
975980
* The Package name for the result class.
976981
*/
@@ -1129,6 +1134,18 @@ public Builder setMainClassName(final String value) {
11291134
return this;
11301135
}
11311136

1137+
/**
1138+
* Don't make insternal generated classes as static ones
1139+
*
1140+
* @return the builder instance, must not be null
1141+
* @since 1.4.0
1142+
*/
1143+
public Builder doInternalClassesNonStatic() {
1144+
assertNonLocked();
1145+
this.internalClassesNotStatic = true;
1146+
return this;
1147+
}
1148+
11321149
/**
11331150
* Set the superclass for the main generated class. <b>NB! Also it affects read and write methods of the main class, the class will be used as the return type.</b>
11341151
*

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private byte[] loadResource(final String name) throws Exception {
6262
}
6363
}
6464

65-
public static abstract class TestSuperclass {
65+
public static class TestSuperclass {
6666
public String str;
6767
public String[] strarr;
6868
public float flt;
@@ -71,12 +71,12 @@ public static abstract class TestSuperclass {
7171
public double[] dblarr;
7272
public char len;
7373

74-
public static abstract class Ins {
74+
public class Ins {
7575
public byte[] a;
7676
public byte b;
7777
public byte c;
7878

79-
public static abstract class InsIns {
79+
public class InsIns {
8080
public byte a;
8181
}
8282

@@ -112,17 +112,20 @@ public void testReadWrite_ExtendsSuperClassAndUseItsFields() throws Exception {
112112
superclasses.put("ins",TestSuperclass.Ins.class.getCanonicalName());
113113
superclasses.put("ins.insins",TestSuperclass.Ins.InsIns.class.getCanonicalName());
114114

115+
final String thePackage = JBBPToJava6ConverterReadWriteTest.class.getPackage().getName();
116+
115117
final String text = JBBPToJava6Converter.makeBuilder(parser)
116118
.setMainClassName(CLASS_NAME)
117-
.setMainClassPackage(PACKAGE_NAME)
119+
.setMainClassPackage(thePackage)
118120
.disableGenerateFields()
119121
.setSuperClass(TestSuperclass.class.getCanonicalName())
120122
.setMapSubClassesSuperclasses(superclasses)
121123
.setAddGettersSetters(false)
124+
.doInternalClassesNonStatic()
122125
.build()
123126
.convert();
124127

125-
final String fullClassName = PACKAGE_NAME + '.' + CLASS_NAME;
128+
final String fullClassName = thePackage + '.' + CLASS_NAME;
126129
final ClassLoader classLoader = saveAndCompile(new JavaClassContent(fullClassName, text));
127130

128131
final Object instance = ReflectUtils.newInstance(classLoader.loadClass(fullClassName));

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ protected Object callRead(final Object instance, final JBBPBitInputStream inStre
9898
instance.getClass().getMethod("read", JBBPBitInputStream.class).invoke(instance, inStream);
9999
return instance;
100100
} catch (InvocationTargetException ex) {
101-
if (ex.getCause() != null) {
102-
throw (Exception) ex.getCause();
101+
if (ex.getTargetException() != null) {
102+
throw (Exception) ex.getTargetException();
103103
} else {
104104
throw ex;
105105
}

0 commit comments

Comments
 (0)