80 List<String> log; 81 List<String> expected = List.of(""); 82 83 // Warning disabled, no messages expected 84 log = new JavacTask(tb) 85 .options("-Xlint:-missing-explicit-ctor", "-Werror") 86 .outdir(classes) 87 .files(tb.findJavaFiles(src)) 88 .run(Expect.SUCCESS) 89 .writeAll() 90 .getOutputLines(Task.OutputKind.DIRECT); 91 92 if (!expected.equals(log)) { 93 throw new AssertionError("Unexpected output: " + log); 94 } 95 96 expected = 97 List.of("Foo.java:4:8: compiler.warn.missing-explicit-ctor: pkg1.Foo, pkg1, mod", 98 "Foo.java:12:12: compiler.warn.missing-explicit-ctor: pkg1.Foo.FooNest, pkg1, mod", 99 "Foo.java:16:19: compiler.warn.missing-explicit-ctor: pkg1.Foo.StaticFooNest, pkg1, mod", 100 "3 warnings"); 101 102 // Warning enable, 103 log = new JavacTask(tb) 104 .options("-Xlint:missing-explicit-ctor", "-XDrawDiagnostics") 105 .outdir(classes) 106 .files(tb.findJavaFiles(src)) 107 .run(Expect.SUCCESS) 108 .writeAll() 109 .getOutputLines(Task.OutputKind.DIRECT); 110 111 if (!expected.equals(log)) { 112 throw new AssertionError("Unexpected output: " + log); 113 } 114 } 115 116 protected void runTests() throws Exception { 117 runTests(m -> new Object[] { Paths.get(m.getName()).toAbsolutePath() }); 118 } 119 120 private static final String MOD_INFO_SRC = 121 """ 122 module mod { 123 exports pkg1; 124 // Do *not* export pkg2. 125 exports pkg3 to java.base; 126 } 127 """; 128 129 private static final String PKG1_BAR_SRC = 130 """ 131 package pkg1; 132 133 // Neither the top-level class nor the nested classes should generate 134 // a warning since Bar is not public. 135 136 // No explicit constructor; use a default. 137 class Bar { 138 139 // No explicit constructor; use a default. 140 public class FooNest { 141 } 142 143 // No explicit constructor; use a default. 144 public static class StaticFooNest { 145 } 146 147 // Package-access classes 148 149 // No explicit constructor; use a default. 150 /*package*/ class PkgFooNest { 151 } 152 153 // No explicit constructor; use a default. 154 /*package*/ static class PkgStaticFooNest { 155 } 156 // Private classes 157 158 // No explicit constructor; use a default. 159 private class PrvFooNest { 160 } 161 162 // No explicit constructor; use a default. 163 private static class PrvStaticFooNest { 164 } 165 } 166 """; 167 168 private static final String PKG1_FOO_SRC = 169 """ 170 package pkg1; 171 172 // No explicit constructor; use a default. 173 public class Foo { 174 175 /* 176 * Of the nexted classes, only FooNest and StaticFooNest should 177 * generate warnings. 178 */ 179 180 // No explicit constructor; use a default. 181 public class FooNest { 182 } 183 184 // No explicit constructor; use a default. 185 public static class StaticFooNest { 186 } 187 188 // No explicit constructor; use a default. 189 @SuppressWarnings("missing-explicit-ctor") 190 public static class SuppressedStaticFooNest { 191 } 192 193 // Package-access classes 194 195 // No explicit constructor; use a default. 196 /*package*/ class PkgFooNest { 197 } 198 199 // No explicit constructor; use a default. 200 /*package*/ static class PkgStaticFooNest { 201 } 202 // Private classes 203 204 // No explicit constructor; use a default. 205 private class PrvFooNest { 206 } 207 208 // No explicit constructor; use a default. 209 private static class PrvStaticFooNest { 210 } 211 } 212 """; 213 214 private static final String PKG2_BAZ_SRC = 215 """ 216 package pkg2; 217 218 // None of these classes should generate warnings since pkg2 is not 219 // exported unconditionally. 220 221 // No explicit constructor; use a default. 222 public class Baz { 223 224 // No explicit constructor; use a default. 225 public class FooNest { | 80 List<String> log; 81 List<String> expected = List.of(""); 82 83 // Warning disabled, no messages expected 84 log = new JavacTask(tb) 85 .options("-Xlint:-missing-explicit-ctor", "-Werror") 86 .outdir(classes) 87 .files(tb.findJavaFiles(src)) 88 .run(Expect.SUCCESS) 89 .writeAll() 90 .getOutputLines(Task.OutputKind.DIRECT); 91 92 if (!expected.equals(log)) { 93 throw new AssertionError("Unexpected output: " + log); 94 } 95 96 expected = 97 List.of("Foo.java:4:8: compiler.warn.missing-explicit-ctor: pkg1.Foo, pkg1, mod", 98 "Foo.java:12:12: compiler.warn.missing-explicit-ctor: pkg1.Foo.FooNest, pkg1, mod", 99 "Foo.java:16:19: compiler.warn.missing-explicit-ctor: pkg1.Foo.StaticFooNest, pkg1, mod", 100 "Foo.java:25:15: compiler.warn.missing-explicit-ctor: pkg1.Foo.ProtectedFooNest, pkg1, mod", 101 "Foo.java:27:19: compiler.warn.missing-explicit-ctor: pkg1.Foo.ProtectedFooNest.ProtectedFooNestNest, pkg1, mod", 102 "5 warnings"); 103 104 // Warning enable, 105 log = new JavacTask(tb) 106 .options("-Xlint:missing-explicit-ctor", "-XDrawDiagnostics") 107 .outdir(classes) 108 .files(tb.findJavaFiles(src)) 109 .run(Expect.SUCCESS) 110 .writeAll() 111 .getOutputLines(Task.OutputKind.DIRECT); 112 113 if (!expected.equals(log)) { 114 throw new AssertionError("Unexpected output: " + log); 115 } 116 } 117 118 protected void runTests() throws Exception { 119 runTests(m -> new Object[] { Paths.get(m.getName()).toAbsolutePath() }); 120 } 121 122 private static final String MOD_INFO_SRC = 123 """ 124 module mod { 125 exports pkg1; 126 // Do *not* export pkg2. 127 exports pkg3 to java.base; 128 } 129 """; 130 131 private static final String PKG1_BAR_SRC = 132 """ 133 package pkg1; 134 135 // Neither the top-level class nor the nested classes should generate 136 // a warning since Bar is not public. 137 138 // No explicit constructor; use a default. 139 class Bar { 140 141 // No explicit constructor; use a default. 142 public class BarNest { 143 } 144 145 // No explicit constructor; use a default. 146 public static class StaticBaryNest { 147 } 148 149 // No explicit constructor; use a default. 150 protected class ProtectedBarNest { 151 } 152 153 // Package-access classes 154 155 // No explicit constructor; use a default. 156 /*package*/ class PkgBarNest { 157 } 158 159 // No explicit constructor; use a default. 160 /*package*/ static class PkgStaticBarNest { 161 } 162 // Private classes 163 164 // No explicit constructor; use a default. 165 private class PrvBarNest { 166 } 167 168 // No explicit constructor; use a default. 169 private static class PrvStaticBarNest { 170 } 171 } 172 """; 173 174 private static final String PKG1_FOO_SRC = 175 """ 176 package pkg1; 177 178 // No explicit constructor; use a default. 179 public class Foo { 180 181 /* 182 * Of the nexted classes, only FooNest and StaticFooNest should 183 * generate warnings. 184 */ 185 186 // No explicit constructor; use a default. 187 public class FooNest { 188 } 189 190 // No explicit constructor; use a default. 191 public static class StaticFooNest { 192 } 193 194 // No explicit constructor; use a default. 195 @SuppressWarnings("missing-explicit-ctor") 196 public static class SuppressedStaticFooNest { 197 } 198 199 // No explicit constructor; use a default. 200 protected class ProtectedFooNest { 201 // No explicit constructor; use a default. 202 protected class ProtectedFooNestNest {} 203 } 204 205 // Package-access classes 206 207 // No explicit constructor; use a default. 208 /*package*/ class PkgFooNest { 209 // No explicit constructor; use a default. 210 protected class PkgFooNestNest {} 211 } 212 213 // No explicit constructor; use a default. 214 /*package*/ static class PkgStaticFooNest { 215 } 216 // Private classes 217 218 // No explicit constructor; use a default. 219 private class PrvFooNest { 220 // No explicit constructor; use a default. 221 protected class PrvFooNestNest {} 222 } 223 224 // No explicit constructor; use a default. 225 private static class PrvStaticFooNest { 226 } 227 } 228 """; 229 230 private static final String PKG2_BAZ_SRC = 231 """ 232 package pkg2; 233 234 // None of these classes should generate warnings since pkg2 is not 235 // exported unconditionally. 236 237 // No explicit constructor; use a default. 238 public class Baz { 239 240 // No explicit constructor; use a default. 241 public class FooNest { |