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 (-10 / +41 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(Signature.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 
183
		 * of the qualifier
184
		 */
185
		if (isStatic) {
186
			return Signature.getQualifier(name);
187
		}
188
		
189
		int index = CharOperation.indexOf(Signature.C_DOT, name.toCharArray());
190
		/* Stop at the last fragment */
191
		while(index != -1 && name.length() > index) {
192
			IJavaElement fragment = null;
193
			try {
194
				fragment = ((JavaProject) this.compilationUnit.getJavaProject()).findPackageFragment(name);
195
			} catch (JavaModelException e) {
196
				// Should never happen
197
			}
198
			if (fragment != null) { 
199
				return name; 
200
			}
201
			name = Signature.getQualifier(name);
202
		}
203
		return name;
174
	}
204
	}
175
205
176
	private static String getFullName(ImportDeclaration decl) {
206
	private static String getFullName(ImportDeclaration decl) {
Lines 401-413 Link Here
401
	}
431
	}
402
432
403
	public void addImport(String fullTypeName, boolean isStatic) {
433
	public void addImport(String fullTypeName, boolean isStatic) {
404
		String typeContainerName= Signature.getQualifier(fullTypeName);
434
		String typeContainerName= getQualifier(fullTypeName, isStatic);
405
		ImportDeclEntry decl= new ImportDeclEntry(fullTypeName, isStatic, null);
435
		ImportDeclEntry decl= new ImportDeclEntry(fullTypeName, isStatic, null);
406
		sortIn(typeContainerName, decl, isStatic);
436
		sortIn(typeContainerName, decl, isStatic);
407
	}
437
	}
