Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jarek/7015: overwrite java classes #7125

Merged
merged 4 commits into from
Apr 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ protected void doReloadEvaluator() {

public abstract ClassLoader getClassLoader();

public ClassLoader getClassLoaderForImport() {
return getClassLoader();
}

@Override
public List<Path> addJarsToClasspath(List<PathToJar> paths) {
LinkedList<Path> addedPaths = new LinkedList<>();
Expand All @@ -115,7 +119,7 @@ private boolean addJarToClasspath(PathToJar path) {

@Override
public AddImportStatus addImport(ImportPath anImport) {
AddImportStatus add = imports.add(anImport, getClassLoader());
AddImportStatus add = imports.add(anImport, getClassLoaderForImport());
if (AddImportStatus.ADDED.equals(add)) {
addImportToClassLoader(anImport);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@

import static com.twosigma.beakerx.util.Preconditions.checkNotNull;

public class BeakerxUrlClassLoader extends URLClassLoader {
public class BeakerXUrlClassLoader extends URLClassLoader {

public BeakerxUrlClassLoader(URL[] urls, ClassLoader parent) {
public BeakerXUrlClassLoader(URL[] urls, ClassLoader parent) {
super(urls, parent);
}

public BeakerxUrlClassLoader(ClassLoader parent) {
public BeakerXUrlClassLoader(ClassLoader parent) {
this(new URL[0], parent);
}

public BeakerxUrlClassLoader() {
public BeakerXUrlClassLoader() {
this(ClassLoader.getSystemClassLoader());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class DynamicClassLoaderSimple extends ClassLoader {

private final static Logger logger = LoggerFactory.getLogger(DynamicClassLoaderSimple.class.getName());

private BeakerxUrlClassLoader myloader = new BeakerxUrlClassLoader(new URL[0], null);
private BeakerXUrlClassLoader myloader = new BeakerXUrlClassLoader(new URL[0], null);
private String outDir;
protected final Map<String, Class<?>> classes;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.twosigma.beakerx.TryResult;
import com.twosigma.beakerx.autocomplete.AutocompleteResult;
import com.twosigma.beakerx.inspect.InspectResult;
import com.twosigma.beakerx.jvm.classloader.BeakerxUrlClassLoader;
import com.twosigma.beakerx.jvm.classloader.BeakerXUrlClassLoader;
import com.twosigma.beakerx.jvm.object.SimpleEvaluationObject;
import com.twosigma.beakerx.jvm.threads.CellExecutor;
import com.twosigma.beakerx.kernel.CacheFolderFactory;
Expand Down Expand Up @@ -48,7 +48,7 @@ public class EvaluatorTest extends BaseEvaluator {
private Classpath classpath = new Classpath();
private Imports imports = new Imports(new ArrayList<>());
private int resetEnvironmentCounter = 0;
private BeakerxUrlClassLoader loader = new BeakerxUrlClassLoader(Thread.currentThread().getContextClassLoader());
private BeakerXUrlClassLoader loader = new BeakerXUrlClassLoader(Thread.currentThread().getContextClassLoader());

public EvaluatorTest() {
this("idEvaluatorTest", "sIdEvaluatorTest", TestBeakerCellExecutor.cellExecutor(), KERNEL_PARAMETERS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.twosigma.beakerx.kernel;

import com.twosigma.beakerx.jvm.classloader.BeakerxUrlClassLoader;
import com.twosigma.beakerx.jvm.classloader.BeakerXUrlClassLoader;
import org.junit.Before;
import org.junit.Test;

Expand All @@ -26,12 +26,12 @@
public class ImportsTest {

private Imports imports;
private BeakerxUrlClassLoader urlClassLoader;
private BeakerXUrlClassLoader urlClassLoader;

@Before
public void setUp() throws Exception {
imports = new Imports(new ArrayList<>());
urlClassLoader = new BeakerxUrlClassLoader();
urlClassLoader = new BeakerXUrlClassLoader();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.twosigma.beakerx.groovy.evaluator;

import com.twosigma.beakerx.jvm.classloader.BeakerxUrlClassLoader;
import com.twosigma.beakerx.jvm.classloader.BeakerXUrlClassLoader;
import com.twosigma.beakerx.kernel.Classpath;
import com.twosigma.beakerx.kernel.ImportPath;
import com.twosigma.beakerx.kernel.Imports;
Expand Down Expand Up @@ -92,8 +92,8 @@ public static void addImportPathToImportCustomizer(ImportCustomizer icz, ImportP
}
}

public static BeakerxUrlClassLoader newParentClassLoader(Classpath classpath) {
BeakerxUrlClassLoader loader = new BeakerxUrlClassLoader(ClassLoader.getSystemClassLoader());
public static BeakerXUrlClassLoader newParentClassLoader(Classpath classpath) {
BeakerXUrlClassLoader loader = new BeakerXUrlClassLoader(ClassLoader.getSystemClassLoader());
loader.addPathToJars(classpath.getPaths());
return loader;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
import com.twosigma.beakerx.evaluator.TempFolderFactoryImpl;
import com.twosigma.beakerx.groovy.autocomplete.GroovyAutocomplete;
import com.twosigma.beakerx.groovy.autocomplete.GroovyClasspathScanner;
import com.twosigma.beakerx.inspect.Inspect;
import com.twosigma.beakerx.jvm.classloader.BeakerxUrlClassLoader;
import com.twosigma.beakerx.jvm.classloader.BeakerXUrlClassLoader;
import com.twosigma.beakerx.jvm.object.SimpleEvaluationObject;
import com.twosigma.beakerx.jvm.threads.BeakerCellExecutor;
import com.twosigma.beakerx.jvm.threads.CellExecutor;
Expand Down Expand Up @@ -54,7 +53,7 @@ public class GroovyEvaluator extends BaseEvaluator {
private GroovyClassLoader groovyClassLoader;
private Binding scriptBinding = null;
private ImportCustomizer icz;
private BeakerxUrlClassLoader beakerxUrlClassLoader;
private BeakerXUrlClassLoader beakerxUrlClassLoader;

public GroovyEvaluator(String id, String sId, EvaluatorParameters evaluatorParameters) {
this(id, sId, new BeakerCellExecutor("groovy"), new TempFolderFactoryImpl(), evaluatorParameters);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright 2018 TWO SIGMA OPEN SOURCE, LLC
*
* Licensed 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 com.twosigma.beakerx.javash;

import com.twosigma.beakerx.jvm.classloader.BeakerXUrlClassLoader;
import com.twosigma.beakerx.kernel.PathToJar;

import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;
import java.util.stream.Collectors;

import static java.util.Collections.singletonList;

public class JavaBeakerXUrlClassLoader extends URLClassLoader {

private BeakerXUrlClassLoader beakerxUrlClassLoader;
private JavaInternalUrlClassLoader javaURLClassLoader;

public JavaBeakerXUrlClassLoader(ClassLoader systemClassLoader, PathToJar pathToJar) {
super(new URL[0]);
beakerxUrlClassLoader = new BeakerXUrlClassLoader(this.getURLs(), systemClassLoader);
javaURLClassLoader = new JavaInternalUrlClassLoader(beakerxUrlClassLoader, new URL[0]);
javaURLClassLoader.addJar(pathToJar);
}

public void addJar(PathToJar pathToJar) {
javaURLClassLoader.addJar(pathToJar);
}

public void addInitJar(PathToJar pathToJar) {
beakerxUrlClassLoader.addJar(pathToJar);
}

public void addInitPathToJars(List<PathToJar> paths) {
for (PathToJar dir : paths) {
addInitJar(dir);
}
}

public Class<?> loadClass(String name) throws ClassNotFoundException {
Class<?> aClass = null;
try {
aClass = javaURLClassLoader.loadClass(name);
} catch (Exception e) {
}
if (aClass != null) {
return aClass;
}
return beakerxUrlClassLoader.loadClass(name);
}

public void resetClassloader() {
javaURLClassLoader = new JavaInternalUrlClassLoader(beakerxUrlClassLoader, javaURLClassLoader.getURLs());
}


public JavaInternalUrlClassLoader getJavaURLClassLoader() {
return javaURLClassLoader;
}

static class JavaInternalUrlClassLoader extends BeakerXUrlClassLoader {

private static final List<String> excludedClasses = singletonList("slf4j");

private BeakerXUrlClassLoader external;

public JavaInternalUrlClassLoader(BeakerXUrlClassLoader external, URL[] urls) {
super(urls, null);
this.external = external;
}

public Class<?> loadClass(String name) throws ClassNotFoundException {
if (isNotInExcludedClasses(name)) {
Class<?> aClass = null;
try {
aClass = super.loadClass(name);
} catch (Exception e) {
}
if (aClass != null) {
return aClass;
}
}
return external.loadClass(name);
}

private boolean isNotInExcludedClasses(String name) {
return excludedClasses.stream()
.filter(name::contains)
.collect(Collectors.toList())
.isEmpty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
package com.twosigma.beakerx.javash.autocomplete;

import com.twosigma.beakerx.autocomplete.ClassUtils;
import com.twosigma.beakerx.jvm.classloader.BeakerxUrlClassLoader;
import com.twosigma.beakerx.jvm.classloader.BeakerXUrlClassLoader;

import java.util.List;
import java.util.Optional;

public class JavaClassUtils extends ClassUtils {

private JavaClasspathScanner classpathscanner;
private BeakerxUrlClassLoader classLoader;
private BeakerXUrlClassLoader classLoader;

public JavaClassUtils(JavaClasspathScanner cps, ClassLoader l) {
super(l);
Expand All @@ -44,7 +44,7 @@ protected Class<?> getClass(String name) throws ClassNotFoundException {
if (fname != null) {
try {
if (classLoader == null)
classLoader = new BeakerxUrlClassLoader(loader != null ? loader : getClass().getClassLoader());
classLoader = new BeakerXUrlClassLoader(loader != null ? loader : getClass().getClassLoader());
Class<?> clazz = classLoader.parseClass(fname);
return clazz;
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private boolean compile(Codev codev, String classId, String ret) {
codev.javaSourceCode.append("}\n");

compilationUnit.addJavaSource(codev.getPname() + "." + JavaEvaluator.WRAPPER_CLASS_NAME + classId, codev.javaSourceCode.toString());
boolean compile = javaSourceCompiler.compile(compilationUnit);
boolean compile = javaSourceCompiler.compile(javaEvaluator.getClassLoader(), compilationUnit);
if (compile) {
javaSourceCompiler.persistCompiledClasses(compilationUnit);
return true;
Expand All @@ -168,6 +168,7 @@ private TryResult compileNewDefinitionClass(JobDescriptor j, Matcher m, Codev co
try {
javaSourceCompiler.compile(compilationUnit);
javaSourceCompiler.persistCompiledClasses(compilationUnit);
javaEvaluator.getJavaClassLoader().resetClassloader();
either = TryResult.createResult(codev.getPname() + "." + cname);
} catch (CompilationException e) {
either = TryResult.createError(buildErrorMessage(e, codev.lineNumbersMapping));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@

import com.twosigma.beakerx.TryResult;
import com.twosigma.beakerx.autocomplete.AutocompleteResult;
import com.twosigma.beakerx.autocomplete.ClasspathScanner;
import com.twosigma.beakerx.evaluator.BaseEvaluator;
import com.twosigma.beakerx.evaluator.JobDescriptor;
import com.twosigma.beakerx.evaluator.TempFolderFactory;
import com.twosigma.beakerx.evaluator.TempFolderFactoryImpl;
import com.twosigma.beakerx.javash.JavaBeakerXUrlClassLoader;
import com.twosigma.beakerx.javash.autocomplete.JavaAutocomplete;
import com.twosigma.beakerx.javash.autocomplete.JavaClasspathScanner;
import com.twosigma.beakerx.jvm.classloader.BeakerxUrlClassLoader;
import com.twosigma.beakerx.jvm.object.SimpleEvaluationObject;
import com.twosigma.beakerx.jvm.threads.BeakerCellExecutor;
import com.twosigma.beakerx.jvm.threads.CellExecutor;
Expand All @@ -43,7 +42,7 @@ public class JavaEvaluator extends BaseEvaluator {
private final String packageId;
private JavaClasspathScanner cps;
private JavaAutocomplete jac;
private BeakerxUrlClassLoader loader = null;
private JavaBeakerXUrlClassLoader loader = null;

public JavaEvaluator(String id, String sId, EvaluatorParameters evaluatorParameters) {
this(id, sId, new BeakerCellExecutor("javash"), new TempFolderFactoryImpl(), evaluatorParameters);
Expand All @@ -57,6 +56,11 @@ public JavaEvaluator(String id, String sId, CellExecutor cellExecutor, TempFolde
loader = newClassLoader();
}

@Override
public ClassLoader getClassLoaderForImport() {
return getJavaClassLoader().getJavaURLClassLoader();
}

@Override
protected void doResetEnvironment() {
String cpp = createClasspath(classPath, outDir);
Expand Down Expand Up @@ -126,13 +130,13 @@ public String getPackageId() {
return packageId;
}

private BeakerxUrlClassLoader newClassLoader() {
BeakerxUrlClassLoader loader = new BeakerxUrlClassLoader(ClassLoader.getSystemClassLoader());
loader.addPathToJars(getClasspath().getPaths());
private JavaBeakerXUrlClassLoader newClassLoader() {
JavaBeakerXUrlClassLoader loader = new JavaBeakerXUrlClassLoader(ClassLoader.getSystemClassLoader(),new PathToJar(outDir));
loader.addInitPathToJars(getClasspath().getPaths());
return loader;
}

public BeakerxUrlClassLoader getJavaClassLoader() {
public JavaBeakerXUrlClassLoader getJavaClassLoader() {
return loader;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,40 @@ public void evaluateIfStatement() throws Exception {
//then
assertThat((String) evaluate.result()).isEqualTo("AAA");
}

@Test
public void overwriteClass() throws Exception {
//given
runCode("" +
"package hello;\n" +
"public class Main {\n" +
" public static String doSth (){\n" +
" return \"hello\";\n" +
" }\n" +
"}");

runCode("" +
"package hello;\n" +
"return Main.doSth();");
//when
runCode("" +
"package hello;\n" +
"public class Main {\n" +
" public static String doSth (){\n" +
" return \"hello2\";\n" +
" }\n" +
"}");

TryResult result = runCode("" +
"package hello;\n" +
"return Main.doSth();");
//then
assertThat((String) result.result()).contains("hello2");
}

private TryResult runCode(String s) {
SimpleEvaluationObject seo = new SimpleEvaluationObject(s);
return javaEvaluator.evaluate(seo, s);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.twosigma.beakerx.evaluator.JobDescriptor;
import com.twosigma.beakerx.evaluator.TempFolderFactory;
import com.twosigma.beakerx.evaluator.TempFolderFactoryImpl;
import com.twosigma.beakerx.jvm.classloader.BeakerxUrlClassLoader;
import com.twosigma.beakerx.jvm.classloader.BeakerXUrlClassLoader;
import com.twosigma.beakerx.jvm.object.SimpleEvaluationObject;
import com.twosigma.beakerx.jvm.threads.BeakerCellExecutor;
import com.twosigma.beakerx.jvm.threads.CellExecutor;
Expand All @@ -49,7 +49,7 @@ public class KotlinEvaluator extends BaseEvaluator {
private ClasspathScanner cps;
private ReplInterpreter repl;
private ReplClassLoader loader = null;
private BeakerxUrlClassLoader kotlinClassLoader;
private BeakerXUrlClassLoader kotlinClassLoader;

public KotlinEvaluator(String id, String sId, EvaluatorParameters evaluatorParameters) {
this(id, sId, new BeakerCellExecutor("kotlin"), new TempFolderFactoryImpl(), evaluatorParameters);
Expand Down
Loading