Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 57663 Details for
Bug 133277
[actions] Allow Sort Members to be performed on package and project levels
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
[patch]
Patch against JDT Core
org.eclipse.jdt.core.patch (text/plain), 16.12 KB, created by
Alex Blewitt
on 2007-01-28 18:51:39 EST
(
hide
)
Description:
Patch against JDT Core
Filename:
MIME Type:
Creator:
Alex Blewitt
Created:
2007-01-28 18:51:39 EST
Size:
16.12 KB
patch
obsolete
>### Eclipse Workspace Patch 1.0 >#P org.eclipse.jdt.core >Index: model/org/eclipse/jdt/core/util/CompilationUnitSorter.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/core/util/CompilationUnitSorter.java,v >retrieving revision 1.36 >diff -u -r1.36 CompilationUnitSorter.java >--- model/org/eclipse/jdt/core/util/CompilationUnitSorter.java 23 Jan 2007 14:58:32 -0000 1.36 >+++ model/org/eclipse/jdt/core/util/CompilationUnitSorter.java 28 Jan 2007 23:49:21 -0000 >@@ -328,11 +328,145 @@ > if (compilationUnit == null || comparator == null) { > throw new IllegalArgumentException(); > } >- checkASTLevel(level); > ICompilationUnit[] compilationUnits = new ICompilationUnit[] { compilationUnit }; >- SortElementsOperation operation = new SortElementsOperation(level, compilationUnits, positions, comparator); >- operation.runOperation(monitor); >+ int[][] positionses = new int[][] { positions }; >+ >+ sort(level, compilationUnits, positionses, comparator, options, monitor); > } >+ >+ /** >+ * Reorders the declarations in the given compilation units according to >+ * the specified AST level. The caller is responsible for arranging in >+ * advance that the given compilations unit are a working copy, and for >+ * saving the changes afterwards. >+ * <p> >+ * <b>Note:</b> Reordering the members within a type declaration might be >+ * more than a cosmetic change and could have potentially serious >+ * repercussions. Firstly, the order in which the fields of a type are >+ * initialized is significant in the Java language; reordering fields >+ * and initializers may result in compilation errors or change the execution >+ * behavior of the code. Secondly, reordering a class's members may affect >+ * how its instances are serialized. This operation should therefore be used >+ * with caution and due concern for potential negative side effects. >+ * </p> >+ * <p> >+ * The optional <code>positionses</code> array contains an array of non-decreasing >+ * ordered list of character-based source positions within the corresponding compilation >+ * unit's source code string. Upon return from this method, the positionses in >+ * the array reflect the corresponding new locations in the modified source >+ * code string. Note that this operation modifies the given array in place. >+ * </p> >+ * <p> >+ * The <code>compare</code> method of the given comparator is passed pairs >+ * of body declarations (subclasses of <code>BodyDeclaration</code>) >+ * representing body declarations at the same level. The nodes are from an >+ * AST of the specified level >+ * ({@link org.eclipse.jdt.core.dom.ASTParser#newParser(int)}. Clients >+ * will generally specify AST.JLS3 since that will cover all constructs found >+ * in Java 1.0, 1.1, 1.2, 1.3, 1.4, and 1.5 source code. >+ * The comparator is called on body declarations of nested classes, including >+ * anonymous and local classes, but always at the same level. Clients need to provide >+ * a comparator implementation (there is no standard comparator). The >+ * <code>RELATIVE_ORDER</code> property attached to these AST nodes afforts >+ * the comparator a way to preserve the original relative order. >+ * </p> >+ * <p> >+ * The body declarations passed as parameters to the comparator >+ * always carry at least the following minimal signature information: >+ * <br> >+ * <table border="1" width="80%" cellpadding="5"> >+ * <tr> >+ * <td width="20%"><code>TypeDeclaration</code></td> >+ * <td width="50%"><code>modifiers, isInterface, name, superclass, >+ * superInterfaces, typeParameters<br> >+ * RELATIVE_ORDER property</code></td> >+ * </tr> >+ * <tr> >+ * <td width="20%"><code>FieldDeclaration</code></td> >+ * <td width="50%"><code>modifiers, type, fragments >+ * (VariableDeclarationFragments >+ * with name only)<br> >+ * RELATIVE_ORDER property</code></td> >+ * </tr> >+ * <tr> >+ * <td width="20%"><code>MethodDeclaration</code></td> >+ * <td width="50%"><code>modifiers, isConstructor, returnType, name, >+ * typeParameters, parameters >+ * (SingleVariableDeclarations with name, type, and modifiers only), >+ * thrownExceptions<br> >+ * RELATIVE_ORDER property</code></td> >+ * </tr> >+ * <tr> >+ * <td width="20%"><code>Initializer</code></td> >+ * <td width="50%"><code>modifiers<br> >+ * RELATIVE_ORDER property</code></td> >+ * </tr> >+ * <tr> >+ * <td width="20%"><code>AnnotationTypeDeclaration</code></td> >+ * <td width="50%"><code>modifiers, name<br> >+ * RELATIVE_ORDER property</code></td> >+ * </tr> >+ * <tr> >+ * <td width="20%"><code>AnnotationTypeMemberDeclaration</code></td> >+ * <td width="50%"><code>modifiers, name, type, default<br> >+ * RELATIVE_ORDER property</code></td> >+ * </tr> >+ * <tr> >+ * <td width="20%"><code>EnumDeclaration</code></td> >+ * <td width="50%"><code>modifiers, name, superInterfaces<br> >+ * RELATIVE_ORDER property</code></td> >+ * </tr> >+ * <tr> >+ * <td width="20%"><code>EnumConstantDeclaration</code></td> >+ * <td width="50%"><code>modifiers, name, arguments<br> >+ * RELATIVE_ORDER property</code></td> >+ * </tr> >+ * </table> >+ * Clients should not rely on the AST nodes being properly parented or on >+ * having source range information. (Future releases may provide options >+ * for requesting additional information like source positions, full ASTs, >+ * non-recursive sorting, etc.) >+ * </p> >+ * >+ * @param level the AST level; one of the AST LEVEL constants >+ * @param compilationUnits the given compilation units, which must be a >+ * working copy >+ * @param positionses an array of array of source positions to map, or >+ * <code>null</code> if none. If supplied, the positions must >+ * character-based source positions within the original source code for >+ * the given compilation unit, arranged in non-decreasing order. >+ * The array is updated in place when this method returns to reflect the >+ * corresponding source positions in the permuted source code string >+ * (but not necessarily any longer in non-decreasing order). >+ * @param comparator the comparator capable of ordering >+ * <code>BodyDeclaration</code>s; this comparator is passed AST nodes >+ * from an AST of the specified AST level >+ * @param options bitwise-or of option flags; <code>0</code> for default >+ * behavior (reserved for future growth) >+ * @param monitor the progress monitor to notify, or <code>null</code> if >+ * none >+ * @exception JavaModelException 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> >+ * @exception IllegalArgumentException if the given compilation unit is null >+ * or if the given comparator is null, or if <code>level</code> is not one of >+ * the AST JLS level constants. >+ * @see org.eclipse.jdt.core.dom.BodyDeclaration >+ * @see #RELATIVE_ORDER >+ * @since 3.1 >+ */ >+ public static void sort(int level, ICompilationUnit[] compilationUnits, >+ int[][] positionses, Comparator comparator, int options, IProgressMonitor monitor) >+ throws JavaModelException { >+ checkASTLevel(level); >+ SortElementsOperation operation = new SortElementsOperation(compilationUnits, comparator, level, positionses); >+ operation.runOperation(monitor); >+ } > > /** > * Reorders the declarations in the given compilation unit according to the >@@ -463,7 +597,7 @@ > if (unit == null || comparator == null) { > throw new IllegalArgumentException(); > } >- SortElementsOperation operation = new SortElementsOperation(AST.JLS3, new IJavaElement[] { unit.getJavaElement() }, null, comparator); >+ SortElementsOperation operation = new SortElementsOperation(new IJavaElement[] { unit.getJavaElement() }, comparator, AST.JLS3, null); > return operation.calculateEdit(unit, group); > } > } >Index: model/org/eclipse/jdt/internal/core/SortElementsOperation.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/SortElementsOperation.java,v >retrieving revision 1.37 >diff -u -r1.37 SortElementsOperation.java >--- model/org/eclipse/jdt/internal/core/SortElementsOperation.java 23 Jan 2007 14:58:32 -0000 1.37 >+++ model/org/eclipse/jdt/internal/core/SortElementsOperation.java 28 Jan 2007 23:49:21 -0000 >@@ -51,12 +51,36 @@ > * > * @since 2.1 > */ >-public class SortElementsOperation extends JavaModelOperation { >+public class SortElementsOperation extends MultiOperation { > public static final String CONTAINS_MALFORMED_NODES = "malformed"; //$NON-NLS-1$ > > Comparator comparator; >+ // TODO ASB Is this important? We should be using postitionses[] instead. >+ /** >+ * @deprecated use positionses[0] instead >+ */ > int[] positions; >+ int[][] positionses; > int apiLevel; >+ >+ /** >+ * Constructor for SortElementsOperation. >+ * >+ * @param elements the array of elements (possibly more than one) >+ * @param comparator the comparator to use for comparison purposes >+ * @param level the AST API level; one of the AST LEVEL constants >+ * @param positionses the arrray of array of elements to track for the element >+ * @Since 3.3 >+ */ >+ public SortElementsOperation(IJavaElement[] elements, Comparator comparator, int level, int[][] positionses) { >+ // NB reordered field declarations to avoid ambiguity with the positionses element >+ super(elements, false); >+ this.comparator = comparator; >+ this.apiLevel = level; >+ this.positionses = positionses; >+ // TODO Do we need to keep this field? >+ this.positions = (positionses != null && positionses.length > 0 ? positionses[0] : null) ; >+ } > > /** > * Constructor for SortElementsOperation. >@@ -65,20 +89,13 @@ > * @param elements > * @param positions > * @param comparator >+ * @deprecated since this only works on 1 element, use the other one instead >+ * @throws IllegalArgumentException if called with more than one element > */ > public SortElementsOperation(int level, IJavaElement[] elements, int[] positions, Comparator comparator) { >- super(elements); >- this.comparator = comparator; >- this.positions = positions; >- this.apiLevel = level; >- } >- >- /** >- * Returns the amount of work for the main task of this operation for >- * progress reporting. >- */ >- protected int getMainAmountOfWork(){ >- return this.elementsToProcess.length; >+ this(elements,comparator, level, (positions == null ? null : new int[][] { positions })); >+ if (elements.length > 1) >+ throw new IllegalArgumentException("Can only handle one element"); // TODO ASB Decide on right message > } > > boolean checkMalformedNodes(ASTNode node) { >@@ -91,26 +108,28 @@ > return (node.getFlags() & ASTNode.MALFORMED) != 0; > } > >- /** >- * @see org.eclipse.jdt.internal.core.JavaModelOperation#executeOperation() >- */ >- protected void executeOperation() throws JavaModelException { >- try { >- beginTask(Messages.operation_sortelements, getMainAmountOfWork()); >- CompilationUnit copy = (CompilationUnit) this.elementsToProcess[0]; >- ICompilationUnit unit = copy.getPrimary(); >- IBuffer buffer = copy.getBuffer(); >- if (buffer == null) { >- return; >- } >- char[] bufferContents = buffer.getCharacters(); >- String result = processElement(unit, bufferContents); >- if (!CharOperation.equals(result.toCharArray(), bufferContents)) { >- copy.getBuffer().setContents(result); >- } >- worked(1); >- } finally { >- done(); >+ protected void processElement(IJavaElement element) throws JavaModelException { >+ CompilationUnit copy = (CompilationUnit) element; >+ ICompilationUnit unit = copy.getPrimary(); >+ IBuffer buffer = copy.getBuffer(); >+ if (buffer == null) { >+ return; >+ } >+ char[] bufferContents = buffer.getCharacters(); >+ int[] thePositions = null; >+ if (positionses != null) { >+ // find out which array we need >+ int i; >+ for(i=0;i<elementsToProcess.length;i++) { >+ if (elementsToProcess[i] == element) >+ break; >+ } >+ if (i<positionses.length) >+ thePositions = positionses[i]; >+ } >+ String result = processElement(unit, bufferContents,thePositions); >+ if (!CharOperation.equals(result.toCharArray(), bufferContents)) { >+ copy.getBuffer().setContents(result); > } > } > >@@ -125,7 +144,7 @@ > throw new JavaModelException(status); > } > try { >- beginTask(Messages.operation_sortelements, getMainAmountOfWork()); >+ beginTask(Messages.operation_sortelements, 1); > > ICompilationUnit cu= (ICompilationUnit)this.elementsToProcess[0]; > String content= cu.getBuffer().getContents(); >@@ -146,7 +165,7 @@ > * @param unit > * @param source > */ >- private String processElement(ICompilationUnit unit, char[] source) { >+ private String processElement(ICompilationUnit unit, char[] source, int positions[]) { > Document document = new Document(new String(source)); > CompilerOptions options = new CompilerOptions(unit.getJavaProject().getOptions(true)); > ASTParser parser = ASTParser.newParser(this.apiLevel); >@@ -163,18 +182,18 @@ > TextEdit edits = rewriter.rewriteAST(document, null); > > RangeMarker[] markers = null; >- if (this.positions != null) { >- markers = new RangeMarker[this.positions.length]; >- for (int i = 0, max = this.positions.length; i < max; i++) { >- markers[i]= new RangeMarker(this.positions[i], 0); >+ if (positions != null) { >+ markers = new RangeMarker[positions.length]; >+ for (int i = 0, max = positions.length; i < max; i++) { >+ markers[i]= new RangeMarker(positions[i], 0); > insert(edits, markers[i]); > } > } > try { > edits.apply(document, TextEdit.UPDATE_REGIONS); >- if (this.positions != null) { >+ if (positions != null) { > for (int i= 0, max = markers.length; i < max; i++) { >- this.positions[i]= markers[i].getOffset(); >+ positions[i]= markers[i].getOffset(); > } > } > } catch (BadLocationException e) { >@@ -317,27 +336,6 @@ > > return rewriter; > } >- >- /** >- * Possible failures: >- * <ul> >- * <li>NO_ELEMENTS_TO_PROCESS - the compilation unit supplied to the operation is <code>null</code></li>. >- * <li>INVALID_ELEMENT_TYPES - the supplied elements are not an instance of IWorkingCopy</li>. >- * </ul> >- * @return IJavaModelStatus >- */ >- public IJavaModelStatus verify() { >- if (this.elementsToProcess.length != 1) { >- return new JavaModelStatus(IJavaModelStatusConstants.NO_ELEMENTS_TO_PROCESS); >- } >- if (this.elementsToProcess[0] == null) { >- return new JavaModelStatus(IJavaModelStatusConstants.NO_ELEMENTS_TO_PROCESS); >- } >- if (!(this.elementsToProcess[0] instanceof ICompilationUnit) || !((ICompilationUnit) this.elementsToProcess[0]).isWorkingCopy()) { >- return new JavaModelStatus(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, this.elementsToProcess[0]); >- } >- return JavaModelStatus.VERIFIED_OK; >- } > > public static void insert(TextEdit parent, TextEdit edit) { > if (!parent.hasChildren()) { >@@ -381,4 +379,19 @@ > return thisOffset <= otherOffset && otherEnd <= thisEnd; > } > } >+ >+ protected String getMainTaskName() { >+ return Messages.operation_sortelements; >+ } >+ >+ /** >+ * Elements must be ICompilationUnits and be working copies, otherwise an >+ * {@link IJavaModelStatusConstants#INVALID_ELEMENT_TYPES} >+ * @throws JavaModelException if there is an exception determining the working copy status >+ */ >+ protected void verify(IJavaElement element) throws JavaModelException { >+ if (!(element instanceof ICompilationUnit) || !((ICompilationUnit) element).isWorkingCopy()) { >+ error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element); >+ } >+ } > }
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 133277
:
57663
|
57664
|
58624
|
62936
|
62938