Search code examples
javareflectionclassloader

Converting a given class (let's say, java.lang.Object) to a byte array. Is it possible?


Given that class loaders accept to take as input a byte array of a given class, returning a Class<?>, I wonder whether it is possible to do the reverse, that is, to pass a Class<?> and get its byte array?

Keep in mind that I'm not talking about serialization!


Solution

  • You can use ClassLoader.getResourceAsInputStream() however it is not guaranteed this will be the same bytes as that which was loaded. I don't believe the actual bytes loaded are stored anywhere.