The org.eclipse.jdt.ui.cleanUps extension point enables you to contribute your own Java code clean ups and Java editor save actions. The clean ups help in resolving the problems for a compilation unit. A save action is a special clean up which also resolves problems by doing the selected actions on save automatically.
To create a new extension for the org.eclipse.jdt.ui.cleanUps extension point you need to first provide the required extensions in the plugin.xml. There are 3 extensions that need to be declared as shown below with the example of a clean up which updates the copyrights for a file on save:
<extension point="org.eclipse.jdt.ui.cleanUps"> <cleanUp id="org.eclipse.jdt.copyrightsaveaction.copyright_clean_up" class="org.eclipse.jdt.ui.internal.copyrightupdater.CopyrightUpdaterCleanUp"> </cleanUp> <cleanUpOptionsInitializer class="org.eclipse.jdt.ui.internal.copyrightupdater.CopyrightOnSaveOptionsInitializer" cleanUpKind="saveAction"> </cleanUpOptionsInitializer> <cleanUpConfigurationUI class="org.eclipse.jdt.ui.internal.copyrightupdater.CopyrightTabPage" name="%cleanUpConfigurationUI.name" cleanUpKind="saveAction"> </cleanUpConfigurationUI> </extension>For a description of the attributes list please refer to the extension point document.
To contribute a clean up, you need to first create the class that implements the ICleanUp Interface.
Lets create the CopyrightUpdaterCleanUp
class for our example clean up and implement the inherited methods :
public class CopyrightUpdaterCleanUp implements ICleanUp { private CleanUpOptions fOptions; private RefactoringStatus fStatus; public CopyrightUpdaterCleanUp() { }
The CleanUpRequirements contain various requirements for the clean up such as an AST or a fresh AST containing changes from other clean ups, compiler options and changed
regions, which are used by the CleanUpContext
to create the fix. It can be obtained using the method getRequirements()
public CleanUpRequirements getRequirements() { boolean changedRegionsRequired= false; Map compilerOptions= null; boolean isUpdateCopyrights= fOptions.isEnabled("cleanup.update_copyrights");//$NON-NLS-1$ return new CleanUpRequirements(isUpdateCopyrights, isUpdateCopyrights, changedRegionsRequired, compilerOptions); }A human readable description can be set for each step of the clean up using getStepDescriptions()
public String[] getStepDescriptions() { if (fOptions.isEnabled("cleanup.update_copyrights"))//$NON-NLS-1$ return new String[] {"Update Copyrights"};//$NON-NLS-1$ return null; }The CleanUpOptions for the given options keys need to be set using the setOptions(...)
public void setOptions(CleanUpOptions options) { Assert.isLegal(options != null); Assert.isTrue(fOptions == null); fOptions= options; }The clean up pre-conditions and post-conditions should be checked for an OK status using checkPreConditions(...) and checkPostConditions(...)
public RefactoringStatus checkPreConditions(IJavaProject project, ICompilationUnit[] compilationUnits, IProgressMonitor monitor) throws CoreException { if (fOptions.isEnabled("cleanup.update_copyrights")) { //$NON-NLS-1$ fStatus= new RefactoringStatus(); } return new RefactoringStatus(); }
public RefactoringStatus checkPostConditions(IProgressMonitor monitor) throws CoreException { try { if (fStatus == null || fStatus.isOK()) { return new RefactoringStatus(); } else { return fStatus; } } finally { fStatus= null; } }Finally a ICleanUpFix is to be created which fixes all the problems for the given context using createFix(...)
public ICleanUpFix createFix(CleanUpContext context) throws CoreException { CompilationUnit compilationUnit= context.getAST(); if (compilationUnit == null) return null; return CopyrightsFix.createCleanUp(compilationUnit, fOptions.isEnabled("cleanup.update_copyrights"));//$NON-NLS-1$ }
To create the UI for the clean up , an options provider tab page has to be created by implementing the ICleanUpConfigurationUI interface.
The page can be created by implementing the createContents(...) method in the CopyrightTabPage
class. The preferences page along with the desired groups and options can be
created using the method doCreatePreferences().
Optionally a code snippet of the new clean up with the given options can be shown in the clean up preview tab using the method getPreview(). If the CleanUpOptions get modified in the UI,
they need to be set again using the method setOptions(...).
A clean up options initializer, which returns the default options for each clean up kind can be created by implementing the ICleanUpOptionsInitializer. The options initializer can either be for a normal code clean up or for a save action.
public class CopyrightOnSaveOptionsInitializer implements ICleanUpOptionsInitializer { public CopyrightOnSaveOptionsInitializer() { }The default options for this initializer can be set using setDefaultOptions(...)
public void setDefaultOptions(CleanUpOptions options) { options.setOption("cleanup.update_copyrights", CleanUpOptions.TRUE); } }
The UI code to configure the options can be found here: CleanUpTabPage.java.