Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 459320
Collapse All | Expand All

(-)dom/org/eclipse/jdt/internal/core/dom/rewrite/ImportRewriteAnalyzer.java (-2 / +22 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2013 IBM Corporation and others.
2
 * Copyright (c) 2000, 2015 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 25-30 Link Here
25
import org.eclipse.jdt.core.IBuffer;
25
import org.eclipse.jdt.core.IBuffer;
26
import org.eclipse.jdt.core.ICompilationUnit;
26
import org.eclipse.jdt.core.ICompilationUnit;
27
import org.eclipse.jdt.core.IJavaElement;
27
import org.eclipse.jdt.core.IJavaElement;
28
import org.eclipse.jdt.core.IPackageDeclaration;
28
import org.eclipse.jdt.core.JavaCore;
29
import org.eclipse.jdt.core.JavaCore;
29
import org.eclipse.jdt.core.JavaModelException;
30
import org.eclipse.jdt.core.JavaModelException;
30
import org.eclipse.jdt.core.Signature;
31
import org.eclipse.jdt.core.Signature;
Lines 617-626 Link Here
617
			return true;
618
			return true;
618
		}
619
		}
619
		ICompilationUnit cu= this.compilationUnit;
620
		ICompilationUnit cu= this.compilationUnit;
620
		String packageName= cu.getParent().getElementName();
621
		IJavaElement packageFragment = cu.getParent();
622
		String packageName= packageFragment.getElementName();
621
		if (qualifier.equals(packageName)) {
623
		if (qualifier.equals(packageName)) {
622
			return true;
624
			return true;
623
		}
625
		}
626
		if (packageName.isEmpty() && !packageFragment.exists() && cu.exists()) {
627
			/*
628
			 * For a file outside of the build path, JavaCore#create(IFile) creates an
629
			 * ICompilationUnit with the file's parent folder as package fragment root, and a default package.
630
			 * That "wrong" package is problematic for the ImportRewrite, since it doesn't get filtered
631
			 * and eventually leads to unused import statements.
632
			 */
633
			try {
634
				IPackageDeclaration[] packageDeclarations = this.compilationUnit.getPackageDeclarations();
635
				if (packageDeclarations.length > 0) {
636
					if (qualifier.equals(packageDeclarations[0].getElementName())) {
637
						return true;
638
					}
639
				}
640
			} catch (JavaModelException e) {
641
				// continue
642
			}
643
		}
624
		String mainTypeName= JavaCore.removeJavaLikeExtension(cu.getElementName());
644
		String mainTypeName= JavaCore.removeJavaLikeExtension(cu.getElementName());
625
		if (packageName.length() == 0) {
645
		if (packageName.length() == 0) {
626
			return qualifier.equals(mainTypeName);
646
			return qualifier.equals(mainTypeName);

Return to bug 459320