As Thomas stated, you can reflectively invoke the protected defineClass()
method of the ClassLoader into which you would like to load your class.
Here's an example of how this could be achieved:
public static Class<?> loadClass(byte[] code, ClassLoader loadInto) throws InvocationTargetException
{
try {
Method m = ClassLoader.class.getDeclaredMethod("defineClass", byte[].class, int.class, int.class);
m.setAccessible(true); // Make sure we can invoke the method
return (Class<?>) m.invoke(loadInto, code, 0, code.length);
}
// An exception should only be thrown if the bytecode is invalid
// or a class with the same name is already loaded
catch (NoSuchMethodException e) { throw new RuntimeException(e); }
catch (IllegalAccessException e){ throw new RuntimeException(e); }
}
Although, what I'm getting the feeling that you're referring to is runtime compilation of a String containing valid Java into bytecode, based on the link you included. Though I do not know of any way of doing this, I would recommend you take a look at this: https://github.com/linkedin/dexmaker