4

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?

4

2 回答 2

2

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).

于 2009-08-27T13:52:35.480 回答
1

我不确定 APT 能否做到这一点,因为您需要访问源代码才能输出结果。

您最好使用一个简单的程序来识别以注释为前缀的方法,并用学生的占位符替换方法打开和关闭大括号的内容。

另一种可能更简单的机制是使用自定义注释来标记可替换区域,然后仅处理此文件以获得结果。例如

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

然后,您只需执行一些简单的代码即可删除注释和它们之间的内容。

于 2009-08-27T12:46:20.017 回答