4869999: Error on import statement naming package containing no class files

Ensure that the compiler does not prematurely decide a package is not observable.

Reviewed-by: jlahoda
This commit is contained in:
Srikanth Adayapalam 2015-06-28 12:58:24 +05:30
parent ffe4c77137
commit 2943d4bd40
3 changed files with 52 additions and 11 deletions

View File

@ -37,6 +37,7 @@ import com.sun.tools.javac.resources.CompilerProperties.Errors;
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 @@ public class Check {
}
}
// 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;

View File

@ -218,6 +218,7 @@ public class TypeEnter implements Completer {
resolve.run();
chk.checkImportsUnique(toplevel);
chk.checkImportsResolvable(toplevel);
chk.checkImportedPackagesObservable(toplevel);
toplevel.namedImportScope.finalizeScope();
toplevel.starImportScope.finalizeScope();
} finally {
@ -323,7 +324,10 @@ public class TypeEnter implements Completer {
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 @@ public class TypeEnter implements Completer {
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);
}

View File

@ -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 {
}