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

serp.bytecode.BCClassLoader Maven / Gradle / Ivy

There is a newer version: 4.0.1
Show newest version
package serp.bytecode;

/**
 * Class loader that will attempt to find requested classes in a given
 * {@link Project}.
 *
 * @author Abe White
 */
public class BCClassLoader extends ClassLoader {
    private Project _project = null;

    /**
     * Constructor. Supply the project to use when looking for classes.
     */
    public BCClassLoader(Project project) {
        _project = project;
    }

    /**
     * Constructor. Supply the project to use when looking for classes.
     *
     * @param parent the parent classoader
     */
    public BCClassLoader(Project project, ClassLoader loader) {
        super(loader);
        _project = project;
    }

    /**
     * Return this class loader's project.
     */
    public Project getProject() {
        return _project;
    }

    protected Class findClass(String name) throws ClassNotFoundException {
        byte[] bytes;
        try {
            BCClass type;
            if (!_project.containsClass(name))
                type = createClass(name);
            else
                type = _project.loadClass(name);
            if (type == null)
                throw new ClassNotFoundException(name);
            bytes = type.toByteArray();
        } catch (RuntimeException re) {
            throw new ClassNotFoundException(re.toString());
        }
        return defineClass(name, bytes, 0, bytes.length);
    }

    /**
     * Override this method if unfound classes should be created on-the-fly.
     * Returns null by default.
     */
    protected BCClass createClass(String name) {
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy