-5

Is there a way to dynamically create a class having a string containing the code of the class or maybe adding a method in the same way to an existing class previously created?

Use case scenario: A student is doing an exam in which he has to write some methods without an IDE (So he submits a simple string). What i want to achieve is a way to execute a simple unit test on his submission. For example a clickable button to run unit test back-provided by the professor.

Sorry for my bad english and thanks in advance!

giku93113
  • 13
  • 1
  • 1
  • 4
  • 1
    So the student hands in a .txt file? Why not a .java file - it could be added to the project easily and so there's no such strange problem. – Tobias Brösamle Jun 22 '16 at 08:26
  • The student has a textbox in front of him with two buttons "Submit" and "run tests". In this textbox he has the class code and he has to edit it adding 2 or 3 methods. When he clicks the "run tests" button i imagine something like putting all the class code into a String, *magical way to create a class dynamically from this string with a given name*, run junit tests from code, give back the results to him. – giku93113 Jun 22 '16 at 08:35
  • @TobiasBrösamle 'the project'? Which project are you referring to? OP needs some sort of automated unit testing on source code submitted as text, without IDE. – Adriaan Koster Jun 22 '16 at 08:35
  • Have a look at [Javassist](http://jboss-javassist.github.io/javassist/tutorial/tutorial.html) on how to create new or modify existing classes at runtime. How code can be added to an existing class file can be seen in [this](https://github.com/RovoMe/PluginApplication/blob/master/PluginFramework/PluginCore/src/main/java/at/rovo/core/classloader/InjectionLoaderStrategyDecorator.java) example, though this was done for a slightly different reason. – Roman Vottner Jun 22 '16 at 08:38
  • @Adriaan Koster exactly, i have no project at all. – giku93113 Jun 22 '16 at 08:39
  • @giku93113 I see, that was not clear to me. I thought it's handed in - in this case you could create a project, add the .java file to your project and then run unit tests on it. Just from your post I didn't understand you want to give immediate response to your students. – Tobias Brösamle Jun 22 '16 at 08:39

2 Answers2

0

The KIT (a university in Karlsruhe, Germany) has created a tool to test programming tasks from students. May you look at it here

CloudPotato
  • 1,255
  • 1
  • 17
  • 32
0

The student will be submitting Java source code, not byte code. Java source code has to be compiled into byte code using a Java compiler before it can be loaded by a classloader. You could do this by invoking the compiler programmatically (see this answer). Loading byte code of this new .class file at runtime can be done like this:

class NetworkClassLoader extends ClassLoader {
     String host;
     int port;

     public Class findClass(String name) {
         byte[] b = loadClassData(name);
         return defineClass(name, b, 0, b.length);
     }

     private byte[] loadClassData(String name) {
         // load the class data from the connection
          . . .
     }
 }

(example from here, thanks @Robert).

Once you have an instance you could feed it to your testing app to run some tests and get results.

It would be easiest to have the submitted class implement an interface you define and have your testing code test against that interface. In that way your test will compile beforehand and can still handle the dynamically compiled & instantiated submission at runtime.

Community
  • 1
  • 1
Adriaan Koster
  • 15,870
  • 5
  • 45
  • 60
  • Loading a dynamic created class at runtime via the standard classloader and `loadClass` is IMHO not a good solution. Better is a custom ClassLoader that creates the class directly from byte code data. See ["NetworkClassLoader" example in JavaDoc](https://docs.oracle.com/javase/8/docs/api/java/lang/ClassLoader.html) – Robert Jun 22 '16 at 09:12
  • I guess you are right, because the new class will not be on the classpath. Didn't think about it long enough ;-D – Adriaan Koster Jun 22 '16 at 09:44