@Override public void visitStart() { this.flagSet.set(0); this.foundNamedFields.clear(); this.anonymousFieldCounter.set(1234); this.specialFieldsCounter.set(1); this.specialSection.clean(); this.structStack.clear(); this.specialMethods.clean(); this.structStack.add(new Struct(null, this.builder.mainClassName, "public")); }
String makeSrcPath(final Struct currentStruct) { final String fieldName = prepFldName(info.getFieldName()); if (this.struct == currentStruct) { return "this." + fieldName; } else { final String structPath = this.struct.getPath(); if (currentStruct.isRoot()) { return "this." + (structPath.length() == 0 ? "" : structPath + ".") + fieldName; } else { return "this." + NAME_ROOT_STRUCT + '.' + (structPath.length() == 0 ? "" : structPath + ".") + fieldName; } } } }
private void processSkipRemainingFlag() { if (this.isFlagSkipRemainingFieldsIfEOF()) { this.getCurrentStruct().getReadFunc().indent().println(String.format("if (!%s.hasAvailableData()) return this;", NAME_INPUT_STREAM)); } }
this.structStack.get(0).write(buffer, hasAbstractMethods ? "abstract" : null, this.builder.superClass,
final String customText, final boolean useSuperclassForReadWrite) { final String interfaceForGetSet = mapStructInterfaces == null ? null : mapStructInterfaces.get(this.getPath()); this.className, superClass != null ? " extends " + superClass + ' ' : "", interfaceForGetSet == null ? implementedInterfaces != null && !implementedInterfaces.isEmpty() ? " implements " + interfaces2str(implementedInterfaces) + ' ' : "" : " implements " + interfaceForGetSet ); buffer.incIndent(); c.write(buffer, null, mapStructSuperclasses.get(c.getPath()), null, mapStructInterfaces, mapStructSuperclasses, null, null, null, false); buffer.indent().println("private final " + findRoot().className + ' ' + NAME_ROOT_STRUCT + ';'); .print(this.parent == null ? "" : (findRoot().className + " root")) .println(") {");
fieldType = "JBBPAbstractArrayField<? extends JBBPAbstractField>"; if (this.builder.generateFields) { this.getCurrentStruct().getFields().printf("%s %s %s;%n", fieldModifier, fieldType, fieldName); this.getCurrentStruct().getReadFunc().printf("%s = %s;%n", fieldName, String.format("%s.readVarArray(this, In, %s, %s, %s, %b, %s)", this.getCurrentStruct().isRoot() ? "this" : "this." + NAME_ROOT_STRUCT, "JBBPByteOrder." + byteOrder.name(), nullableNameFieldInfo == null ? "null" : specialFieldName_fieldNameInfo, this.getCurrentStruct().getWriteFunc().printf("%s.writeVarArray(this, this.%s, Out, %s, %s, %s, %s);%n", this.getCurrentStruct().isRoot() ? "this" : "this." + NAME_ROOT_STRUCT, fieldName, "JBBPByteOrder." + byteOrder.name(), fieldType = "JBBPAbstractField"; if (this.builder.generateFields) { this.getCurrentStruct().getFields().printf("%s %s %s;%n", fieldModifier, fieldType, fieldName); this.getCurrentStruct().getReadFunc().printf("%s = %s;%n", fieldName, String.format("%s.readVarField(this, In, %s, %s, %s)", this.getCurrentStruct().isRoot() ? "this" : "this." + NAME_ROOT_STRUCT, "JBBPByteOrder." + byteOrder.name(), nullableNameFieldInfo == null ? "null" : specialFieldName_fieldNameInfo, this.getCurrentStruct().getWriteFunc().printf("%s.writeVarField(this, this.%s, Out, %s, %s, %s);%n", this.getCurrentStruct().isRoot() ? "this" : "this." + NAME_ROOT_STRUCT,
return String.format("%s.getNamedValue(this, \"%s\")", (getCurrentStruct().isRoot() ? "this" : "this." + NAME_ROOT_STRUCT), obj.toString()); } else if (obj instanceof JBBPNamedFieldInfo) { final NamedFieldInfo namedFieldInfo = foundNamedFields.get(obj);
textFieldType = type.asJavaSingleFieldType(); if (this.builder.generateFields) { getCurrentStruct().getFields().printf("%s %s %s;%n", fieldModifier, textFieldType, fieldName); getCurrentStruct().getReadFunc().println(String.format("this.%s = %s;", fieldName, type.makeReaderForSingleField(NAME_INPUT_STREAM, byteOrder))); getCurrentStruct().getWriteFunc().print(type.makeWriterForSingleField(NAME_OUTPUT_STREAM, "this." + fieldName, byteOrder)).println(";"); } else { textFieldType = type.asJavaArrayFieldType() + " []"; if (this.builder.generateFields) { getCurrentStruct().getFields().printf("%s %s %s;%n", fieldModifier, textFieldType, fieldName); getCurrentStruct().getReadFunc().printf("this.%s = %s;%n", fieldName, type.makeReaderForArray(NAME_INPUT_STREAM, arraySizeIn, byteOrder)); if (readWholeStreamAsArray) { getCurrentStruct().getWriteFunc().print(type.makeWriterForArrayWithUnknownSize(NAME_OUTPUT_STREAM, "this." + fieldName, byteOrder)).println(";"); } else { getCurrentStruct().getWriteFunc().print(type.makeWriterForArray(NAME_OUTPUT_STREAM, "this." + fieldName, arraySizeOut, byteOrder)).println(";");
final String arraySizeIn = nullableArraySize == null ? null : evaluatorToString(NAME_INPUT_STREAM, offsetInCompiledBlock, nullableArraySize, this.flagSet, true); final String arraySizeOut = nullableArraySize == null ? null : evaluatorToString(NAME_OUTPUT_STREAM, offsetInCompiledBlock, nullableArraySize, this.flagSet, true); final Struct newStruct = new Struct(this.getCurrentStruct(), structBaseTypeName, "public" + (builder.internalClassesNotStatic ? "" : " static")); structType = structBaseTypeName; if (this.builder.generateFields) { this.getCurrentStruct().getFields().indent().print(fieldModifier).printf(" %s %s;", structType, structName).println(); this.getCurrentStruct().getReadFunc().indent() .printf("if ( this.%1$s == null) { this.%1$s = new %2$s(%3$s);}", structName, structType, pathToRootObject) .printf(" %s.read(%s);%n", toType.length() == 0 ? "this." + structName : '(' + toType + "this." + structName + ')', NAME_INPUT_STREAM); this.getCurrentStruct().getWriteFunc().indent().print(toType.length() == 0 ? structName : '(' + toType + structName + ')').println(".write(Out);"); } else { structType = structBaseTypeName + " []"; if (this.builder.generateFields) { this.getCurrentStruct().getFields().indent().print(fieldModifier).printf(" %s %s;", structType, structName).println(); this.getCurrentStruct().getReadFunc().indent() .printf("List<%3$s> __%1$s_tmplst__ = new ArrayList<%3$s>(); while (%5$s.hasAvailableData()){ __%1$s_tmplst__.add(new %3$s(%4$s).read(%5$s));} this.%1$s = __%1$s_tmplst__.toArray(new %3$s[__%1$s_tmplst__.size()]);__%1$s_tmplst__ = null;%n", structName, pathToRootObject, NAME_INPUT_STREAM); 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); } else { this.getCurrentStruct().getReadFunc().indent() .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, pathToRootObject) .printf("for (int I=0;I<%2$s;I++){ this.%1$s[I].read(%3$s); }%n", toType + structName, arraySizeIn, NAME_INPUT_STREAM); this.getCurrentStruct().getWriteFunc().indent().printf("for (int I=0;I<%2$s;I++){ this.%1$s[I].write(%3$s); }", toType + structName, arraySizeOut, NAME_OUTPUT_STREAM);
this.getCurrentStruct().getFields().printf("%s JBBPAbstractField %s;%n", fieldModifier, fieldName); this.getCurrentStruct().getReadFunc().printf("%s = %s;%n", fieldName, String.format("%s.readCustomFieldType(this, In, %s, %s, %s, %b, %s)", this.getCurrentStruct().isRoot() ? "this" : "this." + NAME_ROOT_STRUCT, specialFieldName_typeParameterContainer, nullableNameFieldInfo == null ? "null" : specialFieldName_fieldNameInfo, this.getCurrentStruct().getWriteFunc().printf("%s;%n", String.format("%s.writeCustomFieldType(this, Out, %s, %s, %s, %s, %b, %s)", this.getCurrentStruct().isRoot() ? "this" : "this." + NAME_ROOT_STRUCT, "this." + fieldName, specialFieldName_typeParameterContainer,
getCurrentStruct().getReadFunc().indent().printf("this.%s = In.readBitField(%s);%n", fieldName, sizeOfFieldIn); } else { getCurrentStruct().getReadFunc().indent().print(fieldName).print(" = In.readBitsArray(").print(arraySizeIn).print(",").print(sizeOfFieldIn).println(");"); getCurrentStruct().getWriteFunc().indent().printf("%s.writeBits(this.%s,%s);%n", NAME_OUTPUT_STREAM, fieldName, sizeOfFieldOut); } else { if ("-1".equals(arraySizeIn)) { getCurrentStruct().getWriteFunc().indent().printf("for(int I=0; I<%s.length; I++)", fieldName).printf(" Out.writeBits(this.%s[I],%s);%n", fieldName, sizeOfFieldOut); } else { getCurrentStruct().getWriteFunc().indent().printf("for(int I=0; I<%s; I++)", arraySizeOut).printf(" Out.writeBits(this.%s[I],%s);%n", fieldName, sizeOfFieldOut); getCurrentStruct().getFields().indent().printf("%s %s %s;%n", fieldModifier, fieldType, fieldName);
@Override public void visitActionItem(final int offsetInCompiledBlock, final int actionType, final JBBPIntegerValueEvaluator nullableArgument) { final String valueTxtIn = nullableArgument == null ? "1" : evaluatorToString(NAME_INPUT_STREAM, offsetInCompiledBlock, nullableArgument, this.flagSet, true); final String valueTxtOut = nullableArgument == null ? "1" : evaluatorToString(NAME_OUTPUT_STREAM, offsetInCompiledBlock, nullableArgument, this.flagSet, true); switch (actionType) { case CODE_RESET_COUNTER: { getCurrentStruct().getReadFunc().println(NAME_INPUT_STREAM + ".resetCounter();"); getCurrentStruct().getWriteFunc().println(NAME_OUTPUT_STREAM + ".resetCounter();"); } break; case CODE_ALIGN: { getCurrentStruct().getReadFunc().indent().print(NAME_INPUT_STREAM + ".align(").print(valueTxtIn).println(");"); getCurrentStruct().getWriteFunc().indent().print(NAME_OUTPUT_STREAM + ".align(").print(valueTxtOut).println(");"); } break; case CODE_SKIP: { getCurrentStruct().getReadFunc().indent().print(NAME_INPUT_STREAM + ".skip(").print(valueTxtIn).println(");"); getCurrentStruct().getWriteFunc().indent().printf("for(int I=0; I<%s; I++) %s.write(0);%n", valueTxtOut, NAME_OUTPUT_STREAM); } break; default: { throw new Error("Detected unknown action, contact developer!"); } } }
@Override public void visitValField(final int offsetInCompiledBlock, final JBBPNamedFieldInfo nameFieldInfo, final JBBPIntegerValueEvaluator expression) { final String fieldName = prepFldName(nameFieldInfo.getFieldName()); FieldType type = FieldType.VAL; registerNamedField(nameFieldInfo, type); final String fieldModifier = makeModifier(nameFieldInfo); processSkipRemainingFlag(); final String textFieldType = type.asJavaSingleFieldType(); if (this.builder.generateFields) { getCurrentStruct().getFields().printf("%s %s %s;%n", fieldModifier, textFieldType, fieldName); } final String valIn = evaluatorToString(NAME_INPUT_STREAM, offsetInCompiledBlock, expression, this.flagSet, false); final String valOut = evaluatorToString(NAME_OUTPUT_STREAM, offsetInCompiledBlock, expression, this.flagSet, false); getCurrentStruct().getReadFunc().println(String.format("this.%s = %s;", fieldName, valIn)); getCurrentStruct().getWriteFunc().println(String.format("this.%s = %s;", fieldName, valOut)); if (this.builder.addGettersSetters) { registerGetterSetter(textFieldType, fieldName, true); } }
private void registerMaker(final String rawFieldType, final String fieldName, final String pathToRootObject, final boolean array) { if (!this.getCurrentStruct().getGettersSetters().isEmpty()) { this.getCurrentStruct().getGettersSetters().println(); } if (array) { this.getCurrentStruct() .getGettersSetters().indent().printf( "public %1$s[] make%2$s(int _Len_){ this.%3$s = new %1$s[_Len_]; for(int i=0;i < _Len_;i++) this.%3$s[i]=new %1$s(%4$s); return this.%3$s; }", rawFieldType, fieldName.toUpperCase(Locale.ENGLISH), fieldName, pathToRootObject ); } else { this.getCurrentStruct() .getGettersSetters().indent().printf( "public %1$s make%2$s(){ this.%3$s = new %1$s(%4$s); return this.%3$s; }", rawFieldType, fieldName.toUpperCase(Locale.ENGLISH), fieldName, pathToRootObject ); } }
private void processSkipRemainingFlagForWriting(final String structFieldName) { if (this.isFlagSkipRemainingFieldsIfEOF()) { this.getCurrentStruct().getWriteFunc().indent().println(String.format("if (%s == null) return this;", structFieldName)); } }
Struct findRoot() { if (this.parent == null) { return this; } return this.parent.findRoot(); }
private void registerGetterSetter(final String fieldType, final String fieldName, final boolean makeSetter) { if (!this.getCurrentStruct().getGettersSetters().isEmpty()) { this.getCurrentStruct().getGettersSetters().println(); } if (makeSetter) { this.getCurrentStruct().getGettersSetters().indent().printf("public void set%s(%s value) { this.%s = value;}%n", fieldName.toUpperCase(Locale.ENGLISH), fieldType, fieldName); } this.getCurrentStruct().getGettersSetters().indent().printf("public %s get%s() { return this.%s;}%n", fieldType, fieldName.toUpperCase(Locale.ENGLISH), fieldName); }