Skip to content
Brian S. O'Neill edited this page Jun 9, 2024 · 6 revisions

Q: How can I see the generated classes?

Run with the -Dorg.cojen.maker.ClassMaker.DEBUG=true command line option. It prints a message (to standard out) for every class being generated, and it writes a class file to the temp directory.

ClassMaker writing to /tmp/ClassMaker/org.cojen.maker.ClassMaker-1(0).class

If the class isn't abstract but it still has abstract methods, the message also reports which methods still need to be implemented.

ClassMaker writing to /tmp/ClassMaker/Test-1(0).class; unimplemented methods: [int applyAsInt(long)]

The debug option also disables this check: java.lang.IllegalStateException: Accessing an unassigned variable. In doing so, an unverifiable class is generated, and a class file is written which can be inspected with a disassembler or decompiler.

Q: How can I define a mutually recursive function?

If function "a" calls function "b" which then calls function "a", then both of the functions must be declared before they are defined. Otherwise an IllegalStateException is thrown because the dependent function cannot be found.

    // Declare both functions before defining any code.
    MethodMaker mma = cm.addMethod(int.class, "a", int.class).static_();
    MethodMaker mmb = cm.addMethod(int.class, "b", int.class).static_();

    // Define code for "a", which invokes "b".
    ...
    mma.invoke("b", ...);

    // Define code for "b", which invokes "a".
    ...
    mmb.invoke("a", ...);

Note that in this example, the methods are declared static_ up front. This ensures that the correct method invocation instructions are generated.