View | Details | Raw Unified | Return to bug 24804 | Differences between
and this patch

Collapse All | Expand All

(-)dom/org/eclipse/jdt/internal/core/dom/rewrite/ImportRewriteAnalyzer.java (-6 / +146 lines)
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 58-63 Link Here
58
	private boolean useContextToFilterImplicitImports;
58
	private boolean useContextToFilterImplicitImports;
59
	private boolean findAmbiguousImports;
59
	private boolean findAmbiguousImports;
60
60
61
	private IRegion[] preserveExistingCommentsRanges;
62
61
	private int flags= 0;
63
	private int flags= 0;
62
64
63
	private static final int F_NEEDS_LEADING_DELIM= 2;
65
	private static final int F_NEEDS_LEADING_DELIM= 2;
Lines 89-94 Link Here
89
		this.replaceRange= evaluateReplaceRange(root);
91
		this.replaceRange= evaluateReplaceRange(root);
90
		if (restoreExistingImports) {
92
		if (restoreExistingImports) {
91
			addExistingImports(root);
93
			addExistingImports(root);
94
		} else {
95
			// collect all existing comments inside imports and concatenate them
96
			this.preserveExistingCommentsRanges = retrieveExistingCommentsInImports(root);
92
		}
97
		}
93
98
94
		PackageEntry[] order= new PackageEntry[importOrder.length];
99
		PackageEntry[] order= new PackageEntry[importOrder.length];
Lines 261-272 Link Here
261
			int nextLength= next.getLength();
266
			int nextLength= next.getLength();
262
			int nextOffsetLine= root.getLineNumber(nextOffset);
267
			int nextOffsetLine= root.getLineNumber(nextOffset);
263
268
269
			int extendedStart = root.getExtendedStartPosition(next);
270
			int extendedLength = root.getExtendedLength(next);
264
			// if next import is on a different line, modify the end position to the next line begin offset
271
			// if next import is on a different line, modify the end position to the next line begin offset
265
			if (currEndLine < nextOffsetLine) {
272
			if (currEndLine < nextOffsetLine) {
266
				currEndLine++;
273
				currEndLine++;
267
				nextOffset= root.getPosition(currEndLine, 0);
274
				nextOffset= root.getPosition(currEndLine, 0);
268
			}
275
			}
269
			currPackage.add(new ImportDeclEntry(packName.length(), name, isStatic, new Region(currOffset, nextOffset - currOffset)));
276
			// retrieve preceding and trailing comments if any
277
			IRegion rangeBefore = null;
278
			IRegion rangeAfter = null;
279
			if (nextOffset != extendedStart) {
280
				rangeBefore = new Region(extendedStart, extendedStart - nextOffset + 1);
281
			}
282
			if (nextLength != extendedLength) {
283
				rangeAfter = new Region(nextOffset + nextLength, extendedLength - nextLength + 1);
284
			}
285
			currPackage.add(
286
					new ImportDeclEntry(
287
							packName.length(), 
288
							name, 
289
							isStatic, 
290
							new Region(currOffset, nextOffset - currOffset),
291
							rangeBefore,
292
							rangeAfter));
270
			currOffset= nextOffset;
293
			currOffset= nextOffset;
271
			curr= next;
294
			curr= next;
272
295
Lines 294-299 Link Here
294
		currPackage.add(new ImportDeclEntry(packName.length(), name, isStatic, new Region(curr.getStartPosition(), length)));
317
		currPackage.add(new ImportDeclEntry(packName.length(), name, isStatic, new Region(curr.getStartPosition(), length)));
295
	}
318
	}
