It is possible to write quick-fix processors to fix errors that your APT processor detects. To do that, you must do two things:
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:
- printFixableError(SourcePosition pos, String msg, String pluginId, String errorId)
- printFixableError(String msg, String pluginId, String errorId)
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.
org.eclipse.jdt.apt.ui.quickfix.IAPTQuickFixProvider,
which has only one method:
- public IJavaCompletionProposal [] getProposals(IInvocationContext context,
IProblemLocation [] locations) throws CoreException;
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.