クラスを拡張したり、クラス リテラルを取得したり、型パラメーターとして使用したり、キャストしたりしたいと考えています。
すでに回避策がありますが、まだ興味があります。
現在、ASM で実行時にクラスを定義し、Object
タイプでインスタンスを作成し、リフレクションでそのメソッドを呼び出すことができますが、それをクラス タイプとして使用する方法がわかりません。
確かに、コンパイラーは実行時に定義されたクラスについて認識していませんが、この場合、私はそれを認識しており、ASM 化された .java ファイル (以下の例) に隠されていることがわかると思います。コンパイル時に実際にクラスの型を知っていることをコンパイラに納得させるにはどうすればよいでしょうか?
ご覧いただきありがとうございます。
-ジュリアン
ASM 化されたクラス B の例: なりすましのバイトコード生成コードclass B{String hello = "Hello B!"}
:
import java.util.*;
import org.objectweb.asm.*;
//import org.objectweb.asm.attrs.*;
public class BDump implements Opcodes {
public static byte[] dump () throws Exception {
ClassWriter cw = new ClassWriter(0);
FieldVisitor fv;
MethodVisitor mv;
AnnotationVisitor av0;
cw.visit(V1_6, ACC_SUPER, "B", null, "java/lang/Object", null);
{
fv = cw.visitField(0, "hello", "Ljava/lang/String;", null, null);
fv.visitEnd();
}
{
mv = cw.visitMethod(0, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
mv.visitVarInsn(ALOAD, 0);
mv.visitLdcInsn("Hello B!");
mv.visitFieldInsn(PUTFIELD, "B", "hello", "Ljava/lang/String;");
mv.visitInsn(RETURN);
mv.visitMaxs(2, 1);
mv.visitEnd();
}
cw.visitEnd();
return cw.toByteArray();
}
}