In Java, is it possible to create a class definition on the fly while an application is running, and then create an object of that class?
For example, a running application would read in a text file that contains a list of class members to include in the new class. The application would then define a class definition based on the list of members, and then insantiate it.
解决方案
Yes its possible to do so in theory your class file is byte code which is at the end a byte array! you can then use the method defineClass(String, byte[], int, int) to get a Class instance that can be used to instantiate objects via reflection.
In practice you can use something like CGLib or javaassist.
You can also use the long way of generating the java code in a file, invoking the compiler, then loading the file.