ch.obermuhlner.scriptengine.java.MemoryClassLoader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-scriptengine Show documentation
Show all versions of java-scriptengine Show documentation
Java script engine for Java as a scripting language.
package ch.obermuhlner.scriptengine.java;
import java.util.Map;
/**
* A {@link ClassLoader} that loads classes from memory.
*/
public class MemoryClassLoader extends ClassLoader {
private Map mapClassBytes;
/**
* Creates a {@link MemoryClassLoader}.
*
* @param mapClassBytes the map of class names to compiled classes
* @param parent the parent {@link ClassLoader}
*/
public MemoryClassLoader(Map mapClassBytes, ClassLoader parent) {
super(parent);
this.mapClassBytes = mapClassBytes;
}
@Override
public Class> loadClass(String name) throws ClassNotFoundException {
byte[] bytes = mapClassBytes.get(name);
if (bytes == null) {
return super.loadClass(name);
}
return defineClass(name, bytes, 0, bytes.length);
}
}