Implemented issue: #65
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -242,7 +242,26 @@ public class LuajavaLib extends VarArgFunction {
|
|||||||
|
|
||||||
// load classes using app loader to allow luaj to be used as an extension
|
// load classes using app loader to allow luaj to be used as an extension
|
||||||
protected Class classForName(String name) throws ClassNotFoundException {
|
protected Class classForName(String name) throws ClassNotFoundException {
|
||||||
Class clazz = Class.forName(name, true, ClassLoader.getSystemClassLoader());
|
Class clazz = null;
|
||||||
|
ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
|
||||||
|
if (contextLoader != null) {
|
||||||
|
try {
|
||||||
|
clazz = Class.forName(name, true, contextLoader);
|
||||||
|
} catch (ClassNotFoundException ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (clazz == null) {
|
||||||
|
ClassLoader libraryLoader = getClass().getClassLoader();
|
||||||
|
if (libraryLoader != null) {
|
||||||
|
try {
|
||||||
|
clazz = Class.forName(name, true, libraryLoader);
|
||||||
|
} catch (ClassNotFoundException ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (clazz == null) {
|
||||||
|
clazz = Class.forName(name, true, ClassLoader.getSystemClassLoader());
|
||||||
|
}
|
||||||
if (!isClassVisible(clazz))
|
if (!isClassVisible(clazz))
|
||||||
throw new ClassNotFoundException("class is not visible: " + name);
|
throw new ClassNotFoundException("class is not visible: " + name);
|
||||||
return clazz;
|
return clazz;
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package org.luaj.vm2.libs.jse;
|
||||||
|
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
|
public class LuajavaLibClassLoaderTest extends TestCase {
|
||||||
|
|
||||||
|
public void testClassForNameFallsBackToLibraryLoader() throws Exception {
|
||||||
|
ExposedLuajavaLib lib = new ExposedLuajavaLib();
|
||||||
|
ClassLoader previous = Thread.currentThread().getContextClassLoader();
|
||||||
|
Thread.currentThread().setContextClassLoader(new RejectingClassLoader());
|
||||||
|
try {
|
||||||
|
Class clazz = lib.exposedClassForName(LocalVisibleClass.class.getName());
|
||||||
|
assertEquals(LocalVisibleClass.class, clazz);
|
||||||
|
} finally {
|
||||||
|
Thread.currentThread().setContextClassLoader(previous);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static final class ExposedLuajavaLib extends LuajavaLib {
|
||||||
|
Class exposedClassForName(String name) throws ClassNotFoundException {
|
||||||
|
return classForName(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static final class RejectingClassLoader extends ClassLoader {
|
||||||
|
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
|
||||||
|
if (LocalVisibleClass.class.getName().equals(name)) {
|
||||||
|
throw new ClassNotFoundException(name);
|
||||||
|
}
|
||||||
|
return super.loadClass(name, resolve);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final class LocalVisibleClass {
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user