diff --git a/shared/src/main/scala/io/kaitai/struct/languages/JavaCompiler.scala b/shared/src/main/scala/io/kaitai/struct/languages/JavaCompiler.scala index 4925ec999..df1e59518 100644 --- a/shared/src/main/scala/io/kaitai/struct/languages/JavaCompiler.scala +++ b/shared/src/main/scala/io/kaitai/struct/languages/JavaCompiler.scala @@ -72,6 +72,14 @@ class JavaCompiler(val typeProvider: ClassTypeProvider, config: RuntimeConfig) "" } + out.puts(s""" + |@io.kaitai.struct.annotations.Generated( + | id = "${name}", + | version = "${KSVersion.current}", + | posInfo = ${config.readStoresPos}, + | autoRead = ${config.autoRead}, + | doc = "${doc(typeProvider.nowClass.doc)}" + |)""".stripMargin) out.puts(s"public ${staticStr}class ${type2class(name)} extends $kstructName {") out.inc @@ -198,13 +206,49 @@ class JavaCompiler(val typeProvider: ClassTypeProvider, config: RuntimeConfig) override def readFooter(): Unit = universalFooter override def attributeDeclaration(attr: MemberSpec, index: Int, isNullable: Boolean): Unit = { + out.puts(annotate(attr, index)) out.puts(s"private ${kaitaiType2JavaType(attr.dataTypeComposite, isNullable)} ${idToStr(attr.id)};") } override def attributeReader(attr: MemberSpec, index: Int, isNullable: Boolean): Unit = { + out.puts(annotate(attr, index)) out.puts(s"public ${kaitaiType2JavaType(attr.dataTypeComposite, isNullable)} ${idToStr(attr.id)}() { return ${idToStr(attr.id)}; }") } + def doc(doc: DocSpec): String = { + doc.summary.map(summary => + val sb = new StringBuilder + val len = text.length + var pos = 0 + while (pos < len) { + text.charAt(pos) match { + case '"' => s.append("\\\"") + case '\b' => s.append("\\b") + case '\f' => s.append("\\f") + case '\n' => s.append("\\n") + case '\t' => s.append("\\t") + case '\r' => s.append("\\r") + case '\\' => s.append("\\\\") + case c => s.append(c) + } + + pos += 1 + } + sb.toString + ).getOrElse("") + } + + def annotate(attr: MemberSpec, index: Int): String = { + attr match { + case param : ParamDefSpec => { + s"""@io.kaitai.struct.annotations.Parameter(id = "${param.id.name}", index = ${index}, doc = "${doc(param.doc)}")""" + } + case field : AttrSpec => { + s"""@io.kaitai.struct.annotations.SeqItem(id = "${field.id.name}", index = ${index}, doc = "${doc(field.doc)}")""" + } + } + } + override def universalDoc(doc: DocSpec): Unit = { out.puts out.puts( "/**") @@ -616,10 +660,12 @@ class JavaCompiler(val typeProvider: ClassTypeProvider, config: RuntimeConfig) // override def instanceDeclaration(instName: InstanceIdentifier, instSpec: InstanceSpec): Unit = { + out.puts(s"""@io.kaitai.struct.annotations.Instance(id = "${instName.name}", doc = "${doc(instSpec.doc)}")""") out.puts(s"private ${kaitaiType2JavaTypeBoxed(instSpec.dataTypeComposite)} ${idToStr(instName)};") } override def instanceHeader(className: String, instName: InstanceIdentifier, instSpec: InstanceSpec): Unit = { + out.puts(s"""@io.kaitai.struct.annotations.Instance(id = "${instName.name}", doc = "${doc(instSpec.doc)}")""") out.puts(s"public ${kaitaiType2JavaTypeBoxed(instSpec.dataTypeComposite)} ${idToStr(instName)}() {") out.inc }