proguard.classfile.util.ClassPoolClassLoader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of proguard-core Show documentation
Show all versions of proguard-core Show documentation
ProGuardCORE is a free library to read, analyze, modify, and write Java class files.
package proguard.classfile.util;
import static proguard.classfile.util.ClassUtil.internalClassName;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import proguard.classfile.ClassPool;
import proguard.classfile.Clazz;
import proguard.classfile.io.ProgramClassWriter;
import proguard.classfile.visitor.ProgramClassFilter;
/**
* A {@link ClassLoader} that can load classes from a ProGuardCORE classpool.
*
* @author James Hamilton
*/
public class ClassPoolClassLoader extends ClassLoader {
private final ClassPool classPool;
public ClassPoolClassLoader(ClassPool classPool) {
this.classPool = classPool;
}
@Override
public Class> findClass(String name) throws ClassNotFoundException {
Clazz clazz = classPool.getClass(internalClassName(name));
if (clazz == null) {
throw new ClassNotFoundException("Class " + name + " not found in class pool");
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
clazz.accept(
new ProgramClassFilter(
new ProgramClassWriter(new DataOutputStream(byteArrayOutputStream))));
byte[] bytes = byteArrayOutputStream.toByteArray();
return defineClass(name, bytes, 0, bytes.length);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy