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

Collapse All | Expand All

(-)dom/org/eclipse/jdt/internal/core/dom/rewrite/ImportRewriteAnalyzer.java (-8 / +35 lines)
Lines 24-29 Link Here
24
import org.eclipse.jdt.core.JavaCore;
24
import org.eclipse.jdt.core.JavaCore;
25
import org.eclipse.jdt.core.JavaModelException;
25
import org.eclipse.jdt.core.JavaModelException;
26
import org.eclipse.jdt.core.Signature;
26
import org.eclipse.jdt.core.Signature;
27
import org.eclipse.jdt.core.compiler.CharOperation;
27
import org.eclipse.jdt.core.dom.ASTNode;
28
import org.eclipse.jdt.core.dom.ASTNode;
28
import org.eclipse.jdt.core.dom.CompilationUnit;
29
import org.eclipse.jdt.core.dom.CompilationUnit;
29
import org.eclipse.jdt.core.dom.ImportDeclaration;
30
import org.eclipse.jdt.core.dom.ImportDeclaration;
Lines 33-38 Link Here
33
import org.eclipse.jdt.core.search.IJavaSearchScope;
34
import org.eclipse.jdt.core.search.IJavaSearchScope;
34
import org.eclipse.jdt.core.search.SearchEngine;
35
import org.eclipse.jdt.core.search.SearchEngine;
35
import org.eclipse.jdt.core.search.TypeNameRequestor;
36
import org.eclipse.jdt.core.search.TypeNameRequestor;
37
import org.eclipse.jdt.internal.core.JavaProject;
36
import org.eclipse.jface.text.IRegion;
38
import org.eclipse.jface.text.IRegion;
37
import org.eclipse.jface.text.Region;
39
import org.eclipse.jface.text.Region;
38
import org.eclipse.text.edits.DeleteEdit;
40
import org.eclipse.text.edits.DeleteEdit;
Lines 168-176 Link Here
168
		}
170
		}
169
	}
171
	}
170
172
171
	private static String getQualifier(ImportDeclaration decl) {
173
	private String getQualifier(ImportDeclaration decl) {
172
		String name= decl.getName().getFullyQualifiedName();
174
		String name= decl.getName().getFullyQualifiedName();
173
		return decl.isOnDemand() ? name : Signature.getQualifier(name);
175
		if (decl.isOnDemand()) {
176
			return name;
177
		}
178
		return getQualifier(name, decl.isStatic());
179
	}
180
181
	private String getQualifier(String name, boolean isStatic) {
182
		// For static imports, return the Type name as well as part of the qualifier
183
		if (isStatic) {
184
			return Signature.getQualifier(name);
185
		}
186
		do {
187
			IJavaElement fragment = null;
188
			try {
189
				fragment = ((JavaProject) this.compilationUnit.getJavaProject()).findPackageFragment(name);
190
			} catch (JavaModelException e) {
191
				// Should never happen
192
			}
193
			// If it's the last fragment, return as Types in default packages aren't allowed in imports
194
			if (fragment != null || CharOperation.indexOf(Signature.C_DOT, name.toCharArray()) == -1 ) {
195
				break;
196
			}
197
			name = Signature.getQualifier(name);
198
		} while (true);
199
		return name;
174
	}
200
	}
175
201
176
	private static String getFullName(ImportDeclaration decl) {
202
	private static String getFullName(ImportDeclaration decl) {
Lines 401-413 Link Here
401
	}
427
	}
402
428
403
	public void addImport(String fullTypeName, boolean isStatic) {
429
	public void addImport(String fullTypeName, boolean isStatic) {
404
		String typeContainerName= Signature.getQualifier(fullTypeName);
430
		String typeContainerName= getQualifier(fullTypeName, isStatic);
405
		ImportDeclEntry decl= new ImportDeclEntry(fullTypeName, isStatic, null);
431
		ImportDeclEntry decl= new ImportDeclEntry(fullTypeName, isStatic, null);
406
		sortIn(typeContainerName, decl, isStatic);
432
		sortIn(typeContainerName, decl, isStatic);
407
	}
433
	}
