|
Lines 1-5
Link Here
|
| 1 |
/******************************************************************************* |
1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2000, 2010 IBM Corporation and others. |
2 |
* Copyright (c) 2000, 2011 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
5 |
* which accompanies this distribution, and is available at |
|
Lines 11-16
Link Here
|
| 11 |
package org.eclipse.jdt.internal.core.dom.rewrite; |
11 |
package org.eclipse.jdt.internal.core.dom.rewrite; |
| 12 |
|
12 |
|
| 13 |
import java.util.ArrayList; |
13 |
import java.util.ArrayList; |
|
|
14 |
import java.util.Arrays; |
| 15 |
import java.util.Collections; |
| 16 |
import java.util.Comparator; |
| 14 |
import java.util.HashMap; |
17 |
import java.util.HashMap; |
| 15 |
import java.util.HashSet; |
18 |
import java.util.HashSet; |
| 16 |
import java.util.List; |
19 |
import java.util.List; |
|
Lines 26-33
Link Here
|
| 26 |
import org.eclipse.jdt.core.Signature; |
29 |
import org.eclipse.jdt.core.Signature; |
| 27 |
import org.eclipse.jdt.core.compiler.CharOperation; |
30 |
import org.eclipse.jdt.core.compiler.CharOperation; |
| 28 |
import org.eclipse.jdt.core.dom.ASTNode; |
31 |
import org.eclipse.jdt.core.dom.ASTNode; |
|
|
32 |
import org.eclipse.jdt.core.dom.Comment; |
| 29 |
import org.eclipse.jdt.core.dom.CompilationUnit; |
33 |
import org.eclipse.jdt.core.dom.CompilationUnit; |
| 30 |
import org.eclipse.jdt.core.dom.ImportDeclaration; |
34 |
import org.eclipse.jdt.core.dom.ImportDeclaration; |
|
|
35 |
import org.eclipse.jdt.core.dom.LineComment; |
| 31 |
import org.eclipse.jdt.core.dom.PackageDeclaration; |
36 |
import org.eclipse.jdt.core.dom.PackageDeclaration; |
| 32 |
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; |
37 |
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; |
| 33 |
import org.eclipse.jdt.core.search.IJavaSearchConstants; |
38 |
import org.eclipse.jdt.core.search.IJavaSearchConstants; |
|
Lines 58-63
Link Here
|
| 58 |
private boolean useContextToFilterImplicitImports; |
63 |
private boolean useContextToFilterImplicitImports; |
| 59 |
private boolean findAmbiguousImports; |
64 |
private boolean findAmbiguousImports; |
| 60 |
|
65 |
|
|
|
66 |
private IRegion[] preserveExistingCommentsRanges; |
| 67 |
|
| 61 |
private int flags= 0; |
68 |
private int flags= 0; |
| 62 |
|
69 |
|
| 63 |
private static final int F_NEEDS_LEADING_DELIM= 2; |
70 |
private static final int F_NEEDS_LEADING_DELIM= 2; |
|
Lines 89-94
Link Here
|
| 89 |
this.replaceRange= evaluateReplaceRange(root); |
96 |
this.replaceRange= evaluateReplaceRange(root); |
| 90 |
if (restoreExistingImports) { |
97 |
if (restoreExistingImports) { |
| 91 |
addExistingImports(root); |
98 |
addExistingImports(root); |
|
|
99 |
} else { |
| 100 |
// collect all existing comments inside imports and concatenate them |
| 101 |
this.preserveExistingCommentsRanges = retrieveExistingCommentsInImports(root); |
| 92 |
} |
102 |
} |
| 93 |
|
103 |
|
| 94 |
PackageEntry[] order= new PackageEntry[importOrder.length]; |
104 |
PackageEntry[] order= new PackageEntry[importOrder.length]; |
|
Lines 261-272
Link Here
|
| 261 |
int nextLength= next.getLength(); |
271 |
int nextLength= next.getLength(); |
| 262 |
int nextOffsetLine= root.getLineNumber(nextOffset); |
272 |
int nextOffsetLine= root.getLineNumber(nextOffset); |
| 263 |
|
273 |
|
|
|
274 |
int extendedStart = root.getExtendedStartPosition(next); |
| 275 |
int extendedLength = root.getExtendedLength(next); |
| 264 |
// if next import is on a different line, modify the end position to the next line begin offset |
276 |
// if next import is on a different line, modify the end position to the next line begin offset |
| 265 |
if (currEndLine < nextOffsetLine) { |
277 |
if (currEndLine < nextOffsetLine) { |
| 266 |
currEndLine++; |
278 |
currEndLine++; |
| 267 |
nextOffset= root.getPosition(currEndLine, 0); |
279 |
nextOffset= root.getPosition(currEndLine, 0); |
| 268 |
} |
280 |
} |
| 269 |
currPackage.add(new ImportDeclEntry(packName.length(), name, isStatic, new Region(currOffset, nextOffset - currOffset))); |
281 |
// retrieve preceding and trailing comments if any |
|
|
282 |
IRegion rangeBefore = null; |
| 283 |
IRegion rangeAfter = null; |
| 284 |
if (nextOffset != extendedStart) { |
| 285 |
rangeBefore = new Region(extendedStart, extendedStart - nextOffset + 1); |
| 286 |
} |
| 287 |
if (nextLength != extendedLength) { |
| 288 |
rangeAfter = new Region(nextOffset + nextLength, extendedLength - nextLength + 1); |
| 289 |
} |
| 290 |
currPackage.add( |
| 291 |
new ImportDeclEntry( |
| 292 |
packName.length(), |
| 293 |
name, |
| 294 |
isStatic, |
| 295 |
new Region(currOffset, nextOffset - currOffset), |
| 296 |
rangeBefore, |
| 297 |
rangeAfter)); |
| 270 |
currOffset= nextOffset; |
298 |
currOffset= nextOffset; |
| 271 |
curr= next; |
299 |
curr= next; |
| 272 |
|
300 |
|
|
Lines 294-299
Link Here
|
| 294 |
currPackage.add(new ImportDeclEntry(packName.length(), name, isStatic, new Region(curr.getStartPosition(), length))); |
322 |
currPackage.add(new ImportDeclEntry(packName.length(), name, isStatic, new Region(curr.getStartPosition(), length))); |
| 295 |
} |
323 |
} |
| 296 |
|
324 |
|
|
|
325 |
private IRegion[] retrieveExistingCommentsInImports(CompilationUnit root) { |
| 326 |
List/*ImportDeclaration*/ decls= root.imports(); |
| 327 |
if (decls.isEmpty()) { |
| 328 |
return null; |
| 329 |
} |
| 330 |
|
| 331 |
List commentList = root.getCommentList(); |
| 332 |
int numberOfComments = commentList.size(); |
| 333 |
List regions = null; |
| 334 |
int currentExtendedEnd = -1; |
| 335 |
int currEndLine= -1; |
| 336 |
|
| 337 |
/* for the first comment, we only take the trailing comment if any and the replace range doesn't |
| 338 |
* include the preceding comment |
| 339 |
*/ |
| 340 |
for (int i= 0; i < decls.size(); i++) { |
| 341 |
ImportDeclaration next= (ImportDeclaration) decls.get(i); |
| 342 |
int nextOffset= next.getStartPosition(); |
| 343 |
int nextLength= next.getLength(); |
| 344 |
|
| 345 |
int extendedStart = root.getExtendedStartPosition(next); |
| 346 |
int extendedLength = root.getExtendedLength(next); |
| 347 |
int nextOffsetLine= root.getLineNumber(nextOffset); |
| 348 |
|
| 349 |
if (nextOffset != extendedStart) { |
| 350 |
// preceding comment |
| 351 |
int lengthOfPrecedingComment = nextOffset - extendedStart; |
| 352 |
if (i != 0) { |
| 353 |
if (regions == null) { |
| 354 |
regions = new ArrayList(); |
| 355 |
} |
| 356 |
regions.add(new Region(extendedStart, lengthOfPrecedingComment)); |
| 357 |
} |
| 358 |
|
| 359 |
if (extendedLength != (nextLength + lengthOfPrecedingComment)) { |
| 360 |
// Preceding and trailing comments |
| 361 |
int regionLength = extendedLength - (nextLength + lengthOfPrecedingComment); |
| 362 |
if (regions == null) { |
| 363 |
regions = new ArrayList(); |
| 364 |
} |
| 365 |
regions.add(new Region(nextOffset + nextLength, regionLength)); |
| 366 |
} |
| 367 |
} else if (nextLength != extendedLength) { |
| 368 |
// no extended start - only trailing comment |
| 369 |
int regionLength = extendedLength - nextLength; |
| 370 |
if (regions == null) { |
| 371 |
regions = new ArrayList(); |
| 372 |
} |
| 373 |
regions.add(new Region(nextOffset + nextLength, regionLength)); |
| 374 |
} |
| 375 |
if (i > 0) { |
| 376 |
// record comments between the previous comment and the current one that are not part |
| 377 |
// of any comment extended range. |
| 378 |
if ((nextOffsetLine - currEndLine) > 1) { |
| 379 |
// check for comments between the two imports |
| 380 |
LineComment comment = root.getAST().newLineComment(); |
| 381 |
comment.setSourceRange(currentExtendedEnd + 1, 0); |
| 382 |
int index = Collections.binarySearch(commentList, comment, new Comparator() { |
| 383 |
public int compare(Object o1, Object o2) { |
| 384 |
return ((Comment) o1).getStartPosition() - ((Comment) o2).getStartPosition(); |
| 385 |
} |
| 386 |
}); |
| 387 |
// index = -(insertion point) - 1. |
| 388 |
if (index < 0) { |
| 389 |
loop: for (int j = -(index + 1); j < numberOfComments; j++) { |
| 390 |
Comment currentComment = (Comment) commentList.get(j); |
| 391 |
int commentStartPosition = currentComment.getStartPosition(); |
| 392 |
int commentLength = currentComment.getLength(); |
| 393 |
if ((commentStartPosition > currentExtendedEnd) |
| 394 |
&& ((commentStartPosition + commentLength - 1) < extendedStart)) { |
| 395 |
if (regions == null) { |
| 396 |
regions = new ArrayList(); |
| 397 |
} |
| 398 |
regions.add(new Region(commentStartPosition, commentLength)); |
| 399 |
} else { |
| 400 |
break loop; |
| 401 |
} |
| 402 |
} |
| 403 |
} |
| 404 |
} |
| 405 |
} |
| 406 |
currentExtendedEnd = extendedStart + extendedLength - 1; |
| 407 |
currEndLine = root.getLineNumber(currentExtendedEnd); |
| 408 |
} |
| 409 |
if (regions == null) { |
| 410 |
return null; |
| 411 |
} |
| 412 |
// sort regions according to their positions to restore comments in the same order |
| 413 |
IRegion[] result = (IRegion[]) regions.toArray(new IRegion[regions.size()]); |
| 414 |
Arrays.sort(result, new Comparator() { |
| 415 |
public int compare(Object o1, Object o2) { |
| 416 |
return ((IRegion) o1).getOffset() - ((IRegion) o2).getOffset(); |
| 417 |
} |
| 418 |
}); |
| 419 |
return result; |
| 420 |
} |
| 297 |
/** |
421 |
/** |
| 298 |
* Specifies that implicit imports (for types in <code>java.lang</code>, types in the same package as the rewrite |
422 |
* Specifies that implicit imports (for types in <code>java.lang</code>, types in the same package as the rewrite |
| 299 |
* compilation unit and types in the compilation unit's main type) should not be created, except if necessary to |
423 |
* compilation unit and types in the compilation unit's main type) should not be created, except if necessary to |
|
Lines 617-623
Link Here
|
| 617 |
|
741 |
|
| 618 |
boolean doStarImport= pack.hasStarImport(threshold, onDemandConflicts); |
742 |
boolean doStarImport= pack.hasStarImport(threshold, onDemandConflicts); |
| 619 |
if (doStarImport && (pack.find("*") == null)) { //$NON-NLS-1$ |
743 |
if (doStarImport && (pack.find("*") == null)) { //$NON-NLS-1$ |
| 620 |
String[] imports = getNewImportStrings(pack, isStatic, lineDelim); |
744 |
String[] imports = getNewImportStrings(buffer, pack, isStatic, lineDelim); |
| 621 |
for (int j = 0, max = imports.length; j < max; j++) { |
745 |
for (int j = 0, max = imports.length; j < max; j++) { |
| 622 |
stringsToInsert.add(imports[j]); |
746 |
stringsToInsert.add(imports[j]); |
| 623 |
} |
747 |
} |
|
Lines 648-654
Link Here
|
| 648 |
} else if (doStarImport && !currDecl.isOnDemand()) { |
772 |
} else if (doStarImport && !currDecl.isOnDemand()) { |
| 649 |
String simpleName = currDecl.getTypeQualifiedName(); |
773 |
String simpleName = currDecl.getTypeQualifiedName(); |
| 650 |
if (simpleName.indexOf('.') != -1) { |
774 |
if (simpleName.indexOf('.') != -1) { |
| 651 |
String str= getNewImportString(currDecl.getElementName(), isStatic, lineDelim); |
775 |
IRegion rangeBefore = currDecl.getPrecedingCommentRange(); |
|
|
776 |
if (rangeBefore != null) { |
| 777 |
stringsToInsert.add(buffer.getText(rangeBefore.getOffset(), rangeBefore.getLength())); |
| 778 |
} |
| 779 |
IRegion rangeAfter = currDecl.getTrailingCommentRange(); |
| 780 |
String trailingComment = null; |
| 781 |
if (rangeAfter != null) { |
| 782 |
trailingComment = buffer.getText(rangeAfter.getOffset(), rangeAfter.getLength()); |
| 783 |
} |
| 784 |
String str= getNewImportString(currDecl.getElementName(), isStatic, trailingComment, lineDelim); |
| 652 |
if (stringsToInsert.indexOf(str) == -1) { |
785 |
if (stringsToInsert.indexOf(str) == -1) { |
| 653 |
stringsToInsert.add(str); |
786 |
stringsToInsert.add(str); |
| 654 |
} |
787 |
} |
|
Lines 657-662
Link Here
|
| 657 |
} |
790 |
} |
| 658 |
} |
791 |
} |
| 659 |
|
792 |
|
|
|
793 |
// insert back all existing imports comments since existing imports were not preserved |
| 794 |
if (this.preserveExistingCommentsRanges != null) { |
| 795 |
for (int i = 0, max = this.preserveExistingCommentsRanges.length; i < max; i++) { |
| 796 |
IRegion region = this.preserveExistingCommentsRanges[i]; |
| 797 |
String text = buffer.getText(region.getOffset(), region.getLength()); |
| 798 |
// remove preceding whitespaces |
| 799 |
int index = 0; |
| 800 |
int length = text.length(); |
| 801 |
loop: while (index < length) { |
| 802 |
if (Character.isWhitespace(text.charAt(index))) { |
| 803 |
index++; |
| 804 |
} else { |
| 805 |
break loop; |
| 806 |
} |
| 807 |
} |
| 808 |
if (index != 0) { |
| 809 |
text = text.substring(index); |
| 810 |
} |
| 811 |
if (!text.endsWith(lineDelim)) { |
| 812 |
text += lineDelim; |
| 813 |
} |
| 814 |
stringsToInsert.add(text); |
| 815 |
} |
| 816 |
} |
| 660 |
int end= importsStart + importsLen; |
817 |
int end= importsStart + importsLen; |
| 661 |
removeAndInsertNew(buffer, currPos, end, stringsToInsert, resEdit); |
818 |
removeAndInsertNew(buffer, currPos, end, stringsToInsert, resEdit); |
| 662 |
|
819 |
|
|
Lines 787-792
Link Here
|
| 787 |
} |
944 |
} |
| 788 |
|
945 |
|
| 789 |
private String getNewImportString(String importName, boolean isStatic, String lineDelim) { |
946 |
private String getNewImportString(String importName, boolean isStatic, String lineDelim) { |
|
|
947 |
return getNewImportString(importName, isStatic, null, lineDelim); |
| 948 |
} |
| 949 |
|
| 950 |
private String getNewImportString(String importName, boolean isStatic, String trailingComment, String lineDelim) { |
| 790 |
StringBuffer buf= new StringBuffer(); |
951 |
StringBuffer buf= new StringBuffer(); |
| 791 |
buf.append("import "); //$NON-NLS-1$ |
952 |
buf.append("import "); //$NON-NLS-1$ |
| 792 |
if (isStatic) { |
953 |
if (isStatic) { |
|
Lines 795-800
Link Here
|
| 795 |
buf.append(importName); |
956 |
buf.append(importName); |
| 796 |
if (insertSpaceBeforeSemicolon()) buf.append(' '); |
957 |
if (insertSpaceBeforeSemicolon()) buf.append(' '); |
| 797 |
buf.append(';'); |
958 |
buf.append(';'); |
|
|
959 |
if (trailingComment != null) { |
| 960 |
buf.append(trailingComment); |
| 961 |
} |
| 798 |
buf.append(lineDelim); |
962 |
buf.append(lineDelim); |
| 799 |
|
963 |
|
| 800 |
if (isStatic) { |
964 |
if (isStatic) { |
|
Lines 805-826
Link Here
|
| 805 |
return buf.toString(); |
969 |
return buf.toString(); |
| 806 |
} |
970 |
} |
| 807 |
|
971 |
|
| 808 |
private String[] getNewImportStrings(PackageEntry packageEntry, boolean isStatic, String lineDelim) { |
972 |
private String[] getNewImportStrings(IBuffer buffer, PackageEntry packageEntry, boolean isStatic, String lineDelim) { |
| 809 |
boolean isStarImportAdded = false; |
973 |
boolean isStarImportAdded = false; |
| 810 |
List allImports = new ArrayList(); |
974 |
List allImports = new ArrayList(); |
| 811 |
int nImports = packageEntry.getNumberOfImports(); |
975 |
int nImports = packageEntry.getNumberOfImports(); |
|
|
976 |
StringBuffer allComments = null; |
| 812 |
for (int i= 0; i < nImports; i++) { |
977 |
for (int i= 0; i < nImports; i++) { |
| 813 |
ImportDeclEntry curr= packageEntry.getImportAt(i); |
978 |
ImportDeclEntry curr= packageEntry.getImportAt(i); |
| 814 |
String simpleName = curr.getTypeQualifiedName(); |
979 |
String simpleName = curr.getTypeQualifiedName(); |
| 815 |
if (simpleName.indexOf('.') != -1) { |
980 |
if (simpleName.indexOf('.') != -1) { |
| 816 |
// member type imports - we preserve it |
981 |
// member type imports - we preserve it |
| 817 |
allImports.add(getNewImportString(curr.getElementName(), isStatic, lineDelim)); |
982 |
IRegion rangeBefore = curr.getPrecedingCommentRange(); |
|
|
983 |
if (rangeBefore != null) { |
| 984 |
allImports.add(buffer.getText(rangeBefore.getOffset(), rangeBefore.getLength())); |
| 985 |
} |
| 986 |
IRegion rangeAfter = curr.getTrailingCommentRange(); |
| 987 |
String trailingComment = null; |
| 988 |
if (rangeAfter != null) { |
| 989 |
trailingComment = buffer.getText(rangeAfter.getOffset(), rangeAfter.getLength()); |
| 990 |
} |
| 991 |
allImports.add(getNewImportString(curr.getElementName(), isStatic, trailingComment, lineDelim)); |
| 818 |
} else if (!isStarImportAdded) { |
992 |
} else if (!isStarImportAdded) { |
| 819 |
String starImportString= packageEntry.getName() + ".*"; //$NON-NLS-1$ |
993 |
String starImportString= packageEntry.getName() + ".*"; //$NON-NLS-1$ |
| 820 |
allImports.add(getNewImportString(starImportString, isStatic, lineDelim)); |
994 |
allImports.add(getNewImportString(starImportString, isStatic, lineDelim)); |
| 821 |
isStarImportAdded = true; |
995 |
isStarImportAdded = true; |
|
|
996 |
} else { |
| 997 |
// collect all comments |
| 998 |
IRegion rangeBefore = curr.getPrecedingCommentRange(); |
| 999 |
if (rangeBefore != null) { |
| 1000 |
if (allComments == null) { |
| 1001 |
allComments = new StringBuffer(); |
| 1002 |
} |
| 1003 |
allComments.append(buffer.getText(rangeBefore.getOffset(), rangeBefore.getLength())).append(lineDelim); |
| 1004 |
} |
| 1005 |
IRegion rangeAfter = curr.getTrailingCommentRange(); |
| 1006 |
if (rangeAfter != null) { |
| 1007 |
if (allComments == null) { |
| 1008 |
allComments = new StringBuffer(); |
| 1009 |
} |
| 1010 |
allComments.append(buffer.getText(rangeAfter.getOffset(), rangeAfter.getLength())).append(lineDelim); |
| 1011 |
} |
| 822 |
} |
1012 |
} |
| 823 |
} |
1013 |
} |
|
|
1014 |
if (allComments != null) { |
| 1015 |
allImports.add(0, String.valueOf(allComments)); |
| 1016 |
} |
| 824 |
return (String[]) allImports.toArray(new String[allImports.size()]); |
1017 |
return (String[]) allImports.toArray(new String[allImports.size()]); |
| 825 |
} |
1018 |
} |
| 826 |
|
1019 |
|
|
Lines 879-884
Link Here
|
| 879 |
private IRegion sourceRange; |
1072 |
private IRegion sourceRange; |
| 880 |
private final boolean isStatic; |
1073 |
private final boolean isStatic; |
| 881 |
private int containerNameLength; |
1074 |
private int containerNameLength; |
|
|
1075 |
private IRegion precedingCommentRange; |
| 1076 |
private IRegion trailingCommentRange; |
| 1077 |
|
| 1078 |
public ImportDeclEntry( |
| 1079 |
int containerNameLength, |
| 1080 |
String elementName, |
| 1081 |
boolean isStatic, |
| 1082 |
IRegion sourceRange, |
| 1083 |
IRegion precedingCommentRange, |
| 1084 |
IRegion trailingCommentRange) { |
| 1085 |
this(containerNameLength, elementName, isStatic, sourceRange); |
| 1086 |
this.precedingCommentRange = precedingCommentRange; |
| 1087 |
this.trailingCommentRange = trailingCommentRange; |
| 1088 |
} |
| 882 |
|
1089 |
|
| 883 |
public ImportDeclEntry(int containerNameLength, String elementName, boolean isStatic, IRegion sourceRange) { |
1090 |
public ImportDeclEntry(int containerNameLength, String elementName, boolean isStatic, IRegion sourceRange) { |
| 884 |
this.elementName= elementName; |
1091 |
this.elementName= elementName; |
|
Lines 929-935
Link Here
|
| 929 |
public IRegion getSourceRange() { |
1136 |
public IRegion getSourceRange() { |
| 930 |
return this.sourceRange; |
1137 |
return this.sourceRange; |
| 931 |
} |
1138 |
} |
|
|
1139 |
|
| 1140 |
public IRegion getPrecedingCommentRange() { |
| 1141 |
return this.precedingCommentRange; |
| 1142 |
} |
| 932 |
|
1143 |
|
|
|
1144 |
public IRegion getTrailingCommentRange() { |
| 1145 |
return this.trailingCommentRange; |
| 1146 |
} |
| 933 |
} |
1147 |
} |
| 934 |
|
1148 |
|
| 935 |
/* |
1149 |
/* |