Skip to content

Commit 01d51a1

Browse files
lgxbslgxjonathan-gibbons
authored andcommitted
8231622: SuppressWarning("serial") ignored on field serialVersionUID
Reviewed-by: jjg
1 parent a06cea5 commit 01d51a1

File tree

2 files changed

+126
-2
lines changed
  • src/jdk.compiler/share/classes/com/sun/tools/javac/comp
  • test/langtools/tools/javac/T8231622

2 files changed

+126
-2
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5353,7 +5353,7 @@ private void attribClassBody(Env<AttrContext> env, ClassSymbol c) {
53535353
&& isSerializable(c.type)
53545354
&& (c.flags() & (Flags.ENUM | Flags.INTERFACE)) == 0
53555355
&& !c.isAnonymous()) {
5356-
checkSerialVersionUID(tree, c);
5356+
checkSerialVersionUID(tree, c, env);
53575357
}
53585358
if (allowTypeAnnos) {
53595359
// Correctly organize the positions of the type annotations
@@ -5386,7 +5386,7 @@ boolean isSerializable(Type t) {
53865386
}
53875387

53885388
/** Check that an appropriate serialVersionUID member is defined. */
5389-
private void checkSerialVersionUID(JCClassDecl tree, ClassSymbol c) {
5389+
private void checkSerialVersionUID(JCClassDecl tree, ClassSymbol c, Env<AttrContext> env) {
53905390

53915391
// check for presence of serialVersionUID
53925392
VarSymbol svuid = null;
@@ -5403,6 +5403,13 @@ private void checkSerialVersionUID(JCClassDecl tree, ClassSymbol c) {
54035403
return;
54045404
}
54055405

5406+
// Check if @SuppressWarnings("serial") is an annotation of serialVersionUID.
5407+
// See JDK-8231622 for more information.
5408+
Lint lint = env.info.lint.augment(svuid);
5409+
if (lint.isSuppressed(LintCategory.SERIAL)) {
5410+
return;
5411+
}
5412+
54065413
// check that it is static final
54075414
if ((svuid.flags() & (STATIC | FINAL)) !=
54085415
(STATIC | FINAL))
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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

Comments
 (0)