Creating a Quick-Fix processor for APT

Instructions for creating an Eclipse Quick Fix Processor

It is possible to write quick-fix processors to fix errors that your APT processor detects. To do that, you must do two things:

  1. Print errors indicating that they are fixable
  2. Write an APT quick fix processor and register it with APT

1. Print errors indicating that they are fixable

When running inside Eclipse, you may downcast the Messager object that you get from the AnnotationProcessorEnvironment's getMessager() method. Inside Eclipse, that object will also be an org.eclipse.jdt.apt.core.util.EclipseMessager, which declares some additional methods over the standard Messager, like the following: The pluginId should be the ID of the plugin within which you will be creating your quick fix processor. The errorId should be a String that will make sense in the context of your quick fix processor, say, "requiredAnnotationValueMissing", or whatever will provide the necessary information to provide your quick fix.

2. Create an APT Quick Fix Processor

Begin by creating a class that implements org.eclipse.jdt.apt.ui.quickfix.IAPTQuickFixProvider, which has only one method: You'll notice this is very similar to the standard Eclipse quick fix processor API, but you will only get called for problems that you created.

Once you've created your quick fix processor implementation, you can register it with an extension point on the APT UI plugin, aptQuickFixProvider. Here is an example of what to put in your plugin's plugin.xml:
<extension
         point="org.eclipse.jdt.apt.ui.aptQuickFixProvider">
      <quickFixProvider
            className="com.foo.MyAptQuickFixProvider"
            errorCode="com.foo.AptErrors"
            pluginId="com.foo.Apt"/>
</extension>
That's it! Once your plugin is running inside Eclipse, your APT Quick Fix Provider will get called each time the user attempts to quick-fix one of the errors your processor created.