### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.core Index: compiler/org/eclipse/jdt/internal/compiler/lookup/Scope.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/Scope.java,v retrieving revision 1.382 diff -u -r1.382 Scope.java --- compiler/org/eclipse/jdt/internal/compiler/lookup/Scope.java 28 Jul 2011 17:07:22 -0000 1.382 +++ compiler/org/eclipse/jdt/internal/compiler/lookup/Scope.java 13 Sep 2011 16:11:26 -0000 @@ -2389,6 +2389,40 @@ return new ProblemReferenceBinding(compoundName, null /* no closest match since search for pkg*/, ProblemReasons.NotFound); } + /* Answer the package from the compoundName or null if it begins with a type. + * Intended to be used while resolving a package name only. + * + * Internal use only + */ + public final Binding getOnlyPackage(char[][] compoundName) { + compilationUnitScope().recordQualifiedReference(compoundName); + Binding binding = getTypeOrPackage(compoundName[0], Binding.PACKAGE, true); + if (binding == null || !binding.isValidBinding()) { + char[][] qName = new char[][] { compoundName[0] }; + return new ProblemReferenceBinding(qName, null /* no closest match since search for pkg*/, ProblemReasons.NotFound); + } + if (!(binding instanceof PackageBinding)) { + return null; // compoundName does not start with a package + } + + int currentIndex = 1, length = compoundName.length; + PackageBinding packageBinding = (PackageBinding) binding; + while (currentIndex < length) { + binding = packageBinding.getPackage(compoundName[currentIndex++]); + if (binding == null) { + return new ProblemReferenceBinding(CharOperation.subarray(compoundName, 0, currentIndex), null /* no closest match since search for pkg*/, ProblemReasons.NotFound); + } + if (!binding.isValidBinding()) { + return new ProblemReferenceBinding( + CharOperation.subarray(compoundName, 0, currentIndex), + binding instanceof ReferenceBinding ? (ReferenceBinding)((ReferenceBinding)binding).closestMatch() : null, + binding.problemId()); + } + packageBinding = (PackageBinding) binding; + } + return packageBinding; + } + /* Answer the type binding that corresponds the given name, starting the lookup in the receiver. * The name provided is a simple source name (e.g., "Object" , "Point", ...) */ Index: dom/org/eclipse/jdt/core/dom/DefaultBindingResolver.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/DefaultBindingResolver.java,v retrieving revision 1.176 diff -u -r1.176 DefaultBindingResolver.java --- dom/org/eclipse/jdt/core/dom/DefaultBindingResolver.java 28 Jul 2011 17:07:34 -0000 1.176 +++ dom/org/eclipse/jdt/core/dom/DefaultBindingResolver.java 13 Sep 2011 16:11:27 -0000 @@ -1384,7 +1384,7 @@ org.eclipse.jdt.internal.compiler.ast.ASTNode node = (org.eclipse.jdt.internal.compiler.ast.ASTNode) this.newAstToOldAst.get(pkg); if (node instanceof ImportReference) { ImportReference importReference = (ImportReference) node; - Binding binding = this.scope.getTypeOrPackage(CharOperation.subarray(importReference.tokens, 0, importReference.tokens.length)); + Binding binding = this.scope.getOnlyPackage(CharOperation.subarray(importReference.tokens, 0, importReference.tokens.length)); if ((binding != null) && (binding.isValidBinding())) { if (binding instanceof ReferenceBinding) { // this only happens if a type name has the same name as its package #P org.eclipse.jdt.core.tests.model Index: src/org/eclipse/jdt/core/tests/dom/ASTConverterTestAST4_2.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterTestAST4_2.java,v retrieving revision 1.3 diff -u -r1.3 ASTConverterTestAST4_2.java --- src/org/eclipse/jdt/core/tests/dom/ASTConverterTestAST4_2.java 8 Sep 2011 18:36:15 -0000 1.3 +++ src/org/eclipse/jdt/core/tests/dom/ASTConverterTestAST4_2.java 13 Sep 2011 16:11:28 -0000 @@ -122,7 +122,7 @@ static { // TESTS_NAMES = new String[] {"test0602"}; // TESTS_RANGE = new int[] { 721, -1 }; -// TESTS_NUMBERS = new int[] { 723, 724 }; +// TESTS_NUMBERS = new int[] { 725 }; } public static Test suite() { return buildModelTestSuite(ASTConverterTestAST4_2.class); @@ -10684,4 +10684,47 @@ assertFalse(isRecovered((ASTNode) statements.get(1))); assertFalse(isRecovered((ASTNode) statements.get(2))); } + /* + * https://bugs.eclipse.org/bugs/show_bug.cgi?id=357471 + */ + public void test0725() throws JavaModelException { + ICompilationUnit workingCopy = null; + try { + String contents = + "package one.two;\n" + + "public class one {}"; + workingCopy = getWorkingCopy("/Converter/src/one/two/one.java", true/*resolve*/); + CompilationUnit unit = (CompilationUnit) buildAST( + AST.JLS3, + contents, + workingCopy, + true, + true, + true); + PackageDeclaration packageDeclaration = unit.getPackage(); + IPackageBinding packageBinding = packageDeclaration.resolveBinding(); + assertNotNull("No binding", packageBinding); + assertEquals("Wrong name", "one.two", packageBinding.getName()); + Name packageName = packageDeclaration.getName(); + IBinding binding = packageName.resolveBinding(); + assertEquals("Wrong type", IBinding.PACKAGE, binding.getKind()); + packageBinding = (IPackageBinding) binding; + assertEquals("Wrong name", "one.two", packageBinding.getName()); + packageName = ((QualifiedName) packageName).getQualifier(); + binding = packageName.resolveBinding(); + assertEquals("Wrong type", IBinding.PACKAGE, binding.getKind()); + packageBinding = (IPackageBinding) binding; + assertEquals("Wrong name", "one", packageBinding.getName()); + packageName = packageDeclaration.getName(); + packageName = ((QualifiedName) packageName).getName(); + binding = packageName.resolveBinding(); + assertEquals("Wrong type", IBinding.PACKAGE, binding.getKind()); + packageBinding = (IPackageBinding) binding; + assertEquals("Wrong name", "one.two", packageBinding.getName()); + } finally { + if (workingCopy != null) { + workingCopy.discardWorkingCopy(); + } + } + } } \ No newline at end of file