Skip to content

Commit 6a50cdd

Browse files
ECJ should not warn about missing serialVersionUID for anonymous inner classes(eclipse-jdt#4210)
* Fixes eclipse-jdt#4209
1 parent bd4f24a commit 6a50cdd

File tree

3 files changed

+30
-27
lines changed

3 files changed

+30
-27
lines changed

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/TypeDeclaration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,6 +1262,7 @@ public void resolve() {
12621262
boolean needSerialVersion =
12631263
this.scope.compilerOptions().getSeverity(CompilerOptions.MissingSerialVersion) != ProblemSeverities.Ignore
12641264
&& sourceType.isClass()
1265+
&& !sourceType.isAnonymousType()
12651266
&& !sourceType.isRecord()
12661267
&& sourceType.findSuperTypeOriginatingFrom(TypeIds.T_JavaIoExternalizable, false /*Externalizable is not a class*/) == null
12671268
&& sourceType.findSuperTypeOriginatingFrom(TypeIds.T_JavaIoSerializable, false /*Serializable is not a class*/) != null;

org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/GenericTypeTest.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31059,11 +31059,6 @@ public void test0962() {
3105931059
" throw new Exception(\"Bug134645\") {\n" +
3106031060
" ^^^^^^^^^\n" +
3106131061
"The generic class new Exception(){} may not subclass java.lang.Throwable\n" +
31062-
"----------\n" +
31063-
"2. WARNING in X.java (at line 3)\n" +
31064-
" throw new Exception(\"Bug134645\") {\n" +
31065-
" ^^^^^^^^^^^^^^^^^^^^^^\n" +
31066-
"The serializable class does not declare a static final serialVersionUID field of type long\n" +
3106731062
"----------\n",
3106831063
// javac options
3106931064
JavacTestOptions.JavacHasABug.JavacBugFixed_6_10 /* javac test options */);

org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ProblemTypeAndMethodTest.java

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7827,20 +7827,10 @@ public void test376550_11() {
78277827
"}"
78287828
};
78297829
runner.expectedCompilerLog = isMinimumCompliant(ClassFileConstants.JDK11) ?
7830-
"----------\n" +
7831-
"1. WARNING in X.java (at line 6)\n" +
7832-
" return new ArrayList<Object>() {\n" +
7833-
" ^^^^^^^^^^^^^^^^^^^\n" +
7834-
"The serializable class does not declare a static final serialVersionUID field of type long\n" +
7835-
"----------\n"
7830+
""
78367831
:
78377832
"----------\n" +
7838-
"1. WARNING in X.java (at line 6)\n" +
7839-
" return new ArrayList<Object>() {\n" +
7840-
" ^^^^^^^^^^^^^^^^^^^\n" +
7841-
"The serializable class does not declare a static final serialVersionUID field of type long\n" +
7842-
"----------\n" +
7843-
"2. WARNING in X.java (at line 7)\n" +
7833+
"1. WARNING in X.java (at line 7)\n" +
78447834
" { add(o);}\n" +
78457835
" ^\n" +
78467836
"Read access to enclosing field X.o is emulated by a synthetic accessor method\n" +
@@ -7877,11 +7867,6 @@ public void test376550_11a() {
78777867
" public final Collection<Object> go() {\n" +
78787868
" ^^^^\n" +
78797869
"The method go() from the type X can be declared as static\n" +
7880-
"----------\n" +
7881-
"2. WARNING in X.java (at line 6)\n" +
7882-
" return new ArrayList<Object>() {\n" +
7883-
" ^^^^^^^^^^^^^^^^^^^\n" +
7884-
"The serializable class does not declare a static final serialVersionUID field of type long\n" +
78857870
"----------\n";
78867871
runner.javacTestOptions =
78877872
JavacTestOptions.Excuse.EclipseWarningConfiguredAsError;
@@ -7915,11 +7900,6 @@ public void test376550_12() {
79157900
" public final <E1> Collection<E1> go() {\n" +
79167901
" ^^^^\n" +
79177902
"The method go() from the type X<E> can be declared as static\n" +
7918-
"----------\n" +
7919-
"2. WARNING in X.java (at line 6)\n" +
7920-
" return new ArrayList<E1>() {\n" +
7921-
" ^^^^^^^^^^^^^^^\n" +
7922-
"The serializable class does not declare a static final serialVersionUID field of type long\n" +
79237903
"----------\n";
79247904
runner.javacTestOptions =
79257905
JavacTestOptions.Excuse.EclipseWarningConfiguredAsError;
@@ -10135,4 +10115,31 @@ class Impl2 extends Abstract.Nested<@Annot Number> {}
1013510115
""";
1013610116
runner.runNegativeTest();
1013710117
}
10118+
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/4209
10119+
// ECJ should not warn about missing serialVersionUID for anonymous inner classes
10120+
public void testIssue4209() throws Exception {
10121+
this.runNegativeTest(
10122+
new String[] {
10123+
"X.java",
10124+
"""
10125+
public class X {
10126+
void test() {
10127+
new Number() {
10128+
public double doubleValue() {return 0.0;}
10129+
public float floatValue() {return 0.0f;}
10130+
public int intValue() {return 0;}
10131+
public long longValue() {return 0L;}
10132+
};
10133+
return 42;
10134+
}
10135+
}
10136+
""",
10137+
},
10138+
"----------\n" +
10139+
"1. ERROR in X.java (at line 9)\n" +
10140+
" return 42;\n" +
10141+
" ^^^^^^^^^^\n" +
10142+
"Void methods cannot return a value\n" +
10143+
"----------\n");
10144+
}
1013810145
}

0 commit comments

Comments
 (0)