|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, 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 | + * @bug 8231622 |
| 27 | + * @summary SuppressWarning("serial") ignored on field serialVersionUID |
| 28 | + * @library /tools/lib |
| 29 | + * @modules jdk.compiler/com.sun.tools.javac.api |
| 30 | + * jdk.compiler/com.sun.tools.javac.main |
| 31 | + * @build toolbox.ToolBox toolbox.JavacTask |
| 32 | + * @run main T8231622 |
| 33 | + */ |
| 34 | + |
| 35 | +import java.util.List; |
| 36 | +import java.util.Objects; |
| 37 | +import java.util.Arrays; |
| 38 | + |
| 39 | +import toolbox.ToolBox; |
| 40 | +import toolbox.TestRunner; |
| 41 | +import toolbox.JavacTask; |
| 42 | +import toolbox.Task; |
| 43 | + |
| 44 | +public class T8231622 extends TestRunner { |
| 45 | + ToolBox tb; |
| 46 | + |
| 47 | + T8231622() { |
| 48 | + super(System.err); |
| 49 | + tb = new ToolBox(); |
| 50 | + } |
| 51 | + |
| 52 | + public static void main(String[] main) throws Exception { |
| 53 | + T8231622 t = new T8231622(); |
| 54 | + t.runTests(); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testSerialWarning() throws Exception { |
| 59 | + String code = """ |
| 60 | + import java.io.Serializable; |
| 61 | + class T8231622_1 implements Serializable { |
| 62 | + public static final int serialVersionUID = 1; |
| 63 | + }"""; |
| 64 | + |
| 65 | + List<String> output = new JavacTask(tb) |
| 66 | + .sources(code) |
| 67 | + .classpath(".") |
| 68 | + .options("-XDrawDiagnostics", "-Xlint:serial") |
| 69 | + .run() |
| 70 | + .writeAll() |
| 71 | + .getOutputLines(Task.OutputKind.DIRECT); |
| 72 | + List<String> expected = Arrays.asList( |
| 73 | + "T8231622_1.java:3:29: compiler.warn.long.SVUID: T8231622_1", |
| 74 | + "1 warning"); |
| 75 | + tb.checkEqual(expected, output); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testSuppressSerialWarningInClass() throws Exception { |
| 80 | + String code = """ |
| 81 | + import java.io.Serializable; |
| 82 | + @SuppressWarnings("serial") |
| 83 | + class T8231622_2 implements Serializable { |
| 84 | + public static final int serialVersionUID = 1; |
| 85 | + }"""; |
| 86 | + |
| 87 | + List<String> output = new JavacTask(tb) |
| 88 | + .sources(code) |
| 89 | + .classpath(".") |
| 90 | + .options("-XDrawDiagnostics", "-Xlint:serial") |
| 91 | + .run() |
| 92 | + .writeAll() |
| 93 | + .getOutputLines(Task.OutputKind.DIRECT); |
| 94 | + List<String> expected = Arrays.asList(""); |
| 95 | + tb.checkEqual(expected, output); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testSuppressSerialWarningInItsField() throws Exception { |
| 100 | + String code = """ |
| 101 | + import java.io.Serializable; |
| 102 | + class T8231622_3 implements Serializable { |
| 103 | + @SuppressWarnings("serial") |
| 104 | + public static final int serialVersionUID = 1; |
| 105 | + }"""; |
| 106 | + |
| 107 | + List<String> output = new JavacTask(tb) |
| 108 | + .sources(code) |
| 109 | + .classpath(".") |
| 110 | + .options("-XDrawDiagnostics", "-Xlint:serial") |
| 111 | + .run() |
| 112 | + .writeAll() |
| 113 | + .getOutputLines(Task.OutputKind.DIRECT); |
| 114 | + List<String> expected = Arrays.asList(""); |
| 115 | + tb.checkEqual(expected, output); |
| 116 | + } |
| 117 | +} |
0 commit comments