1

I'd like extend the class, get a class literal, use it as a type parameter, or cast to it.

I already have a workaround, but I'm still curious.

Currently I can define a class at runtime with ASM, create an instance with Object type and invoke its methods with reflection, but I don't know how to use it as a class type.

Granted, the compiler doesn't know about classes defined at runtime, but in this case I know about it, and I think that I see it hidden in the ASMified .java file (example below). How can convince the compiler that it actually does know the class type at compile time?

Thanks for taking a look,

-Julian

Example of an ASMified class B: bytecode-generating code that spoofs 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();
}
}

1 Answer 1

4

You can't use a class at compile time which doesn't exist at compile time. What you can do is

  • use a class or interface which exists at compile time but is replaced at runtime with the desired implementation.
  • use an interface which is available at compile time but is only implemented at runtime.
  • use reflection to call the constructor/methods at runtime.
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the helpful response. It begs more questions:) Where is the breakdown, exactly? Since there is nothing I don't know about the class at compile time, I naively expect to be able to provide the compiler with a spoofed version of whatever it should expect, but I couldn't find a "Java Compiler For Dummies" on the web.
Generating byte code is something which few developers with ten years experience have done whereas you should understand the basics of how to use compiler after a few months. I am a little confused as to your familiarity with Java.
Noob status here, learning Java through some fun projects. I know some of the basics about compilers, e.g. lexer/scanner-parser-analyzer phases, but I can't seem to find out how exactly ASTs are used and what exactly happens in each phase so that I might be able to circumvent whatever check is throwing the error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.