Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJay Arthanareeswaran2017-09-06 11:44:08 +0000
committerJay Arthanareeswaran2017-09-06 13:53:58 +0000
commit39f06c360c3afa72b1c211d620c70a07e6fca943 (patch)
treea2708a9ae2fc12e717cb6bb28224d01bada0e93d
parent5868e44fb4163075f677d883e30f1db88724fc89 (diff)
downloadeclipse.jdt.core-39f06c360c3afa72b1c211d620c70a07e6fca943.tar.gz
eclipse.jdt.core-39f06c360c3afa72b1c211d620c70a07e6fca943.tar.xz
eclipse.jdt.core-39f06c360c3afa72b1c211d620c70a07e6fca943.zip
Bug 520874: [compiler] difference in behavior in single static import
and on-demand Static import of private nested types should be rejected in outer type's declaration. e.g type parameter, super class reference etc. Change-Id: I8af2e0e9bc8b50438cfd426fde6dd1b6179d8c22 Signed-off-by: Jay Arthanareeswaran <jarthana@in.ibm.com>
-rw-r--r--org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/StaticImportTest.java199
-rw-r--r--org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/CompilationUnitScope.java1
-rw-r--r--org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/Scope.java18
3 files changed, 174 insertions, 44 deletions
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/StaticImportTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/StaticImportTest.java
index e5ec3d5041..57214e11a6 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/StaticImportTest.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/StaticImportTest.java
@@ -5,6 +5,10 @@
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
+ * This is an implementation of an early-draft specification developed under the Java
+ * Community Process (JCP) and is made available for testing and evaluation purposes
+ * only. The code is not compatible with any specification of the JCP.
+ *
* Contributors:
* IBM Corporation - initial API and implementation
* Stephan Herrmann - Contributions for
@@ -23,7 +27,7 @@ public class StaticImportTest extends AbstractComparableTest {
// Static initializer to specify tests subset using TESTS_* static variables
// All specified tests which do not belong to the class are skipped...
static {
-// TESTS_NAMES = new String[] { "testBug401271" };
+// TESTS_NAMES = new String[] { "test075" };
// TESTS_NAMES = new String[] { "test085c" };
// TESTS_NUMBERS = new int[] { 80 };
// TESTS_RANGE = new int[] { 75, -1 };
@@ -2573,12 +2577,7 @@ public class StaticImportTest extends AbstractComparableTest {
" }\n" +
"}\n",
},
- "----------\n" +
- "1. ERROR in A\\A.java (at line 3)\n" +
- " import static B.B.C1;\n" +
- " ^^^^^^\n" +
- "The import B.B.C1 collides with another import statement\n" +
- "----------\n"
+ ""
);
}
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=302865
@@ -2678,7 +2677,8 @@ public class StaticImportTest extends AbstractComparableTest {
}
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=302865
// To verify that a static import importing a type which has already been
- // imported by a single type import is reported as duplicate
+ // imported by a single type import is not reported as duplicate
+ // if they are just the same type
public void test079() {
this.runNegativeTest(
new String[] {
@@ -2697,10 +2697,10 @@ public class StaticImportTest extends AbstractComparableTest {
"}\n",
},
"----------\n" +
- "1. ERROR in A\\A.java (at line 3)\n" +
- " import B.B.C1;\n" +
- " ^^^^^^\n" +
- "The import B.B.C1 collides with another import statement\n" +
+ "1. WARNING in A\\A.java (at line 2)\n" +
+ " import static B.B.C1;\n" +
+ " ^^^^^^\n" +
+ "The import B.B.C1 is never used\n" +
"----------\n"
);
}
@@ -3258,22 +3258,165 @@ public class StaticImportTest extends AbstractComparableTest {
"}"
});
}
- // https://bugs.eclipse.org/bugs/show_bug.cgi?id=521859
- // [compiler] Invalid static import that resolves to a package is not reported as an error
- public void testBug521859() {
+ public void testBug520874a() {
+ if (this.complianceLevel <= ClassFileConstants.JDK1_8) {
+ return;
+ }
runNegativeTest(
- new String[] {
- "pack1/Test.java",
- "package pack1;\n" +
- "import static pack1.*;\n" +
- "public class Test {\n" +
- "}\n"
- },
- "----------\n" +
- "1. ERROR in pack1\\Test.java (at line 2)\n" +
- " import static pack1.*;\n" +
- " ^^^^^\n" +
- "Only a type can be imported. pack1 resolves to a package\n" +
- "----------\n");
+ new String[] {
+ "p/X.java",
+ "package p;\n" +
+ "import static p.A1.Outer.*;\n" +
+ "import static p.A1.AnotherOuter.Inner;\n" +
+ "public class X {}\n" +
+ "class A1 {\n" +
+ " static class Outer<T extends Inner> {\n" +
+ " private static interface Inner {}\n" +
+ " }\n" +
+ " static class AnotherOuter {\n" +
+ " private static class Inner {}\n" +
+ " }\n" +
+ "}\n"
+ },
+ "----------\n" +
+ "1. ERROR in p\\X.java (at line 3)\n" +
+ " import static p.A1.AnotherOuter.Inner;\n" +
+ " ^^^^^^^^^^^^^^^^^^^^^^^\n" +
+ "The type p.A1.AnotherOuter.Inner is not visible\n" +
+ "----------\n" +
+ "2. ERROR in p\\X.java (at line 6)\n" +
+ " static class Outer<T extends Inner> {\n" +
+ " ^^^^^\n" +
+ "The type Inner is not visible\n" +
+ "----------\n");
+ }
+ public void testBug520874b() {
+ if (this.complianceLevel <= ClassFileConstants.JDK1_8) {
+ return;
+ }
+ runNegativeTest(
+ new String[] {
+ "p/X.java",
+ "package p;\n" +
+ "import p.A1.Outer.*;\n" +
+ "public class X {}\n" +
+ "class A1 {\n" +
+ " static class Outer<T extends Inner> {\n" +
+ " private static interface Inner {}\n" +
+ " }\n" +
+ "}\n"
+ },
+ "----------\n" +
+ "1. ERROR in p\\X.java (at line 5)\n" +
+ " static class Outer<T extends Inner> {\n" +
+ " ^^^^^\n" +
+ "The type Inner is not visible\n" +
+ "----------\n");
+ }
+ public void testBug520874c() {
+ if (this.complianceLevel <= ClassFileConstants.JDK1_8) {
+ return;
+ }
+ runNegativeTest(
+ new String[] {
+ "p/X.java",
+ "package p;\n" +
+ "import static p.A1.Outer.Inner;\n" +
+ "import static p.A1.AnotherOuter.Inner;\n" +
+ "public class X {}\n" +
+ "class A1 {\n" +
+ " static class Outer<T extends Inner> {\n" +
+ " private static interface Inner {}\n" +
+ " }\n" +
+ " static class AnotherOuter<T extends Inner> {\n" +
+ " private static class Inner {}\n" +
+ " }\n" +
+ "}\n"
+ },
+ "----------\n" +
+ "1. ERROR in p\\X.java (at line 2)\n" +
+ " import static p.A1.Outer.Inner;\n" +
+ " ^^^^^^^^^^^^^^^^\n" +
+ "The type p.A1.Outer.Inner is not visible\n" +
+ "----------\n" +
+ "2. ERROR in p\\X.java (at line 3)\n" +
+ " import static p.A1.AnotherOuter.Inner;\n" +
+ " ^^^^^^^^^^^^^^^^^^^^^^^\n" +
+ "The type p.A1.AnotherOuter.Inner is not visible\n" +
+ "----------\n" +
+ "3. ERROR in p\\X.java (at line 6)\n" +
+ " static class Outer<T extends Inner> {\n" +
+ " ^^^^^\n" +
+ "Inner cannot be resolved to a type\n" +
+ "----------\n" +
+ "4. ERROR in p\\X.java (at line 9)\n" +
+ " static class AnotherOuter<T extends Inner> {\n" +
+ " ^^^^^\n" +
+ "Inner cannot be resolved to a type\n" +
+ "----------\n");
+ }
+ public void testBug520874d() {
+ if (this.complianceLevel <= ClassFileConstants.JDK1_8) {
+ return;
+ }
+ runNegativeTest(
+ new String[] {
+ "p/X.java",
+ "package p;\n" +
+ "import static p.A.B.Inner;\n" +
+ "import p.Bar.Inner;\n" +
+ "public class X {}\n" +
+ "class A {\n" +
+ " static class B extends Bar {}\n" +
+ "}\n",
+ "p/Bar.java",
+ "package p;\n" +
+ "public class Bar {;\n" +
+ " public static class Inner {}\n" +
+ "}\n"
+ },
+ "----------\n" +
+ "1. WARNING in p\\X.java (at line 2)\n" +
+ " import static p.A.B.Inner;\n" +
+ " ^^^^^^^^^^^\n" +
+ "The import p.A.B.Inner is never used\n" +
+ "----------\n" +
+ "2. WARNING in p\\X.java (at line 3)\n" +
+ " import p.Bar.Inner;\n" +
+ " ^^^^^^^^^^^\n" +
+ "The import p.Bar.Inner is never used\n" +
+ "----------\n");
+ }
+ public void testBug520874e() {
+ if (this.complianceLevel <= ClassFileConstants.JDK1_8) {
+ return;
+ }
+ runNegativeTest(
+ new String[] {
+ "p/X.java",
+ "package p;\n" +
+ "import static p.A.B.Inner;\n" +
+ "import p.Bar.*;\n" +
+ "public class X {}\n" +
+ "class A {\n" +
+ " static class B extends Bar {}\n" +
+ "}\n",
+ "p/Bar.java",
+ "package p;\n" +
+ "public class Bar {;\n" +
+ " public static class Inner {}\n" +
+ "}\n"
+ },
+ "----------\n" +
+ "1. WARNING in p\\X.java (at line 2)\n" +
+ " import static p.A.B.Inner;\n" +
+ " ^^^^^^^^^^^\n" +
+ "The import p.A.B.Inner is never used\n" +
+ "----------\n" +
+ "2. WARNING in p\\X.java (at line 3)\n" +
+ " import p.Bar.*;\n" +
+ " ^^^^^\n" +
+ "The import p.Bar is never used\n" +
+ "----------\n");
}
} \ No newline at end of file
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/CompilationUnitScope.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/CompilationUnitScope.java
index 8fc622f1de..44814f7b0b 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/CompilationUnitScope.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/CompilationUnitScope.java
@@ -980,7 +980,6 @@ private int checkAndRecordImportBinding(
}
} else if (resolved.resolvedImport == referenceBinding) {
if (importReference.isStatic() != resolved.isStatic()) {
- problemReporter().duplicateImport(importReference);
recordImportBinding(new ImportBinding(compoundName, false, importBinding, importReference));
}
}
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 fe96381035..0b1aac3c37 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
@@ -1239,19 +1239,6 @@ public abstract class Scope {
if (memberType.canBeSeenBy(getCurrentPackage())) {
return memberType;
}
- // maybe some type in the compilation unit is extending some class in some package
- // and the selection is for some protected inner class of that superclass
- // https://bugs.eclipse.org/bugs/show_bug.cgi?id=235658
- if (this instanceof CompilationUnitScope) {
- TypeDeclaration[] types = ((CompilationUnitScope)this).referenceContext.types;
- if (types != null) {
- for (int i = 0, max = types.length; i < max; i++) {
- if (memberType.canBeSeenBy(enclosingType, types[i].binding)) {
- return memberType;
- }
- }
- }
- }
} else if (memberType.canBeSeenBy(enclosingType, enclosingReceiverType)) {
return memberType;
}
@@ -3289,11 +3276,12 @@ public abstract class Scope {
if (resolvedImport instanceof PackageBinding) {
temp = findType(name, (PackageBinding) resolvedImport, currentPackage);
} else if (someImport.isStatic()) {
- temp = findMemberType(name, (ReferenceBinding) resolvedImport); // static imports are allowed to see inherited member types
+ // Imports are always resolved in the CU Scope (bug 520874)
+ temp = compilationUnitScope().findMemberType(name, (ReferenceBinding) resolvedImport); // static imports are allowed to see inherited member types
if (temp != null && !temp.isStatic())
temp = null;
} else {
- temp = findDirectMemberType(name, (ReferenceBinding) resolvedImport);
+ temp = compilationUnitScope().findDirectMemberType(name, (ReferenceBinding) resolvedImport);
}
if (TypeBinding.notEquals(temp, type) && temp != null) {
if (temp.isValidBinding()) {

Back to the top