views:

192

answers:

2

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

To avoid duplication, it would be nice if the students' versions could be generated from the compilable and correct answer source-files. It struck me that the Java Annotation Processing Tool (that APT, not the debian APT) could possibly be used to generate the exercises, to have APT spit out methods as empty if the input method carries an annotation to do so.

Is this possible to do using APT? If so, how would one do it?

Are there better/easier ways to avoid having duplication, to generate the exercises and correct answers from a single source, that I am overlooking?

+1  A: 

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

You're probably better off with a simple program that recognises methods prefixed by the annotation and replaces the contents of the methods opening and closing braces with a place holder for the students.

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

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
   }
}

You can then just do a simple bit of code to remove the comments and the content between them.

BenM
Yes, magical comments are probably better. I think awk can handle regions bounded by lines that match regexps. I'll have to look into it.
Christian
+2  A: 

The APT doesn't strike me as an ideal way to do this, though it could be done. In general, the APT is only supposed to let you generate new artefacts and provides a limited amount of structural information. You can only get to the AST tree through compiler-specific hacks (as Project Lombok does).

McDowell