Using Java reflection to create eval() method

后端 未结 4 538
-上瘾入骨i
-上瘾入骨i 2020-12-10 19:00

I have a question about reflection I am trying to have some kind of eval() method. So i can call for example:

eval(\"test(\'woohoo\')\");

4条回答
  •  不思量自难忘°
    2020-12-10 19:17

    You can loop over all methods of a class using:

    cls.getMethods(); // gets all public methods (from the whole class hierarchy)
    

    or

    cls.getDeclaredMethods(); // get all methods declared by this class
    

    .

    for (Method method : cls.getMethods()) {
        // make your checks and calls here
    }
    

提交回复
热议问题