Skip to content

Commit

Permalink
#22: Support annotations in Java generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingun committed Jan 13, 2020
1 parent 33d99ef commit 3ddcd37
Showing 1 changed file with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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( "/**")
Expand Down Expand Up @@ -616,10 +660,12 @@ class JavaCompiler(val typeProvider: ClassTypeProvider, config: RuntimeConfig)
//</editor-fold>

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
}
Expand Down

0 comments on commit 3ddcd37

Please sign in to comment.