-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Java 8 classes. Again. #28
Changes from 4 commits
6675ae9
9e2cb23
2e10924
3cf9be2
d8876da
cb6a757
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
out |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package; | ||
|
||
import java.StdTypes.Int8; | ||
import java.NativeArray; | ||
import java.io.FileOutputStream; | ||
import java.io.File; | ||
import java.nio.file.Files; | ||
import org.objectweb.asm.ClassReader; | ||
import org.objectweb.asm.ClassWriter; | ||
import org.objectweb.asm.ClassVisitor; | ||
import org.objectweb.asm.MethodVisitor; | ||
import org.objectweb.asm.Opcodes.Opcodes_Statics; | ||
import java.Lib.println; | ||
|
||
using StringTools; | ||
|
||
/** | ||
* HOW TO USE: | ||
* | ||
* 1) Unzip JAR file into some folder. For example "jar_content"; | ||
* 2) Run `java -jar Main.jar jar_content`; | ||
* 3) Pick up generated `hxjava-std.jar`. | ||
*/ | ||
final class Main { | ||
static function main():Void { | ||
function process(dir:String) { | ||
if (sys.FileSystem.exists(dir)) { | ||
for (file in sys.FileSystem.readDirectory(dir)) { | ||
final path = haxe.io.Path.join([dir, file]); | ||
if (!sys.FileSystem.isDirectory(path)) { | ||
if (path.endsWith(".class")) { | ||
println('Process $path... '); | ||
final obj:NativeArray<Int8> = Files.readAllBytes(new File(path).toPath()); | ||
final reader = new ClassReader(obj); | ||
final writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES); | ||
final visitor = new MyClassVisitor(writer); | ||
|
||
reader.accept(visitor, 0); | ||
|
||
final stream = new FileOutputStream(path); | ||
stream.write(writer.toByteArray()); | ||
Sys.sleep(0.001); | ||
} | ||
} else { | ||
process(haxe.io.Path.addTrailingSlash(path)); | ||
} | ||
} | ||
} else { | ||
println('Wrong path. $dir is not exists'); | ||
} | ||
} | ||
|
||
final args:Array<String> = Sys.args(); | ||
if (args.length > 0) { | ||
final dir = args[0]; | ||
process(dir); | ||
println("\nPacking jar...\n"); | ||
if (Sys.command('jar', ["cvf", "hxjava-std.jar", "-C", dir, "."]) == 0) { | ||
println("\nDone!\n"); | ||
} else { | ||
println("Oops, something went wrong."); | ||
} | ||
} | ||
} | ||
} | ||
|
||
final class MyClassVisitor extends ClassVisitor { | ||
var isInterface:Bool = false; | ||
|
||
public function new(visitor:ClassVisitor) { | ||
super(Opcodes_Statics.ASM7, visitor); | ||
} | ||
|
||
@:overload | ||
override function visit(version:Int, access:Int, name:String, signature:String, superName:String, interfaces:NativeArray<String>):Void { | ||
cv.visit(version, access, name, signature, superName, interfaces); | ||
isInterface = (access & Opcodes_Statics.ACC_INTERFACE) != 0; | ||
} | ||
|
||
@:overload | ||
override function visitMethod(access:Int, name:String, desc:String, signature:String, exceptions:NativeArray<String>):MethodVisitor { | ||
final mv:MethodVisitor = cv.visitMethod(access, name, desc, signature, exceptions); | ||
if (!isInterface && mv != null) { | ||
return new MyMethodVisitor(mv); | ||
} | ||
return mv; | ||
} | ||
} | ||
|
||
final class MyMethodVisitor extends MethodVisitor { | ||
final target:MethodVisitor; | ||
|
||
public function new(target:MethodVisitor) { | ||
super(Opcodes_Statics.ASM7, null); | ||
this.target = target; | ||
} | ||
|
||
@:overload | ||
override function visitCode() { | ||
target.visitCode(); | ||
// target.visitTypeInsn(Opcodes_Statics.NEW, "java/io/IOException"); | ||
// target.visitInsn(Opcodes_Statics.DUP); | ||
target.visitMethodInsn(Opcodes_Statics.INVOKESPECIAL, "java/io/IOException", "<init>", "()V", false); | ||
target.visitInsn(Opcodes_Statics.ATHROW); | ||
target.visitMaxs(2, 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this do? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It replaces existing class methods bodies with I checked old There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean this call in particular. Does it set the max stack? Because that should be 1 instead of 2 here, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, https://asm.ow2.io/javadoc/org/objectweb/asm/MethodVisitor.html#visitMaxs-int-int- There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are no locals so it should be |
||
target.visitEnd(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
-m Main | ||
-D jvm | ||
--java ./out | ||
|
||
--java-lib asm-7.1.jar |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this check something along the lines of "if there is any code" instead of explicitly checking for interfaces? There are abstract methods too where we shouldn't add any code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but I check it here to be sure it's a code:
https://github.com/dmitryhryppa/hxjava/blob/master/jar-tool/Main.hx#L31
But as I remember abstracts can contain the code in Java 8. Or it's not true?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not what I mean. The condition should check if there is a Code attribute.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like
visitCode
only does anything if there is aCode
attribute. Though in this case the interface check should be unnecessary, but that's not very important.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it. I did a small cleanup based on your comments.