changeset 4260:8bd01d119749

Service implementations no longer need to be public, or have a public constructor.
author jlahoda
date Wed, 17 Aug 2016 19:37:47 +0200
parents cbc5f0fe29a7
children bb5eb2a299dc
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, 24 insertions(+), 119 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/AttrContext.java	Fri Aug 12 22:50:05 2016 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/AttrContext.java	Wed Aug 17 19:37:47 2016 +0200
@@ -74,6 +74,11 @@
      */
     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;
@@ -120,6 +125,7 @@
         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	Fri Aug 12 22:50:05 2016 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java	Wed Aug 17 19:37:47 2016 +0200
@@ -758,7 +758,14 @@
         @Override
         public void visitProvides(JCProvides tree) {
             Type st = attr.attribType(tree.serviceName, env, syms.objectType);
-            Type it = attr.attribType(tree.implName, 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;
+            }
             ClassSymbol service = (ClassSymbol) st.tsym;
             ClassSymbol impl = (ClassSymbol) it.tsym;
             if (!types.isSubtype(it, st)) {
@@ -774,8 +781,6 @@
                 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	Fri Aug 12 22:50:05 2016 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java	Wed Aug 17 19:37:47 2016 +0200
@@ -304,8 +304,10 @@
         if (env.enclMethod != null && (env.enclMethod.mods.flags & ANONCONSTR) != 0)
             return true;
 
-        boolean isAccessible = false;
-        switch ((short)(c.flags() & AccessFlags)) {
+        boolean isAccessible;
+        short accessFlag = !env.info.ignoreAccessModifiers ?
+                (short)(c.flags() & AccessFlags) : PUBLIC;
+        switch (accessFlag) {
             case PRIVATE:
                 isAccessible =
                     env.enclClass.sym.outermostClass() ==
@@ -321,7 +323,6 @@
                 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	Fri Aug 12 22:50:05 2016 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Wed Aug 17 19:37:47 2016 +0200
@@ -2742,10 +2742,6 @@
     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}
 
--- a/test/tools/javac/diags/examples/ServiceImplementationNoArgsConstructorNotPublic/exported/Service.java	Fri Aug 12 22:50:05 2016 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,27 +0,0 @@
-/*
- * 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 {
-}
--- a/test/tools/javac/diags/examples/ServiceImplementationNoArgsConstructorNotPublic/impl/ServiceImplementation.java	Fri Aug 12 22:50:05 2016 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,31 +0,0 @@
-/*
- * 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() {
-    }
-}
--- a/test/tools/javac/diags/examples/ServiceImplementationNoArgsConstructorNotPublic/module-info.java	Fri Aug 12 22:50:05 2016 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,29 +0,0 @@
-/*
- * 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	Fri Aug 12 22:50:05 2016 +0200
+++ b/test/tools/javac/modules/ProvidesTest.java	Wed Aug 17 19:37:47 2016 +0200
@@ -309,19 +309,11 @@
                 "package p1; public class C1 { }",
                 "package p2; class C2 extends p1.C1 { }");
 
-        List<String> output = new JavacTask(tb)
-                .options("-XDrawDiagnostics")
+        new JavacTask(tb)
                 .outdir(Files.createDirectories(base.resolve("classes")))
                 .files(findJavaFiles(src))
-                .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");
-        }
+                .run()
+                .writeAll();
     }
 
     @Test
@@ -355,19 +347,11 @@
                 "package p1; public class C1 { }",
                 "package p2; public class C2 extends p1.C1 { private C2() { } }");
 
-        List<String> output = new JavacTask(tb)
-                .options("-XDrawDiagnostics")
+        new JavacTask(tb)
                 .outdir(Files.createDirectories(base.resolve("classes")))
                 .files(findJavaFiles(src))
-                .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");
-        }
+                .run()
+                .writeAll();
     }
 
     @Test