Skip to content

Commit 7e30323

Browse files
committed
#1591 #1521 class retention and meta-annotation AllStructural support for structural serialization
1 parent d294b2d commit 7e30323

6 files changed

Lines changed: 59 additions & 15 deletions

File tree

serial/src/org/immutables/serial/Serial.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
*/
4141
@Target({ElementType.TYPE, ElementType.PACKAGE, ElementType.ANNOTATION_TYPE})
4242
@Retention(RetentionPolicy.SOURCE)
43-
public @interface Version {
43+
@interface Version {
4444
long value();
4545
}
4646

@@ -61,5 +61,15 @@
6161
*/
6262
@Target({ElementType.TYPE, ElementType.PACKAGE, ElementType.ANNOTATION_TYPE})
6363
@Retention(RetentionPolicy.SOURCE)
64-
public @interface Structural {}
64+
@interface Structural {}
65+
66+
/**
67+
* Class-retention variant of {@link Structural}. Used to cover packages and style meta-annotation
68+
* across module/jar boundaries where source code is not available. Originally, {@link Structural}
69+
* were class retention annotation, but it was changed due to some (former) issues to be
70+
* the source retained.
71+
*/
72+
@Target({ElementType.TYPE, ElementType.PACKAGE, ElementType.ANNOTATION_TYPE})
73+
@Retention(RetentionPolicy.CLASS)
74+
@interface AllStructural {}
6575
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.immutables.fixture.serial;
2+
3+
import org.immutables.serial.Serial;
4+
import org.immutables.value.Value;
5+
6+
@Value.Style(allParameters = true)
7+
@Serial.AllStructural
8+
public @interface StructStyle {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.immutables.fixture.serial;
2+
3+
import org.immutables.value.Value;
4+
5+
@StructStyle
6+
@Value.Immutable
7+
public interface Structed {
8+
int a();
9+
String b();
10+
}

value-fixture/test/org/immutables/fixture/serial/SerialTest.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,17 @@
2222
import java.io.Serializable;
2323
import java.lang.reflect.Field;
2424
import org.junit.jupiter.api.Test;
25-
import static org.immutables.check.Checkers.*;
25+
import static org.immutables.check.Checkers.check;
2626

2727
public class SerialTest {
28-
@Test
29-
public void readResolveInterned() throws Exception {
28+
@Test void hasReadReplace() throws NoSuchMethodException {
29+
// this checks if AllStructural were applied
30+
var writeReplaceMethod = ImmutableStructed.class.getDeclaredMethod("writeReplace");
31+
// actually exception will be thrown, this is just to avoid unused variable warning
32+
check(writeReplaceMethod).notNull();
33+
}
34+
35+
@Test void readResolveInterned() throws Exception {
3036
ImmutableSomeSer instance0 = ImmutableSomeSer.builder().build();
3137
ImmutableSomeSer instance1 = ImmutableSomeSer.builder().regular(1).build();
3238
ImmutableSomeSer instance1_2 = ImmutableSomeSer.builder().regular(1).build();
@@ -40,8 +46,7 @@ public void readResolveInterned() throws Exception {
4046
check(deserialize(serialize(ImmutableOthSer.of()))).same(ImmutableOthSer.builder().build());
4147
}
4248

43-
@Test
44-
public void copySerialVersion() throws Exception {
49+
@Test void copySerialVersion() throws Exception {
4550
for (Field field : ImmutableSomeSer.class.getDeclaredFields()) {
4651
field.setAccessible(true);
4752
if (field.getName().equals("serialVersionUID") && field.get(null).equals(1L)) {
@@ -52,15 +57,13 @@ public void copySerialVersion() throws Exception {
5257
check(false);
5358
}
5459

55-
@Test
56-
public void serializeModifiable() throws Exception {
60+
@Test void serializeModifiable() throws Exception {
5761
ModifiableSomeSer instance = ModifiableSomeSer.create().setRegular(1);
5862
// interning
5963
check(deserialize(serialize(instance))).is(ModifiableSomeSer.create().setRegular(1));
6064
}
6165

62-
@Test
63-
public void copySerialVersionModifiable() throws Exception {
66+
@Test void copySerialVersionModifiable() throws Exception {
6467
for (Field field : ModifiableSomeSer.class.getDeclaredFields()) {
6568
field.setAccessible(true);
6669
if (field.getName().equals("serialVersionUID") && field.get(null).equals(1L)) {

value-processor/src/org/immutables/value/processor/meta/Proto.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,14 @@ public Optional<Long> serialVersion() {
182182
Optional<VersionMirror> version = VersionMirror.find(element());
183183
return version.isPresent()
184184
? Optional.of(version.get().value())
185-
: Optional.<Long>absent();
185+
: Optional.absent();
186186
}
187187

188188
@Value.Derived
189189
@Value.Auxiliary
190190
public boolean isSerialStructural() {
191191
return environment().hasSerialModule()
192-
&& StructuralMirror.isPresent(element());
192+
&& (StructuralMirror.isPresent(element()) || AllStructuralMirror.isPresent(element()));
193193
}
194194

195195
@Value.Derived
@@ -771,12 +771,22 @@ public Optional<Long> serialVersion() {
771771
}
772772
}
773773

774-
return Optional.<Long>absent();
774+
return Optional.absent();
775775
}
776776

777777
@Value.Lazy
778778
public boolean isSerialStructural() {
779-
return StructuralMirror.isPresent(element());
779+
if (!environment().hasSerialModule()) return false;
780+
781+
if (StructuralMirror.isPresent(element()) || AllStructuralMirror.isPresent(element())) {
782+
return true;
783+
}
784+
785+
for (MetaAnnotated m : metaAnnotated()) {
786+
if (m.isSerialStructural()) return true;
787+
}
788+
789+
return false;
780790
}
781791

782792
@Value.Lazy

value-processor/src/org/immutables/value/processor/meta/SerialMirrors.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,7 @@ private SerialMirrors() {}
2727

2828
@Mirror.Annotation("org.immutables.serial.Serial.Structural")
2929
public @interface Structural {}
30+
31+
@Mirror.Annotation("org.immutables.serial.Serial.AllStructural")
32+
public @interface AllStructural {}
3033
}

0 commit comments

Comments
 (0)