changeset 51152:7bf4f1b5e438

8193302: Javac AssertionError: typeSig ERROR on usage of @Generated Summary: Importing from an invisible package whose name is a prefix of a visible package is not allowed. Reviewed-by: vromero
author jlahoda
date Wed, 13 Jun 2018 12:52:21 +0200
parents f36d08a3e700
children b93bf5846277
files src/java.compiler/share/classes/javax/annotation/processing/Messager.java src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java test/jdk/java/rmi/transport/dgcDeadLock/TestImpl.java test/jdk/sun/security/tools/jarsigner/JarSigningNonAscii.java test/langtools/tools/javac/importChecks/ImportsObservable.java test/langtools/tools/javac/importChecks/ImportsObservable.out test/langtools/tools/javac/modules/ConvenientAccessErrorsTest.java
diffstat 9 files changed, 169 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.compiler/share/classes/javax/annotation/processing/Messager.java	Wed Jun 13 10:34:51 2018 +0200
+++ b/src/java.compiler/share/classes/javax/annotation/processing/Messager.java	Wed Jun 13 12:52:21 2018 +0200
@@ -25,7 +25,6 @@
 
 package javax.annotation.processing;
 
-import javax.annotation.*;
 import javax.tools.Diagnostic;
 import javax.lang.model.element.*;
 
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java	Wed Jun 13 10:34:51 2018 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java	Wed Jun 13 12:52:21 2018 +0200
@@ -181,7 +181,8 @@
         DIAMOND_WITH_ANONYMOUS_CLASS_CREATION(JDK9, Fragments.FeatureDiamondAndAnonClass, DiagKind.NORMAL),
         UNDERSCORE_IDENTIFIER(MIN, JDK8),
         PRIVATE_INTERFACE_METHODS(JDK9, Fragments.FeaturePrivateIntfMethods, DiagKind.PLURAL),
-        LOCAL_VARIABLE_TYPE_INFERENCE(JDK10);
+        LOCAL_VARIABLE_TYPE_INFERENCE(JDK10),
+        IMPORT_ON_DEMAND_OBSERVABLE_PACKAGES(JDK1_2, JDK8);
 
         enum DiagKind {
             NORMAL,
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java	Wed Jun 13 10:34:51 2018 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java	Wed Jun 13 12:52:21 2018 +0200
@@ -3650,17 +3650,8 @@
         OUTER: for (JCImport imp : toplevel.getImports()) {
             if (!imp.staticImport && TreeInfo.name(imp.qualid) == names.asterisk) {
                 TypeSymbol tsym = ((JCFieldAccess)imp.qualid).selected.type.tsym;
-                if (toplevel.modle.visiblePackages != null) {
-                    //TODO - unclear: selects like javax.* will get resolved from the current module
-                    //(as javax is not an exported package from any module). And as javax in the current
-                    //module typically does not contain any classes or subpackages, we need to go through
-                    //the visible packages to find a sub-package:
-                    for (PackageSymbol known : toplevel.modle.visiblePackages.values()) {
-                        if (Convert.packagePart(known.fullname) == tsym.flatName())
-                            continue OUTER;
-                    }
-                }
-                if (tsym.kind == PCK && tsym.members().isEmpty() && !tsym.exists()) {
+                if (tsym.kind == PCK && tsym.members().isEmpty() &&
+                    !(Feature.IMPORT_ON_DEMAND_OBSERVABLE_PACKAGES.allowedInSource(source) && tsym.exists())) {
                     log.error(DiagnosticFlag.RESOLVE_ERROR, imp.pos, Errors.DoesntExist(tsym));
                 }
             }
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java	Wed Jun 13 10:34:51 2018 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java	Wed Jun 13 12:52:21 2018 +0200
@@ -2012,7 +2012,7 @@
                     }
                 }
                 return null;
-            }, sym -> sym.kind == Kind.TYP, false, typeNotFound);
+            }, sym -> sym.kind == Kind.TYP, typeNotFound);
         }
     };
 
@@ -2049,18 +2049,11 @@
         PackageSymbol pack = syms.lookupPackage(env.toplevel.modle, name);
 
         if (allowModules && isImportOnDemand(env, name)) {
-            pack.complete();
-            if (!pack.exists()) {
-                Name nameAndDot = name.append('.', names.empty);
-                boolean prefixOfKnown =
-                        env.toplevel.modle.visiblePackages.values()
-                                                          .stream()
-                                                          .anyMatch(p -> p.fullname.startsWith(nameAndDot));
-
+            if (pack.members().isEmpty()) {
                 return lookupInvisibleSymbol(env, name, syms::getPackagesForName, syms::enterPackage, sym -> {
                     sym.complete();
-                    return sym.exists();
-                }, prefixOfKnown, pack);
+                    return !sym.members().isEmpty();
+                }, pack);
             }
         }
 
@@ -2087,7 +2080,6 @@
                                                             Function<Name, Iterable<S>> get,
                                                             BiFunction<ModuleSymbol, Name, S> load,
                                                             Predicate<S> validate,
-                                                            boolean suppressError,
                                                             Symbol defaultResult) {
         //even if a class/package cannot be found in the current module and among packages in modules
         //it depends on that are exported for any or this module, the class/package may exist internally
@@ -2097,7 +2089,7 @@
 
         for (S sym : candidates) {
             if (validate.test(sym))
-                return createInvisibleSymbolError(env, suppressError, sym);
+                return createInvisibleSymbolError(env, sym);
         }
 
         Set<ModuleSymbol> recoverableModules = new HashSet<>(syms.getAllModules());
@@ -2117,7 +2109,7 @@
                     S sym = load.apply(ms, name);
 
                     if (sym != null && validate.test(sym)) {
-                        return createInvisibleSymbolError(env, suppressError, sym);
+                        return createInvisibleSymbolError(env, sym);
                     }
                 }
             }
@@ -2126,11 +2118,11 @@
         return defaultResult;
     }
 
-    private Symbol createInvisibleSymbolError(Env<AttrContext> env, boolean suppressError, Symbol sym) {
+    private Symbol createInvisibleSymbolError(Env<AttrContext> env, Symbol sym) {
         if (symbolPackageVisible(env, sym)) {
             return new AccessError(env, null, sym);
         } else {
-            return new InvisibleSymbolError(env, suppressError, sym);
+            return new InvisibleSymbolError(env, false, sym);
         }
     }
 
--- a/test/jdk/java/rmi/transport/dgcDeadLock/TestImpl.java	Wed Jun 13 10:34:51 2018 +0200
+++ b/test/jdk/java/rmi/transport/dgcDeadLock/TestImpl.java	Wed Jun 13 12:52:21 2018 +0200
@@ -26,7 +26,6 @@
 
 import java.rmi.*;
 import sun.rmi.transport.*;
-import sun.rmi.*;
 import java.io.*;
 import java.lang.reflect.*;
 import java.rmi.dgc.*;
--- a/test/jdk/sun/security/tools/jarsigner/JarSigningNonAscii.java	Wed Jun 13 10:34:51 2018 +0200
+++ b/test/jdk/sun/security/tools/jarsigner/JarSigningNonAscii.java	Wed Jun 13 12:52:21 2018 +0200
@@ -29,9 +29,7 @@
  * @run main/othervm JarSigningNonAscii
  */
 
-import sun.security.tools.*;
 import java.io.*;
-import java.security.Security;
 import java.util.*;
 import java.util.jar.*;
 import java.security.cert.Certificate;
--- a/test/langtools/tools/javac/importChecks/ImportsObservable.java	Wed Jun 13 10:34:51 2018 +0200
+++ b/test/langtools/tools/javac/importChecks/ImportsObservable.java	Wed Jun 13 12:52:21 2018 +0200
@@ -23,9 +23,10 @@
 
 /*
  * @test
- * @bug 4869999
+ * @bug 4869999 8193302
  * @summary Verify that the compiler does not prematurely decide a package is not observable.
- * @compile ImportsObservable.java
+ * @compile -source 8 -Xlint:-options ImportsObservable.java
+ * @compile/fail/ref=ImportsObservable.out -XDrawDiagnostics ImportsObservable.java
  */
 
 import javax.*;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/importChecks/ImportsObservable.out	Wed Jun 13 12:52:21 2018 +0200
@@ -0,0 +1,2 @@
+ImportsObservable.java:32:1: compiler.err.doesnt.exist: javax
+1 error
--- a/test/langtools/tools/javac/modules/ConvenientAccessErrorsTest.java	Wed Jun 13 10:34:51 2018 +0200
+++ b/test/langtools/tools/javac/modules/ConvenientAccessErrorsTest.java	Wed Jun 13 12:52:21 2018 +0200
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 8169197 8172668 8173117 8175007 8189765
+ * @bug 8169197 8172668 8173117 8175007 8189765 8193302
  * @summary Check convenient errors are produced for inaccessible classes.
  * @library /tools/lib
  * @modules jdk.compiler/com.sun.tools.javac.api
@@ -433,29 +433,6 @@
     }
 
     @Test
-    public void testUnusedImportOnDemand1(Path base) throws Exception {
-        Path src = base.resolve("src");
-        tb.writeJavaFiles(src,
-                          "package test; import javax.annotation.*; public class Test { }");
-        Path classes = base.resolve("classes");
-        tb.createDirectories(classes);
-
-        List<String> log = new JavacTask(tb)
-                .options("-XDrawDiagnostics",
-                         "--add-modules", "java.compiler")
-                .outdir(classes)
-                .files(findJavaFiles(src))
-                .run()
-                .writeAll()
-                .getOutputLines(Task.OutputKind.DIRECT);
-
-        List<String> expected = Arrays.asList("");
-
-        if (!expected.equals(log))
-            throw new Exception("expected output not found; actual: " + log);
-    }
-
-    @Test
     public void testUnusedImportOnDemand2(Path base) throws Exception {
         Path src = base.resolve("src");
         Path src_m1 = src.resolve("m1x");
@@ -744,4 +721,155 @@
         if (!expected.equals(log))
             throw new Exception("expected output not found; actual: " + log);
     }
+
+    @Test
+    public void testPackagesUniquelyVisibleInImportOnDemand(Path base) throws Exception {
+        Path src = base.resolve("src");
+        Path src_ma = src.resolve("ma");
+        tb.writeJavaFiles(src_ma,
+                          "module ma { exports ma; }",
+                          "package ma; public class Api { }");
+        Path src_mb = src.resolve("mb");
+        tb.writeJavaFiles(src_mb,
+                          "module mb { exports ma.mb; }",
+                          "package ma.mb; public class Api { }");
+        Path classes = base.resolve("classes");
+        tb.createDirectories(classes);
+
+        new JavacTask(tb)
+            .options("--module-source-path", src.toString())
+            .outdir(classes)
+            .files(findJavaFiles(src))
+            .run()
+            .writeAll();
+
+        Path test = src.resolve("test");
+        tb.writeJavaFiles(test,
+                          "module test { requires mb; }",
+                          "package test; import ma.*; public class Test { }");
+
+        List<String> log = new JavacTask(tb)
+                .options("-XDrawDiagnostics",
+                         "--module-path", classes.toString())
+                .outdir(classes)
+                .files(findJavaFiles(test))
+                .run(Task.Expect.FAIL)
+                .writeAll()
+                .getOutputLines(Task.OutputKind.DIRECT);
+
+        List<String> expected = Arrays.asList(
+                "Test.java:1:22: compiler.err.package.not.visible: ma, (compiler.misc.not.def.access.does.not.read: test, ma, ma)",
+                "1 error");
+
+        if (!expected.equals(log))
+            throw new Exception("expected output not found; actual: " + log);
+    }
+
+    @Test
+    public void testPackagesUniquelyVisibleInImportOnDemandNoPrefixes(Path base) throws Exception {
+        Path src = base.resolve("src");
+        Path src_mb = src.resolve("mb");
+        tb.writeJavaFiles(src_mb,
+                          "module mb { exports ma.mb; }",
+                          "package ma.mb; public class Api { }");
+        Path classes = base.resolve("classes");
+        tb.createDirectories(classes);
+
+        new JavacTask(tb)
+            .options("--module-source-path", src.toString())
+            .outdir(classes)
+            .files(findJavaFiles(src))
+            .run()
+            .writeAll();
+
+        Path test = src.resolve("test");
+        tb.writeJavaFiles(test,
+                          "module test { requires mb; }",
+                          "package test; import ma.mb.*; import ma.*; public class Test { }");
+
+        List<String> log = new JavacTask(tb)
+                .options("-XDrawDiagnostics",
+                         "--module-path", classes.toString())
+                .outdir(classes)
+                .files(findJavaFiles(test))
+                .run(Task.Expect.FAIL)
+                .writeAll()
+                .getOutputLines(Task.OutputKind.DIRECT);
+
+        List<String> expected = Arrays.asList(
+                "Test.java:1:31: compiler.err.doesnt.exist: ma",
+                "1 error");
+
+        if (!expected.equals(log))
+            throw new Exception("expected output not found; actual: " + log);
+    }
+
+    @Test
+    public void testPackagesUniquelyVisibleInImportOnDemandThisModule(Path base) throws Exception {
+        Path src = base.resolve("src");
+        Path test = src.resolve("test");
+        tb.writeJavaFiles(test,
+                          "module test { }",
+                          "package ma.mb; public class Impl { }",
+                          "package test; import ma.*; public class Test { }");
+
+        Path classes = base.resolve("classes");
+        tb.createDirectories(classes);
+
+        List<String> log = new JavacTask(tb)
+                .options("-XDrawDiagnostics")
+                .outdir(classes)
+                .files(findJavaFiles(test))
+                .run(Task.Expect.FAIL)
+                .writeAll()
+                .getOutputLines(Task.OutputKind.DIRECT);
+
+        List<String> expected = Arrays.asList(
+                "Test.java:1:15: compiler.err.doesnt.exist: ma",
+                "1 error");
+
+        if (!expected.equals(log))
+            throw new Exception("expected output not found; actual: " + log);
+
+        new JavacTask(tb)
+                .options("-source", "8")
+                .outdir(classes)
+                .files(findJavaFiles(test.resolve("ma"), test.resolve("test")))
+                .run(Task.Expect.SUCCESS)
+                .writeAll();
+    }
+
+    @Test
+    public void testPackagesUniquelyVisibleInImportOnDemandThisModuleUnnamed(Path base) throws Exception {
+        Path src = base.resolve("src");
+        Path test = src.resolve("test");
+        tb.writeJavaFiles(test,
+                          "package ma.mb; public class Impl { }",
+                          "package test; import ma.*; public class Test { }");
+
+        Path classes = base.resolve("classes");
+        tb.createDirectories(classes);
+
+        List<String> log = new JavacTask(tb)
+                .options("-XDrawDiagnostics")
+                .outdir(classes)
+                .files(findJavaFiles(test))
+                .run(Task.Expect.FAIL)
+                .writeAll()
+                .getOutputLines(Task.OutputKind.DIRECT);
+
+        List<String> expected = Arrays.asList(
+                "Test.java:1:15: compiler.err.doesnt.exist: ma",
+                "1 error");
+
+        if (!expected.equals(log))
+            throw new Exception("expected output not found; actual: " + log);
+
+        new JavacTask(tb)
+                .options("-source", "8")
+                .outdir(classes)
+                .files(findJavaFiles(test))
+                .run(Task.Expect.SUCCESS)
+                .writeAll();
+    }
 }