jodd.proxetta.DefineClass Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jodd-proxetta Show documentation
Show all versions of jodd-proxetta Show documentation
Jodd Proxetta is the fastest proxy creator with unique approach for defying pointcuts and advices.
The newest version!
package jodd.proxetta;
import jodd.util.ClassLoaderUtil;
import java.lang.reflect.Method;
public class DefineClass {
/**
* Defines a class from byte array into the specified class loader.
* Warning: this is a hack!
* @param className optional class name, may be null
* @param classData bytecode data
* @param classLoader classloader that will load class
*/
public static Class of(final String className, final byte[] classData, ClassLoader classLoader) {
if (classLoader == null) {
classLoader = ClassLoaderUtil.getDefaultClassLoader();
}
try {
final Method defineClassMethod = ClassLoader.class.getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class);
defineClassMethod.setAccessible(true);
return (Class) defineClassMethod.invoke(classLoader, className, classData, 0, classData.length);
} catch (final Throwable th) {
throw new RuntimeException("Define class failed: " + className, th);
}
}
}