Skip to content

Commit

Permalink
removed all trailing whitespaces (w/ sed...)
Browse files Browse the repository at this point in the history
  • Loading branch information
psuter committed Apr 7, 2012
1 parent f0412e5 commit 15e985f
Show file tree
Hide file tree
Showing 15 changed files with 94 additions and 94 deletions.
28 changes: 14 additions & 14 deletions src/main/scala/Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ object Test {
def main(args : Array[String]) : Unit = {
import cafebabe.AbstractByteCodes._
import cafebabe.ByteCodes._

val cf = new cafebabe.ClassFile("Test", None)

cf.addField("Ljava/lang/String;", "name")

cf.addDefaultConstructor

{
val ch = cf.addMethod("I", "sayHello", "I", "Z", "Ljava/lang/String;").codeHandler
ch << Ldc(41)
ch << DefaultNew("Oh") << Ldc(3.14) << InvokeVirtual("Oh", "getInt", "(D)I") << IADD
ch << IRETURN
ch.freeze
}

{
val ch = cf.addMethod("I", "jumpOver").codeHandler
ch << Ldc(42)
Expand All @@ -27,33 +27,33 @@ object Test {
ch << IRETURN
ch.freeze
}

{
val ch = cf.addMethod("I", "fact", "I").codeHandler
val label = ch.getFreshLabel("else")
ch << ILoad(1) << Ldc(1) << If_ICmpGt(label) << Ldc(1) <<
IRETURN << Label(label) << ILoad(1) << ALoad(0) <<
ILoad(1) << Ldc(1) << ISUB << InvokeVirtual("Test", "fact", "(I)I") <<
IMUL << IRETURN

ch.freeze
}

cf.writeToFile("./classfiles/Test.class")

val classFile = new cafebabe.ClassFile("HWGenerated", None)
classFile.addDefaultConstructor
val codeHandler = classFile.addMainMethod.codeHandler
codeHandler <<
GetStatic("java/lang/System", "out", "Ljava/io/PrintStream;") <<

codeHandler <<
GetStatic("java/lang/System", "out", "Ljava/io/PrintStream;") <<
Ldc("Hello world!") <<
InvokeVirtual("java/io/PrintStream", "println", "(Ljava/lang/String;)V") <<
RETURN

codeHandler.freeze
classFile.writeToFile("./classfiles/HWGenerated.class")

System.out.println("written files.")
}
}
4 changes: 2 additions & 2 deletions src/main/scala/cafebabe/AttributeInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cafebabe
import ClassFileTypes._

object AttributeInfo {
def apply(attributeNameIndex: U2, info: Seq[U1]) : AttributeInfo =
def apply(attributeNameIndex: U2, info: Seq[U1]) : AttributeInfo =
new AttributeInfo(attributeNameIndex, info)

def unapply(ai: AttributeInfo) : Option[(U2,Seq[U1])] =
Expand All @@ -17,7 +17,7 @@ class AttributeInfo(val attributeNameIndex: U2, val info: Seq[U1]) extends Strea
info.foreach(stream << _)
stream
}

def size: Int = 6 + info.size
}

6 changes: 3 additions & 3 deletions src/main/scala/cafebabe/ByteCodes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ package cafebabe
object ByteCodes {
import ClassFileTypes._
import AbstractByteCodes._

sealed abstract class ByteCode(val code: U1, se: Option[Int], l: Option[Int]) extends AbstractByteCode {
val size: Int = 1
val stackEffect: Option[Int] = se
val length: Option[Int] = l
override def toStream(bs: ByteStream): ByteStream = bs << code
}

private implicit def intToOptionInt(i: Int) = Some(i)

case object AALOAD extends ByteCode(0x32, -1, 1)
case object AASTORE extends ByteCode(0x53, -3, 1)
case object ACONST_NULL extends ByteCode(0x1, 1, 1)
Expand Down
12 changes: 6 additions & 6 deletions src/main/scala/cafebabe/ByteStream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ trait Streamable {
class ByteStream {
import java.io.{DataOutputStream,ByteArrayOutputStream}
import ClassFileTypes._

private var bytes = new ByteArrayOutputStream
protected[cafebabe] def getBytes : Array[Byte] = bytes.toByteArray
private var stream: DataOutputStream = new DataOutputStream(bytes)

// appends bytes to the stream
def <<(u1: U1): ByteStream = { stream.write(u1); this }
def <<(u2: U2): ByteStream = { stream.writeShort(u2); this }
Expand All @@ -23,19 +23,19 @@ class ByteStream {
def <<(seq: Seq[Streamable]): ByteStream = {
seq.foreach(_.toStream(this))
this
}
}
// appends an entire other byte stream to the stream
def <<(bs: ByteStream): ByteStream = {
bs.stream.flush
bs.bytes.writeTo(this.stream)
this
}

def size: Int = stream.size

def writeToFile(fileName: String): Unit = {
import java.io.FileOutputStream

val fileStream = new FileOutputStream(fileName)
stream.flush
bytes.writeTo(fileStream)
Expand Down
6 changes: 3 additions & 3 deletions src/main/scala/cafebabe/CPEntries.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ClassFileTypes._

object CPTags {
import ClassFileTypes._

val Class: U1 = 7
val Fieldref: U1 = 9
val Methodref: U1 = 10
Expand Down Expand Up @@ -82,10 +82,10 @@ case class CPNameAndTypeInfo(val nameIndex: U2, val descriptorIndex: U2) extends

case class CPUtf8Info(val bytes: Seq[U1]) extends CPEntry(CPTags.Utf8) {
private var original: String = _

def setSource(str: String): CPUtf8Info = { original = str; this }
def getSource: String = original

override def toStream(stream: ByteStream): ByteStream = {
stream << tag << bytes.length.asInstanceOf[U2]
bytes.foreach(b => { stream << b })
Expand Down
28 changes: 14 additions & 14 deletions src/main/scala/cafebabe/ClassFile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package cafebabe
class ClassFile(val className: String, parentName: Option[String] = None) extends Streamable {
import ClassFileTypes._
import Defaults._

private var magic: U4 = defaultMagic
private var minor: U2 = defaultMinor
private var major: U2 = defaultMajor
Expand All @@ -17,24 +17,24 @@ class ClassFile(val className: String, parentName: Option[String] = None) extend
private lazy val sourceFileNameIndex: U2 = constantPool.addString("SourceFile")

private var accessFlags: U2 = defaultClassAccessFlags

private val thisClass: U2 = constantPool.addClass(constantPool.addString(className))

private val superClassName: String = parentName match {
case None => "java/lang/Object"
case Some(name) => name
}
private var superClass: U2 = constantPool.addClass(constantPool.addString(superClassName))

private var fields: List[FieldInfo] = Nil
private var methods: List[MethodInfo] = Nil

// TODO
private var interfacesCount: U2 = 0
private var interfaces: List[U1] = Nil

private var attributes : List[AttributeInfo] = Nil

private var _srcNameWasSet = false
/** Attaches the name of the original source file to the class file. */
def setSourceFile(sf : String) : Unit = {
Expand All @@ -55,7 +55,7 @@ class ClassFile(val className: String, parentName: Option[String] = None) extend
fields = fields ::: (inf :: Nil)
new FieldHandler(inf, constantPool)
}

/** Adds a method with arbitrarily many arguments, using the default flags and no attributes. */
def addMethod(retTpe: String, name: String, args: String*): MethodHandler = addMethod(retTpe,name,args.toList)

Expand All @@ -71,14 +71,14 @@ class ClassFile(val className: String, parentName: Option[String] = None) extend

new MethodHandler(inf, code, constantPool, concatArgs)
}

/** Adds the main method */
def addMainMethod: MethodHandler = {
val handler = addMethod("V", "main", "[Ljava/lang/String;")
handler.setFlags(Flags.METHOD_ACC_PUBLIC | Flags.METHOD_ACC_STATIC)
handler
}

/** Adds a default constructor. */
def addDefaultConstructor: MethodHandler = {
val accessFlags: U2 = Flags.METHOD_ACC_PUBLIC
Expand All @@ -88,17 +88,17 @@ class ClassFile(val className: String, parentName: Option[String] = None) extend
val inf = MethodInfo(accessFlags, nameIndex, descriptorIndex, List(code))
methods = methods ::: (inf :: Nil)
val mh = new MethodHandler(inf, code, constantPool, "")

import ByteCodes._
import AbstractByteCodes._

mh.codeHandler << ALOAD_0
mh.codeHandler << InvokeSpecial(superClassName, constructorName, constructorSig)
mh.codeHandler << RETURN
mh.codeHandler.freeze
mh
}

/** Writes the binary representation of this class file to a file. */
def writeToFile(fileName : String) : Unit = {
// The stream we'll ultimately use to write the class file data
Expand All @@ -116,7 +116,7 @@ class ClassFile(val className: String, parentName: Option[String] = None) extend
CustomClassLoader.registerClass(className, bytes)
*/
}

def toStream(byteStream: ByteStream): ByteStream = {
byteStream <<
magic <<
Expand All @@ -132,7 +132,7 @@ class ClassFile(val className: String, parentName: Option[String] = None) extend
attributes.size.asInstanceOf[U2] << attributes

}

def stringToDescriptor(s: String) = s
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/cafebabe/ClassFileTypes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ object ClassFileTypes {
type U1 = Byte
type U2 = Short
type U4 = Int

implicit def IntToU1(i: Int): U1 = i.asInstanceOf[U1]
implicit def IntToU2(i: Int): U2 = i.asInstanceOf[U2]
implicit def IntToU4(i: Int): U4 = i.asInstanceOf[U4]
Expand Down
6 changes: 3 additions & 3 deletions src/main/scala/cafebabe/CodeAttributeInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CodeAttributeInfo(val codeNameIndex: U2) extends AttributeInfo(codeNameInd
var maxStack: U2 = 0 // gets set when the code handler 'freezes'
var maxLocals: U2 = 0 // gets set when the code handler 'freezes'
var code: ByteStream = new ByteStream

case class ExceptionTableEntry(startPC: U2, endPC: U2, handlerPC: U2, catchType: U2) extends Streamable {
override def toStream(stream: ByteStream) = stream
}
Expand All @@ -31,11 +31,11 @@ class CodeAttributeInfo(val codeNameIndex: U2) extends AttributeInfo(codeNameInd
stream << exceptionTableLength << exceptionTable
stream << attributesCount << attributes
}

private def attributesSize: Int = {
attributes.foldLeft[Int](0)((s:Int, c:AttributeInfo) => { s + c.size })
}

override def size: Int = {
18 + code.size + (exceptionTable.size * 8) + attributesSize
}
Expand Down
Loading

0 comments on commit 15e985f

Please sign in to comment.