1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @summary tests for module declarations
  27  * @library /tools/lib
  28  * @modules jdk.compiler/com.sun.tools.javac.api
  29  *          jdk.compiler/com.sun.tools.javac.main
  30  * @build toolbox.ToolBox toolbox.JavacTask ModuleTestBase
  31  * @run main ExportsUnexported
  32  */
  33 
  34 import java.nio.file.Path;
  35 import java.util.ArrayList;
  36 import java.util.Arrays;
  37 import java.util.Collections;
  38 import java.util.List;
  39 
  40 import toolbox.JavacTask;
  41 import toolbox.Task;
  42 
  43 public class ExportsUnexported extends ModuleTestBase {
  44 
  45     public static void main(String... args) throws Exception {
  46         ExportsUnexported t = new ExportsUnexported();
  47         t.runTests();
  48     }
  49 
  50     @Test
  51     public void testLocations(Path base) throws Exception {
  52         String warningsTest = "package api;\n" +
  53                       "import impl.impl.*;\n" +
  54                       "@impl.impl^.DocAnn\n" +
  55                       "public abstract class Api<T extends impl.impl^.Cls&impl.impl^.Intf> extends impl.impl^.Cls implements impl.impl^.Intf, impl.impl^.NonDocAnn, impl.impl^.DocAnn {\n" +
  56                       "    public static <E extends impl.impl^.Cls&impl.impl^.Intf> impl.impl^.Cls m(impl.impl^.Intf i, impl.impl^.Cls c) throws impl.impl^.Exc { return null; }\n" +
  57                       "    public static impl.impl^.Cls f;\n" +
  58                       "}";
  59         String noWarningsTest = "package api;\n" +
  60                       "import impl.impl.*;\n" +
  61                       "@impl.impl.NonDocAnn\n" +
  62                       "public abstract class Api {\n" +
  63                       "    private static abstract class I <T extends impl.impl.Cls&impl.impl.Intf> extends impl.impl.Cls implements impl.impl.Intf, impl.impl.NonDocAnn, impl.impl.DocAnn {\n" +
  64                       "        public static abstract class II <T extends impl.impl.Cls&impl.impl.Intf> extends impl.impl.Cls implements impl.impl.Intf, impl.impl.NonDocAnn, impl.impl.DocAnn { }\n" +
  65                       "        public static <E extends impl.impl.Cls&impl.impl.Intf> impl.impl.Cls m(impl.impl.Intf i, impl.impl.Cls c) throws impl.impl.Exc { return null; }\n" +
  66                       "        public static impl.impl.Cls f;\n" +
  67                       "    }\n" +
  68                       "    private static <E extends impl.impl.Cls&impl.impl.Intf> impl.impl.Cls m(impl.impl.Intf i, impl.impl.Cls c) throws impl.impl.Exc { return null; }\n" +
  69                       "    private static impl.impl.Cls f1;\n" +
  70                       "    public static void m() { new impl.impl.Cls(); }\n" +
  71                       "    public static Object f2 = new impl.impl.Cls();\n" +
  72                       "}";
  73         for (String genericTest : new String[] {warningsTest, noWarningsTest}) {
  74             for (String test : new String[] {genericTest, genericTest.replaceAll("impl\\.impl\\^.([A-Za-z])", "^$1").replaceAll("impl\\.impl\\.([A-Za-z])", "$1")}) {
  75                 System.err.println("testing: " + test);
  76 
  77                 Path src = base.resolve("src");
  78                 Path src_m1 = src.resolve("m1");
  79                 StringBuilder testCode = new StringBuilder();
  80                 List<String> expectedLog = new ArrayList<>();
  81                 int line = 1;
  82                 int col  = 1;
  83 
  84                 for (int i = 0; i < test.length(); i++) {
  85                     char c = test.charAt(i);
  86 
  87                     if (c == '^') {
  88                         StringBuilder typeName = new StringBuilder();
  89                         for (int j = i + 1 + (test.charAt(i + 1) == '.' ? 1 : 0);
  90                              j < test.length() && Character.isJavaIdentifierPart(test.charAt(j));
  91                              j++) {
  92                             typeName.append(test.charAt(j));
  93                         }
  94                         String kindName;
  95                         switch (typeName.toString()) {
  96                             case "Exc": case "DocAnn": case "NonDocAnn":
  97                             case "Cls": kindName = "kindname.class"; break;
  98                             case "Intf": kindName = "kindname.interface"; break;
  99                             default:
 100                                 throw new AssertionError(typeName.toString());
 101                         }
 102                         expectedLog.add("Api.java:" + line + ":" + col + ": compiler.warn.leaks.not.accessible.unexported: " + kindName + ", impl.impl." + typeName + ", m1");
 103                         continue;
 104                     }
 105 
 106                     if (c == '\n') {
 107                         line++;
 108                         col = 0;
 109                     }
 110 
 111                     testCode.append(c);
 112                     col++;
 113                 }
 114 
 115                 if (!expectedLog.isEmpty()) {
 116                     expectedLog.add("" + expectedLog.size() + " warning" + (expectedLog.size() == 1 ? "" : "s"));
 117                     expectedLog.add("- compiler.err.warnings.and.werror");
 118                     expectedLog.add("1 error");
 119                 }
 120 
 121                 Collections.sort(expectedLog);
 122 
 123                 tb.writeJavaFiles(src_m1,
 124                                   "module m1 { exports api; }",
 125                                   testCode.toString(),
 126                                   "package impl.impl; public class Cls { }",
 127                                   "package impl.impl; public class Exc extends Exception { }",
 128                                   "package impl.impl; public interface Intf { }",
 129                                   "package impl.impl; @java.lang.annotation.Documented public @interface DocAnn { }",
 130                                   "package impl.impl; public @interface NonDocAnn { }");
 131                 Path classes = base.resolve("classes");
 132                 tb.createDirectories(classes);
 133 
 134                 List<String> log = new JavacTask(tb)
 135                         .options("-XDrawDiagnostics",
 136                                  "-Werror",
 137                                  "-modulesourcepath", src.toString(),
 138                                  "-Xlint:exports")
 139                         .outdir(classes)
 140                         .files(findJavaFiles(src))
 141                         .run(expectedLog.isEmpty() ? Task.Expect.SUCCESS : Task.Expect.FAIL)
 142                         .writeAll()
 143                         .getOutputLines(Task.OutputKind.DIRECT);
 144 
 145                 log = new ArrayList<>(log);
 146                 Collections.sort(log);
 147 
 148                 if (expectedLog.isEmpty() ? !log.equals(Arrays.asList("")) : !log.equals(expectedLog)) {
 149                     throw new Exception("expected output not found in: " + log + "; " + expectedLog);
 150                 }
 151             }
 152         }
 153     }
 154 
 155     @Test
 156     public void testAccessibleToSpecificOrAll(Path base) throws Exception {
 157         Path src = base.resolve("src");
 158         Path src_lib1 = src.resolve("lib1");
 159         tb.writeJavaFiles(src_lib1,
 160                           "module lib1 { exports lib1; }",
 161                           "package lib1; public class Lib1 {}");
 162         Path src_lib2 = src.resolve("lib2");
 163         tb.writeJavaFiles(src_lib2,
 164                           "module lib2 { exports lib2; }",
 165                           "package lib2; public class Lib2 {}");
 166         Path src_api = src.resolve("api");
 167         tb.writeJavaFiles(src_api,
 168                           "module api {\n" +
 169                           "    exports api;\n" +
 170                           "    exports qapi1 to qual1;\n" +
 171                           "    exports qapi2 to qual1, qual2;\n" +
 172                           "    requires public lib1;\n" +
 173                           "    requires lib2;\n" +
 174                           "}\n",
 175                           "package api;\n" +
 176                           "public class Api {\n" +
 177                           "    public lib1.Lib1 lib1;\n" +
 178                           "    public lib2.Lib2 lib2;\n" +
 179                           "    public qapi1.QApi1 qapi1;\n" +
 180                           "    public impl.Impl impl;\n" +
 181                           "}",
 182                           "package qapi1;\n" +
 183                           "public class QApi1 {\n" +
 184                           "    public qapi2.QApi2 qapi2;\n" +
 185                           "}",
 186                           "package qapi2;\n" +
 187                           "public class QApi2 {\n" +
 188                           "    public qapi1.QApi1 qapi1;\n" +
 189                           "}",
 190                           "package impl;\n" +
 191                           "public class Impl {\n" +
 192                           "}");
 193         Path src_qual1 = src.resolve("qual1");
 194         tb.writeJavaFiles(src_qual1, "module qual1 { }");
 195         Path src_qual2 = src.resolve("qual2");
 196         tb.writeJavaFiles(src_qual2, "module qual2 { }");
 197         Path classes = base.resolve("classes");
 198         tb.createDirectories(classes);
 199 
 200         List<String> log = new JavacTask(tb)
 201                 .options("-XDrawDiagnostics",
 202                          "-Werror",
 203                          "-modulesourcepath", src.toString(),
 204                          "-Xlint:exports")
 205                 .outdir(classes)
 206                 .files(findJavaFiles(src))
 207                 .run(Task.Expect.FAIL)
 208                 .writeAll()
 209                 .getOutputLines(Task.OutputKind.DIRECT);
 210 
 211         List<String> expected = Arrays.asList(
 212             "Api.java:4:16: compiler.warn.leaks.not.accessible.not.required.public: kindname.class, lib2.Lib2, lib2",
 213             "Api.java:5:17: compiler.warn.leaks.not.accessible.unexported.qualified: kindname.class, qapi1.QApi1, api",
 214             "Api.java:6:16: compiler.warn.leaks.not.accessible.unexported: kindname.class, impl.Impl, api",
 215             "- compiler.err.warnings.and.werror",
 216             "1 error",
 217             "3 warnings"
 218         );
 219 
 220         if (!log.equals(expected))
 221             throw new Exception("expected output not found");
 222     }
 223 
 224     @Test
 225     public void testNestedClasses(Path base) throws Exception {
 226         Path src = base.resolve("src");
 227         Path src_api = src.resolve("api");
 228         tb.writeJavaFiles(src_api,
 229                           "module api {\n" +
 230                           "    exports api;\n" +
 231                           "}\n",
 232                           "package api;\n" +
 233                           "import impl.Impl.Nested;\n" +
 234                           "public class Api {\n" +
 235                           "    public impl.Impl impl1;\n" +
 236                           "    public impl.Impl.Nested impl2;\n" +
 237                           "    public Nested impl3;\n" +
 238                           "}",
 239                           "package impl;\n" +
 240                           "public class Impl {\n" +
 241                           "    public static class Nested {\n" +
 242                           "    }\n" +
 243                           "}");
 244         Path classes = base.resolve("classes");
 245         tb.createDirectories(classes);
 246 
 247         List<String> log = new JavacTask(tb)
 248                 .options("-XDrawDiagnostics",
 249                          "-Werror",
 250                          "-modulesourcepath", src.toString(),
 251                          "-Xlint:exports")
 252                 .outdir(classes)
 253                 .files(findJavaFiles(src))
 254                 .run(Task.Expect.FAIL)
 255                 .writeAll()
 256                 .getOutputLines(Task.OutputKind.DIRECT);
 257 
 258         List<String> expected = Arrays.asList(
 259             "Api.java:4:16: compiler.warn.leaks.not.accessible.unexported: kindname.class, impl.Impl, api",
 260             "Api.java:5:16: compiler.warn.leaks.not.accessible.unexported: kindname.class, impl.Impl, api",
 261             "Api.java:6:12: compiler.warn.leaks.not.accessible.unexported: kindname.class, impl.Impl.Nested, api",
 262             "- compiler.err.warnings.and.werror",
 263             "1 error",
 264             "3 warnings"
 265         );
 266 
 267         if (!log.equals(expected))
 268             throw new Exception("expected output not found");
 269     }
 270 
 271     @Test
 272     public void testProtectedAndInaccessible(Path base) throws Exception {
 273         Path src = base.resolve("src");
 274         Path src_api = src.resolve("api");
 275         tb.writeJavaFiles(src_api,
 276                           "module api {\n" +
 277                           "    exports api;\n" +
 278                           "}\n",
 279                           "package api;\n" +
 280                           "public class Api extends PackagePrivateClass<PackagePrivateInterface> implements PackagePrivateInterface<PackagePrivateClass> {\n" +
 281                           "    protected PackagePrivateClass<?> f1;\n" +
 282                           "    protected PackagePrivateInterface<?> f2;\n" +
 283                           "    protected Inner f3;\n" +
 284                           "    protected PrivateInner f4;\n" +
 285                           "    protected impl.Impl f5;\n" +
 286                           "    public static class InnerClass extends PrivateInner {}\n" +
 287                           "    protected static class Inner {}\n" +
 288                           "    private static class PrivateInner {}\n" +
 289                           "}\n" +
 290                           "class PackagePrivateClass<T> {}\n" +
 291                           "interface PackagePrivateInterface<T> {}",
 292                           "package impl;\n" +
 293                           "public class Impl {\n" +
 294                           "}");
 295         Path classes = base.resolve("classes");
 296         tb.createDirectories(classes);
 297 
 298         List<String> log = new JavacTask(tb)
 299                 .options("-XDrawDiagnostics",
 300                          "-Werror",
 301                          "-modulesourcepath", src.toString(),
 302                          "-Xlint:exports")
 303                 .outdir(classes)
 304                 .files(findJavaFiles(src))
 305                 .run(Task.Expect.FAIL)
 306                 .writeAll()
 307                 .getOutputLines(Task.OutputKind.DIRECT);
 308 
 309         List<String> expected = Arrays.asList(
 310             "Api.java:2:46: compiler.warn.leaks.not.accessible: kindname.interface, api.PackagePrivateInterface, api",
 311             "Api.java:2:106: compiler.warn.leaks.not.accessible: kindname.class, api.PackagePrivateClass, api",
 312             "Api.java:3:15: compiler.warn.leaks.not.accessible: kindname.class, api.PackagePrivateClass, api",
 313             "Api.java:4:15: compiler.warn.leaks.not.accessible: kindname.interface, api.PackagePrivateInterface, api",
 314             "Api.java:6:15: compiler.warn.leaks.not.accessible: kindname.class, api.Api.PrivateInner, api",
 315             "Api.java:7:19: compiler.warn.leaks.not.accessible.unexported: kindname.class, impl.Impl, api",
 316             "- compiler.err.warnings.and.werror",
 317             "1 error",
 318             "6 warnings"
 319         );
 320 
 321         if (!log.equals(expected))
 322             throw new Exception("expected output not found");
 323     }
 324 
 325     @Test
 326     public void testSuppressResetProperly(Path base) throws Exception {
 327         Path src = base.resolve("src");
 328         Path src_api = src.resolve("api");
 329         tb.writeJavaFiles(src_api,
 330                           "module api {\n" +
 331                           "    exports api;\n" +
 332                           "}\n",
 333                           "package api;\n" +
 334                           "public class Api {\n" +
 335                           "    @SuppressWarnings(\"exports\")\n" +
 336                           "    public PackagePrivateClass f1;\n" +
 337                           "    public PackagePrivateClass f2;\n" +
 338                           "    @SuppressWarnings(\"exports\")\n" +
 339                           "    public void t() {}\n" +
 340                           "    public PackagePrivateClass f3;\n" +
 341                           "    @SuppressWarnings(\"exports\")\n" +
 342                           "    public static class C {\n" +
 343                           "        public PackagePrivateClass f4;\n" +
 344                           "    }\n" +
 345                           "    public PackagePrivateClass f5;\n" +
 346                           "}\n" +
 347                           "class PackagePrivateClass<T> {}\n");
 348         Path classes = base.resolve("classes");
 349         tb.createDirectories(classes);
 350 
 351         List<String> log = new JavacTask(tb)
 352                 .options("-XDrawDiagnostics",
 353                          "-Werror",
 354                          "-modulesourcepath", src.toString(),
 355                          "-Xlint:exports")
 356                 .outdir(classes)
 357                 .files(findJavaFiles(src))
 358                 .run(Task.Expect.FAIL)
 359                 .writeAll()
 360                 .getOutputLines(Task.OutputKind.DIRECT);
 361 
 362         List<String> expected = Arrays.asList(
 363             "Api.java:5:12: compiler.warn.leaks.not.accessible: kindname.class, api.PackagePrivateClass, api",
 364             "Api.java:8:12: compiler.warn.leaks.not.accessible: kindname.class, api.PackagePrivateClass, api",
 365             "Api.java:13:12: compiler.warn.leaks.not.accessible: kindname.class, api.PackagePrivateClass, api",
 366             "- compiler.err.warnings.and.werror",
 367             "1 error",
 368             "3 warnings"
 369         );
 370 
 371         if (!log.equals(expected))
 372             throw new Exception("expected output not found");
 373     }
 374 
 375 }