public final class JavaScriptUnitSorter
extends java.lang.Object
This class provides all functionality via static members; it is not intended to be instantiated or subclassed.
Provisional API: This class/interface is part of an interim API that is still under development and expected to change significantly before reaching stability. It is being made available at this early stage to solicit feedback from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken (repeatedly) as the API evolves.Modifier and Type | Field and Description |
---|---|
static java.lang.String |
RELATIVE_ORDER
Name of auxillary property whose value can be used to determine the
original relative order of two body declarations.
|
Modifier and Type | Method and Description |
---|---|
static void |
sort(int level,
IJavaScriptUnit compilationUnit,
int[] positions,
java.util.Comparator comparator,
int options,
IProgressMonitor monitor)
Reorders the declarations in the given javascript unit according to
the specified AST level.
|
static TextEdit |
sort(JavaScriptUnit unit,
java.util.Comparator comparator,
int options,
TextEditGroup group,
IProgressMonitor monitor)
Reorders the declarations in the given javascript unit according to the
specified comparator.
|
public static final java.lang.String RELATIVE_ORDER
All body declarations passed to the comparator's compare
method by JavaScriptUnitSorter.sort
carry an
Integer-valued property. The body declaration with the lower value
comes before the one with the higher value. The exact numeric value
of these properties is unspecified.
Example usage:
BodyDeclaration b1 = (BodyDeclaration) object1; BodyDeclaration b2 = (BodyDeclaration) object2; Integer i1 = (Integer) b1.getProperty(RELATIVE_ORDER); Integer i2 = (Integer) b2.getProperty(RELATIVE_ORDER); return i1.intValue() - i2.intValue(); // preserve original order
BodyDeclaration
,
Constant Field Valuespublic static void sort(int level, IJavaScriptUnit compilationUnit, int[] positions, java.util.Comparator comparator, int options, IProgressMonitor monitor) throws JavaScriptModelException
Note: 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 JavaScript 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.
The optional positions
array contains a non-decreasing
ordered list of character-based source positions within the compilation
unit's source code string. Upon return from this method, the positions in
the array reflect the corresponding new locations in the modified source
code string. Note that this operation modifies the given array in place.
The compare
method of the given comparator is passed pairs
of body declarations (subclasses of BodyDeclaration
)
representing body declarations at the same level. The nodes are from an
AST of the specified level
(ASTParser.newParser(int)
. Clients
will generally specify AST.JLS3 since that will cover all constructs found
in JavaScript 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
RELATIVE_ORDER
property attached to these AST nodes afforts
the comparator a way to preserve the original relative order.
The body declarations passed as parameters to the comparator
always carry at least the following minimal signature information:
TypeDeclaration |
modifiers, isInterface, name, superclass,
superInterfaces, typeParameters |
FieldDeclaration |
modifiers, type, fragments
(VariableDeclarationFragments
with name only) |
FunctionDeclaration |
modifiers, isConstructor, returnType, name,
typeParameters, parameters
(SingleVariableDeclarations with name, type, and modifiers only),
thrownExceptions |
Initializer |
modifiers |
AnnotationTypeDeclaration |
modifiers, name |
AnnotationTypeMemberDeclaration |
modifiers, name, type, default |
EnumDeclaration |
modifiers, name, superInterfaces |
EnumConstantDeclaration |
modifiers, name, arguments |
level
- the AST level; one of the AST LEVEL constantscompilationUnit
- the given javascript unit , which must be a
working copypositions
- an array of source positions to map, or
null
if none. If supplied, the positions must
character-based source positions within the original source code for
the given javascript 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).comparator
- the comparator capable of ordering
BodyDeclaration
s; this comparator is passed AST nodes
from an AST of the specified AST leveloptions
- bitwise-or of option flags; 0
for default
behavior (reserved for future growth)monitor
- the progress monitor to notify, or null
if
noneJavaScriptModelException
- if the javascript unit could not be
sorted. Reasons include:
CoreException
occurred while accessing the underlying
resource
java.lang.IllegalArgumentException
- if the given javascript unit is null
or if the given comparator is null, or if level
is not one of
the AST JLS level constants.BodyDeclaration
,
RELATIVE_ORDER
public static TextEdit sort(JavaScriptUnit unit, java.util.Comparator comparator, int options, TextEditGroup group, IProgressMonitor monitor) throws JavaScriptModelException
Note: 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 JavaScript 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.
The compare
method of the given comparator is passed pairs
of body declarations (subclasses of BodyDeclaration
)
representing body declarations at the same level.
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 RELATIVE_ORDER
property attached to these
AST nodes affords the comparator a way to preserve the original relative
order.
The body declarations passed as parameters to the comparator always carry
at least the following minimal signature information:
TypeDeclaration |
modifiers, isInterface, name, superclass,
superInterfaces, typeParameters |
FieldDeclaration |
modifiers, type, fragments
(VariableDeclarationFragments
with name only) |
FunctionDeclaration |
modifiers, isConstructor, returnType, name,
typeParameters, parameters
(SingleVariableDeclarations with name, type, and modifiers only),
thrownExceptions |
Initializer |
modifiers |
AnnotationTypeDeclaration |
modifiers, name |
AnnotationTypeMemberDeclaration |
modifiers, name, type, default |
EnumDeclaration |
modifiers, name, superInterfaces |
EnumConstantDeclaration |
modifiers, name, arguments |
unit
- the JavaScriptUnit to sortcomparator
- the comparator capable of ordering
BodyDeclaration
s; this comparator is passed
AST nodes from an AST of the specified AST leveloptions
- bitwise-or of option flags; 0
for default
behavior (reserved for future growth)group
- the text edit group to use when generating text edits, or null
monitor
- the progress monitor to notify, or null
if nonenull
if sorting is not requiredJavaScriptModelException
- if the javascript unit could not be sorted. Reasons
include:
java.lang.IllegalArgumentException
- if the given javascript unit is null or if the given
comparator is null, or if options
is not one
of the supported levels.BodyDeclaration
,
RELATIVE_ORDER
Copyright (c) IBM Corp. and others 2000, 2016. All Rights Reserved.