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 (-3 / +25 lines)
Lines 21-26 Link Here
21
import org.eclipse.jdt.core.IBuffer;
21
import org.eclipse.jdt.core.IBuffer;
22
import org.eclipse.jdt.core.ICompilationUnit;
22
import org.eclipse.jdt.core.ICompilationUnit;
23
import org.eclipse.jdt.core.IJavaElement;
23
import org.eclipse.jdt.core.IJavaElement;
24
import org.eclipse.jdt.core.IType;
24
import org.eclipse.jdt.core.JavaCore;
25
import org.eclipse.jdt.core.JavaCore;
25
import org.eclipse.jdt.core.JavaModelException;
26
import org.eclipse.jdt.core.JavaModelException;
26
import org.eclipse.jdt.core.Signature;
27
import org.eclipse.jdt.core.Signature;
Lines 168-176 Link Here
168
		}
169
		}
169
	}
170
	}
170
171
171
	private static String getQualifier(ImportDeclaration decl) {
172
	private String getQualifier(ImportDeclaration decl) {
172
		String name= decl.getName().getFullyQualifiedName();
173
		String name= decl.getName().getFullyQualifiedName();
173
		return decl.isOnDemand() ? name : Signature.getQualifier(name);
174
		if (decl.isOnDemand()) {
175
			return name;
176
		}
177
		return getQualifier(name);
178
	}
179
180
	private String getQualifier(String name) {
181
		try {
182
			// If our element is an inner class, we need to back up to the top-level
183
			// enclosing type and use the package that it is in as a qualifier.
184
			// This fixes bug 194358.
185
			do {
186
				IType element = this.compilationUnit.getJavaProject().findType(name);
187
				name = Signature.getQualifier(name);
188
				if (element == null || element.getDeclaringType() == null) {
189
					break;
190
				}
191
			} while (true);
192
		} catch (JavaModelException e) {
193
			// Should never happen.
194
		}
195
		return name;
174
	}
196
	}
175
197
176
	private static String getFullName(ImportDeclaration decl) {
198
	private static String getFullName(ImportDeclaration decl) {
Lines 401-407 Link Here
401
	}
423
	}
402
424
403
	public void addImport(String fullTypeName, boolean isStatic) {
425
	public void addImport(String fullTypeName, boolean isStatic) {
404
		String typeContainerName= Signature.getQualifier(fullTypeName);
426
		String typeContainerName= getQualifier(fullTypeName);
405
		ImportDeclEntry decl= new ImportDeclEntry(fullTypeName, isStatic, null);
427
		ImportDeclEntry decl= new ImportDeclEntry(fullTypeName, isStatic, null);
406
		sortIn(typeContainerName, decl, isStatic);
428
		sortIn(typeContainerName, decl, isStatic);
407
	}
429
	}
(-)src/org/eclipse/jdt/core/tests/performance/FullSourceWorkspaceASTTests.java (+47 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 723-728 Link Here
723
		commitMeasurements();
728
		commitMeasurements();
724
		assertPerformance();
729
		assertPerformance();
725
	}
730
	}
731
	private ImportRewrite rewriteImport(ICompilationUnit cu, String[] order,
732
			int normalThreshold, int staticThreshold,
733
			boolean restoreExistingImports) throws CoreException,
734
			BackingStoreException, MalformedTreeException, BadLocationException {
735
736
		ImportRewrite rewrite = ImportRewrite
737
				.create(cu, restoreExistingImports);
738
		rewrite.setImportOrder(order);
739
		rewrite.setOnDemandImportThreshold(normalThreshold);
740
		rewrite.setStaticOnDemandImportThreshold(staticThreshold);
741
742
		rewrite.rewriteImports(null);
743
744
		return rewrite;
745
	}
