java - How can I execute a method of a class that has been uploaded? -
the goal implement web application can execute methods of .class
files has been uploaded. uploaded class file available byte[]
. public class in .class
file implements specific interface.
after upload, i'd call method (interface implementation).
is there way in running java application? if yes, how?
btw. i'm aware of security risks.
shouldn't hard:
- create own classloader (not hard, see below).
- load class using
classloader#defineclass(string, byte[], int, int)
. - check implements interface (
yourinterface.class.isassignablefrom(loadedclass);
). - use java reflection/introspection on
class<?>
got on step 1. (e.g.yourinterface obj = (yourinterface)loadedclass.newinstance();
). - call method:
obj.shinymethod();
re creating own classloader: here's simple 1 delegates system class loader:
class myclassloader extends classloader { public myclassloader() { super(classloader.getsystemclassloader()); } // our custom public function loading byte array, // here because defineclass final, // can't make public. name can want. public class<?> loadclassfrombytearray(byte[] data) throws classformaterror { return this.defineclass(null, data, 0, data.length); } }
Comments
Post a Comment