296
319
320
	private IRegion[] retrieveExistingCommentsInImports(CompilationUnit root) {
321
		List/*ImportDeclaration*/ decls= root.imports();
322
		if (decls.isEmpty()) {
323
			return null;
324
		}
325
		
326
		List regions = new ArrayList();
327
		
328
		/* for the first comment, we only take the trailing comment if any and the replace range doesn't 
329
		 * include the preceding comment
330
		 */ 
331
		for (int i= 0; i < decls.size(); i++) {
332
			ImportDeclaration next= (ImportDeclaration) decls.get(i);
333
			int nextOffset= next.getStartPosition();
334
			int nextLength= next.getLength();
335
336
			int extendedStart = root.getExtendedStartPosition(next);
337
			int extendedLength = root.getExtendedLength(next);
338
			if (nextOffset != extendedStart) {
339
				// preceding comment
340
				int lengthOfPrecedingComment = nextOffset - extendedStart;
341
				if (i != 0) {
342
					regions.add(new Region(extendedStart, lengthOfPrecedingComment));
343
				}
344
				
345
				if (extendedLength != (nextLength + lengthOfPrecedingComment)) {
346
					// Preceding and trailing comments 
347
					int regionLength = extendedLength - (nextLength + lengthOfPrecedingComment);
348
					regions.add(new Region(nextOffset + nextLength, regionLength));
349
				}
350
			} else if (nextLength != extendedLength) {
351
				// no extended start - only trailing comment
352
				int regionLength = extendedLength - nextLength;
353
				regions.add(new Region(nextOffset + nextLength, regionLength));
354
			}
355
		}
356
		return (IRegion[]) regions.toArray(new IRegion[regions.size()]);
357
	}