726
746
727
	/**
747
	/**
728
	 * Create AST nodes tree for all compilation units in JUnit project.
748
	 * Create AST nodes tree for all compilation units in JUnit project.
Lines 733-736 Link Here
733
		tagAsSummary("DOM AST tree for project files (JLS3)", true); // put in fingerprint
753
		tagAsSummary("DOM AST tree for project files (JLS3)", true); // put in fingerprint
734
		runAstCreation(getProject("org.eclipse.search"));
754
		runAstCreation(getProject("org.eclipse.search"));
735
	}
755
	}
756
757
	/**
758
	 * Rewrite imports for a CompilationUnit with many import statements - JavaProject.java has about 80 and 
759
	 * used here.
760
	 * 
761
	 */
762
	public void testPerfImportRewrite() throws MalformedTreeException, CoreException, BackingStoreException, BadLocationException {
763
		tagAsSummary("Test Performance of Import Rewrite", false); // do NOT put in fingerprint
764
765
		String[] order= new String[] {"org", "javax", "java"};
766
		
767
		int measures = MEASURES_COUNT * 2;
768
		int internalLoop = 20;
769
		for (int i = 0; i < measures; i++) {
770
			runGc();
771
			startMeasuring();
772
			for (int index = 0; index < internalLoop; index++ ) {
773
				ICompilationUnit unit = getCompilationUnit("org.eclipse.jdt.core", "org.eclipse.jdt.internal.core", "JavaProject.java");
774
				rewriteImport(unit, order, 99, 99, false);
775
			}
776
			stopMeasuring();
777
		}
778
		commitMeasurements();
779
		assertPerformance();		
780
	
781
	}
782
	
736
}
783
}
(-)src/org/eclipse/jdt/core/tests/rewrite/describing/ImportRewriteTest.java (+51 lines)
Lines 572-577 Link Here
572
		assertEqualString(cu.getSource(), buf.toString());
572
		assertEqualString(cu.getSource(), buf.toString());
573
	}
573
	}
574
574
575
	/**
576
	 * Test that import statements of inner classes are written right after the enclosing type
577
	 * and before other types.
578
	 * 
579
	 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=194358"
580
	 */
581
	public void testAddImports_bug194358() throws Exception {
582
583
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("pack1", false, null);
584
		StringBuffer buf= new StringBuffer();
585
		buf.append("package pack1;\n");
586
		buf.append("\n");
587
		buf.append("import pack2.A;\n");
588
		buf.append("import pack2.A.Inner;\n");
589
		buf.append("import pack2.B;\n");
590
		buf.append("\n");
591
		buf.append("public class C {\n");
592
		buf.append("}\n");
593
594
		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
595
596
		// We need to actually make some state in the AST for the classes, to test that we can 
597
		// disambiguate between packages and inner classes (see the bug for details).
598
		IPackageFragment pack2= this.sourceFolder.createPackageFragment("pack2", false, null);
599
		ICompilationUnit aUnit= pack2.createCompilationUnit("A.java", "", false, null);
600
		ICompilationUnit bUnit= pack2.createCompilationUnit("B.java", "", false, null);
601
		bUnit.createType("class B {}", null, false, null);
602
		
603
		IType aType= aUnit.createType("class A {}", null, false, null);
604
		aType.createType("class Inner {}", null, false, null);
605
		String[] order= new String[] { "java" };
606
607
		ImportRewrite imports= newImportsRewrite(cu, order, 99, 99, false);
608
		imports.addImport("pack2.A");
609
		imports.addImport("pack2.A.Inner");
610
		imports.addImport("pack2.B");
611
612
		apply(imports);
613
		
614
		buf= new StringBuffer();
615
		buf.append("package pack1;\n");
616
		buf.append("\n");
617
		buf.append("import pack2.A;\n");
618
		buf.append("import pack2.A.Inner;\n");
619
		buf.append("import pack2.B;\n");
620
		buf.append("\n");
621
		buf.append("public class C {\n");
622
		buf.append("}\n");
623
		assertEqualString(cu.getSource(), buf.toString());
624
	}
625
	
575
	public void testAddStaticImports1() throws Exception {
626
	public void testAddStaticImports1() throws Exception {
576
627
577
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("pack1", false, null);
628
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("pack1", false, null);

Return to bug 194358