This is an automated email from the ASF dual-hosted git repository.

libenchao pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/main by this push:
     new 324bcb0833 [CALCITE-5442] Tweak janino code generation in 
EnumerableInterpretable to allow debugging
324bcb0833 is described below

commit 324bcb08334336b219d62957053fc0c1a5e7591d
Author: Benchao Li <[email protected]>
AuthorDate: Thu Jan 26 15:12:50 2023 +0800

    [CALCITE-5442] Tweak janino code generation in EnumerableInterpretable to 
allow debugging
    
    Close apache/calcite#3045
---
 .../enumerable/EnumerableInterpretable.java        | 46 ++++++++++++++--------
 1 file changed, 29 insertions(+), 17 deletions(-)

diff --git 
a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableInterpretable.java
 
b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableInterpretable.java
index 73079b1b47..c156652304 100644
--- 
a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableInterpretable.java
+++ 
b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableInterpretable.java
@@ -42,7 +42,6 @@ import org.apache.calcite.runtime.ArrayBindable;
 import org.apache.calcite.runtime.Bindable;
 import org.apache.calcite.runtime.Hook;
 import org.apache.calcite.runtime.Typed;
-import org.apache.calcite.runtime.Utilities;
 import org.apache.calcite.util.Util;
 
 import com.google.common.cache.Cache;
@@ -51,11 +50,10 @@ import com.google.common.cache.CacheBuilder;
 import org.checkerframework.checker.nullness.qual.Nullable;
 import org.codehaus.commons.compiler.CompileException;
 import org.codehaus.commons.compiler.CompilerFactoryFactory;
-import org.codehaus.commons.compiler.IClassBodyEvaluator;
 import org.codehaus.commons.compiler.ICompilerFactory;
+import org.codehaus.commons.compiler.ISimpleCompiler;
 
-import java.io.IOException;
-import java.io.StringReader;
+import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Modifier;
 import java.util.List;
 import java.util.Map;
@@ -132,8 +130,9 @@ public class EnumerableInterpretable extends ConverterImpl
     }
   }
 
-  static Bindable getBindable(ClassDeclaration expr, String s, int fieldCount)
-      throws CompileException, IOException, ExecutionException {
+  static Bindable getBindable(ClassDeclaration expr, String classBody, int 
fieldCount)
+      throws CompileException, ExecutionException, ClassNotFoundException,
+      InvocationTargetException, InstantiationException, 
IllegalAccessException {
     ICompilerFactory compilerFactory;
     ClassLoader classLoader =
         Objects.requireNonNull(EnumerableInterpretable.class.getClassLoader(), 
"classLoader");
@@ -143,27 +142,40 @@ public class EnumerableInterpretable extends ConverterImpl
       throw new IllegalStateException(
           "Unable to instantiate java compiler", e);
     }
-    final IClassBodyEvaluator cbe = compilerFactory.newClassBodyEvaluator();
-    cbe.setClassName(expr.name);
-    cbe.setExtendedClass(Utilities.class);
-    cbe.setImplementedInterfaces(
-        fieldCount == 1
-            ? new Class[] {Bindable.class, Typed.class}
-            : new Class[] {ArrayBindable.class});
-    cbe.setParentClassLoader(classLoader);
+    final ISimpleCompiler compiler = compilerFactory.newSimpleCompiler();
+    compiler.setParentClassLoader(classLoader);
+    final String s = "public final class " + expr.name + " implements "
+        + (fieldCount == 1
+          ? Bindable.class.getCanonicalName() + ", " + 
Typed.class.getCanonicalName()
+          : ArrayBindable.class.getCanonicalName())
+        + " {\n"
+        + classBody
+        + "\n"
+        + "}";
+
     if (CalciteSystemProperty.DEBUG.value()) {
       // Add line numbers to the generated janino class
-      cbe.setDebuggingInformation(true, true, true);
+      compiler.setDebuggingInformation(true, true, true);
     }
 
     if (CalciteSystemProperty.BINDABLE_CACHE_MAX_SIZE.value() != 0) {
       StaticFieldDetector detector = new StaticFieldDetector();
       expr.accept(detector);
       if (!detector.containsStaticField) {
-        return BINDABLE_CACHE.get(s, () -> (Bindable) cbe.createInstance(new 
StringReader(s)));
+        return BINDABLE_CACHE.get(classBody, () ->  
compileToBindable(expr.name, s, compiler));
       }
     }
-    return (Bindable) cbe.createInstance(new StringReader(s));
+    return compileToBindable(expr.name, s, compiler);
+  }
+
+  private static Bindable<?> compileToBindable(String className, String s, 
ISimpleCompiler compiler)
+      throws CompileException, ClassNotFoundException, 
InvocationTargetException,
+      InstantiationException, IllegalAccessException {
+    compiler.cook(s);
+    return (Bindable<?>) compiler.getClassLoader()
+        .loadClass(className)
+        .getDeclaredConstructors()[0]
+        .newInstance();
   }
 
   /**

Reply via email to