From 6a5366a874957c505c360bef82b169e9fb8118a8 Mon Sep 17 00:00:00 2001 From: Etienne Kneuss Date: Mon, 10 Dec 2012 15:40:46 +0100 Subject: [PATCH] Implement support for interfaces --- src/main/scala/cafebabe/ClassFile.scala | 12 +++++++----- src/main/scala/cafebabe/InterfaceInfo.scala | 9 +++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 src/main/scala/cafebabe/InterfaceInfo.scala diff --git a/src/main/scala/cafebabe/ClassFile.scala b/src/main/scala/cafebabe/ClassFile.scala index fba3e53..f97407b 100644 --- a/src/main/scala/cafebabe/ClassFile.scala +++ b/src/main/scala/cafebabe/ClassFile.scala @@ -28,12 +28,14 @@ class ClassFile(val className: String, parentName: Option[String] = None) extend private var fields: List[FieldInfo] = Nil private var methods: List[MethodInfo] = Nil + private var interfaces: List[InterfaceInfo] = Nil + private var attributes : List[AttributeInfo] = Nil - // TODO - private var interfacesCount: U2 = 0 - private var interfaces: List[U1] = Nil + def addInterface(name: String) { + val nameIndex = constantPool.addClass(constantPool.addString(name)) - private var attributes : List[AttributeInfo] = Nil + interfaces = InterfaceInfo(name, nameIndex) :: interfaces + } private var _srcNameWasSet = false /** Attaches the name of the original source file to the class file. */ @@ -140,7 +142,7 @@ class ClassFile(val className: String, parentName: Option[String] = None) extend accessFlags << thisClass << superClass << - interfacesCount << + interfaces.size.asInstanceOf[U2] << interfaces.reverse << fields.size.asInstanceOf[U2] << fields << methods.size.asInstanceOf[U2] << methods << attributes.size.asInstanceOf[U2] << attributes diff --git a/src/main/scala/cafebabe/InterfaceInfo.scala b/src/main/scala/cafebabe/InterfaceInfo.scala new file mode 100644 index 0000000..1820a4d --- /dev/null +++ b/src/main/scala/cafebabe/InterfaceInfo.scala @@ -0,0 +1,9 @@ +package cafebabe + +import ClassFileTypes._ + +case class InterfaceInfo(interfaceName: String, nameIndex: U2) extends Streamable { + override def toStream(stream: ByteStream): ByteStream = { + stream << nameIndex + } +}