Map<String,Method> functions = Collections.singletonMap("bar", barMethod);
assertNotSame(thisLoader, fooClass.getClassLoader());
assertNotSame(thisLoader, barMethod.getDeclaringClass().getClassLoader());
// this should pass:
Expression expr = JavascriptCompiler.compile("bar()", functions, childLoader);
assertEquals(2.0, expr.evaluate(0, null), DELTA);
// use our classloader, not the foreign one, which should fail!
try {
JavascriptCompiler.compile("bar()", functions, thisLoader);
fail();
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("is not declared by a class which is accessible by the given parent ClassLoader"));
}
// mix foreign and default functions
Map<String,Method> mixedFunctions = new HashMap<String,Method>(JavascriptCompiler.DEFAULT_FUNCTIONS);
mixedFunctions.putAll(functions);
expr = JavascriptCompiler.compile("bar()", mixedFunctions, childLoader);
assertEquals(2.0, expr.evaluate(0, null), DELTA);
expr = JavascriptCompiler.compile("sqrt(20)", mixedFunctions, childLoader);
assertEquals(Math.sqrt(20), expr.evaluate(0, null), DELTA);
// use our classloader, not the foreign one, which should fail!
try {
JavascriptCompiler.compile("bar()", mixedFunctions, thisLoader);
fail();