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 / +220 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 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
	/*
(-)src/org/eclipse/jdt/core/tests/rewrite/describing/ImportRewriteTest.java (-1 / +181 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 1307-1312 Link Here
1307
		assertEqualString(units[2].getSource(), buf.toString());
1307
		assertEqualString(units[2].getSource(), buf.toString());
1308
	}
1308
	}
1309
1309
1310
	public void testAddImports_bug24804() throws Exception {
1311
1312
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("pack1", false, null);
1313
		StringBuffer buf= new StringBuffer();
1314
		buf.append("package pack1;\n");
1315
		buf.append("\n");
1316
		buf.append("import java.lang.String;\n");
1317
		buf.append("/** comment */\n");
1318
		buf.append("import java.lang.System;\n");
1319
		buf.append("\n");
1320
		buf.append("public class C {\n");
1321
		buf.append("}\n");
1322
		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
1323
1324
		String[] order= new String[] { "java" };
1325
1326
		ImportRewrite imports= newImportsRewrite(cu, order, 99, 99, false);
1327
		imports.addImport("java.io.Exception");
1328
1329
		apply(imports);
1330
1331
		buf= new StringBuffer();
1332
		buf.append("package pack1;\n");
1333
		buf.append("\n");
1334
		buf.append("import java.io.Exception;\n");
1335
		buf.append("/** comment */\n");
1336
		buf.append("\n");
1337
		buf.append("public class C {\n");
1338
		buf.append("}\n");
1339
		assertEqualString(cu.getSource(), buf.toString());
1340
	}
1341
1342
	public void testAddImports_bug24804_2() throws Exception {
1343
1344
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("pack1", false, null);
1345
		StringBuffer buf= new StringBuffer();
1346
		buf.append("package pack1;\n");
1347
		buf.append("\n");
1348
		buf.append("import java.lang.AssertionError;//test\n");
1349
		buf.append("\n");
1350
		buf.append("/** comment2 */\n");
1351
		buf.append("\n");
1352
		buf.append("/** comment */\n");
1353
		buf.append("import java.lang.System;\n");
1354
		buf.append("\n");
1355
		buf.append("public class C {\n");
1356
		buf.append("}\n");
1357
		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
1358
1359
		String[] order= new String[] { "java" };
1360
1361
		ImportRewrite imports= newImportsRewrite(cu, order, 99, 99, true);
1362
		imports.addImport("java.io.Exception");
1363
1364
		apply(imports);
1365
1366
		buf= new StringBuffer();
1367
		buf.append("package pack1;\n");
1368
		buf.append("\n");
1369
		buf.append("import java.io.Exception;\n");
1370
		buf.append("import java.lang.AssertionError;//test\n");
1371
		buf.append("\n");
1372
		buf.append("/** comment2 */\n");
1373
		buf.append("\n");
1374
		buf.append("/** comment */\n");
1375
		buf.append("import java.lang.System;\n");
1376
		buf.append("\n");
1377
		buf.append("public class C {\n");
1378
		buf.append("}\n");
1379
		assertEqualString(cu.getSource(), buf.toString());
1380
	}
1381
1382
	public void testAddImports_bug24804_3() throws Exception {
1383
1384
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("pack1", false, null);
1385
		StringBuffer buf= new StringBuffer();
1386
		buf.append("package pack1;\n");
1387
		buf.append("\n");
1388
		buf.append("import java.lang.String;//test\n");
1389
		buf.append("/** comment */\n");
1390
		buf.append("import java.lang.System;\n");
1391
		buf.append("\n");
1392
		buf.append("public class C {\n");
1393
		buf.append("}\n");
1394
		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
1395
1396
		String[] order= new String[] { "java" };
1397
1398
		ImportRewrite imports= newImportsRewrite(cu, order, 99, 99, false);
1399
		imports.addImport("java.io.Exception");
1400
1401
		apply(imports);
1402
1403
		buf= new StringBuffer();
1404
		buf.append("package pack1;\n");
1405
		buf.append("\n");
1406
		buf.append("import java.io.Exception;\n");
1407
		buf.append("//test\n");
1408
		buf.append("/** comment */\n");
1409
		buf.append("\n");
1410
		buf.append("public class C {\n");
1411
		buf.append("}\n");
1412
		assertEqualString(cu.getSource(), buf.toString());
1413
	}
1414
1415
	public void testAddImports_bug24804_4() throws Exception {
1416
1417
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("pack1", false, null);
1418
		StringBuffer buf= new StringBuffer();
1419
		buf.append("package pack1;\n");
1420
		buf.append("\n");
1421
		buf.append("import java.lang.AssertionError;//test\n");
1422
		buf.append("\n");
1423
		buf.append("/** comment2 */\n");
1424
		buf.append("\n");
1425
		buf.append("/** comment */\n");
1426
		buf.append("import java.lang.System; /** comment3 */\n");
1427
		buf.append("\n");
1428
		buf.append("public class C {\n");
1429
		buf.append("}\n");
1430
		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
1431
1432
		String[] order= new String[] { "java" };
1433
1434
		ImportRewrite imports= newImportsRewrite(cu, order, 99, 99, false);
1435
		imports.addImport("java.io.Exception");
1436
1437
		apply(imports);
1438
1439
		buf= new StringBuffer();
1440
		buf.append("package pack1;\n");
1441
		buf.append("\n");
1442
		buf.append("import java.io.Exception;\n");
1443
		buf.append("//test\n");
1444
		buf.append("/** comment2 */\n");
1445
		buf.append("/** comment */\n");
1446
		buf.append("/** comment3 */\n");
1447
		buf.append("\n");
1448
		buf.append("public class C {\n");
1449
		buf.append("}\n");
1450
		assertEqualString(cu.getSource(), buf.toString());
1451
	}
