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); // 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); } }
public static ICleanUpFix createCleanUp(CompilationUnit compilationUnit, boolean sortMembers, boolean sortFields) throws CoreException { if (!sortMembers && !sortFields) return null; ICompilationUnit cu= (ICompilationUnit)compilationUnit.getJavaElement(); String label= FixMessages.SortMembersFix_Change_description; CategorizedTextEditGroup group= new CategorizedTextEditGroup(label, new GroupCategorySet(new GroupCategory(label, label, label))); TextEdit edit= CompilationUnitSorter.sort(compilationUnit, new DefaultJavaElementComparator(!sortFields), 0, group, null); if (edit == null) return null; return new SortMembersFix(edit, cu, FixMessages.SortMembersFix_Fix_description); }
private int preserveRelativeOrder(BodyDeclaration bodyDeclaration1, BodyDeclaration bodyDeclaration2) { int value1= ((Integer) bodyDeclaration1.getProperty(CompilationUnitSorter.RELATIVE_ORDER)).intValue(); int value2= ((Integer) bodyDeclaration2.getProperty(CompilationUnitSorter.RELATIVE_ORDER)).intValue(); return value1 - value2; }
/** * Runs the operation. * @param monitor a monitor to use to report progress * @throws CoreException if the compilation unit could not be * sorted. Reasons include: * <ul> * <li> The given compilation unit does not exist (ELEMENT_DOES_NOT_EXIST)</li> * <li> The given compilation unit is not a working copy (INVALID_ELEMENT_TYPES)</li> * <li> A <code>CoreException</code> occurred while accessing the underlying * resource * </ul> */ public void run(IProgressMonitor monitor) throws CoreException { CompilationUnitSorter.sort(ASTProvider.SHARED_AST_LEVEL, fCompilationUnit, fPositions, new DefaultJavaElementComparator(fDoNotSortFields), 0, monitor); }