8152062: obscure error message for bad 'provides'

Reviewed-by: jjg
This commit is contained in:
Vicente Romero 2016-05-31 13:00:17 -04:00
parent dee728f027
commit 8d93dc2a15
5 changed files with 109 additions and 5 deletions

View file

@ -68,6 +68,7 @@ import com.sun.tools.javac.code.Symbol.ModuleSymbol;
import com.sun.tools.javac.code.Symbol.PackageSymbol;
import com.sun.tools.javac.code.Symtab;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.Types;
import com.sun.tools.javac.jvm.ClassWriter;
import com.sun.tools.javac.jvm.JNIWriter;
import com.sun.tools.javac.main.Option;
@ -123,6 +124,7 @@ public class Modules extends JCTree.Visitor {
private final Symtab syms;
private final Attr attr;
private final TypeEnvs typeEnvs;
private final Types types;
private final JavaFileManager fileManager;
private final ModuleFinder moduleFinder;
private final boolean allowModules;
@ -160,6 +162,7 @@ public class Modules extends JCTree.Visitor {
attr = Attr.instance(context);
typeEnvs = TypeEnvs.instance(context);
moduleFinder = ModuleFinder.instance(context);
types = Types.instance(context);
fileManager = context.get(JavaFileManager.class);
allowModules = Source.instance(context).allowModules();
Options options = Options.instance(context);
@ -713,9 +716,12 @@ public class Modules extends JCTree.Visitor {
@Override
public void visitProvides(JCProvides tree) {
Type st = attr.attribType(tree.serviceName, env, syms.objectType);
Type it = attr.attribType(tree.implName, env, st);
Type it = attr.attribType(tree.implName, env, syms.objectType);
ClassSymbol service = (ClassSymbol) st.tsym;
ClassSymbol impl = (ClassSymbol) it.tsym;
if (!types.isSubtype(it, st)) {
log.error(tree.implName.pos(), Errors.ServiceImplementationMustBeSubtypeOfServiceInterface);
}
if ((impl.flags() & ABSTRACT) != 0) {
log.error(tree.implName.pos(), Errors.ServiceImplementationIsAbstract(impl));
} else if (impl.isInner()) {

View file

@ -2715,6 +2715,9 @@ compiler.err.duplicate.uses=\
compiler.err.service.implementation.is.abstract=\
the service implementation is an abstract class: {0}
compiler.err.service.implementation.must.be.subtype.of.service.interface=\
the service implementation type must be a subtype of the service interface type
# 0: symbol
compiler.err.service.implementation.is.inner=\
the service implementation is an inner class: {0}

View file

@ -0,0 +1,29 @@
/*
* Copyright (c) 2016, 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.
*/
// key: compiler.err.service.implementation.must.be.subtype.of.service.interface
// key: compiler.err.service.implementation.not.in.right.module
module ServiceImplMustBeSubtypeOfServiceIntf {
provides java.lang.String with java.io.File;
}

View file

@ -0,0 +1,64 @@
/*
* Copyright (c) 2016, 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 8152062
* @summary obscure error message for bad 'provides'
* @library /tools/lib
* @modules
* jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.main
* @build toolbox.ToolBox toolbox.JavacTask ModuleTestBase
* @run main ObscureMessageForBadProvidesTest
*/
import java.nio.file.Path;
import toolbox.JavacTask;
import toolbox.Task;
import toolbox.ToolBox;
public class ObscureMessageForBadProvidesTest extends ModuleTestBase {
public static void main(String... args) throws Exception {
new ObscureMessageForBadProvidesTest().runTests();
}
@Test
public void theTest(Path base) throws Exception {
Path mod = base.resolve("mod");
tb.writeJavaFiles(mod, "module mod { provides java.lang.String with java.io.File; }");
Path classes = base.resolve("classes");
tb.createDirectories(classes);
String log = new JavacTask(tb)
.options("-XDrawDiagnostics")
.outdir(classes)
.files(findJavaFiles(mod))
.run(Task.Expect.FAIL)
.writeAll()
.getOutput(Task.OutputKind.DIRECT);
if (!log.startsWith("module-info.java:1:52: compiler.err.service.implementation.must.be.subtype.of.service.interface"))
throw new Exception("expected output not found");
}
}

View file

@ -143,8 +143,7 @@ public class ProvidesTest extends ModuleTestBase {
List<String> expected = Arrays.asList(
"C.java:1:36: compiler.err.cant.resolve.location: kindname.class, Missing, , , (compiler.misc.location: kindname.package, p, null)",
"module-info.java:1:22: compiler.err.cant.resolve.location: kindname.class, Missing, , , (compiler.misc.location: kindname.package, p, null)",
"module-info.java:1:37: compiler.err.service.implementation.doesnt.have.a.no.args.constructor: <any>",
"3 errors");
"2 errors");
if (!output.containsAll(expected)) {
throw new Exception("Expected output not found");
}
@ -192,8 +191,11 @@ public class ProvidesTest extends ModuleTestBase {
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
List<String> expected = Arrays.asList("module-info.java:1:31: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: p.B, p.A)",
"1 error");
List<String> expected = Arrays.asList(
"module-info.java:1:31: compiler.err.service.implementation.must.be.subtype.of.service.interface",
"module-info.java:1:12: compiler.warn.service.provided.but.not.exported.or.used: p.A",
"1 error",
"1 warning");
if (!output.containsAll(expected)) {
throw new Exception("Expected output not found");
}