How to create Java classes Dynamically by Java Reflection?

后端 未结 4 930
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-03 16:57

Reflection is used to load java class classes and manipulate them on the fly. But I have across a weird question that is asking me how to create Java classes on the fly by R

4条回答
  •  说谎
    说谎 (楼主)
    2021-01-03 17:18

    You can take a look look at Bean Shell's eval method It lets you execute any Java code on the fly without the need to compile the code into bytecode. You can pass a string containing all the Java code for your class to it's eval method and you'll get back an instance of the dynamically created class. Let me know if you're interested in it and want me to give you an example.

提交回复
热议问题