psuter / cafebabe Goto Github PK
View Code? Open in Web Editor NEWTiny Scala library to generate class files for the JVM
License: Other
Tiny Scala library to generate class files for the JVM
License: Other
Extracting the .jar with 'jar xf' and adding the parent folder to the classpath works just fine.
Maybe I'm just doing it wrong, but it seems fishy.
On line 118 here in ClassFile[1] there's a string literal "()V"
which is the signature of the default constructor of the super class. There is a constant constructorSig
in defaults.scala already for this purpose.
1:
CC: @manoskouk
By adding duplicated methods/fields is possible to generate ill-formed class files without any warnings; failing at runtime with ClassFormatError.
Tracking duplicated methods/fields is a bit hard since methods can change their static-ness; the check should be done on build-time...
I have a slightly more general solution; which consists in loading the class file (without resolving any dependencies) after being generated just to verify it's correctness by ensuring it passes the JVM checks.
Any comments?
Currently there is no way to generate a static initializer( i.e static{}; ) for a class, which is sometimes important for initialization before calling either Constructor or main() method.
Hi,
As asked here is minimal code reproducting the issue :
object App extends App {
import cafebabe.ByteCodes._
import cafebabe.AbstractByteCodes._
val classFile = new cafebabe.ClassFile("HW", None)
classFile.setSourceFile("HW.java")
classFile.addDefaultConstructor
val codeHandler = classFile.addMainMethod.codeHandler
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/HW.class")
}
As you can see, I deliberately commented the RETURN bytecode.
And here is the stack trace :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
at cafebabe.CodeHandler.setHeight$1(CodeHandler.scala:147)
at cafebabe.CodeHandler.computeMaxStack(CodeHandler.scala:222)
at cafebabe.CodeHandler.freeze(CodeHandler.scala:129)
at App$delayedInit$body.apply(App.scala:22)
at scala.Function0$class.apply$mcV$sp(Function0.scala:34)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App$$anonfun$main$1.apply(App.scala:60)
at scala.App$$anonfun$main$1.apply(App.scala:60)
at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59)
at scala.collection.immutable.List.foreach(List.scala:76)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:30)
at scala.App$class.main(App.scala:60)
at App$.main(App.scala:3)
at App.main(App.scala)
There of course a lot of other situation where an ArrayIndexOutOfBoundsException is thrown. As soon as some required bytecodes are missing it seems to suffer from this issue.
I'm using scala 2.9.3 with cafebabe 1.2. If you need any more information, let me know.
Hi there,
So I noticed you implemented support for interfaces, but you forgot to add a method in ClassFile.scala that adds a method with no code attribute. I locally wrote a method that handles this, called addMethodHead. Maybe you can implement it (or some variation of it) in your source code. The following picture shows the method I added:
Have a good one!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.