List of usage examples for org.eclipse.jdt.core.util CompilationUnitSorter sort
public static TextEdit sort(CompilationUnit unit, Comparator comparator, int options, TextEditGroup group, IProgressMonitor monitor) throws JavaModelException
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); } }