Createing Java Emitter Templates

There are many ways you can go about to produce your Java Emitter Template (JET). During the process of developing my plug-in I gathered some experience that might make the process of creating a JET easier.

  1. Make sure that your JET files in Eclipse open with the JET editor.
  2. Use a data model containing your class meta model (e.g. members, superclass, interfaces, …)
  3. Create helper classes. Much of the stuff in your model needs processing before you can use it. If your model contains a class field indicating the super class. Your class has to extend it if the superclass is not java.lang.Object. So you might write something like:
    < % if (model.getSuperClass()!=null || model.getSuperClass()!=java.lang.Object) { %> or you could put a method in your model that allows for < % if (model.hasSuperClass()){ %>. A further example is the list of imports. Throughout the class you will encounter types. Some won’t need any import statement like primitives or types from java.lang, but others do and you won’t always write the fully qualified class name. Therefore put a method in your helper class that gets you all imports needed for this class. Now you must only pay attention if you have multiple helper classes that you eliminate the duplicate import statements (best done with a Set)
  4. When you put an opening bracket in the JET immediately add the closing bracket. Missing brackets are the most common error.
  5. Create a class your JET would produce in a conventional way. This way you can always check what should be done. Furthermore there are certainly parts that you can copy into your JET
  6. For each fragment in your JET first write it down in a method to see if it compiles. Pay special attention to class casts the second most source of errors
  7. Use the for each loop to minimize the potential risk of class cast exceptions. Where this is not possible double check the indices especially if you have nested for loops.
  8. If you are finished, test it. It may be that the generating class that was generated from your JET does not compile. So check this class out. In my plugin you must first enable the *.ressources to be visible to see the project. Fix the errors in the resulting class and fix their causes.

Schreibe einen Kommentar