changeset 4296:92b50b698e6e

Service implementations must be public per current design.
author jlahoda
date Tue, 13 Sep 2016 18:21:01 +0200
parents b403a0007b81
children 4ad21ff4219c
files src/jdk.compiler/share/classes/com/sun/tools/javac/comp/AttrContext.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties test/tools/javac/diags/examples/ServiceImplementationNoArgsConstructorNotPublic/exported/Service.java test/tools/javac/diags/examples/ServiceImplementationNoArgsConstructorNotPublic/impl/ServiceImplementation.java test/tools/javac/diags/examples/ServiceImplementationNoArgsConstructorNotPublic/module-info.java test/tools/javac/modules/ProvidesTest.java
diffstat 8 files changed, 120 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/AttrContext.java	Mon Sep 12 11:44:51 2016 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/AttrContext.java	Tue Sep 13 18:21:01 2016 +0200
@@ -74,11 +74,6 @@
      */
     boolean isNewClass = false;
 
-    /**
-     *  Should access modifiers be ignored?
-     */
-    boolean ignoreAccessModifiers = false;
-
     /** Are arguments to current function applications boxed into an array for varargs?
      */
     Resolve.MethodResolutionPhase pendingResolutionPhase = null;
@@ -125,7 +120,6 @@
         info.isSpeculative = isSpeculative;
         info.isAnonymousDiamond = isAnonymousDiamond;
         info.isNewClass = isNewClass;
-        info.ignoreAccessModifiers = ignoreAccessModifiers;
         info.preferredTreeForDiagnostics = preferredTreeForDiagnostics;
         return info;
     }
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java	Mon Sep 12 11:44:51 2016 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java	Tue Sep 13 18:21:01 2016 +0200
@@ -98,6 +98,7 @@
 import com.sun.tools.javac.util.Names;
 import com.sun.tools.javac.util.Options;
 
+import static com.sun.tools.javac.code.Flags.PUBLIC;
 import static com.sun.tools.javac.code.Flags.UNATTRIBUTED;
 import static com.sun.tools.javac.code.Kinds.Kind.MDL;
 import static com.sun.tools.javac.code.TypeTag.CLASS;