1452
1453
	public void testAddImports_bug24804_5() throws Exception {
1454
1455
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("pack1", false, null);
1456
		StringBuffer buf= new StringBuffer();
1457
		buf.append("package pack1;\n");
1458
		buf.append("\n");
1459
		buf.append("import java.lang.AssertionError; //test\n");
1460
		buf.append("\n");
1461
		buf.append("/** comment2 */\n");
1462
		buf.append("\n");
1463
		buf.append("/** comment */\n");
1464
		buf.append("import java.lang.System;\n");
1465
		buf.append("\n");
1466
		buf.append("public class C {\n");
1467
		buf.append("}\n");
1468
		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
1469
1470
		String[] order= new String[] { "java" };
1471
1472
		ImportRewrite imports= newImportsRewrite(cu, order, 1, 1, false);
1473
		imports.addImport("java.io.Exception");
1474
1475
		apply(imports);
1476
1477
		buf= new StringBuffer();
1478
		buf.append("package pack1;\n");
1479
		buf.append("\n");
1480
		buf.append("import java.io.*;\n");
1481
		buf.append("//test\n");
1482
		buf.append("/** comment2 */\n");
1483
		buf.append("/** comment */\n");
1484
		buf.append("\n");
1485
		buf.append("public class C {\n");
1486
		buf.append("}\n");
1487
		assertEqualString(cu.getSource(), buf.toString());
1488
	}
1489
1310
	private void assertAddedAndRemoved(ImportRewrite imports, String[] expectedAdded, String[] expectedRemoved, String[] expectedAddedStatic, String[] expectedRemovedStatic) {
1490
	private void assertAddedAndRemoved(ImportRewrite imports, String[] expectedAdded, String[] expectedRemoved, String[] expectedAddedStatic, String[] expectedRemovedStatic) {
1311
		assertEqualStringsIgnoreOrder(imports.getAddedImports(), expectedAdded);
1491
		assertEqualStringsIgnoreOrder(imports.getAddedImports(), expectedAdded);
1312
		assertEqualStringsIgnoreOrder(imports.getAddedStaticImports(), expectedAddedStatic);
1492
		assertEqualStringsIgnoreOrder(imports.getAddedStaticImports(), expectedAddedStatic);
(-)ui/org/eclipse/jdt/ui/tests/core/ImportOrganizeTest.java (+5 lines)
Lines 578-583 Link Here
578
		buf.append("package pack1;\r\n");
578
		buf.append("package pack1;\r\n");
579
		buf.append("\r\n");
579
		buf.append("\r\n");
580
		buf.append("import java.util.Vector;\r\n");
580
		buf.append("import java.util.Vector;\r\n");
581
		buf.append("// comment\r\n");
581
		buf.append("\r\n");
582
		buf.append("\r\n");
582
		buf.append("public class C {\r\n");
583
		buf.append("public class C {\r\n");
583
		buf.append("    Vector v;\r\n");
584
		buf.append("    Vector v;\r\n");
Lines 2575-2580 Link Here
2575
		buf.append("\n");
2576
		buf.append("\n");
2576
		buf.append("import java.util.ArrayList;\n");
2577
		buf.append("import java.util.ArrayList;\n");
2577
		buf.append("import java.util.HashMap;\n");
2578
		buf.append("import java.util.HashMap;\n");
2579
		buf.append("// some comment;\n");
2580
		buf.append("/*another comment*/\n");
2578
		buf.append("\n");
2581
		buf.append("\n");
2579
		buf.append("public class C {\n");
2582
		buf.append("public class C {\n");
2580
		buf.append("    public void foo() {\n");
2583
		buf.append("    public void foo() {\n");
Lines 2625-2630 Link Here
2625
		buf.append("import java.util.HashMap;\n");
2628
		buf.append("import java.util.HashMap;\n");
2626
		buf.append("\n");
2629
		buf.append("\n");
2627
		buf.append("import static java.io.File.pathSeparator;\n");
2630
		buf.append("import static java.io.File.pathSeparator;\n");
2631
		buf.append("// some comment;\n");
2632
		buf.append("/*another comment*/\n");
2628
		buf.append("\n");
2633
		buf.append("\n");
2629
		buf.append("public class C {\n");
2634
		buf.append("public class C {\n");
2630
		buf.append("    public void foo() {\n");
2635
		buf.append("    public void foo() {\n");

Return to bug 24804