408
434
409
	public boolean removeImport(String qualifiedName, boolean isStatic) {
435
	public boolean removeImport(String qualifiedName, boolean isStatic) {
410
		String containerName= Signature.getQualifier(qualifiedName);
436
		String containerName= getQualifier(qualifiedName, isStatic);  
411
437
412
		int nPackages= this.packageEntries.size();
438
		int nPackages= this.packageEntries.size();
413
		for (int i= 0; i < nPackages; i++) {
439
		for (int i= 0; i < nPackages; i++) {
Lines 525-531 Link Here
525
				int nImports= pack.getNumberOfImports();
551
				int nImports= pack.getNumberOfImports();
526
552
527
				if (this.filterImplicitImports && !pack.isStatic() && isImplicitImport(pack.getName(), this.compilationUnit)) {
553
				if (this.filterImplicitImports && !pack.isStatic() && isImplicitImport(pack.getName(), this.compilationUnit)) {
528
					pack.removeAllNew(onDemandConflicts);
554
					pack.filterImplicitImports(this.compilationUnit);
529
					nImports= pack.getNumberOfImports();
555
					nImports= pack.getNumberOfImports();
530
				}
556
				}
531
				if (nImports == 0) {
557
				if (nImports == 0) {
Lines 930-945 Link Here
930
			return false;
956
			return false;
931
		}
957
		}
932
958
933
		public void removeAllNew(Set onDemandConflicts) {
959
		public void filterImplicitImports(ICompilationUnit compilationUnit) {
934
			int nInports= this.importEntries.size();
960
			int nInports= this.importEntries.size();
935
			for (int i= nInports - 1; i >= 0; i--) {
961
			for (int i= nInports - 1; i >= 0; i--) {
936
				ImportDeclEntry curr= getImportAt(i);
962
				ImportDeclEntry curr= getImportAt(i);
937
				if (curr.isNew() /*&& (onDemandConflicts == null || onDemandConflicts.contains(curr.getSimpleName()))*/) {
963
				boolean internalClassImport = curr.getElementName().lastIndexOf('.') > getName().length();
964
				if (curr.isNew() && !internalClassImport) {
938
					this.importEntries.remove(i);
965
					this.importEntries.remove(i);
939
				}
966
				}
940
			}
967
			}
941
		}
968
		}
942
969
		
943
		public ImportDeclEntry getImportAt(int index) {
970
		public ImportDeclEntry getImportAt(int index) {
944
			return (ImportDeclEntry) this.importEntries.get(index);
971
			return (ImportDeclEntry) this.importEntries.get(index);
945
		}
972
		}
(-)src/org/eclipse/jdt/core/tests/rewrite/describing/ImportRewriteTest.java (-1 / +99 lines)
Lines 75-81 Link Here
75
75
76
76
77
		this.sourceFolder = getPackageFragmentRoot("P", "src");
77
		this.sourceFolder = getPackageFragmentRoot("P", "src");
78
78
		this.sourceFolder.createPackageFragment("java.util", false, null);
79
		this.sourceFolder.createPackageFragment("java.net", false, null);
80
		this.sourceFolder.createPackageFragment("java.awt", false, null);
81
		
79
		waitUntilIndexesReady();
82
		waitUntilIndexesReady();
80
	}
83
	}
81
84
Lines 572-577 Link Here
572
		assertEqualString(cu.getSource(), buf.toString());
575
		assertEqualString(cu.getSource(), buf.toString());
573
	}
576
	}
574
577
578
	/**
579
	 * Test that the Inner class import comes in the right order (i.e. after the enclosing type's import) when re-organized
580
	 * 
581
	 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=194358"
582
	 */
583
	public void testAddImports_bug194358() throws Exception {
584
585
	  StringBuffer buf= new StringBuffer();
586
	  buf.append("package pack1;\n");
587
	  buf.append("\n");
588
	  buf.append("import pack2.A;\n");
589
	  buf.append("import pack2.A.Inner;\n");
590
	  buf.append("import pack2.B;\n");
591
	  buf.append("\n");
592
	  buf.append("public class C {\n");
593
	  buf.append("}\n");
594
595
      IPackageFragment pack1= this.sourceFolder.createPackageFragment("pack1", false, null);
596
	  ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
597
598
	  // We need to actually make some state in the AST for the classes, to test that we can 
599
	  // disambiguate between packages and inner classes (see the bug for details).
600
	  IPackageFragment pack2= this.sourceFolder.createPackageFragment("pack2", false, null);
601
	  ICompilationUnit aUnit= pack2.createCompilationUnit("A.java", "", false, null);
602
	  ICompilationUnit bUnit= pack2.createCompilationUnit("B.java", "", false, null);
603
	  bUnit.createType("class B {}", null, false, null);
604
605
	  IType aType= aUnit.createType("class A {}", null, false, null);
606
	  aType.createType("class Inner {}", null, false, null);
607
	  String[] order= new String[] { "java" };
608
609
	  ImportRewrite imports= newImportsRewrite(cu, order, 99, 99, false);
610
	  imports.addImport("pack2.A");
611
	  imports.addImport("pack2.A.Inner");
612
	  imports.addImport("pack2.B");
613
614
	  apply(imports);
615
616
	  buf= new StringBuffer();
617
	  buf.append("package pack1;\n");
618
	  buf.append("\n");
619
	  buf.append("import pack2.A;\n");
620
	  buf.append("import pack2.A.Inner;\n");
621
	  buf.append("import pack2.B;\n");
622
	  buf.append("\n");
623
	  buf.append("public class C {\n");
624
	  buf.append("}\n");
625
	  assertEqualString(cu.getSource(), buf.toString());
626
	}
627
628
	/**
629
	 * Test that a valid inner class import is not removed even if it's in the same package.
630
	 * 
631
	 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=235253"
632
	 */
