< prev index next >

test/tools/javac/modules/ExportsUnexported.java

Print this page




  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                                  "--module-source-path", 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 transitive 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                          "--module-source-path", 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.transitive: 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" +




  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("m1x");
  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
 103                                 + ": compiler.warn.leaks.not.accessible.unexported: "
 104                                 + kindName + ", impl.impl." + typeName + ", m1x");
 105                         continue;
 106                     }
 107 
 108                     if (c == '\n') {
 109                         line++;
 110                         col = 0;
 111                     }
 112 
 113                     testCode.append(c);
 114                     col++;
 115                 }
 116 
 117                 if (!expectedLog.isEmpty()) {
 118                     expectedLog.add("" + expectedLog.size() + " warning" + (expectedLog.size() == 1 ? "" : "s"));
 119                     expectedLog.add("- compiler.err.warnings.and.werror");
 120                     expectedLog.add("1 error");
 121                 }
 122 
 123                 Collections.sort(expectedLog);
 124 
 125                 tb.writeJavaFiles(src_m1,
 126                                   "module m1x { exports api; }",
 127                                   testCode.toString(),
 128                                   "package impl.impl; public class Cls { }",
 129                                   "package impl.impl; public class Exc extends Exception { }",
 130                                   "package impl.impl; public interface Intf { }",
 131                                   "package impl.impl; @java.lang.annotation.Documented public @interface DocAnn { }",
 132                                   "package impl.impl; public @interface NonDocAnn { }");
 133                 Path classes = base.resolve("classes");
 134                 tb.createDirectories(classes);
 135 
 136                 List<String> log = new JavacTask(tb)
 137                         .options("-XDrawDiagnostics",
 138                                  "-Werror",
 139                                  "--module-source-path", src.toString(),
 140                                  "-Xlint:exports")
 141                         .outdir(classes)
 142                         .files(findJavaFiles(src))
 143                         .run(expectedLog.isEmpty() ? Task.Expect.SUCCESS : Task.Expect.FAIL)
 144                         .writeAll()
 145                         .getOutputLines(Task.OutputKind.DIRECT);
 146 
 147                 log = new ArrayList<>(log);
 148                 Collections.sort(log);
 149 
 150                 if (expectedLog.isEmpty() ? !log.equals(Arrays.asList("")) : !log.equals(expectedLog)) {
 151                     throw new Exception("expected output not found in: " + log + "; " + expectedLog);
 152                 }
 153             }
 154         }
 155     }
 156 
 157     @Test
 158     public void testAccessibleToSpecificOrAll(Path base) throws Exception {
 159         Path src = base.resolve("src");
 160         Path src_lib1 = src.resolve("lib1x");
 161         tb.writeJavaFiles(src_lib1,
 162                           "module lib1x { exports lib1; }",
 163                           "package lib1; public class Lib1 {}");
 164         Path src_lib2 = src.resolve("lib2x");
 165         tb.writeJavaFiles(src_lib2,
 166                           "module lib2x { exports lib2; }",
 167                           "package lib2; public class Lib2 {}");
 168         Path src_api = src.resolve("api");
 169         tb.writeJavaFiles(src_api,
 170                           "module api {\n" +
 171                           "    exports api;\n" +
 172                           "    exports qapi1 to qual1x;\n" +
 173                           "    exports qapi2 to qual1x, qual2x;\n" +
 174                           "    requires transitive lib1x;\n" +
 175                           "    requires lib2x;\n" +
 176                           "}\n",
 177                           "package api;\n" +
 178                           "public class Api {\n" +
 179                           "    public lib1.Lib1 lib1;\n" +
 180                           "    public lib2.Lib2 lib2;\n" +
 181                           "    public qapi1.QApi1 qapi1;\n" +
 182                           "    public impl.Impl impl;\n" +
 183                           "}",
 184                           "package qapi1;\n" +
 185                           "public class QApi1 {\n" +
 186                           "    public qapi2.QApi2 qapi2;\n" +
 187                           "}",
 188                           "package qapi2;\n" +
 189                           "public class QApi2 {\n" +
 190                           "    public qapi1.QApi1 qapi1;\n" +
 191                           "}",
 192                           "package impl;\n" +
 193                           "public class Impl {\n" +
 194                           "}");
 195         Path src_qual1 = src.resolve("qual1x");
 196         tb.writeJavaFiles(src_qual1, "module qual1x { }");
 197         Path src_qual2 = src.resolve("qual2x");
 198         tb.writeJavaFiles(src_qual2, "module qual2x { }");
 199         Path classes = base.resolve("classes");
 200         tb.createDirectories(classes);
 201 
 202         List<String> log = new JavacTask(tb)
 203                 .options("-XDrawDiagnostics",
 204                          "-Werror",
 205                          "--module-source-path", src.toString(),
 206                          "-Xlint:exports")
 207                 .outdir(classes)
 208                 .files(findJavaFiles(src))
 209                 .run(Task.Expect.FAIL)
 210                 .writeAll()
 211                 .getOutputLines(Task.OutputKind.DIRECT);
 212 
 213         List<String> expected = Arrays.asList(
 214             "Api.java:4:16: compiler.warn.leaks.not.accessible.not.required.transitive: kindname.class, lib2.Lib2, lib2x",
 215             "Api.java:5:17: compiler.warn.leaks.not.accessible.unexported.qualified: kindname.class, qapi1.QApi1, api",
 216             "Api.java:6:16: compiler.warn.leaks.not.accessible.unexported: kindname.class, impl.Impl, api",
 217             "- compiler.err.warnings.and.werror",
 218             "1 error",
 219             "3 warnings"
 220         );
 221 
 222         if (!log.equals(expected))
 223             throw new Exception("expected output not found");
 224     }
 225 
 226     @Test
 227     public void testNestedClasses(Path base) throws Exception {
 228         Path src = base.resolve("src");
 229         Path src_api = src.resolve("api");
 230         tb.writeJavaFiles(src_api,
 231                           "module api {\n" +
 232                           "    exports api;\n" +
 233                           "}\n",
 234                           "package api;\n" +


< prev index next >