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 14, 2020
1 parent 33d99ef commit 425b1bb
Showing 1 changed file with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ class JavaCompiler(val typeProvider: ClassTypeProvider, config: RuntimeConfig)
""
}

out.puts("@io.kaitai.struct.annotations.Generated(")
out.inc
out.puts(s"""id = "${name}",""")
out.puts(s"""version = "${KSVersion.current}",""")
out.puts(s"""posInfo = ${config.readStoresPos},""")
out.puts(s"""autoRead = ${config.autoRead},""")
out.puts(s"""doc = "${doc(typeProvider.nowClass.doc)}"""")
out.dec
out.puts(")")
out.puts(s"public ${staticStr}class ${type2class(name)} extends $kstructName {")
out.inc

Expand Down Expand Up @@ -198,13 +207,56 @@ class JavaCompiler(val typeProvider: ClassTypeProvider, config: RuntimeConfig)
override def readFooter(): Unit = universalFooter

override def attributeDeclaration(attr: MemberSpec, index: Int, isNullable: Boolean): Unit = {
annotate(attr, index)
out.puts(s"private ${kaitaiType2JavaType(attr.dataTypeComposite, isNullable)} ${idToStr(attr.id)};")
}

override def attributeReader(attr: MemberSpec, index: Int, isNullable: Boolean): Unit = {
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 = summary.length
var pos = 0
while (pos < len) {
summary.charAt(pos) match {
case '"' => sb.append("\\\"")
case '\b' => sb.append("\\b")
case '\f' => sb.append("\\f")
case '\n' => sb.append("\\n")
case '\t' => sb.append("\\t")
case '\r' => sb.append("\\r")
case '\\' => sb.append("\\\\")
case c => sb.append(c)
}

pos += 1
}
sb.toString
}).getOrElse("")
}

def annotate(attr: MemberSpec, index: Int) = {
attr match {
case param : ParamDefSpec => {
out.puts(s"""@io.kaitai.struct.annotations.Parameter(id = "${param.id.humanReadable}", index = ${index}, doc = "${doc(param.doc)}")""")
}
case field : AttrSpec => {
field.id match {
case NamedIdentifier(name) =>
out.puts(s"""@io.kaitai.struct.annotations.SeqItem(id = "${name}", index = ${index}, doc = "${doc(field.doc)}")""")
case NumberedIdentifier(_) =>
out.puts(s"""@io.kaitai.struct.annotations.SeqItem(id = "", index = ${index}, doc = "${doc(field.doc)}")""")
case _ => {}
}
}
case _ => {}
}
}

override def universalDoc(doc: DocSpec): Unit = {
out.puts
out.puts( "/**")
Expand Down Expand Up @@ -616,10 +668,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 425b1bb

Please sign in to comment.