Skip to content

Commit

Permalink
Fix #543 don't cast ClassLoader.getSystemClassLoader() to URLClassLoader
Browse files Browse the repository at this point in the history
- the user can decide if a secure ClassLoader should be used by setting
  Config.URL_CLASS_LOADER=...
- if Config.URL_CLASS_LOADER==null; ClassLoader.getSystemClassLoader()
  is used
- Refactored JavaLinkTests to core module as JavaLinkTestCase JUnit
  tests
- Loaded classes have to be available on the classpath
- Config.FILESYSTEM_ENABLED = true m,uist be set;see JUnit tests
  • Loading branch information
axkr committed Jun 20, 2022
1 parent ca841ec commit a59841c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.IOException;
import java.io.InputStream;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Properties;
Expand Down Expand Up @@ -594,7 +593,7 @@ public static String getVersion() {
public static int BUILTIN_PROTECTED = ISymbol.PROTECTED;

/** Global dynamic classloader */
public static URLClassLoader URL_CLASS_LOADER = null;
public static ClassLoader URL_CLASS_LOADER = null;

public static void setScriptCommandLine(final String[] args) {
IASTAppendable commandLine = F.ListAlloc(args.length + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.matheclipse.core.interfaces.IAST;
import org.matheclipse.core.interfaces.IExpr;
import org.matheclipse.core.interfaces.ISymbol;
import org.matheclipse.parser.client.ParserConfig;

public class JavaFunctions {
private static final Logger LOGGER = LogManager.getLogger();
Expand All @@ -38,15 +37,13 @@ private static class Initializer {

private static void init() {
if (!Config.FUZZY_PARSER) {
if (!ParserConfig.PARSER_USE_LOWERCASE_SYMBOLS) {
S.AddToClassPath.setEvaluator(new AddToClassPath());
S.InstanceOf.setEvaluator(new InstanceOf());
S.JavaNew.setEvaluator(new JavaNew());
S.JavaObject.setEvaluator(new JavaObject());
S.JavaObjectQ.setEvaluator(new JavaObjectQ());
S.LoadJavaClass.setEvaluator(new LoadJavaClass());
S.SameObjectQ.setEvaluator(new SameObjectQ());
}
}
}
}
Expand All @@ -57,9 +54,9 @@ private static class AddToClassPath extends AbstractEvaluator {
public IExpr evaluate(final IAST ast, EvalEngine engine) {
try {
if (Config.URL_CLASS_LOADER == null) {
Config.URL_CLASS_LOADER = (URLClassLoader) ClassLoader.getSystemClassLoader();
Config.URL_CLASS_LOADER = ClassLoader.getSystemClassLoader();
}
URLClassLoader child = Config.URL_CLASS_LOADER;
ClassLoader child = Config.URL_CLASS_LOADER;
for (int i = 1; i < ast.size(); i++) {
IExpr arg = ast.get(i);
if (arg.isString()) {
Expand Down Expand Up @@ -129,7 +126,7 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
if (arg2.isString()) {
try {
if (Config.URL_CLASS_LOADER == null) {
Config.URL_CLASS_LOADER = (URLClassLoader) ClassLoader.getSystemClassLoader();
Config.URL_CLASS_LOADER = ClassLoader.getSystemClassLoader();
}
arg2 = JavaClassExpr.newInstance(arg2.toString(), Config.URL_CLASS_LOADER);
} catch (ClassNotFoundException cnfex) {
Expand Down Expand Up @@ -205,7 +202,7 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
if (arg1.isString()) {
try {
if (Config.URL_CLASS_LOADER == null) {
Config.URL_CLASS_LOADER = (URLClassLoader) ClassLoader.getSystemClassLoader();
Config.URL_CLASS_LOADER = ClassLoader.getSystemClassLoader();
}
arg1 = JavaClassExpr.newInstance(arg1.toString(), Config.URL_CLASS_LOADER);
} catch (ClassNotFoundException cnfex) {
Expand Down Expand Up @@ -444,7 +441,7 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
try {
String className = arg1.toString();
if (Config.URL_CLASS_LOADER == null) {
Config.URL_CLASS_LOADER = (URLClassLoader) ClassLoader.getSystemClassLoader();
Config.URL_CLASS_LOADER = ClassLoader.getSystemClassLoader();
}
JavaClassExpr jClazz = JavaClassExpr.newInstance(className, Config.URL_CLASS_LOADER);
Class<?> clazz = jClazz.toData();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package org.matheclipse.core.javalink;
package org.matheclipse.core.system;

import org.matheclipse.core.basic.Config;
import org.matheclipse.core.basic.ToggleFeature;
import org.matheclipse.core.eval.EvalEngine;
import org.matheclipse.core.interfaces.ISymbol;
import org.matheclipse.core.rubi.AbstractRubiTestCase;
import org.matheclipse.io.IOInit;
import org.matheclipse.core.eval.ExprEvaluator;
import org.matheclipse.core.expression.F;
import org.matheclipse.parser.client.ParserConfig;

public class JavaLinkTests extends AbstractRubiTestCase {
public class JavaLinkTestCase extends ExprEvaluatorTestCase {

public JavaLinkTests(String name) {
super(name, false);
public JavaLinkTestCase(String name) {
super(name);
}

public void testJavaNew001() {
Expand All @@ -19,7 +20,7 @@ public void testJavaNew001() {
+ "dm = JavaNew[\"java.text.DecimalFormat\", \"#.00\", ds]", //
"JavaObject[class java.text.DecimalFormat]");
check("dm@format[0.815]", //
"\".81\"");
".81");
}

public void testInstanceOf001() {
Expand All @@ -39,19 +40,20 @@ public void testLoadJavaClass001() {
check("clazz= LoadJavaClass[\"java.lang.Math\"]", //
"JavaClass[java.lang.Math]");
check("Math`sin[0.5]", //
"0.479425538604203");
"0.479426");
}

public void testLoadJavaClass002() {
check("clazz= LoadJavaClass[\"org.jsoup.Jsoup\"]", //
"JavaClass[org.jsoup.Jsoup]");
check("conn=Jsoup`connect[\"https://jsoup.org/\"];", //
"Null");
check("doc=conn@get[ ];", //
"Null");
check("Print[doc@title[ ]];", //
"Null");
}
// will only work if JSoup is on classpath
// public void testLoadJavaClass002() {
// check("clazz= LoadJavaClass[\"org.jsoup.Jsoup\"]", //
// "JavaClass[org.jsoup.Jsoup]");
// check("conn=Jsoup`connect[\"https://jsoup.org/\"];", //
// "Null");
// check("doc=conn@get[ ];", //
// "Null");
// check("Print[doc@title[ ]];", //
// "Null");
// }

public void testJavaObjectQ001() {

Expand Down Expand Up @@ -85,13 +87,34 @@ public void testJavaShow001() {
/** The JUnit setup method */
@Override
protected void setUp() {
Config.BUILTIN_PROTECTED = ISymbol.NOATTRIBUTE;
super.setUp();
fSeconds = 600;
Config.SHORTEN_STRING_LENGTH = 1024;
Config.MAX_AST_SIZE = 1000000;
EvalEngine.get().setIterationLimit(50000);
IOInit.init();
try {
synchronized (fScriptManager) {
ToggleFeature.COMPILE = true;
ToggleFeature.COMPILE_PRINT = true;
Config.SHORTEN_STRING_LENGTH = 80;
Config.MAX_AST_SIZE = 20000;
Config.MAX_MATRIX_DIMENSION_SIZE = 100;
Config.MAX_BIT_LENGTH = 200000;
Config.MAX_POLYNOMIAL_DEGREE = 100;
Config.FILESYSTEM_ENABLED = true;
// if you need MMA syntax set relaxedSyntax = false;
boolean relaxedSyntax = true;
ParserConfig.PARSER_USE_LOWERCASE_SYMBOLS = relaxedSyntax;
F.await();

EvalEngine engine = new EvalEngine(relaxedSyntax);
EvalEngine.set(engine);
engine.init();
engine.setRecursionLimit(512);
engine.setIterationLimit(500);
engine.setOutListDisabled(false, (short) 10);

evaluator = new ExprEvaluator(engine, false, (short) 100);
evaluatorN = new ExprEvaluator(engine, false, (short) 100);
}
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
Expand Down

0 comments on commit a59841c

Please sign in to comment.