633
	public void testAddImports_bug235253() throws Exception {
634
	  StringBuffer buf= new StringBuffer();
635
	  buf.append("package com.pack1;\n");
636
	  buf.append("\n");
637
	  buf.append("import com.pack1.A;\n");
638
	  buf.append("import com.pack1.A.Inner;\n");
639
	  buf.append("import com.pack2.B;\n");
640
	  buf.append("\n");
641
	  buf.append("public class C {\n");
642
	  buf.append("}\n");
643
644
      IPackageFragment pack1= this.sourceFolder.createPackageFragment("com.pack1", false, null);
645
	  ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
646
      ICompilationUnit aUnit= pack1.createCompilationUnit("A.java", "", false, null);
647
648
	  IPackageFragment pack2= this.sourceFolder.createPackageFragment("com.pack2", false, null);
649
      ICompilationUnit bUnit= pack2.createCompilationUnit("B.java", "", false, null);
650
	  bUnit.createType("class B {}", null, false, null);
651
	  IType aType= aUnit.createType("class A {}", null, false, null);
652
	  aType.createType("class Inner {}", null, false, null);
653
	  String[] order= new String[] { "java" };
654
655
	  ImportRewrite imports= newImportsRewrite(cu, order, 99, 99, false);
656
	  imports.addImport("com.pack1.A");
657
	  imports.addImport("com.pack1.A.Inner");
658
	  imports.addImport("com.pack2.B");
659
660
	  apply(imports);
661
662
	  buf= new StringBuffer();
663
	  buf.append("package com.pack1;\n");
664
	  buf.append("\n");
665
	  buf.append("import com.pack1.A.Inner;\n");
666
	  buf.append("import com.pack2.B;\n");
667
	  buf.append("\n");
668
	  buf.append("public class C {\n");
669
	  buf.append("}\n");
670
	  assertEqualString(cu.getSource(), buf.toString());
671
	}
672
	
575
	public void testAddStaticImports1() throws Exception {
673
	public void testAddStaticImports1() throws Exception {
576
674
577
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("pack1", false, null);
675
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("pack1", false, null);
(-)src/org/eclipse/jdt/core/tests/performance/FullSourceWorkspaceASTTests.java (+40 lines)
Lines 19-28 Link Here
19
19
20
import junit.framework.*;
20
import junit.framework.*;
21
21
22
import org.eclipse.core.runtime.CoreException;
22
import org.eclipse.jdt.core.*;
23
import org.eclipse.jdt.core.*;
23
import org.eclipse.jdt.core.compiler.IProblem;
24
import org.eclipse.jdt.core.compiler.IProblem;
24
import org.eclipse.jdt.core.dom.*;
25
import org.eclipse.jdt.core.dom.*;
26
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
25
import org.eclipse.jdt.core.tests.model.AbstractJavaModelTests;
27
import org.eclipse.jdt.core.tests.model.AbstractJavaModelTests;
28
import org.eclipse.jface.text.BadLocationException;
29
import org.eclipse.text.edits.MalformedTreeException;
30
import org.osgi.service.prefs.BackingStoreException;
26
31
27
/**
32
/**
28
 */
33
 */
Lines 733-736 Link Here
733
		tagAsSummary("DOM AST tree for project files (JLS3)", true); // put in fingerprint
738
		tagAsSummary("DOM AST tree for project files (JLS3)", true); // put in fingerprint
734
		runAstCreation(getProject("org.eclipse.search"));
739
		runAstCreation(getProject("org.eclipse.search"));
735
	}
740
	}
741
	
742
	private void rewriteImport(ICompilationUnit cu, String[] order,
743
			int normalThreshold, int staticThreshold,
744
			boolean restoreExistingImports) throws CoreException,
745
			BackingStoreException, MalformedTreeException, BadLocationException {
746
747
		ImportRewrite rewrite = ImportRewrite
748
				.create(cu, restoreExistingImports);
749
		rewrite.setImportOrder(order);
750
		rewrite.setOnDemandImportThreshold(normalThreshold);
751
		rewrite.setStaticOnDemandImportThreshold(staticThreshold);
752
		rewrite.rewriteImports(null);
753
	}
754
755
	public void testPerfImportRewrite() throws MalformedTreeException, CoreException, BackingStoreException, BadLocationException {
756
		tagAsSummary("testPerfImportRewrite", false); // do NOT put in fingerprint
757
758
		String[] order= new String[] {"org", "javax", "java"};
759
		ICompilationUnit unit = getCompilationUnit("org.eclipse.jdt.core", "org.eclipse.jdt.internal.core", "JavaProject.java");
760
		
761
		int measures = MEASURES_COUNT;
762
		int internalLoop = 10;
763
		for (int i = 0; i < measures; i++) {
764
			runGc();
765
			startMeasuring();
766
			for (int index = 0; index < internalLoop; index++ ) {
767
				rewriteImport(unit, order, 99, 99, false);
768
			}
769
			stopMeasuring();
770
		}
771
		commitMeasurements();
772
		assertPerformance();		
773
	
774
	}
775
	
736
}
776
}

Return to bug 194358