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:

  1. create own classloader (not hard, see below).
  2. load class using classloader#defineclass(string, byte[], int, int).
  3. check implements interface (yourinterface.class.isassignablefrom(loadedclass);).
  4. use java reflection/introspection on class<?> got on step 1. (e.g. yourinterface obj = (yourinterface)loadedclass.newinstance();).
  5. 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

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -