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

Collapse All | Expand All

(-)dom/org/eclipse/jdt/internal/core/dom/rewrite/ImportRewriteAnalyzer.java (-10 / +36 lines)
Lines 266-272 Link Here
266
				currEndLine++;
266
				currEndLine++;
267
				nextOffset= root.getPosition(currEndLine, 0);
267
				nextOffset= root.getPosition(currEndLine, 0);
268
			}
268
			}
269
			currPackage.add(new ImportDeclEntry(name, isStatic, new Region(currOffset, nextOffset - currOffset)));
269
			currPackage.add(new ImportDeclEntry(packName.length(), name, isStatic, new Region(currOffset, nextOffset - currOffset)));
270
			currOffset= nextOffset;
270
			currOffset= nextOffset;
271
			curr= next;
271
			curr= next;
272
272
Lines 276-282 Link Here
276
276
277
				currPackage= new PackageEntry(); // create a comment package entry for this
277
				currPackage= new PackageEntry(); // create a comment package entry for this
278
				this.packageEntries.add(currPackage);
278
				this.packageEntries.add(currPackage);
279
				currPackage.add(new ImportDeclEntry(null, false, new Region(currOffset, nextOffset - currOffset)));
279
				currPackage.add(new ImportDeclEntry(packName.length(), null, false, new Region(currOffset, nextOffset - currOffset)));
280
280
281
				currOffset= nextOffset;
281
				currOffset= nextOffset;
282
			}
282
			}
Lines 291-297 Link Here
291
			this.packageEntries.add(currPackage);
291
			this.packageEntries.add(currPackage);
292
		}
292
		}
293
		int length= this.replaceRange.getOffset() + this.replaceRange.getLength() - curr.getStartPosition();
293
		int length= this.replaceRange.getOffset() + this.replaceRange.getLength() - curr.getStartPosition();
294
		currPackage.add(new ImportDeclEntry(name, isStatic, new Region(curr.getStartPosition(), length)));
294
		currPackage.add(new ImportDeclEntry(packName.length(), name, isStatic, new Region(curr.getStartPosition(), length)));
295
	}
295
	}
296
296
297
	/**
297
	/**
Lines 468-474 Link Here
468
468
469
	public void addImport(String fullTypeName, boolean isStatic) {
469
	public void addImport(String fullTypeName, boolean isStatic) {
470
		String typeContainerName= getQualifier(fullTypeName, isStatic);
470
		String typeContainerName= getQualifier(fullTypeName, isStatic);
471
		ImportDeclEntry decl= new ImportDeclEntry(fullTypeName, isStatic, null);
471
		ImportDeclEntry decl= new ImportDeclEntry(typeContainerName.length(), fullTypeName, isStatic, null);
472
		sortIn(typeContainerName, decl, isStatic);
472
		sortIn(typeContainerName, decl, isStatic);
473
	}
473
	}
474
474
Lines 617-625 Link Here
617
617
618
				boolean doStarImport= pack.hasStarImport(threshold, onDemandConflicts);
618
				boolean doStarImport= pack.hasStarImport(threshold, onDemandConflicts);
619
				if (doStarImport && (pack.find("*") == null)) { //$NON-NLS-1$
619
				if (doStarImport && (pack.find("*") == null)) { //$NON-NLS-1$
620
					String starImportString= pack.getName() + ".*"; //$NON-NLS-1$
620
					String[] imports = getNewImportStrings(pack, isStatic, lineDelim);
621
					String str= getNewImportString(starImportString, isStatic, lineDelim);
621
					for (int j = 0, max = imports.length; j < max; j++) {
622
					stringsToInsert.add(str);
622
						stringsToInsert.add(imports[j]);
623
					}
623
				}
624
				}
624
625
625
				for (int k= 0; k < nImports; k++) {
626
				for (int k= 0; k < nImports; k++) {
Lines 789-794 Link Here
789
		}
790
		}
790
		return buf.toString();
791
		return buf.toString();
791
	}
792
	}
793
	
794
	private String[] getNewImportStrings(PackageEntry packageEntry, boolean isStatic, String lineDelim) {
795
		boolean isStarImportAdded = false;
796
		List allImports = new ArrayList();
797
		int nImports = packageEntry.getNumberOfImports();
798
		for (int i= 0; i < nImports; i++) {
799
			ImportDeclEntry curr= packageEntry.getImportAt(i);
800
			String simpleName = curr.getTypeQualifiedName();
801
			if (simpleName.indexOf('.') != -1) {
802
				// member type imports - we preserve it
803
				allImports.add(getNewImportString(curr.getElementName(), isStatic, lineDelim));
804
			} else if (!isStarImportAdded) {
805
				String starImportString= packageEntry.getName() + ".*"; //$NON-NLS-1$
806
				allImports.add(getNewImportString(starImportString, isStatic, lineDelim));
807
				isStarImportAdded = true;
808
			}
809
		}
810
		return (String[]) allImports.toArray(new String[allImports.size()]);
811
	}
792
812
793
	private static int getFirstTypeBeginPos(CompilationUnit root) {
813
	private static int getFirstTypeBeginPos(CompilationUnit root) {
794
		List types= root.types();
814
		List types= root.types();
Lines 844-854 Link Here
844
		private String elementName;
864
		private String elementName;
845
		private IRegion sourceRange;
865
		private IRegion sourceRange;
846
		private final boolean isStatic;
866
		private final boolean isStatic;
867
		private int containerNameLength;
847
868
848
		public ImportDeclEntry(String elementName, boolean isStatic, IRegion sourceRange) {
869
		public ImportDeclEntry(int containerNameLength, String elementName, boolean isStatic, IRegion sourceRange) {
849
			this.elementName= elementName;
870
			this.elementName= elementName;
850
			this.sourceRange= sourceRange;
871
			this.sourceRange= sourceRange;
851
			this.isStatic= isStatic;
872
			this.isStatic= isStatic;
873
			this.containerNameLength = containerNameLength;
852
		}
874
		}
853
875
854
		public String getElementName() {
876
		public String getElementName() {
Lines 870-875 Link Here
870
			return Signature.getSimpleName(this.elementName);
892
			return Signature.getSimpleName(this.elementName);
871
		}
893
		}
872
894
895
		public String getTypeQualifiedName() {
896
			return this.elementName.substring(this.containerNameLength + 1);
897
		}
898
		
873
		public boolean isOnDemand() {
899
		public boolean isOnDemand() {
874
			return this.elementName != null && this.elementName.endsWith(".*"); //$NON-NLS-1$
900
			return this.elementName != null && this.elementName.endsWith(".*"); //$NON-NLS-1$
875
		}
901
		}
Lines 1078-1088 Link Here
1078
				int nImports= getNumberOfImports();
1104
				int nImports= getNumberOfImports();
1079
				for (int i= 0; i < nImports; i++) {
1105
				for (int i= 0; i < nImports; i++) {
1080
					ImportDeclEntry curr= getImportAt(i);
1106
					ImportDeclEntry curr= getImportAt(i);
1081
					buf.append("  "); //$NON-NLS-1$
1107
					buf.append(" "); //$NON-NLS-1$
1082
					if (curr.isStatic()) {
1108
					if (curr.isStatic()) {
1083
						buf.append("static "); //$NON-NLS-1$
1109
						buf.append("static "); //$NON-NLS-1$
1084
					}
1110
					}
1085
					buf.append(curr.getSimpleName());
1111
					buf.append(curr.getTypeQualifiedName());
1086
					if (curr.isNew()) {
1112
					if (curr.isNew()) {
1087
						buf.append(" (new)"); //$NON-NLS-1$
1113
						buf.append(" (new)"); //$NON-NLS-1$
1088
					}
1114
					}
(-)src/org/eclipse/jdt/core/tests/rewrite/describing/ImportRewriteTest.java (+33 lines)
Lines 302-307 Link Here
302
		assertEqualString(cu.getSource(), buf.toString());
302
		assertEqualString(cu.getSource(), buf.toString());
303
	}
303
	}
304
304
305
	public void testAddImports5() throws Exception {
306
		getJavaProject("P").setOption(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_SEMICOLON, JavaCore.INSERT);
307
		
308
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("pack1", false, null);
309
		StringBuffer buf= new StringBuffer();
310
		buf.append("package pack1;\n");
311
		buf.append("\n");
312
		buf.append("public class C {\n");
313
		buf.append("}\n");
314
		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
315
316
		String[] order= new String[] { "java", "java.util", "com", "pack" };
317
318
		ImportRewrite imports= newImportsRewrite(cu, order, 1, 1, true);
319
		imports.setUseContextToFilterImplicitImports(true);
320
		imports.addImport("java.util.Map");
321
		imports.addImport("java.util.Set");
322
		imports.addImport("java.util.Map.Entry");
323
		imports.addImport("java.util.Collections");
324
325
		apply(imports);
326
327
		buf= new StringBuffer();
328
		buf.append("package pack1;\n");
329
		buf.append("\n");
330
		buf.append("import java.util.* ;\n");
331
		buf.append("import java.util.Map.Entry ;\n");
332
		buf.append("\n");
333
		buf.append("public class C {\n");
334
		buf.append("}\n");
335
		assertEqualString(cu.getSource(), buf.toString());
336
	}
337
305
	public void testAddImportsWithGroupsOfUnmatched1() throws Exception {
338
	public void testAddImportsWithGroupsOfUnmatched1() throws Exception {
306
339
307
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("pack1", false, null);
340
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("pack1", false, null);

Return to bug 303776