![JAR search and dependency download from the Maven repository](/logo.png)
fi.jumi.core.util.LocallyDefiningClassLoader Maven / Gradle / Ivy
// Copyright © 2011-2013, Esko Luontola
// This software is released under the Apache License 2.0.
// The license text is at http://www.apache.org/licenses/LICENSE-2.0
package fi.jumi.core.util;
import org.apache.commons.io.IOUtils;
import javax.annotation.concurrent.ThreadSafe;
import java.io.*;
@ThreadSafe
public class LocallyDefiningClassLoader extends ClassLoader {
private final String classNamePrefix;
public LocallyDefiningClassLoader(String classNamePrefix, ClassLoader parent) {
super(parent);
this.classNamePrefix = classNamePrefix;
}
@Override
public Class> loadClass(String name) throws ClassNotFoundException {
if (name.startsWith(classNamePrefix)) {
byte[] bytes = getBytecode(name);
if (bytes != null) {
defineClass(name, bytes, 0, bytes.length);
}
}
return super.loadClass(name);
}
private byte[] getBytecode(String name) {
String resource = name.replace('.', '/') + ".class";
InputStream in = getResourceAsStream(resource);
if (in == null) {
return null;
}
try {
return IOUtils.toByteArray(in);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
IOUtils.closeQuietly(in);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy