diff options
author | Paul Rogers <progers@maprtech.com> | 2016-11-19 18:29:24 -0800 |
---|---|---|
committer | Jinfeng Ni <jni@apache.org> | 2016-12-19 15:57:36 -0800 |
commit | bbcf4b765e6946a8b6c7110372c4e1cadbfefa44 (patch) | |
tree | c6134f346284e9c5a971b22b184cf44640c648f5 /exec/java-exec/src/main/java/org/apache/drill/exec/compile/QueryClassLoader.java | |
parent | 03928af0b5cafd52e5b153aa852e5642b505f2c6 (diff) |
DRILL-5052: Option to debug generated Java code using an IDE
Provides a second compilation path for generated code: “plan old Java”
in which generated code inherit from their templates. Such code can be
compiled directly, allowing easy debugging of generated code.
Also show to generate two classes in the External Sort Batch as “plain
old Java” to enable IDE debugging of that generated code. Required
minor clean-up of the templates.
Fixes some broken toString( ) methods in code generation classes
Fixes a variety of small compilation warnings
Adds Java doc to a few classes
Includes clean-up from code review comments.
close apache/drill#660
Diffstat (limited to 'exec/java-exec/src/main/java/org/apache/drill/exec/compile/QueryClassLoader.java')
-rw-r--r-- | exec/java-exec/src/main/java/org/apache/drill/exec/compile/QueryClassLoader.java | 91 |
1 files changed, 6 insertions, 85 deletions
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/compile/QueryClassLoader.java b/exec/java-exec/src/main/java/org/apache/drill/exec/compile/QueryClassLoader.java index da038022e..31b464bf4 100644 --- a/exec/java-exec/src/main/java/org/apache/drill/exec/compile/QueryClassLoader.java +++ b/exec/java-exec/src/main/java/org/apache/drill/exec/compile/QueryClassLoader.java @@ -20,52 +20,23 @@ package org.apache.drill.exec.compile; import java.io.IOException; import java.net.URL; import java.net.URLClassLoader; -import java.util.Arrays; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.atomic.AtomicLong; import org.apache.drill.common.config.DrillConfig; -import org.apache.drill.common.exceptions.UserException; import org.apache.drill.exec.compile.ClassTransformer.ClassNames; import org.apache.drill.exec.exception.ClassTransformationException; import org.apache.drill.exec.server.options.OptionManager; -import org.apache.drill.exec.server.options.OptionValidator; -import org.apache.drill.exec.server.options.OptionValue; -import org.apache.drill.exec.server.options.TypeValidators.BooleanValidator; -import org.apache.drill.exec.server.options.TypeValidators.LongValidator; -import org.apache.drill.exec.server.options.TypeValidators.StringValidator; import org.codehaus.commons.compiler.CompileException; import com.google.common.collect.MapMaker; -public class QueryClassLoader extends URLClassLoader { - private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(QueryClassLoader.class); - - public static final String JAVA_COMPILER_OPTION = "exec.java_compiler"; - public static final StringValidator JAVA_COMPILER_VALIDATOR = new StringValidator(JAVA_COMPILER_OPTION, CompilerPolicy.DEFAULT.toString()) { - @Override - public void validate(final OptionValue v, final OptionManager manager) { - super.validate(v, manager); - try { - CompilerPolicy.valueOf(v.string_val.toUpperCase()); - } catch (IllegalArgumentException e) { - throw UserException.validationError() - .message("Invalid value '%s' specified for option '%s'. Valid values are %s.", - v.string_val, getOptionName(), Arrays.toString(CompilerPolicy.values())) - .build(logger); - } - } - }; - - public static final String JAVA_COMPILER_DEBUG_OPTION = "exec.java_compiler_debug"; - public static final OptionValidator JAVA_COMPILER_DEBUG = new BooleanValidator(JAVA_COMPILER_DEBUG_OPTION, true); - - public static final String JAVA_COMPILER_JANINO_MAXSIZE_OPTION = "exec.java_compiler_janino_maxsize"; - public static final OptionValidator JAVA_COMPILER_JANINO_MAXSIZE = new LongValidator(JAVA_COMPILER_JANINO_MAXSIZE_OPTION, 256*1024); +/** + * Per-compilation unit class loader that holds both caching and compilation + * steps. */ - public static final String JAVA_COMPILER_CONFIG = "drill.exec.compile.compiler"; - public static final String JAVA_COMPILER_DEBUG_CONFIG = "drill.exec.compile.debug"; - public static final String JAVA_COMPILER_JANINO_MAXSIZE_CONFIG = "drill.exec.compile.janino_maxsize"; +public class QueryClassLoader extends URLClassLoader { + static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(QueryClassLoader.class); private ClassCompilerSelector compilerSelector; @@ -75,7 +46,7 @@ public class QueryClassLoader extends URLClassLoader { public QueryClassLoader(DrillConfig config, OptionManager sessionOptions) { super(new URL[0], Thread.currentThread().getContextClassLoader()); - compilerSelector = new ClassCompilerSelector(config, sessionOptions); + compilerSelector = new ClassCompilerSelector(this, config, sessionOptions); } public long getNextClassIndex() { @@ -104,54 +75,4 @@ public class QueryClassLoader extends URLClassLoader { return compilerSelector.getClassByteCode(className, sourceCode); } - public enum CompilerPolicy { - DEFAULT, JDK, JANINO; - } - - private class ClassCompilerSelector { - private final CompilerPolicy policy; - private final long janinoThreshold; - - private final AbstractClassCompiler jdkClassCompiler; - private final AbstractClassCompiler janinoClassCompiler; - - - ClassCompilerSelector(DrillConfig config, OptionManager sessionOptions) { - OptionValue value = sessionOptions.getOption(JAVA_COMPILER_OPTION); - this.policy = CompilerPolicy.valueOf((value != null) ? value.string_val.toUpperCase() : config.getString(JAVA_COMPILER_CONFIG).toUpperCase()); - - value = sessionOptions.getOption(JAVA_COMPILER_JANINO_MAXSIZE_OPTION); - this.janinoThreshold = (value != null) ? value.num_val : config.getLong(JAVA_COMPILER_JANINO_MAXSIZE_CONFIG); - - value = sessionOptions.getOption(JAVA_COMPILER_DEBUG_OPTION); - boolean debug = (value != null) ? value.bool_val : config.getBoolean(JAVA_COMPILER_DEBUG_CONFIG); - - this.janinoClassCompiler = (policy == CompilerPolicy.JANINO || policy == CompilerPolicy.DEFAULT) ? new JaninoClassCompiler(QueryClassLoader.this, debug) : null; - this.jdkClassCompiler = (policy == CompilerPolicy.JDK || policy == CompilerPolicy.DEFAULT) ? JDKClassCompiler.newInstance(QueryClassLoader.this, debug) : null; - } - - private byte[][] getClassByteCode(ClassNames className, String sourceCode) - throws CompileException, ClassNotFoundException, ClassTransformationException, IOException { - AbstractClassCompiler classCompiler; - if (jdkClassCompiler != null && - (policy == CompilerPolicy.JDK || (policy == CompilerPolicy.DEFAULT && sourceCode.length() > janinoThreshold))) { - classCompiler = jdkClassCompiler; - } else { - classCompiler = janinoClassCompiler; - } - - byte[][] bc = classCompiler.getClassByteCode(className, sourceCode); - /* - * final String baseDir = System.getProperty("java.io.tmpdir") + File.separator + classCompiler.getClass().getSimpleName(); - * File classFile = new File(baseDir + className.clazz); - * classFile.getParentFile().mkdirs(); - * BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(classFile)); - * out.write(bc[0]); - * out.close(); - */ - return bc; - } - - } - } |