Class ClassBuilder

java.lang.Object
org.apache.drill.exec.compile.ClassBuilder

public class ClassBuilder extends Object
Implements the "plain Java" method of code generation and compilation. Given a CodeGenerator, obtains the generated source code, compiles it with the selected compiler, loads the byte-codes into a class loader and provides the resulting class. Compared with the ClassTransformer mechanism, this one requires the code generator to have generated a complete Java class that is capable of direct compilation and loading. This means the generated class must be a subclass of the template so that the JVM can use normal Java inheritance to associate the template and generated methods.

Here is how to use the plain Java technique to debug generated code:

  • Set the config option drill.exec.compile.code_dir to the location where you want to save the generated source code.
  • Where you generate code (using a CodeGenerator), set the "plain Java" options:
     CodeGenerator<Foo> cg = ...
     cg.plainJavaCapable(true); // Class supports plain Java
     cg.preferPlainJava(true); // Actually generate plain Java
     cg.saveCodeForDebugging(true); // Save code for debugging
     ...
    Note that saveCodeForDebugging automatically sets the PJ option if the generator is capable. Call preferPlainJava only if you want to try PJ for this particular generated class without saving the generated code.
  • In your favorite IDE, add to the code lookup path the code directory saved earlier. In Eclipse, for example, you do this in the debug configuration you will use to debug Drill.
  • Set a breakpoint in template used for the generated code.
  • Run Drill. The IDE will stop at your breakpoint.
  • Step into the generated code. Examine class field and local variables. Have fun!

Most generated classes have been upgraded to support Plain Java compilation. Once this work is complete, the calls to plainJavaCapable can be removed as all generated classes will be capable.

The setting to prefer plain Java is ignored for any remaining generated classes not marked as plain Java capable.