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

Collapse All | Expand All

(-)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
}
(-)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/rewrite/describing/ImportRewriteTest.java (+44 lines)
Lines 572-577 Link Here
572
		assertEqualString(cu.getSource(), buf.toString());
572
		assertEqualString(cu.getSource(), buf.toString());
573
	}
573
	}
574
574
575
	public void testAddImports_bug194358() throws Exception {
576
577
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("pack1", false, null);
578
		StringBuffer buf= new StringBuffer();
579
		buf.append("package pack1;\n");
580
		buf.append("\n");
581
		buf.append("import pack2.A;\n");
582
		buf.append("import pack2.A.Inner;\n");
583
		buf.append("import pack2.B;\n");
584
		buf.append("\n");
585
		buf.append("public class C {\n");
586
		buf.append("}\n");
587
588
		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
589
590
		// We need to actually make some state in the AST for the classes, to test that we can 
591
		// disambiguate between packages and inner classes (see the bug for details).
592
		IPackageFragment pack2= this.sourceFolder.createPackageFragment("pack2", false, null);
593
		ICompilationUnit aUnit= pack2.createCompilationUnit("A.java", "", false, null);
594
		ICompilationUnit bUnit= pack2.createCompilationUnit("B.java", "", false, null);
595
		bUnit.createType("class B {}", null, false, null);
596
		
597
		IType aType= aUnit.createType("class A {}", null, false, null);
598
		aType.createType("class Inner {}", null, false, null);
599
		String[] order= new String[] { "java" };
600
601
		ImportRewrite imports= newImportsRewrite(cu, order, 99, 99, false);
602
		imports.addImport("pack2.A");
603
		imports.addImport("pack2.A.Inner");
604
		imports.addImport("pack2.B");
605
606
		apply(imports);
607
		
608
		buf= new StringBuffer();
609
		buf.append("package pack1;\n");
610
		buf.append("\n");
611
		buf.append("import pack2.A;\n");
612
		buf.append("import pack2.A.Inner;\n");
613
		buf.append("import pack2.B;\n");
614
		buf.append("\n");
615
		buf.append("public class C {\n");
616
		buf.append("}\n");
617
		assertEqualString(cu.getSource(), buf.toString());
618
	}
575
	public void testAddStaticImports1() throws Exception {
619
	public void testAddStaticImports1() throws Exception {
576
620
577
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("pack1", false, null);
621
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("pack1", false, null);

Return to bug 194358