diff options
10 files changed, 114 insertions, 16 deletions
diff --git a/org.eclipse.jdt.apt.pluggable.tests/resources/targets/bug407841/ModuleCore.java b/org.eclipse.jdt.apt.pluggable.tests/resources/targets/bug407841/ModuleCore.java index c8a6cec53a..303e06a614 100644 --- a/org.eclipse.jdt.apt.pluggable.tests/resources/targets/bug407841/ModuleCore.java +++ b/org.eclipse.jdt.apt.pluggable.tests/resources/targets/bug407841/ModuleCore.java @@ -1,6 +1,5 @@ package targets.bug407841; -import targets.bug407841.ModuleLegacy; import org.eclipse.jdt.apt.pluggable.tests.annotations.Module; @Module(key=ModuleCore.KEY) diff --git a/org.eclipse.jdt.apt.pluggable.tests/resources/targets/bug407841/ModuleLegacy.java b/org.eclipse.jdt.apt.pluggable.tests/resources/targets/bug407841/ModuleLegacy.java index 59fe6258ca..5b4cab5edf 100644 --- a/org.eclipse.jdt.apt.pluggable.tests/resources/targets/bug407841/ModuleLegacy.java +++ b/org.eclipse.jdt.apt.pluggable.tests/resources/targets/bug407841/ModuleLegacy.java @@ -1,6 +1,5 @@ package targets.bug407841; -import targets.bug407841.ModuleCore; import org.eclipse.jdt.apt.pluggable.tests.annotations.Module; @Module(key=ModuleCore.KEY) diff --git a/org.eclipse.jdt.apt.pluggable.tests/resources/targets/bug468893/Annotated.java b/org.eclipse.jdt.apt.pluggable.tests/resources/targets/bug468893/Annotated.java index 186ef5b3b6..f82b20c0a1 100644 --- a/org.eclipse.jdt.apt.pluggable.tests/resources/targets/bug468893/Annotated.java +++ b/org.eclipse.jdt.apt.pluggable.tests/resources/targets/bug468893/Annotated.java @@ -1,5 +1,4 @@ package targets.bug468893; -import targets.bug468893.Annotation; @Annotation public class Annotated { diff --git a/org.eclipse.jdt.apt.pluggable.tests/src/org/eclipse/jdt/apt/pluggable/tests/BuilderTests.java b/org.eclipse.jdt.apt.pluggable.tests/src/org/eclipse/jdt/apt/pluggable/tests/BuilderTests.java index 8e2824f904..709272ac45 100644 --- a/org.eclipse.jdt.apt.pluggable.tests/src/org/eclipse/jdt/apt/pluggable/tests/BuilderTests.java +++ b/org.eclipse.jdt.apt.pluggable.tests/src/org/eclipse/jdt/apt/pluggable/tests/BuilderTests.java @@ -12,6 +12,7 @@ * eclipse@cafewalter.com - initial API and implementation * Harry Terkelsen <het@google.com> - Contribution for Bug 437414 - Annotation processing is broken when build is batched * Fabian Steeg <steeg@hbz-nrw.de> - Pass automatically provided options to Java 6 processors - https://bugs.eclipse.org/341298 + * Pierre-Yves B. <pyvesdev@gmail.com> - Contribution for bug 559618 - No compiler warning for import from same package *******************************************************************************/ package org.eclipse.jdt.apt.pluggable.tests; @@ -215,7 +216,7 @@ public class BuilderTests extends TestBase fullBuild(); expectingNoProblems(); assertEquals("Elements should have been processed", 0, BugsProc.getUnprocessedElements()); - assertEquals("Elements should have been processed", 3, BugsProc.getNumRounds()); + assertEquals("Elements should have been processed", 4, BugsProc.getNumRounds()); } finally { org.eclipse.jdt.internal.core.builder.AbstractImageBuilder.MAX_AT_ONCE = old; } diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullTypeAnnotationTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullTypeAnnotationTest.java index 4324b64ea4..66d6ea278a 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullTypeAnnotationTest.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullTypeAnnotationTest.java @@ -16,6 +16,8 @@ * Bug 467482 - TYPE_USE null annotations: Incorrect "Redundant null check"-warning * Bug 473713 - [1.8][null] Type mismatch: cannot convert from @NonNull A1 to @NonNull A1 * Bug 467430 - TYPE_USE Null Annotations: Confusing error message with known null value + * Pierre-Yves B. <pyvesdev@gmail.com> - Contribution for + * Bug 559618 - No compiler warning for import from same package *******************************************************************************/ package org.eclipse.jdt.core.tests.compiler.regression; @@ -1343,7 +1345,6 @@ public class NullTypeAnnotationTest extends AbstractNullAnnotationTest { "p/X1.java", "package p;\n" + "import java.util.Map;\n" + - "import p.List;\n" + "import org.eclipse.jdt.annotation.*;\n" + "import static java.lang.annotation.ElementType.*;\n" + "import java.lang.annotation.*;\n" + diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/CompilationUnitTests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/CompilationUnitTests.java index 296eb4f839..8b3d8c7986 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/CompilationUnitTests.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/CompilationUnitTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2019 IBM Corporation and others. + * Copyright (c) 2000, 2020 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -11,6 +11,7 @@ * Contributors: * IBM Corporation - initial API and implementation * Stephan Herrmann - contribution for bug 337868 - [compiler][model] incomplete support for package-info.java when using SearchableEnvironment + * Pierre-Yves B. <pyvesdev@gmail.com> - Contribution for bug 559618 - No compiler warning for import from same package *******************************************************************************/ package org.eclipse.jdt.core.tests.model; @@ -2699,4 +2700,92 @@ public void testBug526615() throws CoreException { deleteFolder("/P/src/test1"); } } +public void testBug559618_1() throws CoreException { + try { + createFile("/P/src/p/C.java", + "package p;\n" + + "public class C{};\n"); + + createFile("/P/src/p/D.java", + "package p;\n" + + "import p.C;\n" + + "public class D {\n" + + " C getC() {\n" + + " return null;\n" + + " }\n" + + "}\n"); + ICompilationUnit cuD = getCompilationUnit("/P/src/p/D.java"); + + ASTParser parser = ASTParser.newParser(AST_INTERNAL_LATEST); + parser.setProject(this.testProject); + parser.setSource(cuD); + parser.setResolveBindings(true); + org.eclipse.jdt.core.dom.CompilationUnit cuAST = (org.eclipse.jdt.core.dom.CompilationUnit) parser.createAST(null); + IProblem[] problems = cuAST.getProblems(); + assertEquals("Should have 1 problem", 1, problems.length); + assertEquals("Should have only an unused warning", "The import p.C is never used", problems[0].getMessage()); + } finally { + deleteFile("/P/src/p/C.java"); + deleteFile("/P/src/p/D.java"); + } +} +public void testBug559618_2() throws CoreException { // Same as testBug559618_1, but with wildcard import. + try { + createFile("/P/src/p/C.java", + "package p;\n" + + "public class C{};\n"); + + createFile("/P/src/p/D.java", + "package p;\n" + + "import p.*;\n" + + "public class D {\n" + + " C getC() {\n" + + " return null;\n" + + " }\n" + + "}\n"); + ICompilationUnit cuD = getCompilationUnit("/P/src/p/D.java"); + + ASTParser parser = ASTParser.newParser(AST_INTERNAL_LATEST); + parser.setProject(this.testProject); + parser.setSource(cuD); + parser.setResolveBindings(true); + org.eclipse.jdt.core.dom.CompilationUnit cuAST = (org.eclipse.jdt.core.dom.CompilationUnit) parser.createAST(null); + IProblem[] problems = cuAST.getProblems(); + assertEquals("Should have 1 problem", 1, problems.length); + assertEquals("Should have only an unused warning", "The import p is never used", problems[0].getMessage()); + } finally { + deleteFile("/P/src/p/C.java"); + deleteFile("/P/src/p/D.java"); + } +} +public void testBug559618_3() throws CoreException { // Nested class imports must not be flagged as unused. + try { + createFile("/P/src/p/C.java", + "package p;\n" + + "public class C{\n" + + " public class C1{};\n" + + "};\n"); + + createFile("/P/src/p/D.java", + "package p;\n" + + "import p.C.C1;\n" + + "public class D {\n" + + " C1 getC1() {\n" + + " return null;\n" + + " }\n" + + "}\n"); + ICompilationUnit cuD = getCompilationUnit("/P/src/p/D.java"); + + ASTParser parser = ASTParser.newParser(AST_INTERNAL_LATEST); + parser.setProject(this.testProject); + parser.setSource(cuD); + parser.setResolveBindings(true); + org.eclipse.jdt.core.dom.CompilationUnit cuAST = (org.eclipse.jdt.core.dom.CompilationUnit) parser.createAST(null); + IProblem[] problems = cuAST.getProblems(); + assertEquals("Should have no problems", 0, problems.length); + } finally { + deleteFile("/P/src/p/C.java"); + deleteFile("/P/src/p/D.java"); + } +} } diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/ExternalAnnotations18Test.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/ExternalAnnotations18Test.java index 11e5b2e641..a4f0234b07 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/ExternalAnnotations18Test.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/ExternalAnnotations18Test.java @@ -10,6 +10,7 @@ * * Contributors: * Stephan Herrmann - initial API and implementation + * Pierre-Yves B. <pyvesdev@gmail.com> - Contributions for bug 559618 - No compiler warning for import from same package *******************************************************************************/ package org.eclipse.jdt.core.tests.model; @@ -2101,7 +2102,6 @@ public class ExternalAnnotations18Test extends ModifyingResourceTests { "\n" + "import libs.Lib1;\n" + "import libs.Lib2;\n" + - "import tests.Lib3;\n" + "\n" + "public class Test1 {\n" + " @NonNull String test1(Lib1<@NonNull String> lib) {\n" + @@ -2119,7 +2119,7 @@ public class ExternalAnnotations18Test extends ModifyingResourceTests { assertProblems(reconciled.getProblems(), new String[] { "Pb(980) Unsafe interpretation of method return type as '@NonNull' based on the receiver type 'Lib1<@NonNull String>'. Type 'Lib1<T>' doesn't seem to be designed with null type annotations in mind", "Pb(980) Unsafe interpretation of method return type as '@NonNull' based on the receiver type 'Lib2<@NonNull String>'. Type 'Lib2<T>' doesn't seem to be designed with null type annotations in mind", - }, new int[] { 10, 13 }, new int[] { ProblemSeverities.Warning, ProblemSeverities.Info } ); + }, new int[] { 9, 12 }, new int[] { ProblemSeverities.Warning, ProblemSeverities.Info } ); } public void testBug490343() throws Exception { diff --git a/org.eclipse.jdt.core.tests.model/workspace/Converter/src/test0554/B.java b/org.eclipse.jdt.core.tests.model/workspace/Converter/src/test0554/B.java index 9326194db7..9a27da253a 100644 --- a/org.eclipse.jdt.core.tests.model/workspace/Converter/src/test0554/B.java +++ b/org.eclipse.jdt.core.tests.model/workspace/Converter/src/test0554/B.java @@ -1,7 +1,5 @@ package test0554; -import test0554.A; - public class B { public static String foo() { return A.getString("xx"); diff --git a/org.eclipse.jdt.core.tests.model/workspace/Converter/src/test0555/B.java b/org.eclipse.jdt.core.tests.model/workspace/Converter/src/test0555/B.java index 7f90455026..0441e1cca0 100644 --- a/org.eclipse.jdt.core.tests.model/workspace/Converter/src/test0555/B.java +++ b/org.eclipse.jdt.core.tests.model/workspace/Converter/src/test0555/B.java @@ -1,7 +1,5 @@ package test0555; -import test0555.A; - public class B { public static String foo() { return A.BUNDLE_NAME; diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/Scope.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/Scope.java index 0742835abe..fb03e95cc7 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/Scope.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/Scope.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2019 IBM Corporation and others. + * Copyright (c) 2000, 2020 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -58,6 +58,8 @@ * Bug 405066 - [1.8][compiler][codegen] Implement code generation infrastructure for JSR335 * Andy Clement (GoPivotal, Inc) aclement@gopivotal.com - Contributions for * Bug 405104 - [1.8][compiler][codegen] Implement support for serializeable lambdas + * Pierre-Yves B. <pyvesdev@gmail.com> - Contributions for + * Bug 559618 - No compiler warning for import from same package *******************************************************************************/ package org.eclipse.jdt.internal.compiler.lookup; @@ -3314,14 +3316,15 @@ public abstract class Scope { Binding cachedBinding = (Binding) typeOrPackageCache.get(name); if (cachedBinding != null) { // can also include NotFound ProblemReferenceBindings if we already know this name is not found if (cachedBinding instanceof ImportBinding) { // single type import cached in faultInImports(), replace it in the cache with the type - ImportReference importReference = ((ImportBinding) cachedBinding).reference; - if (importReference != null) { + ImportBinding importBinding = (ImportBinding) cachedBinding; + ImportReference importReference = importBinding.reference; + if (importReference != null && !isUnnecessarySamePackageImport(importBinding.resolvedImport, unitScope)) { importReference.bits |= ASTNode.Used; } if (cachedBinding instanceof ImportConflictBinding) typeOrPackageCache.put(name, cachedBinding = ((ImportConflictBinding) cachedBinding).conflictingTypeBinding); // already know its visible else - typeOrPackageCache.put(name, cachedBinding = ((ImportBinding) cachedBinding).resolvedImport); // already know its visible + typeOrPackageCache.put(name, cachedBinding = importBinding.resolvedImport); // already know its visible } if ((mask & Binding.TYPE) != 0) { if (foundType != null && foundType.problemId() != ProblemReasons.NotVisible && cachedBinding.problemId() != ProblemReasons.Ambiguous) @@ -3459,6 +3462,17 @@ public abstract class Scope { return foundType; } + private boolean isUnnecessarySamePackageImport(Binding resolvedImport, Scope unitScope) { + if (resolvedImport instanceof ReferenceBinding) { + if (unitScope.getCurrentPackage() == ((ReferenceBinding) resolvedImport).getPackage()) { + if ((resolvedImport.getAnnotationTagBits() & TagBits.IsNestedType) != 0) + return false; // importing nested types is still necessary + return true; + } + } + return false; + } + // Added for code assist... NOT Public API // DO NOT USE to resolve import references since this method assumes 'A.B' is relative to a single type import of 'p1.A' // when it may actually mean the type B in the package A |