Example usage for org.eclipse.jdt.core.util CompilationUnitSorter sort

List of usage examples for org.eclipse.jdt.core.util CompilationUnitSorter sort

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.util CompilationUnitSorter sort.

Prototype

public static TextEdit sort(CompilationUnit unit, Comparator comparator, int options, TextEditGroup group,
        IProgressMonitor monitor) throws JavaModelException 

Source Link

Document

Reorders the declarations in the given compilation unit according to the specified comparator.

Usage

From source file:com.ixenit.membersort.handlers.SortHandler.java

License:Apache License

private void _processUnit(ICompilationUnit cu)
        throws JavaModelException, MalformedTreeException, BadLocationException {

    // Parse the javacode to be able to modify it
    ASTParser parser = ASTParser.newParser(AST.JLS8);
    parser.setSource(cu);//from w  ww.  j  a v  a2s . c o  m

    // Create a copy of the CompilationUnit to work on
    CompilationUnit copyOfUnit = (CompilationUnit) parser.createAST(null);

    MemberComparator comparator = new MemberComparator();

    // This helper method will sort our java code with the given comparator
    TextEdit edits = CompilationUnitSorter.sort(copyOfUnit, comparator, 0, null, null);

    // The sort method gives us null if there weren't any changes
    if (edits != null) {
        ICompilationUnit workingCopy = cu.getWorkingCopy(new WorkingCopyOwner() {
        }, null);

        workingCopy.applyTextEdit(edits, null);

        // Commit changes
        workingCopy.commitWorkingCopy(true, null);
    }
}