All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ch.obermuhlner.scriptengine.java.MemoryClassLoader Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
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);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy