4869999: Error on import statement naming package containing no class files
authorsadayapalam
Sun, 28 Jun 2015 12:58:24 +0530
changeset 31299 81ce5febcae8
parent 31298 d6e83f2d2be3
child 31300 cf6355e77564
4869999: Error on import statement naming package containing no class files Summary: Ensure that the compiler does not prematurely decide a package is not observable. Reviewed-by: jlahoda
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java
langtools/test/tools/javac/importChecks/ImportsObservable.java
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java	Wed Jun 24 15:15:56 2015 -0700
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java	Sun Jun 28 12:58:24 2015 +0530
@@ -37,6 +37,7 @@
 import com.sun.tools.javac.resources.CompilerProperties.Fragments;
 import com.sun.tools.javac.tree.*;
 import com.sun.tools.javac.util.*;
+import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
 import com.sun.tools.javac.util.List;
 
@@ -3612,6 +3613,18 @@
         }
     }
 
+    // Check that packages imported are in scope (JLS 7.4.3, 6.3, 6.5.3.1, 6.5.3.2)
+    public void checkImportedPackagesObservable(final JCCompilationUnit toplevel) {
+        for (JCImport imp : toplevel.getImports()) {
+            if (!imp.staticImport && TreeInfo.name(imp.qualid) == names.asterisk) {
+                TypeSymbol tsym = ((JCFieldAccess)imp.qualid).selected.type.tsym;
+                if (tsym.kind == PCK && tsym.members().isEmpty() && !tsym.exists()) {
+                    log.error(DiagnosticFlag.RESOLVE_ERROR, imp.pos, "doesnt.exist", tsym);
+                }
+            }
+        }
+    }
+
     private boolean checkTypeContainsImportableElement(TypeSymbol tsym, TypeSymbol origin, PackageSymbol packge, Name name, Set<Symbol> processed) {
         if (tsym == null || !processed.add(tsym))
             return false;
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java	Wed Jun 24 15:15:56 2015 -0700
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java	Sun Jun 28 12:58:24 2015 +0530
@@ -218,6 +218,7 @@
             resolve.run();
             chk.checkImportsUnique(toplevel);
             chk.checkImportsResolvable(toplevel);
+            chk.checkImportedPackagesObservable(toplevel);
             toplevel.namedImportScope.finalizeScope();
             toplevel.starImportScope.finalizeScope();
         } finally {
@@ -323,7 +324,10 @@
                                          chk.importAccessible(sym, packge);
 
                 // Import-on-demand java.lang.
-                importAll(tree.pos, syms.enterPackage(names.java_lang), env);
+                PackageSymbol javaLang = syms.enterPackage(names.java_lang);
+                if (javaLang.members().isEmpty() && !javaLang.exists())
+                    throw new FatalError(diags.fragment("fatal.err.no.java.lang"));
+                importAll(tree.pos, javaLang, env);
 
                 // Process the package def and all import clauses.
                 if (tree.getPackage() != null)
@@ -414,16 +418,6 @@
         private void importAll(int pos,
                                final TypeSymbol tsym,
                                Env<AttrContext> env) {
-            // Check that packages imported from exist (JLS ???).
-            if (tsym.kind == PCK && tsym.members().isEmpty() && !tsym.exists()) {
-                // If we can't find java.lang, exit immediately.
-                if (((PackageSymbol)tsym).fullname.equals(names.java_lang)) {
-                    JCDiagnostic msg = diags.fragment("fatal.err.no.java.lang");
-                    throw new FatalError(msg);
-                } else {
-                    log.error(DiagnosticFlag.RESOLVE_ERROR, pos, "doesnt.exist", tsym);
-                }
-            }
             env.toplevel.starImportScope.importAll(types, tsym.members(), typeImportFilter, false);
         }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/importChecks/ImportsObservable.java	Sun Jun 28 12:58:24 2015 +0530
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 4869999
+ * @summary Verify that the compiler does not prematurely decide a package is not observable.
+ * @compile ImportsObservable.java
+ */
+
+import javax.*;
+import javax.swing.*;
+public class ImportsObservable {
+}