Skip to content
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

Merged
merged 6 commits into from
May 16, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jar-tool/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
out
108 changes: 108 additions & 0 deletions jar-tool/Main.hx
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) {
Copy link
Member

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.

Copy link
Contributor Author

@posxposy posxposy May 12, 2019

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?

Copy link
Member

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.

Copy link
Member

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 a Code attribute. Though in this case the interface check should be unnecessary, but that's not very important.

Copy link
Contributor Author

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.

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It replaces existing class methods bodies with throw new IOException();

I checked old hxjava-std with a decompiler and found there this kind of hack, so I tried to make a similar thing too.

Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

@posxposy posxposy May 12, 2019

Choose a reason for hiding this comment

The 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-
I commented the first two lines, so it should be 1 :)
But looks like it may be even (1, 1)? I'm new to JVM, so it's a bit confusing for me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no locals so it should be (1, 0). The only item on the stack is the exception which gets thrown.

target.visitEnd();
}
}
Binary file added jar-tool/asm-7.1.jar
Binary file not shown.
5 changes: 5 additions & 0 deletions jar-tool/build.hxml
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
Binary file modified lib/hxjava-std.jar
Binary file not shown.