297
	/**
358
	/**
298
	 * Specifies that implicit imports (for types in <code>java.lang</code>, types in the same package as the rewrite
359
	 * 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
360
	 * compilation unit and types in the compilation unit's main type) should not be created, except if necessary to
Lines 550-555 Link Here
550
					}
611
					}
551
				}
612
				}
552
			}
613
			}
614
			// concatenate existing comments inside the imports
615
			
553
			return new Region(startPos, endPos - startPos);
616
			return new Region(startPos, endPos - startPos);
554
		} else {
617
		} else {
555
			int start= getPackageStatementEndPos(root);
618
			int start= getPackageStatementEndPos(root);
Lines 617-623 Link Here
617
680
618
				boolean doStarImport= pack.hasStarImport(threshold, onDemandConflicts);
681
				boolean doStarImport= pack.hasStarImport(threshold, onDemandConflicts);
619
				if (doStarImport && (pack.find("*") == null)) { //$NON-NLS-1$
682
				if (doStarImport && (pack.find("*") == null)) { //$NON-NLS-1$
620
					String[] imports = getNewImportStrings(pack, isStatic, lineDelim);
683
					String[] imports = getNewImportStrings(buffer, pack, isStatic, lineDelim);
621
					for (int j = 0, max = imports.length; j < max; j++) {
684
					for (int j = 0, max = imports.length; j < max; j++) {
622
						stringsToInsert.add(imports[j]);
685
						stringsToInsert.add(imports[j]);
623
					}
686
					}
Lines 648-654 Link Here
648
					} else if (doStarImport && !currDecl.isOnDemand()) {
711
					} else if (doStarImport && !currDecl.isOnDemand()) {
649
						String simpleName = currDecl.getTypeQualifiedName();
712
						String simpleName = currDecl.getTypeQualifiedName();
650
						if (simpleName.indexOf('.') != -1) {
713
						if (simpleName.indexOf('.') != -1) {
651
							String str= getNewImportString(currDecl.getElementName(), isStatic, lineDelim);
714
							IRegion rangeBefore = currDecl.getPrecedingCommentRage();
715
							if (rangeBefore != null) {
716
								stringsToInsert.add(buffer.getText(rangeBefore.getOffset(), rangeBefore.getLength()));
717
							}
718
							IRegion rangeAfter = currDecl.getTrailingCommentRange();
719
							String trailingComment = null;
720
							if (rangeAfter != null) {
721
								trailingComment = buffer.getText(rangeAfter.getOffset(), rangeAfter.getLength());
722
							}
723
							String str= getNewImportString(currDecl.getElementName(), isStatic, trailingComment, lineDelim);
652
							if (stringsToInsert.indexOf(str) == -1) {
724
							if (stringsToInsert.indexOf(str) == -1) {
653
								stringsToInsert.add(str);
725
								stringsToInsert.add(str);
654
							}
726
							}
Lines 657-662 Link Here
657
				}
729
				}
658
			}
730
			}
659
731
732
			// insert back all existing imports comments since existing imports were not preserved
733
			if (this.preserveExistingCommentsRanges != null) {
734
				for (int i = 0, max = this.preserveExistingCommentsRanges.length; i < max; i++) {
735
					IRegion region = this.preserveExistingCommentsRanges[i];
736
					String text = buffer.getText(region.getOffset(), region.getLength());
737
					if (!text.endsWith(lineDelim)) {
738
						text += lineDelim;
739
					}
740
					stringsToInsert.add(text);
741
				}
742
			}
660
			int end= importsStart + importsLen;
743
			int end= importsStart + importsLen;
661
			removeAndInsertNew(buffer, currPos, end, stringsToInsert, resEdit);
744
			removeAndInsertNew(buffer, currPos, end, stringsToInsert, resEdit);
662
745
Lines 787-792 Link Here
787
	}
870
	}
788
871
789
	private String getNewImportString(String importName, boolean isStatic, String lineDelim) {
872
	private String getNewImportString(String importName, boolean isStatic, String lineDelim) {
873
		return getNewImportString(importName, isStatic, null, lineDelim);
874
	}
875
	
876
	private String getNewImportString(String importName, boolean isStatic, String trailingComment, String lineDelim) {
790
		StringBuffer buf= new StringBuffer();
877
		StringBuffer buf= new StringBuffer();
791
		buf.append("import "); //$NON-NLS-1$
878
		buf.append("import "); //$NON-NLS-1$
792
		if (isStatic) {
879
		if (isStatic) {
Lines 795-800 Link Here
795
		buf.append(importName);
882
		buf.append(importName);
796
		if (insertSpaceBeforeSemicolon()) buf.append(' ');
883
		if (insertSpaceBeforeSemicolon()) buf.append(' ');
797
		buf.append(';');
884
		buf.append(';');
885
		if (trailingComment != null) {
886
			buf.append(trailingComment);
887
		}
798
		buf.append(lineDelim);
888
		buf.append(lineDelim);
799
889
800
		if (isStatic) {
890
		if (isStatic) {
Lines 805-826 Link Here
805
		return buf.toString();
895
		return buf.toString();
806
	}
896
	}
807
	
897
	
808
	private String[] getNewImportStrings(PackageEntry packageEntry, boolean isStatic, String lineDelim) {
898
	private String[] getNewImportStrings(IBuffer buffer, PackageEntry packageEntry, boolean isStatic, String lineDelim) {
809
		boolean isStarImportAdded = false;
899
		boolean isStarImportAdded = false;
810
		List allImports = new ArrayList();
900
		List allImports = new ArrayList();
811
		int nImports = packageEntry.getNumberOfImports();
901
		int nImports = packageEntry.getNumberOfImports();
902
		StringBuffer allComments = null;
812
		for (int i= 0; i < nImports; i++) {
903
		for (int i= 0; i < nImports; i++) {
813
			ImportDeclEntry curr= packageEntry.getImportAt(i);
904
			ImportDeclEntry curr= packageEntry.getImportAt(i);
814
			String simpleName = curr.getTypeQualifiedName();
905
			String simpleName = curr.getTypeQualifiedName();
815
			if (simpleName.indexOf('.') != -1) {
906
			if (simpleName.indexOf('.') != -1) {
816
				// member type imports - we preserve it
907
				// member type imports - we preserve it
817
				allImports.add(getNewImportString(curr.getElementName(), isStatic, lineDelim));
908
				IRegion rangeBefore = curr.getPrecedingCommentRage();
909
				if (rangeBefore != null) {
910
					allImports.add(buffer.getText(rangeBefore.getOffset(), rangeBefore.getLength()));
911
				}
912
				IRegion rangeAfter = curr.getTrailingCommentRange();
913
				String trailingComment = null;
914
				if (rangeAfter != null) {
915
					trailingComment = buffer.getText(rangeAfter.getOffset(), rangeAfter.getLength());
916
				}
917
				allImports.add(getNewImportString(curr.getElementName(), isStatic, trailingComment, lineDelim));
818
			} else if (!isStarImportAdded) {
918
			} else if (!isStarImportAdded) {
819
				String starImportString= packageEntry.getName() + ".*"; //$NON-NLS-1$
919
				String starImportString= packageEntry.getName() + ".*"; //$NON-NLS-1$
820
				allImports.add(getNewImportString(starImportString, isStatic, lineDelim));
920
				allImports.add(getNewImportString(starImportString, isStatic, lineDelim));
821
				isStarImportAdded = true;
921
				isStarImportAdded = true;
922
			} else {
923
				// collect all comments
924
				IRegion rangeBefore = curr.getPrecedingCommentRage();
925
				if (rangeBefore != null) {
926
					if (allComments == null) {
927
						allComments = new StringBuffer();
928
					}
929
					allComments.append(buffer.getText(rangeBefore.getOffset(), rangeBefore.getLength())).append(lineDelim);
930
				}
931
				IRegion rangeAfter = curr.getTrailingCommentRange();
932
				if (rangeAfter != null) {
933
					if (allComments == null) {
934
						allComments = new StringBuffer();
935
					}
936
					allComments.append(buffer.getText(rangeAfter.getOffset(), rangeAfter.getLength())).append(lineDelim);
937
				}
822
			}
938
			}
823
		}
939
		}
940
		if (allComments != null) {
941
			allImports.add(0, String.valueOf(allComments));
942
		}
824
		return (String[]) allImports.toArray(new String[allImports.size()]);
943
		return (String[]) allImports.toArray(new String[allImports.size()]);
825
	}
944
	}
826
945
Lines 879-884 Link Here
879
		private IRegion sourceRange;
998
		private IRegion sourceRange;
880
		private final boolean isStatic;
999
		private final boolean isStatic;
881
		private int containerNameLength;
1000
		private int containerNameLength;
1001
		private IRegion precedingCommentRange;
1002
		private IRegion trailingCommentRange;
1003
1004
		public ImportDeclEntry(
1005
				int containerNameLength,
1006
				String elementName,
1007
				boolean isStatic,
1008
				IRegion sourceRange,
1009
				IRegion precedingCommentRange,
1010
				IRegion trailingCommentRange) {
1011
			this(containerNameLength, elementName, isStatic, sourceRange);
1012
			this.precedingCommentRange = precedingCommentRange;
1013
			this.trailingCommentRange = trailingCommentRange;
1014
		}
882
1015
883
		public ImportDeclEntry(int containerNameLength, String elementName, boolean isStatic, IRegion sourceRange) {
1016
		public ImportDeclEntry(int containerNameLength, String elementName, boolean isStatic, IRegion sourceRange) {
884
			this.elementName= elementName;
1017
			this.elementName= elementName;
Lines 929-935 Link Here
929
		public IRegion getSourceRange() {
1062
		public IRegion getSourceRange() {
930
			return this.sourceRange;
1063
			return this.sourceRange;
931
		}
1064
		}
1065
		
1066
		public IRegion getPrecedingCommentRage() {
1067
			return this.precedingCommentRange;
1068
		}
932
1069
1070
		public IRegion getTrailingCommentRange() {
1071
			return this.trailingCommentRange;
1072
		}
933
	}
1073
	}
934
1074
935
	/*
1075
	/*

Return to bug 24804