Skip to content
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

Backport: # 2870 Fix Java Serialization warnings for Tuple #2869

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion vavr/generator/Generator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1953,7 +1953,7 @@ def generateMainClasses(): Unit = {
/$javadoc
* The ${j.ordinal} element of this tuple.
*/
public final T$j _$j;
transient public T$j _$j;
Copy link
Member

@pivovarit pivovarit Sep 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

transient means that you want to ignore field during serialization - tuple elements can't be ignored.

We also want them final.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. The transient modifier will likely break other serialization libraries, too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an implementation added in this class for readObject() and writeObject() to explicitly serialize and deserialize these fields so that they are not ignored.

""")("\n\n")}

${if (i == 0) xs"""
Expand Down Expand Up @@ -2211,6 +2211,24 @@ def generateMainClasses(): Unit = {
return ${if (i == 0) "1" else s"""Tuple.hash(${(1 to i).gen(j => s"_$j")(", ")})"""};
}

${(i > 0).gen(xs"""
private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException {
s.defaultWriteObject();
${(1 to i).gen(j => xs"""
s.writeObject(_$j);
""")("\n")}
}

@SuppressWarnings("unchecked")
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject();
${(1 to i).gen(j => xs"""
_$j = (T$j) s.readObject();
""")("\n")}
}
""")}

@Override
public String toString() {
return ${if (i == 0) "\"()\"" else s""""(" + ${(1 to i).gen(j => s"_$j")(" + \", \" + ")} + ")""""};
Expand Down
14 changes: 13 additions & 1 deletion vavr/src-gen/main/java/io/vavr/Tuple1.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public final class Tuple1<T1> implements Tuple, Comparable<Tuple1<T1>>, Serializ
/**
* The 1st element of this tuple.
*/
public final T1 _1;
transient public T1 _1;

/**
* Constructs a tuple of one element.
Expand Down Expand Up @@ -282,6 +282,18 @@ public int hashCode() {
return Tuple.hash(_1);
}

private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException {
s.defaultWriteObject();
s.writeObject(_1);
}

@SuppressWarnings("unchecked")
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject();
_1 = (T1) s.readObject();
}

@Override
public String toString() {
return "(" + _1 + ")";
Expand Down
18 changes: 16 additions & 2 deletions vavr/src-gen/main/java/io/vavr/Tuple2.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ public final class Tuple2<T1, T2> implements Tuple, Comparable<Tuple2<T1, T2>>,
/**
* The 1st element of this tuple.
*/
public final T1 _1;
transient public T1 _1;

/**
* The 2nd element of this tuple.
*/
public final T2 _2;
transient public T2 _2;

/**
* Constructs a tuple of two elements.
Expand Down Expand Up @@ -367,6 +367,20 @@ public int hashCode() {
return Tuple.hash(_1, _2);
}

private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException {
s.defaultWriteObject();
s.writeObject(_1);
s.writeObject(_2);
}

@SuppressWarnings("unchecked")
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject();
_1 = (T1) s.readObject();
_2 = (T2) s.readObject();
}

@Override
public String toString() {
return "(" + _1 + ", " + _2 + ")";
Expand Down
22 changes: 19 additions & 3 deletions vavr/src-gen/main/java/io/vavr/Tuple3.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ public final class Tuple3<T1, T2, T3> implements Tuple, Comparable<Tuple3<T1, T2
/**
* The 1st element of this tuple.
*/
public final T1 _1;
transient public T1 _1;

/**
* The 2nd element of this tuple.
*/
public final T2 _2;
transient public T2 _2;

/**
* The 3rd element of this tuple.
*/
public final T3 _3;
transient public T3 _3;

/**
* Constructs a tuple of three elements.
Expand Down Expand Up @@ -381,6 +381,22 @@ public int hashCode() {
return Tuple.hash(_1, _2, _3);
}

private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException {
s.defaultWriteObject();
s.writeObject(_1);
s.writeObject(_2);
s.writeObject(_3);
}

@SuppressWarnings("unchecked")
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject();
_1 = (T1) s.readObject();
_2 = (T2) s.readObject();
_3 = (T3) s.readObject();
}

@Override
public String toString() {
return "(" + _1 + ", " + _2 + ", " + _3 + ")";
Expand Down
26 changes: 22 additions & 4 deletions vavr/src-gen/main/java/io/vavr/Tuple4.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,22 @@ public final class Tuple4<T1, T2, T3, T4> implements Tuple, Comparable<Tuple4<T1
/**
* The 1st element of this tuple.
*/
public final T1 _1;
transient public T1 _1;

/**
* The 2nd element of this tuple.
*/
public final T2 _2;
transient public T2 _2;

/**
* The 3rd element of this tuple.
*/
public final T3 _3;
transient public T3 _3;

/**
* The 4th element of this tuple.
*/
public final T4 _4;
transient public T4 _4;

/**
* Constructs a tuple of 4 elements.
Expand Down Expand Up @@ -419,6 +419,24 @@ public int hashCode() {
return Tuple.hash(_1, _2, _3, _4);
}

private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException {
s.defaultWriteObject();
s.writeObject(_1);
s.writeObject(_2);
s.writeObject(_3);
s.writeObject(_4);
}

@SuppressWarnings("unchecked")
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject();
_1 = (T1) s.readObject();
_2 = (T2) s.readObject();
_3 = (T3) s.readObject();
_4 = (T4) s.readObject();
}

@Override
public String toString() {
return "(" + _1 + ", " + _2 + ", " + _3 + ", " + _4 + ")";
Expand Down
30 changes: 25 additions & 5 deletions vavr/src-gen/main/java/io/vavr/Tuple5.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,27 @@ public final class Tuple5<T1, T2, T3, T4, T5> implements Tuple, Comparable<Tuple
/**
* The 1st element of this tuple.
*/
public final T1 _1;
transient public T1 _1;

/**
* The 2nd element of this tuple.
*/
public final T2 _2;
transient public T2 _2;

/**
* The 3rd element of this tuple.
*/
public final T3 _3;
transient public T3 _3;

/**
* The 4th element of this tuple.
*/
public final T4 _4;
transient public T4 _4;

/**
* The 5th element of this tuple.
*/
public final T5 _5;
transient public T5 _5;

/**
* Constructs a tuple of 5 elements.
Expand Down Expand Up @@ -458,6 +458,26 @@ public int hashCode() {
return Tuple.hash(_1, _2, _3, _4, _5);
}

private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException {
s.defaultWriteObject();
s.writeObject(_1);
s.writeObject(_2);
s.writeObject(_3);
s.writeObject(_4);
s.writeObject(_5);
}

@SuppressWarnings("unchecked")
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject();
_1 = (T1) s.readObject();
_2 = (T2) s.readObject();
_3 = (T3) s.readObject();
_4 = (T4) s.readObject();
_5 = (T5) s.readObject();
}

@Override
public String toString() {
return "(" + _1 + ", " + _2 + ", " + _3 + ", " + _4 + ", " + _5 + ")";
Expand Down
34 changes: 28 additions & 6 deletions vavr/src-gen/main/java/io/vavr/Tuple6.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,32 @@ public final class Tuple6<T1, T2, T3, T4, T5, T6> implements Tuple, Comparable<T
/**
* The 1st element of this tuple.
*/
public final T1 _1;
transient public T1 _1;

/**
* The 2nd element of this tuple.
*/
public final T2 _2;
transient public T2 _2;

/**
* The 3rd element of this tuple.
*/
public final T3 _3;
transient public T3 _3;

/**
* The 4th element of this tuple.
*/
public final T4 _4;
transient public T4 _4;

/**
* The 5th element of this tuple.
*/
public final T5 _5;
transient public T5 _5;

/**
* The 6th element of this tuple.
*/
public final T6 _6;
transient public T6 _6;

/**
* Constructs a tuple of 6 elements.
Expand Down Expand Up @@ -498,6 +498,28 @@ public int hashCode() {
return Tuple.hash(_1, _2, _3, _4, _5, _6);
}

private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException {
s.defaultWriteObject();
s.writeObject(_1);
s.writeObject(_2);
s.writeObject(_3);
s.writeObject(_4);
s.writeObject(_5);
s.writeObject(_6);
}

@SuppressWarnings("unchecked")
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject();
_1 = (T1) s.readObject();
_2 = (T2) s.readObject();
_3 = (T3) s.readObject();
_4 = (T4) s.readObject();
_5 = (T5) s.readObject();
_6 = (T6) s.readObject();
}

@Override
public String toString() {
return "(" + _1 + ", " + _2 + ", " + _3 + ", " + _4 + ", " + _5 + ", " + _6 + ")";
Expand Down
38 changes: 31 additions & 7 deletions vavr/src-gen/main/java/io/vavr/Tuple7.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,37 +49,37 @@ public final class Tuple7<T1, T2, T3, T4, T5, T6, T7> implements Tuple, Comparab
/**
* The 1st element of this tuple.
*/
public final T1 _1;
transient public T1 _1;

/**
* The 2nd element of this tuple.
*/
public final T2 _2;
transient public T2 _2;

/**
* The 3rd element of this tuple.
*/
public final T3 _3;
transient public T3 _3;

/**
* The 4th element of this tuple.
*/
public final T4 _4;
transient public T4 _4;

/**
* The 5th element of this tuple.
*/
public final T5 _5;
transient public T5 _5;

/**
* The 6th element of this tuple.
*/
public final T6 _6;
transient public T6 _6;

/**
* The 7th element of this tuple.
*/
public final T7 _7;
transient public T7 _7;

/**
* Constructs a tuple of 7 elements.
Expand Down Expand Up @@ -539,6 +539,30 @@ public int hashCode() {
return Tuple.hash(_1, _2, _3, _4, _5, _6, _7);
}

private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException {
s.defaultWriteObject();
s.writeObject(_1);
s.writeObject(_2);
s.writeObject(_3);
s.writeObject(_4);
s.writeObject(_5);
s.writeObject(_6);
s.writeObject(_7);
}

@SuppressWarnings("unchecked")
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject();
_1 = (T1) s.readObject();
_2 = (T2) s.readObject();
_3 = (T3) s.readObject();
_4 = (T4) s.readObject();
_5 = (T5) s.readObject();
_6 = (T6) s.readObject();
_7 = (T7) s.readObject();
}

@Override
public String toString() {
return "(" + _1 + ", " + _2 + ", " + _3 + ", " + _4 + ", " + _5 + ", " + _6 + ", " + _7 + ")";
Expand Down
Loading
Loading