Skip to content

Commit a59841c

Browse files
committed
Fix #543 don't cast ClassLoader.getSystemClassLoader() to URLClassLoader
- 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
1 parent ca841ec commit a59841c

3 files changed

Lines changed: 55 additions & 36 deletions

File tree

symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/basic/Config.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.io.IOException;
44
import java.io.InputStream;
5-
import java.net.URLClassLoader;
65
import java.util.ArrayList;
76
import java.util.HashSet;
87
import java.util.Properties;
@@ -594,7 +593,7 @@ public static String getVersion() {
594593
public static int BUILTIN_PROTECTED = ISymbol.PROTECTED;
595594

596595
/** Global dynamic classloader */
597-
public static URLClassLoader URL_CLASS_LOADER = null;
596+
public static ClassLoader URL_CLASS_LOADER = null;
598597

599598
public static void setScriptCommandLine(final String[] args) {
600599
IASTAppendable commandLine = F.ListAlloc(args.length + 1);

symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/builtin/JavaFunctions.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.matheclipse.core.interfaces.IAST;
2626
import org.matheclipse.core.interfaces.IExpr;
2727
import org.matheclipse.core.interfaces.ISymbol;
28-
import org.matheclipse.parser.client.ParserConfig;
2928

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

3938
private static void init() {
4039
if (!Config.FUZZY_PARSER) {
41-
if (!ParserConfig.PARSER_USE_LOWERCASE_SYMBOLS) {
4240
S.AddToClassPath.setEvaluator(new AddToClassPath());
4341
S.InstanceOf.setEvaluator(new InstanceOf());
4442
S.JavaNew.setEvaluator(new JavaNew());
4543
S.JavaObject.setEvaluator(new JavaObject());
4644
S.JavaObjectQ.setEvaluator(new JavaObjectQ());
4745
S.LoadJavaClass.setEvaluator(new LoadJavaClass());
4846
S.SameObjectQ.setEvaluator(new SameObjectQ());
49-
}
5047
}
5148
}
5249
}
@@ -57,9 +54,9 @@ private static class AddToClassPath extends AbstractEvaluator {
5754
public IExpr evaluate(final IAST ast, EvalEngine engine) {
5855
try {
5956
if (Config.URL_CLASS_LOADER == null) {
60-
Config.URL_CLASS_LOADER = (URLClassLoader) ClassLoader.getSystemClassLoader();
57+
Config.URL_CLASS_LOADER = ClassLoader.getSystemClassLoader();
6158
}
62-
URLClassLoader child = Config.URL_CLASS_LOADER;
59+
ClassLoader child = Config.URL_CLASS_LOADER;
6360
for (int i = 1; i < ast.size(); i++) {
6461
IExpr arg = ast.get(i);
6562
if (arg.isString()) {
@@ -129,7 +126,7 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
129126
if (arg2.isString()) {
130127
try {
131128
if (Config.URL_CLASS_LOADER == null) {
132-
Config.URL_CLASS_LOADER = (URLClassLoader) ClassLoader.getSystemClassLoader();
129+
Config.URL_CLASS_LOADER = ClassLoader.getSystemClassLoader();
133130
}
134131
arg2 = JavaClassExpr.newInstance(arg2.toString(), Config.URL_CLASS_LOADER);
135132
} catch (ClassNotFoundException cnfex) {
@@ -205,7 +202,7 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
205202
if (arg1.isString()) {
206203
try {
207204
if (Config.URL_CLASS_LOADER == null) {
208-
Config.URL_CLASS_LOADER = (URLClassLoader) ClassLoader.getSystemClassLoader();
205+
Config.URL_CLASS_LOADER = ClassLoader.getSystemClassLoader();
209206
}
210207
arg1 = JavaClassExpr.newInstance(arg1.toString(), Config.URL_CLASS_LOADER);
211208
} catch (ClassNotFoundException cnfex) {
@@ -444,7 +441,7 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
444441
try {
445442
String className = arg1.toString();
446443
if (Config.URL_CLASS_LOADER == null) {
447-
Config.URL_CLASS_LOADER = (URLClassLoader) ClassLoader.getSystemClassLoader();
444+
Config.URL_CLASS_LOADER = ClassLoader.getSystemClassLoader();
448445
}
449446
JavaClassExpr jClazz = JavaClassExpr.newInstance(className, Config.URL_CLASS_LOADER);
450447
Class<?> clazz = jClazz.toData();

symja_android_library/matheclipse-io/src/test/java/org/matheclipse/core/javalink/JavaLinkTests.java renamed to symja_android_library/matheclipse-core/src/test/java/org/matheclipse/core/system/JavaLinkTestCase.java

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
package org.matheclipse.core.javalink;
1+
package org.matheclipse.core.system;
22

33
import org.matheclipse.core.basic.Config;
4+
import org.matheclipse.core.basic.ToggleFeature;
45
import org.matheclipse.core.eval.EvalEngine;
5-
import org.matheclipse.core.interfaces.ISymbol;
6-
import org.matheclipse.core.rubi.AbstractRubiTestCase;
7-
import org.matheclipse.io.IOInit;
6+
import org.matheclipse.core.eval.ExprEvaluator;
7+
import org.matheclipse.core.expression.F;
8+
import org.matheclipse.parser.client.ParserConfig;
89

9-
public class JavaLinkTests extends AbstractRubiTestCase {
10+
public class JavaLinkTestCase extends ExprEvaluatorTestCase {
1011

11-
public JavaLinkTests(String name) {
12-
super(name, false);
12+
public JavaLinkTestCase(String name) {
13+
super(name);
1314
}
1415

1516
public void testJavaNew001() {
@@ -19,7 +20,7 @@ public void testJavaNew001() {
1920
+ "dm = JavaNew[\"java.text.DecimalFormat\", \"#.00\", ds]", //
2021
"JavaObject[class java.text.DecimalFormat]");
2122
check("dm@format[0.815]", //
22-
"\".81\"");
23+
".81");
2324
}
2425

2526
public void testInstanceOf001() {
@@ -39,19 +40,20 @@ public void testLoadJavaClass001() {
3940
check("clazz= LoadJavaClass[\"java.lang.Math\"]", //
4041
"JavaClass[java.lang.Math]");
4142
check("Math`sin[0.5]", //
42-
"0.479425538604203");
43+
"0.479426");
4344
}
4445

45-
public void testLoadJavaClass002() {
46-
check("clazz= LoadJavaClass[\"org.jsoup.Jsoup\"]", //
47-
"JavaClass[org.jsoup.Jsoup]");
48-
check("conn=Jsoup`connect[\"https://jsoup.org/\"];", //
49-
"Null");
50-
check("doc=conn@get[ ];", //
51-
"Null");
52-
check("Print[doc@title[ ]];", //
53-
"Null");
54-
}
46+
// will only work if JSoup is on classpath
47+
// public void testLoadJavaClass002() {
48+
// check("clazz= LoadJavaClass[\"org.jsoup.Jsoup\"]", //
49+
// "JavaClass[org.jsoup.Jsoup]");
50+
// check("conn=Jsoup`connect[\"https://jsoup.org/\"];", //
51+
// "Null");
52+
// check("doc=conn@get[ ];", //
53+
// "Null");
54+
// check("Print[doc@title[ ]];", //
55+
// "Null");
56+
// }
5557

5658
public void testJavaObjectQ001() {
5759

@@ -85,13 +87,34 @@ public void testJavaShow001() {
8587
/** The JUnit setup method */
8688
@Override
8789
protected void setUp() {
88-
Config.BUILTIN_PROTECTED = ISymbol.NOATTRIBUTE;
89-
super.setUp();
90-
fSeconds = 600;
91-
Config.SHORTEN_STRING_LENGTH = 1024;
92-
Config.MAX_AST_SIZE = 1000000;
93-
EvalEngine.get().setIterationLimit(50000);
94-
IOInit.init();
90+
try {
91+
synchronized (fScriptManager) {
92+
ToggleFeature.COMPILE = true;
93+
ToggleFeature.COMPILE_PRINT = true;
94+
Config.SHORTEN_STRING_LENGTH = 80;
95+
Config.MAX_AST_SIZE = 20000;
96+
Config.MAX_MATRIX_DIMENSION_SIZE = 100;
97+
Config.MAX_BIT_LENGTH = 200000;
98+
Config.MAX_POLYNOMIAL_DEGREE = 100;
99+
Config.FILESYSTEM_ENABLED = true;
100+
// if you need MMA syntax set relaxedSyntax = false;
101+
boolean relaxedSyntax = true;
102+
ParserConfig.PARSER_USE_LOWERCASE_SYMBOLS = relaxedSyntax;
103+
F.await();
104+
105+
EvalEngine engine = new EvalEngine(relaxedSyntax);
106+
EvalEngine.set(engine);
107+
engine.init();
108+
engine.setRecursionLimit(512);
109+
engine.setIterationLimit(500);
110+
engine.setOutListDisabled(false, (short) 10);
111+
112+
evaluator = new ExprEvaluator(engine, false, (short) 100);
113+
evaluatorN = new ExprEvaluator(engine, false, (short) 100);
114+
}
115+
} catch (Exception e) {
116+
e.printStackTrace();
117+
}
95118
}
96119

97120
@Override

0 commit comments

Comments
 (0)