Skip to content

Commit cb82c95

Browse files
committed
8312415: Expand -Xlint:serial checks to enum constants with specialized class bodies
Reviewed-by: jjg, jlahoda
1 parent c6396dc commit cb82c95

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5213,6 +5213,17 @@ public Void visitTypeAsEnum(TypeElement e,
52135213
}
52145214
}
52155215
}
5216+
5217+
// Also perform checks on any class bodies of enum constants, see JLS 8.9.1.
5218+
case ENUM_CONSTANT -> {
5219+
var field = (VarSymbol)enclosed;
5220+
JCVariableDecl decl = (JCVariableDecl) TreeInfo.declarationFor(field, p);
5221+
if (decl.init instanceof JCNewClass nc && nc.def != null) {
5222+
ClassSymbol enumConstantType = nc.def.sym;
5223+
visitTypeAsEnum(enumConstantType, p);
5224+
}
5225+
}
5226+
52165227
}});
52175228
}
52185229
return null;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
EnumExternClassBody.java:25:25: compiler.warn.ineffectual.extern.method.enum: readExternal
2+
EnumExternClassBody.java:30:25: compiler.warn.ineffectual.extern.method.enum: writeExternal
3+
EnumExternClassBody.java:35:38: compiler.warn.ineffectual.serial.field.enum: serialVersionUID
4+
EnumExternClassBody.java:36:53: compiler.warn.ineffectual.serial.field.enum: serialPersistentFields
5+
EnumExternClassBody.java:38:25: compiler.warn.ineffectual.serial.method.enum: writeObject
6+
EnumExternClassBody.java:42:27: compiler.warn.ineffectual.serial.method.enum: writeReplace
7+
EnumExternClassBody.java:46:25: compiler.warn.ineffectual.serial.method.enum: readObject
8+
EnumExternClassBody.java:51:25: compiler.warn.ineffectual.serial.method.enum: readObjectNoData
9+
EnumExternClassBody.java:55:27: compiler.warn.ineffectual.serial.method.enum: readResolve
10+
EnumExternClassBody.java:66:21: compiler.warn.ineffectual.extern.method.enum: readExternal
11+
EnumExternClassBody.java:71:21: compiler.warn.ineffectual.extern.method.enum: writeExternal
12+
EnumExternClassBody.java:83:25: compiler.warn.ineffectual.extern.method.enum: readExternal
13+
EnumExternClassBody.java:88:25: compiler.warn.ineffectual.extern.method.enum: writeExternal
14+
EnumExternClassBody.java:101:25: compiler.warn.ineffectual.extern.method.enum: readExternal
15+
EnumExternClassBody.java:106:25: compiler.warn.ineffectual.extern.method.enum: writeExternal
16+
EnumExternClassBody.java:112:30: compiler.warn.ineffectual.extern.method.enum: readExternal
17+
EnumExternClassBody.java:113:30: compiler.warn.ineffectual.extern.method.enum: writeExternal
18+
17 warnings
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* @test /nodynamiccopyright/
3+
* @bug 8312415
4+
* @compile/ref=ClassBody.out -XDrawDiagnostics -Xlint:serial EnumExternClassBody.java
5+
* @compile/ref=empty.out -XDrawDiagnostics EnumExternClassBody.java
6+
*/
7+
8+
import java.io.*;
9+
10+
/*
11+
* Verify warnings are generated as appropriate for enum constants
12+
* with specialized class bodies.
13+
*/
14+
class EnumExternClassBody {
15+
16+
/*
17+
* Define externalization methods both in the enum class and a
18+
* specialized enum constant.
19+
*/
20+
private static enum ColorExtern1 implements Externalizable {
21+
RED( 0xFF_00_00),
22+
GREEN(0x00_FF_00),
23+
BLUE( 0x00_00_FF) {
24+
@Override
25+
public void readExternal(ObjectInput in) {
26+
throw new RuntimeException();
27+
}
28+
29+
@Override
30+
public void writeExternal(ObjectOutput out) throws IOException {
31+
throw new RuntimeException();
32+
}
33+
34+
// Look for serialization members too
35+
private static final long serialVersionUID = 42;
36+
private static final ObjectStreamField[] serialPersistentFields = {};
37+
38+
private void writeObject(ObjectOutputStream stream) throws IOException {
39+
throw new RuntimeException();
40+
}
41+
42+
private Object writeReplace() throws ObjectStreamException {
43+
return null;
44+
}
45+
46+
private void readObject(ObjectInputStream stream)
47+
throws IOException, ClassNotFoundException {
48+
throw new RuntimeException();
49+
}
50+
51+
private void readObjectNoData() throws ObjectStreamException {
52+
return;
53+
}
54+
55+
private Object readResolve() throws ObjectStreamException {
56+
return null;
57+
}
58+
};
59+
60+
int rgb;
61+
private ColorExtern1(int rgb) {
62+
this.rgb = rgb;
63+
}
64+
65+
@Override
66+
public void readExternal(ObjectInput in) {
67+
throw new RuntimeException();
68+
}
69+
70+
@Override
71+
public void writeExternal(ObjectOutput out) throws IOException {
72+
throw new RuntimeException();
73+
}
74+
}
75+
76+
/*
77+
* Define externalization methods only on specialized enum
78+
* constants.
79+
*/
80+
private static enum ColorExtern2 implements Externalizable {
81+
CYAN {
82+
@Override
83+
public void readExternal(ObjectInput in) {
84+
throw new RuntimeException();
85+
}
86+
87+
@Override
88+
public void writeExternal(ObjectOutput out) throws IOException {
89+
throw new RuntimeException();
90+
}
91+
};
92+
}
93+
94+
/*
95+
* Define externalization methods only on specialized enum
96+
* constants.
97+
*/
98+
private static enum ColorExtern3 implements Externalizable {
99+
MAGENTA {
100+
@Override
101+
public void readExternal(ObjectInput in) {
102+
throw new RuntimeException();
103+
}
104+
105+
@Override
106+
public void writeExternal(ObjectOutput out) throws IOException {
107+
throw new RuntimeException();
108+
}
109+
};
110+
111+
// Acceptable to have ineffectual warnings for these abstract methods
112+
public abstract void readExternal(ObjectInput in);
113+
public abstract void writeExternal(ObjectOutput out) throws IOException ;
114+
}
115+
}

0 commit comments

Comments
 (0)