408
438
409
	public boolean removeImport(String qualifiedName, boolean isStatic) {
439
	public boolean removeImport(String qualifiedName, boolean isStatic) {
410
		String containerName= Signature.getQualifier(qualifiedName);
440
		String containerName= getQualifier(qualifiedName, isStatic);
411
441
412
		int nPackages= this.packageEntries.size();
442
		int nPackages= this.packageEntries.size();
413
		for (int i= 0; i < nPackages; i++) {
443
		for (int i= 0; i < nPackages; i++) {
Lines 522-533 Link Here
522
			int nPackageEntries= this.packageEntries.size();
552
			int nPackageEntries= this.packageEntries.size();
523
			for (int i= 0; i < nPackageEntries; i++) {
553
			for (int i= 0; i < nPackageEntries; i++) {
524
				PackageEntry pack= (PackageEntry) this.packageEntries.get(i);
554
				PackageEntry pack= (PackageEntry) this.packageEntries.get(i);
525
				int nImports= pack.getNumberOfImports();
526
555
527
				if (this.filterImplicitImports && !pack.isStatic() && isImplicitImport(pack.getName(), this.compilationUnit)) {
556
				if (this.filterImplicitImports && !pack.isStatic() && isImplicitImport(pack.getName(), this.compilationUnit)) {
528
					pack.removeAllNew(onDemandConflicts);
557
					pack.filterImplicitImports();
529
					nImports= pack.getNumberOfImports();
530
				}
558
				}
559
				int nImports= pack.getNumberOfImports();
560
531
				if (nImports == 0) {
561
				if (nImports == 0) {
532
					continue;
562
					continue;
533
				}
563
				}
Lines 930-940 Link Here
930
			return false;
960
			return false;
931
		}
961
		}
932
962
933
		public void removeAllNew(Set onDemandConflicts) {
963
		public void filterImplicitImports() {
934
			int nInports= this.importEntries.size();
964
			int nInports= this.importEntries.size();
935
			for (int i= nInports - 1; i >= 0; i--) {
965
			for (int i= nInports - 1; i >= 0; i--) {
936
				ImportDeclEntry curr= getImportAt(i);
966
				ImportDeclEntry curr= getImportAt(i);
937
				if (curr.isNew() /*&& (onDemandConflicts == null || onDemandConflicts.contains(curr.getSimpleName()))*/) {
967
				boolean internalClassImport = curr.getElementName().lastIndexOf('.') > getName().length();
968
				if (curr.isNew() && !internalClassImport) {
938
					this.importEntries.remove(i);
969
					this.importEntries.remove(i);
939
				}
970
				}
940
			}
971
			}
(-)src/org/eclipse/jdt/core/tests/rewrite/describing/ImportRewriteTest.java (-1 / +140 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 testBug194358() 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.B");
612
	  imports.addImport("pack2.A.Inner");
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 when the container
630
	 * class is implicitly available. This tests the case where the classes are in 
631
	 * different compilation units.
632
	 * 
633
	 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=194358"
634
	 */
635
	public void testBug194358a() throws Exception {
636
	  StringBuffer buf= new StringBuffer();
637
	  buf.append("package com.pack1;\n");
638
	  buf.append("\n");
639
	  buf.append("import com.pack1.A;\n");
640
	  buf.append("import com.pack1.A.Inner;\n");
641
	  buf.append("import com.pack2.B;\n");
642
	  buf.append("\n");
643
	  buf.append("public class C {\n");
644
	  buf.append("}\n");
645
646
      IPackageFragment pack1= this.sourceFolder.createPackageFragment("com.pack1", false, null);
647
	  ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
648
      ICompilationUnit aUnit= pack1.createCompilationUnit("A.java", "", false, null);
649
650
	  IPackageFragment pack2= this.sourceFolder.createPackageFragment("com.pack2", false, null);
651
      ICompilationUnit bUnit= pack2.createCompilationUnit("B.java", "", false, null);
652
	  bUnit.createType("class B {}", null, false, null);
653
	  IType aType= aUnit.createType("class A {}", null, false, null);
654
	  aType.createType("class Inner {}", null, false, null);
655
	  String[] order= new String[] { "java" };
656
657
	  ImportRewrite imports= newImportsRewrite(cu, order, 99, 99, false);
658
	  imports.addImport("com.pack1.A");
659
	  imports.addImport("com.pack1.A.Inner");
660
	  imports.addImport("com.pack2.B");
661
662
	  apply(imports);
663
664
	  buf= new StringBuffer();
665
	  buf.append("package com.pack1;\n");
666
	  buf.append("\n");
667
	  buf.append("import com.pack1.A.Inner;\n");
668
	  buf.append("import com.pack2.B;\n");
669
	  buf.append("\n");
670
	  buf.append("public class C {\n");
671
	  buf.append("}\n");
672
	  assertEqualString(cu.getSource(), buf.toString());
673
	}
674
	/**
675
	 * Test that the Inner type imports are not removed while organizing even though the 
676
	 * containing class is implicitly available - for the case when both the classes are 
677
	 * in the same compilation unit
678
	 * 
679
	 * see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=235253"
680
	 */
681
	public void testBug235253() throws Exception {
682
		StringBuffer buf= new StringBuffer();
683
		buf.append("package bug;\n");
684
		buf.append("\n");
685
		buf.append("class Bug {\n");
686
		buf.append("public void addFile(File file) {}\n");
687
		buf.append("\tinterface Proto{};\n");
688
		buf.append("}\n");		
689
		buf.append("class Foo implements Proto{}");
690
691
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("bug", false, null);
692
		ICompilationUnit cu= pack1.createCompilationUnit("Bug.java", buf.toString(), false, null);
693
		String[] order= new String[] { "bug" , "java" };
694
		ImportRewrite imports= newImportsRewrite(cu, order, 99, 99, false);
695
		imports.addImport("bug.Bug.Proto");
696
		imports.addImport("java.io.File"); 
697
		
698
		apply(imports);
699
		buf = new StringBuffer();
700
		buf.append("package bug;\n");
701
		buf.append("\n");
702
		buf.append("import bug.Bug.Proto;\n");
703
		buf.append("\n");
704
		buf.append("import java.io.File;\n");
705
		buf.append("\n");
706
		buf.append("class Bug {\n");
707
		buf.append("public void addFile(File file) {}\n");
708
		buf.append("\tinterface Proto{};\n");
709
		buf.append("}\n");		
710
		buf.append("class Foo implements Proto{}");
711
		assertEqualString(cu.getSource(), buf.toString());
712
	}
713
		
575
	public void testAddStaticImports1() throws Exception {
714
	public void testAddStaticImports1() throws Exception {
576
715
577
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("pack1", false, null);
716
		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