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

jodd.proxetta.DefineClass Maven / Gradle / Ivy

Go to download

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);
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy