Skip to content

Commit

Permalink
[CALCITE-3745] CompileException in UnitCompiler when using multiple c…
Browse files Browse the repository at this point in the history
…lass loaders

Janino is not able to determine the correct compiler for loading a
given class thus it fails with CompileException.

1. Use the class loader of the calling class instead of the default.
2. Upgrade Janino to 3.1.6 version which contains the fix for
janino-compiler/janino#150 necessary for
passing correctly the class loader to the underlying methods.
3. Add/Update CheckerFramework .astub files for setParentClassLoader
method based on the changes in the Janino APIs. Methods have moved in
the new version so it is necessary to reflect the changes in the stub
files.
4. Enforce class loader is not null before calling
CompilerFactoryFactory#getDefaultCompilerFactory method. The parameter
does not have a nullability annotation so by default CheckerFramework
considers it as not-null. Indeed, if null is given as parameter a NPE
will be raised from within that method.

Close apache#2449
  • Loading branch information
Bruce Irschick authored and zabetak committed Oct 12, 2021
1 parent 460de04 commit 9c0e313
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ExecutionException;

/**
Expand Down Expand Up @@ -134,8 +135,10 @@ public static Bindable toBindable(Map<String, Object> parameters,
static Bindable getBindable(ClassDeclaration expr, String s, int fieldCount)
throws CompileException, IOException, ExecutionException {
ICompilerFactory compilerFactory;
ClassLoader classLoader =
Objects.requireNonNull(EnumerableInterpretable.class.getClassLoader(), "classLoader");
try {
compilerFactory = CompilerFactoryFactory.getDefaultCompilerFactory();
compilerFactory = CompilerFactoryFactory.getDefaultCompilerFactory(classLoader);
} catch (Exception e) {
throw new IllegalStateException(
"Unable to instantiate java compiler", e);
Expand All @@ -147,7 +150,7 @@ static Bindable getBindable(ClassDeclaration expr, String s, int fieldCount)
fieldCount == 1
? new Class[] {Bindable.class, Typed.class}
: new Class[] {ArrayBindable.class});
cbe.setParentClassLoader(EnumerableInterpretable.class.getClassLoader());
cbe.setParentClassLoader(classLoader);
if (CalciteSystemProperty.DEBUG.value()) {
// Add line numbers to the generated janino class
cbe.setDebuggingInformation(true, true, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* Compiles a scalar expression ({@link RexNode}) to an expression that
Expand Down Expand Up @@ -199,16 +200,18 @@ static Scalar.Producer baz(ParameterExpression context_,
static Scalar.Producer getScalar(ClassDeclaration expr, String s)
throws CompileException, IOException {
ICompilerFactory compilerFactory;
ClassLoader classLoader =
Objects.requireNonNull(JaninoRexCompiler.class.getClassLoader(), "classLoader");
try {
compilerFactory = CompilerFactoryFactory.getDefaultCompilerFactory();
compilerFactory = CompilerFactoryFactory.getDefaultCompilerFactory(classLoader);
} catch (Exception e) {
throw new IllegalStateException(
"Unable to instantiate java compiler", e);
}
IClassBodyEvaluator cbe = compilerFactory.newClassBodyEvaluator();
cbe.setClassName(expr.name);
cbe.setImplementedInterfaces(new Class[] {Scalar.Producer.class});
cbe.setParentClassLoader(JaninoRexCompiler.class.getClassLoader());
cbe.setParentClassLoader(classLoader);
if (CalciteSystemProperty.DEBUG.value()) {
// Add line numbers to the generated janino class
cbe.setDebuggingInformation(true, true, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ExecutionException;

/**
Expand Down Expand Up @@ -298,8 +299,10 @@ static <M extends Metadata> MetadataHandler<M> compile(String className,
String classBody, MetadataDef<M> def,
List<Object> argList) throws CompileException, IOException {
final ICompilerFactory compilerFactory;
ClassLoader classLoader =
Objects.requireNonNull(JaninoRelMetadataProvider.class.getClassLoader(), "classLoader");
try {
compilerFactory = CompilerFactoryFactory.getDefaultCompilerFactory();
compilerFactory = CompilerFactoryFactory.getDefaultCompilerFactory(classLoader);
} catch (Exception e) {
throw new IllegalStateException(
"Unable to instantiate java compiler", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import org.apache.calcite.config.CalciteSystemProperty;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.codehaus.commons.compiler.util.resource.MapResourceFinder;
import org.codehaus.commons.compiler.util.resource.ResourceFinder;
import org.codehaus.janino.JavaSourceClassLoader;
import org.codehaus.janino.util.ClassFile;
import org.codehaus.janino.util.resource.MapResourceFinder;
import org.codehaus.janino.util.resource.ResourceFinder;

import java.io.File;
import java.io.FileOutputStream;
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ immutables.version=2.8.8
innodb-java-reader.version=1.0.10
jackson-databind.version=2.9.10.1
jackson.version=2.10.0
janino.version=3.0.11
janino.version=3.1.6
java-diff.version=1.1.2
jcip-annotations.version=1.0-1
jcommander.version=1.72
Expand Down
23 changes: 23 additions & 0 deletions src/main/config/checkerframework/janino/ClassBodyEvaluator.astub
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.codehaus.janino;

import org.checkerframework.checker.nullness.qual.*;

public class ClassBodyEvaluator extends Cookable implements IClassBodyEvaluator {
public void setParentClassLoader(@Nullable ClassLoader parentClassLoader);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.codehaus.janino;
package org.codehaus.commons.compiler;

import org.checkerframework.checker.nullness.qual.*;

class SimpleCompiler {
public interface IClassBodyEvaluator extends ICookable {
void setParentClassLoader(@Nullable ClassLoader optionalParentClassLoader);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ package org.codehaus.commons.compiler;

import org.checkerframework.checker.nullness.qual.*;

interface ICookable {
public interface ISimpleCompiler {
void setParentClassLoader(@Nullable ClassLoader optionalParentClassLoader);
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ public void setup() {

ICompilerFactory compilerFactory;
try {
compilerFactory = CompilerFactoryFactory.getDefaultCompilerFactory();
compilerFactory = CompilerFactoryFactory.getDefaultCompilerFactory(
CodeGenerationBenchmark.class.getClassLoader());
} catch (Exception e) {
throw new IllegalStateException(
"Unable to instantiate java compiler", e);
Expand Down

0 comments on commit 9c0e313

Please sign in to comment.