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 (char c : test.toCharArray()) { 85 if (c == '^') { 86 expectedLog.add("Api.java:" + line + ":" + col + ": compiler.warn.unexported.in.api"); 87 continue; 88 } 89 90 if (c == '\n') { 91 line++; 92 col = 0; 93 } 94 95 testCode.append(c); 96 col++; 97 } 98 99 if (!expectedLog.isEmpty()) { 100 expectedLog.add("" + expectedLog.size() + " warning" + (expectedLog.size() == 1 ? "" : "s")); 101 expectedLog.add("- compiler.err.warnings.and.werror"); 102 expectedLog.add("1 error"); 103 } 104 105 Collections.sort(expectedLog); 106 107 tb.writeJavaFiles(src_m1, 108 "module m1 { exports api; }", 109 testCode.toString(), 110 "package impl.impl; public class Cls { }", 111 "package impl.impl; public class Exc extends Exception { }", 112 "package impl.impl; public interface Intf { }", 113 "package impl.impl; @java.lang.annotation.Documented public @interface DocAnn { }", 114 "package impl.impl; public @interface NonDocAnn { }"); 115 Path classes = base.resolve("classes"); 116 tb.createDirectories(classes); 117 118 List<String> log = new JavacTask(tb) 119 .options("-XDrawDiagnostics", 120 "-Werror", 121 "-modulesourcepath", src.toString(), 122 "-Xlint:unexportedinapi") 123 .outdir(classes) 124 .files(findJavaFiles(src)) 125 .run(expectedLog.isEmpty() ? Task.Expect.SUCCESS : Task.Expect.FAIL) 126 .writeAll() 127 .getOutputLines(Task.OutputKind.DIRECT); 128 129 log = new ArrayList<>(log); 130 Collections.sort(log); 131 132 if (expectedLog.isEmpty() ? !log.equals(Arrays.asList("")) : !log.equals(expectedLog)) { 133 throw new Exception("expected output not found in: " + log + "; " + expectedLog); 134 } 135 } 136 } 137 } 138 139 @Test 140 public void testAccessibleToSpecificOrAll(Path base) throws Exception { 141 Path src = base.resolve("src"); 142 Path src_lib1 = src.resolve("lib1"); 143 tb.writeJavaFiles(src_lib1, 144 "module lib1 { exports lib1; }", 145 "package lib1; public class Lib1 {}"); 146 Path src_lib2 = src.resolve("lib2"); 147 tb.writeJavaFiles(src_lib2, 148 "module lib2 { exports lib2; }", 149 "package lib2; public class Lib2 {}"); 150 Path src_api = src.resolve("api"); 151 tb.writeJavaFiles(src_api, 152 "module api {\n" + 153 " exports api;\n" + 154 " exports qapi1 to qual1;\n" + 155 " exports qapi2 to qual1, qual2;\n" + 156 " requires public lib1;\n" + 157 " requires lib2;\n" + 158 "}\n", 159 "package api;\n" + 160 "public class Api {\n" + 161 " public lib1.Lib1 lib1;\n" + 162 " public lib2.Lib2 lib2;\n" + 163 " public qapi1.QApi1 qapi1;\n" + 164 " public impl.Impl impl;\n" + 165 "}", 166 "package qapi1;\n" + 167 "public class QApi1 {\n" + 168 " public qapi2.QApi2 qapi2;\n" + 169 "}", 170 "package qapi2;\n" + 171 "public class QApi2 {\n" + 172 " public qapi1.QApi1 qapi1;\n" + 173 "}", 174 "package impl;\n" + 175 "public class Impl {\n" + 176 "}"); 177 Path src_qual1 = src.resolve("qual1"); 178 tb.writeJavaFiles(src_qual1, "module qual1 { }"); 179 Path src_qual2 = src.resolve("qual2"); 180 tb.writeJavaFiles(src_qual2, "module qual2 { }"); 181 Path classes = base.resolve("classes"); 182 tb.createDirectories(classes); 183 184 List<String> log = new JavacTask(tb) 185 .options("-XDrawDiagnostics", 186 "-Werror", 187 "-modulesourcepath", src.toString(), 188 "-Xlint:unexportedinapi") 189 .outdir(classes) 190 .files(findJavaFiles(src)) 191 .run(Task.Expect.FAIL) 192 .writeAll() 193 .getOutputLines(Task.OutputKind.DIRECT); 194 195 List<String> expected = Arrays.asList( 196 "Api.java:4:16: compiler.warn.unexported.in.api.not.required.public", 197 "Api.java:5:17: compiler.warn.unexported.in.api.qualified", 198 "Api.java:6:16: compiler.warn.unexported.in.api", 199 "- compiler.err.warnings.and.werror", 200 "1 error", 201 "3 warnings" 202 ); 203 204 if (!log.equals(expected)) 205 throw new Exception("expected output not found"); 206 } 207 208 @Test 209 public void testNestedClasses(Path base) throws Exception { 210 Path src = base.resolve("src"); 211 Path src_api = src.resolve("api"); 212 tb.writeJavaFiles(src_api, 213 "module api {\n" + 214 " exports api;\n" + 215 "}\n", 216 "package api;\n" + 217 "import impl.Impl.Nested;\n" + 218 "public class Api {\n" + 219 " public impl.Impl impl1;\n" + 220 " public impl.Impl.Nested impl2;\n" + 221 " public Nested impl3;\n" + 222 "}", 223 "package impl;\n" + 224 "public class Impl {\n" + 225 " public static class Nested {\n" + 226 " }\n" + 227 "}"); 228 Path classes = base.resolve("classes"); 229 tb.createDirectories(classes); 230 231 List<String> log = new JavacTask(tb) 232 .options("-XDrawDiagnostics", 233 "-Werror", 234 "-modulesourcepath", src.toString(), 235 "-Xlint:unexportedinapi") 236 .outdir(classes) 237 .files(findJavaFiles(src)) 238 .run(Task.Expect.FAIL) 239 .writeAll() 240 .getOutputLines(Task.OutputKind.DIRECT); 241 242 List<String> expected = Arrays.asList( 243 "Api.java:4:16: compiler.warn.unexported.in.api", 244 "Api.java:5:16: compiler.warn.unexported.in.api", 245 "Api.java:6:12: compiler.warn.unexported.in.api", 246 "- compiler.err.warnings.and.werror", 247 "1 error", 248 "3 warnings" 249 ); 250 251 if (!log.equals(expected)) 252 throw new Exception("expected output not found"); 253 } 254 255 @Test 256 public void testProtectedAndInaccessible(Path base) throws Exception { 257 Path src = base.resolve("src"); 258 Path src_api = src.resolve("api"); 259 tb.writeJavaFiles(src_api, 260 "module api {\n" + 261 " exports api;\n" + 262 "}\n", 263 "package api;\n" + 264 "public class Api extends PackagePrivateClass<PackagePrivateInterface> implements PackagePrivateInterface<PackagePrivateClass> {\n" + 265 " protected PackagePrivateClass<?> f1;\n" + 266 " protected PackagePrivateInterface<?> f2;\n" + 267 " protected Inner f3;\n" + 268 " protected PrivateInner f4;\n" + 269 " protected impl.Impl f5;\n" + 270 " public static class InnerClass extends PrivateInner {}\n" + 271 " protected static class Inner {}\n" + 272 " private static class PrivateInner {}\n" + 273 "}\n" + 274 "class PackagePrivateClass<T> {}\n" + 275 "interface PackagePrivateInterface<T> {}", 276 "package impl;\n" + 277 "public class Impl {\n" + 278 "}"); 279 Path classes = base.resolve("classes"); 280 tb.createDirectories(classes); 281 282 List<String> log = new JavacTask(tb) 283 .options("-XDrawDiagnostics", 284 "-Werror", 285 "-modulesourcepath", src.toString(), 286 "-Xlint:unexportedinapi") 287 .outdir(classes) 288 .files(findJavaFiles(src)) 289 .run(Task.Expect.FAIL) 290 .writeAll() 291 .getOutputLines(Task.OutputKind.DIRECT); 292 293 List<String> expected = Arrays.asList( 294 "Api.java:2:46: compiler.warn.inaccessible.in.api", 295 "Api.java:2:106: compiler.warn.inaccessible.in.api", 296 "Api.java:3:15: compiler.warn.inaccessible.in.api", 297 "Api.java:4:15: compiler.warn.inaccessible.in.api", 298 "Api.java:6:15: compiler.warn.inaccessible.in.api", 299 "Api.java:7:19: compiler.warn.unexported.in.api", 300 "- compiler.err.warnings.and.werror", 301 "1 error", 302 "6 warnings" 303 ); 304 305 if (!log.equals(expected)) 306 throw new Exception("expected output not found"); 307 } 308 309 }