Bug 562268 - [14] No compiler error/warning for @Serial annotated methods/fields that are not part of the serialization mechanism
Summary: [14] No compiler error/warning for @Serial annotated methods/fields that are ...
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.15   Edit
Hardware: PC Windows 10
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords: helpwanted
Depends on:
Blocks:
 
Reported: 2020-04-17 11:57 EDT by Johannes Kuhn CLA
Modified: 2023-04-25 20:25 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Johannes Kuhn CLA 2020-04-17 11:57:03 EDT
The @java.io.Serial annotation is used to denote a field is part of the serialization mechanism (See javadoc: https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/io/Serial.html)

The Eclipse Java Compiler ignores this annotation and doesn't produce an error when it is used for methods or fields that are not part of the serialization mechanism.

Specially, this annotation is there to make sure you have named your method/field correctly and the serialization mechanism will use it. Therefore, the following code should at least produce a warning:

    class Container implements Serializable {
        @Serial
        private Object readresolve() {
            return this;
        }
    }

(The error is the wrong method name, it should be readResolve)
javac doesn't complain (yet), and generates the class file.
At least there should be a way to specify the severity of this error.

On a related note, the template for the generated serialVersionUID should also apply the annotation to the field.
Comment 1 Stephan Herrmann CLA 2020-04-17 16:42:11 EDT
Thanks.

The javadoc of Serial also mentions:
" Note that serialization mechanism accesses its designated fields and methods reflectively and those fields and methods may appear otherwise unused in a Serializable class."

We need to make sure, that in an appropriate context these methods & fields are not flagged as unused.

Also note, the special cases like for enums & Externalizable.

(In reply to Johannes Kuhn from comment #0)
> [...]
> javac doesn't complain (yet), and generates the class file.

do you per chance have a bug number were we can check when / if javac implements this?
Comment 2 Johannes Kuhn CLA 2020-04-19 11:39:17 EDT
I did a short search on the JDK bug database, with no results about support (in the sense of generating warnings) to the JDK.

The annotation was added with JDK-8202385 [1].
There is one bug related to the javac compiler: JDK-8231443 [2].
(Doesn't look like an implementation, through.)

I expect an implementation (emitting a warning) to be related of the first bug.

Anyway, I think it could use the Error/Warnings - Annotation preferences, similar to @NonNull.
Also an option to warn if the annotation is missing.

For the unused case, I found this on the eclipse bug tracker:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=31495
Looks like they have been special cased.

[1]: https://bugs.openjdk.java.net/browse/JDK-8202385
[2]: https://bugs.openjdk.java.net/browse/JDK-8231443
Comment 3 Stephan Herrmann CLA 2020-04-19 12:30:45 EDT
(In reply to Johannes Kuhn from comment #2)
> I did a short search on the JDK bug database, with no results about support
> (in the sense of generating warnings) to the JDK.
> 
> The annotation was added with JDK-8202385 [1].
> There is one bug related to the javac compiler: JDK-8231443 [2].
> (Doesn't look like an implementation, through.)

I'm not an expert regarding annotation processing, but I don't see any need for action in JDT regarding [2].
 
> I expect an implementation (emitting a warning) to be related of the first
> bug.

Bug [1] has a link to another related bug, proposing more lint warnings, although it's not what we are looking for:
https://bugs.openjdk.java.net/browse/JDK-8202056
 
> Anyway, I think it could use the Error/Warnings - Annotation preferences,
> similar to @NonNull.

I'm not convinced we need a new preference. Perhaps subsume it with the existing warning regarding serialVersionUID.

> Also an option to warn if the annotation is missing.
> 
> For the unused case, I found this on the eclipse bug tracker:
> https://bugs.eclipse.org/bugs/show_bug.cgi?id=31495
> Looks like they have been special cased.

Right, so most of it is already implemented. I only see method readObjectNoData() still missing. 
 
> [1]: https://bugs.openjdk.java.net/browse/JDK-8202385
> [2]: https://bugs.openjdk.java.net/browse/JDK-8231443
Comment 4 Stephan Herrmann CLA 2020-04-19 17:59:46 EDT
Setting a target just so we can check when javac will implement this.
Comment 5 Manoj N Palat CLA 2020-05-14 08:06:26 EDT
Bulk move out of 4.16
Comment 6 Manoj N Palat CLA 2021-02-10 08:28:03 EST
bulk move out of 4.19 - retarget once an owner is assigned
Comment 7 Eclipse Genie CLA 2023-04-25 20:25:32 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.