例IKVM Java-to-.NET compilerを使用して.NETで動作するように変換:
hello.cs:
using System;
using System.IO;
using org.objectweb.asm;
namespace test.helloWorld
{
public class helloDump
{
public static byte[] dump()
{
ClassWriter cw = new ClassWriter(0);
MethodVisitor mv;
cw.visit(Opcodes.__Fields.V1_6, Opcodes.__Fields.ACC_PUBLIC + Opcodes.__Fields.ACC_SUPER, "hello", null, "java/lang/Object", null);
mv = cw.visitMethod(Opcodes.__Fields.ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(Opcodes.__Fields.ALOAD, 0);
mv.visitMethodInsn(Opcodes.__Fields.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
mv.visitInsn(Opcodes.__Fields.RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
mv = cw.visitMethod(Opcodes.__Fields.ACC_PUBLIC + Opcodes.__Fields.ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
mv.visitCode();
mv.visitFieldInsn(Opcodes.__Fields.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
mv.visitLdcInsn("Hello World!");
mv.visitMethodInsn(Opcodes.__Fields.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
mv.visitInsn(Opcodes.__Fields.RETURN);
mv.visitMaxs(2, 1);
mv.visitEnd();
cw.visitEnd();
return cw.toByteArray();
}
public static void Main(string[] args)
{
FileStream helloWorldFile = new FileStream("hello.class", FileMode.Create);
byte[] helloWorldClass = dump();
helloWorldFile.Seek(0, SeekOrigin.Begin);
helloWorldFile.Write(helloWorldClass, 0, helloWorldClass.Length);
}
}
}
コマンド:
$ ikvmc -out:org.objectweb.asm.dll -target:library -version:3.2.0.0 asm-3.2.jar
$ mcs -r:org.objectweb.asm.dll hello.cs
$ mono hello.exe
$ ls hello.class
$ java hello
の.classファイルを生成するには、どのようなコンパイラですそうです。コードの生成とコンパイルで何が問題になりますか?または.NETのReflection.EmitやDynamic IL生成のようなものです。Javaのために何をしたいですか? – OregonGhost
あなたのプロジェクトはJavaコンパイラを作成していますか? –
私のプロジェクトはコンパイラに似ていますが、C#.NETを使用しています。主な目的の1つは.classファイル(バイトコード)を動的に生成することです。 –