Package org.apache.drill.exec.compile
Class ClassBuilder
java.lang.Object
org.apache.drill.exec.compile.ClassBuilder
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.
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Class<?>
getCompiledClass
(String code, String className, DrillConfig config, OptionSet options) Class<?>
Given a code generator which has already generated plain Java code, compile the code, create a class loader, and return the resulting Java class.
-
Field Details
-
CODE_DIR_OPTION
- See Also:
-
-
Constructor Details
-
ClassBuilder
-
-
Method Details
-
getImplementationClass
Given a code generator which has already generated plain Java code, compile the code, create a class loader, and return the resulting Java class.- Parameters:
cg
- a plain Java capable code generator that has generated plain Java code- Returns:
- the class that the code generator defines
- Throws:
ClassTransformationException
-
getCompiledClass
public static Class<?> getCompiledClass(String code, String className, DrillConfig config, OptionSet options) throws org.codehaus.commons.compiler.CompileException, ClassNotFoundException, ClassTransformationException, IOException - Throws:
org.codehaus.commons.compiler.CompileException
ClassNotFoundException
ClassTransformationException
IOException
-