@@ -840,14 +841,7 @@
         @Override
         public void visitProvides(JCProvides tree) {
             Type st = attr.attribType(tree.serviceName, env, syms.objectType);
-            boolean prevIgnoreAccessModifiers = env.info.ignoreAccessModifiers;
-            Type it;
-            try {
-                env.info.ignoreAccessModifiers = true;
-                it = attr.attribType(tree.implName, env, syms.objectType);
-            } finally {
-                env.info.ignoreAccessModifiers = prevIgnoreAccessModifiers;
-            }
+            Type it = attr.attribType(tree.implName, env, syms.objectType);
             ClassSymbol service = (ClassSymbol) st.tsym;
             ClassSymbol impl = (ClassSymbol) it.tsym;
             if (!types.isSubtype(it, st)) {
@@ -863,6 +857,8 @@
                 MethodSymbol constr = noArgsConstructor(impl);
                 if (constr == null) {
                     log.error(tree.implName.pos(), Errors.ServiceImplementationDoesntHaveANoArgsConstructor(impl));
+                } else if ((constr.flags() & PUBLIC) == 0) {
+                    log.error(tree.implName.pos(), Errors.ServiceImplementationNoArgsConstructorNotPublic(impl));
                 }
             }
             if (st.hasTag(CLASS) && it.hasTag(CLASS)) {
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java	Mon Sep 12 11:44:51 2016 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java	Tue Sep 13 18:21:01 2016 +0200
@@ -304,10 +304,8 @@
         if (env.enclMethod != null && (env.enclMethod.mods.flags & ANONCONSTR) != 0)
             return true;
 
-        boolean isAccessible;
-        short accessFlag = !env.info.ignoreAccessModifiers ?
-                (short)(c.flags() & AccessFlags) : PUBLIC;
-        switch (accessFlag) {
+        boolean isAccessible = false;
+        switch ((short)(c.flags() & AccessFlags)) {
             case PRIVATE:
                 isAccessible =
                     env.enclClass.sym.outermostClass() ==
@@ -323,6 +321,7 @@
                 isAccessible = true;
                 break;
             case PUBLIC:
+                isAccessible = true;
                 if (allowModules && checkModuleAccess) {
                     ModuleSymbol currModule = env.toplevel.modle;
                     currModule.complete();
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Mon Sep 12 11:44:51 2016 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Tue Sep 13 18:21:01 2016 +0200
@@ -2767,6 +2767,10 @@
     the service implementation does not have a default constructor: {0}
 
 # 0: symbol
+compiler.err.service.implementation.no.args.constructor.not.public=\
+    the no arguments constructor of the service implementation is not public: {0}
+
+# 0: symbol
 compiler.err.package.empty.or.not.found=\
     package is empty or does not exist: {0}
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/diags/examples/ServiceImplementationNoArgsConstructorNotPublic/exported/Service.java	Tue Sep 13 18:21:01 2016 +0200
@@ -0,0 +1,27 @@
+/*
+ * 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.
+ */
+
+package exported;
+
+public interface Service {
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/diags/examples/ServiceImplementationNoArgsConstructorNotPublic/impl/ServiceImplementation.java	Tue Sep 13 18:21:01 2016 +0200
@@ -0,0 +1,31 @@
+/*
+ * 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.
+ */
+
+package impl;
+
+import exported.Service;
+
+public class ServiceImplementation implements Service {
+    ServiceImplementation() {
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/diags/examples/ServiceImplementationNoArgsConstructorNotPublic/module-info.java	Tue Sep 13 18:21:01 2016 +0200
@@ -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.no.args.constructor.not.public
+
+module ServiceImplementationNoArgsConstructorNotPublic {
+     exports exported;
+     provides exported.Service with impl.ServiceImplementation;
+}
--- a/test/tools/javac/modules/ProvidesTest.java	Mon Sep 12 11:44:51 2016 +0100
+++ b/test/tools/javac/modules/ProvidesTest.java	Tue Sep 13 18:21:01 2016 +0200
@@ -309,11 +309,19 @@
                 "package p1; public class C1 { }",
                 "package p2; class C2 extends p1.C1 { }");
 
-        new JavacTask(tb)
+        List<String> output = new JavacTask(tb)
+                .options("-XDrawDiagnostics")
                 .outdir(Files.createDirectories(base.resolve("classes")))
                 .files(findJavaFiles(src))
-                .run()
-                .writeAll();
+                .run(Task.Expect.FAIL)
+                .writeAll()
+                .getOutputLines(Task.OutputKind.DIRECT);
+
+        List<String> expected = Arrays.asList("module-info.java:1:34: compiler.err.not.def.public.cant.access: p2.C2, p2",
+                "1 error");
+        if (!output.containsAll(expected)) {
+            throw new Exception("Expected output not found");
+        }
     }
 
     @Test
@@ -347,11 +355,19 @@
                 "package p1; public class C1 { }",
                 "package p2; public class C2 extends p1.C1 { private C2() { } }");
 
-        new JavacTask(tb)
+        List<String> output = new JavacTask(tb)
+                .options("-XDrawDiagnostics")
                 .outdir(Files.createDirectories(base.resolve("classes")))
                 .files(findJavaFiles(src))
-                .run()
-                .writeAll();
+                .run(Task.Expect.FAIL)
+                .writeAll()
+                .getOutputLines(Task.OutputKind.DIRECT);
+
+        List<String> expected = Arrays.asList(
+                "module-info.java:1:46: compiler.err.service.implementation.no.args.constructor.not.public: p2.C2");
+        if (!output.containsAll(expected)) {
+            throw new Exception("Expected output not found");
+        }
     }
 
     @Test