-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Java: Add 'Useless serialization member in record class' query #19950
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tamasvajk
wants to merge
6
commits into
github:main
Choose a base branch
from
tamasvajk:quality/useless-record-member
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+131
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8704f2d
Java: Add 'Useless serialization member in record class' query
tamasvajk 6a48459
Improve query quality
tamasvajk 10f0476
Use inline test expectations
tamasvajk d64e0cb
Adjust expected file for query suite integration test
tamasvajk d4cb23a
Improve alert message
tamasvajk 8a56fe4
Improve query help
tamasvajk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
java/ql/src/Violations of Best Practice/Records/UselessMembersOfTheRecordsClass.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
## Overview | ||
|
||
Record types were introduced in Java 16 as a mechanism to provide simpler data handling as an alternative to regular classes. However, record classes behave slightly differently during serialization. Namely any `writeObject`, `readObject`, `readObjectNoData`, `writeExternal`, and `readExternal` methods and `serialPersistentFields` fields declared in these classes cannot be used to affect the serialization process of any `Record` data type. | ||
|
||
## Recommendation | ||
|
||
Some level of serialization customization is offered by the Java 16 Record feature. The `writeReplace` and `readResolve` methods in a record that implements `java.io.Serializable` can be used to replace the object to be serialized. Otherwise, no further customization of serialization of records is possible, and it is better to consider using a regular class implementing `java.io.Serializable` or `java.io.Externalizable` when customization is needed. | ||
|
||
## Example | ||
|
||
```java | ||
record T1() implements Serializable { | ||
|
||
@Serial | ||
private static final ObjectStreamField[] serialPersistentFields = new ObjectStreamField[0]; // NON_COMPLIANT | ||
|
||
@Serial | ||
private void writeObject(ObjectOutputStream out) throws IOException {} // NON_COMPLIANT | ||
|
||
@Serial | ||
private void readObject(ObjectOutputStream out) throws IOException {}// NON_COMPLIANT | ||
|
||
@Serial | ||
private void readObjectNoData(ObjectOutputStream out) throws IOException { // NON_COMPLIANT | ||
} | ||
} | ||
|
||
record T2() implements Externalizable { | ||
|
||
@Override | ||
public void writeExternal(ObjectOutput out) throws IOException { // NON_COMPLIANT | ||
} | ||
|
||
@Override | ||
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { // NON_COMPLIANT | ||
} | ||
} | ||
|
||
record T3() implements Serializable { | ||
|
||
public Object writeReplace(ObjectOutput out) throws ObjectStreamException { // COMPLIANT | ||
return new Object(); | ||
} | ||
|
||
public Object readResolve(ObjectInput in) throws ObjectStreamException { // COMPLIANT | ||
return new Object(); | ||
} | ||
} | ||
``` | ||
|
||
## References | ||
|
||
- Oracle Serialization Documentation: [Serialization of Records](https://docs.oracle.com/en/java/javase/16/docs/specs/serialization/serial-arch.html#serialization-of-records) | ||
- Java Record: [Feature Specification](https://openjdk.org/jeps/395) |
24 changes: 24 additions & 0 deletions
24
java/ql/src/Violations of Best Practice/Records/UselessMembersOfTheRecordsClass.ql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* @id java/useless-member-of-the-record-class | ||
* @name Useless serialization member of record class | ||
* @description Using certain members of a record class during serialization will result in | ||
* those members being ignored. | ||
* @previous-id java/useless-members-of-the-records-class | ||
* @kind problem | ||
* @precision very-high | ||
* @problem.severity warning | ||
* @tags quality | ||
* reliability | ||
* correctness | ||
*/ | ||
|
||
import java | ||
|
||
from Record record, Member m | ||
where | ||
record.getAMember() = m and | ||
m.hasName([ | ||
"writeObject", "readObject", "readObjectNoData", "writeExternal", "readExternal", | ||
"serialPersistentFields" | ||
]) | ||
select m, "Useless serialization member found in record class $@.", record, record.getName() |
42 changes: 42 additions & 0 deletions
42
java/ql/test/query-tests/UselessMembersOfTheRecordsClass/Test.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import java.io.*; | ||
|
||
public class Test { | ||
record T1() implements Serializable { | ||
|
||
@Serial | ||
private static final ObjectStreamField[] serialPersistentFields = new ObjectStreamField[0]; // $ Alert | ||
|
||
@Serial | ||
private void writeObject(ObjectOutputStream out) throws IOException {} // $ Alert | ||
|
||
@Serial | ||
private void readObject(ObjectOutputStream out) throws IOException {} // $ Alert | ||
|
||
@Serial | ||
private void readObjectNoData(ObjectOutputStream out) throws IOException { // $ Alert | ||
} | ||
|
||
} | ||
|
||
record T2() implements Externalizable { | ||
|
||
@Override | ||
public void writeExternal(ObjectOutput out) throws IOException { // $ Alert | ||
} | ||
|
||
@Override | ||
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { // $ Alert | ||
} | ||
|
||
} | ||
|
||
record T3() implements Serializable { | ||
|
||
public Object writeReplace(ObjectOutput out) throws ObjectStreamException { // COMPLIANT | ||
return new Object(); | ||
} | ||
|
||
public Object readResolve(ObjectInput in) throws ObjectStreamException { // COMPLIANT | ||
return new Object(); | ||
} | ||
}} |
6 changes: 6 additions & 0 deletions
6
...test/query-tests/UselessMembersOfTheRecordsClass/UselessMembersOfTheRecordsClass.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
| Test.java:7:46:7:67 | serialPersistentFields | Useless serialization member found in record class $@. | Test.java:4:12:4:13 | T1 | T1 | | ||
| Test.java:10:18:10:28 | writeObject | Useless serialization member found in record class $@. | Test.java:4:12:4:13 | T1 | T1 | | ||
| Test.java:13:18:13:27 | readObject | Useless serialization member found in record class $@. | Test.java:4:12:4:13 | T1 | T1 | | ||
| Test.java:16:18:16:33 | readObjectNoData | Useless serialization member found in record class $@. | Test.java:4:12:4:13 | T1 | T1 | | ||
| Test.java:24:17:24:29 | writeExternal | Useless serialization member found in record class $@. | Test.java:21:12:21:13 | T2 | T2 | | ||
| Test.java:28:17:28:28 | readExternal | Useless serialization member found in record class $@. | Test.java:21:12:21:13 | T2 | T2 | |
2 changes: 2 additions & 0 deletions
2
...ql/test/query-tests/UselessMembersOfTheRecordsClass/UselessMembersOfTheRecordsClass.qlref
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
query: Violations of Best Practice/Records/UselessMembersOfTheRecordsClass.ql | ||
postprocess: utils/test/InlineExpectationsTestQuery.ql |
1 change: 1 addition & 0 deletions
1
java/ql/test/query-tests/UselessMembersOfTheRecordsClass/options
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
//semmle-extractor-options: --javac-args -source 16 -target 16 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is a new query, the
ccr.qls
suite needs to be updated in the Autofix repo (otherwise the query will not get any autofix suggestions).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add the query to that .qls before a release happens?