How to use the APT tool to create exercises in the course material

I am creating exercises in how to write a plug-in for a system integration tool. We will have the correct answers, which will be implemented for demonstration after the exercises, but the students will get a source where some methods are empty and they have a comment with TODO in them describing what they should do.

To avoid duplication, it would be nice if student versions could be generated from compiled and correct source answer files. It seemed to me that the Java annotation processing tool (APT, not debian APT) could be used to generate exercises so that APT would spit out methods as empty if the input method contains an annotation for this.

Can this be done using APT? If so, how to do it?

Are there any better / simpler ways to avoid duplication, generate exercises, and correct answers from a single source that I skip?

+4
source share
2 answers

I'm not sure APT can do this because you need access to the source code to spit out the results.

You will probably be better off with a simple program that recognizes methods preceded by an annotation and replaces the contents of the opening and closing curly braces with a student place holder.

An alternative and probably simpler mechanism would be to use a custom comment to mark replaceable areas, and then just process this file to get the results. For instance.

public class SomeClass { public SomeClass() { // real code here } public void someMethod() { //EXERCISE:START System.out("put some real compilable code here, "+ "that students will have to implement themselves"); //EXERCISE:END } } 

Then you can simply make a simple bit of code to remove comments and content between them.

+1
source

APT doesn't strike me as the perfect way to do this, although it can be done. In general, APT should allow you to create new artifacts and provide a limited amount of structural information . You can only get into the AST tree through hackers specific to the compiler (as Project Lombok ).

+2
source

All Articles