forked from boskworks/bosk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Polyfill for deserialization (venasolutions#84)
- Loading branch information
Showing
8 changed files
with
272 additions
and
178 deletions.
There are no files selected for viewing
This file contains 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
32 changes: 32 additions & 0 deletions
32
bosk-core/src/main/java/io/vena/bosk/annotations/Polyfill.java
This file contains 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,32 @@ | ||
package io.vena.bosk.annotations; | ||
|
||
import io.vena.bosk.StateTreeNode; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
import java.util.Optional; | ||
|
||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
/** | ||
* Marks a static final field in a {@link StateTreeNode} to indicate that it can be | ||
* used as a default value for a given field, for backward compatibility with external | ||
* systems that don't yet support the field. | ||
* | ||
* <p> | ||
* This is not meant to be used just to supply default values for optional fields; | ||
* that should be achieved by declaring the field {@link Optional} | ||
* and calling {@link Optional#orElse} when the field is used. | ||
* Rather, this is meant to be used <em>temporarily</em> with newly added fields | ||
* to support systems that are not yet aware of those fields. | ||
* | ||
* @author Patrick Doyle | ||
*/ | ||
@Retention(RUNTIME) | ||
@Target({ FIELD }) | ||
public @interface Polyfill { | ||
/** | ||
* The names of the fields for which we're supplying a default value. | ||
*/ | ||
String[] value(); | ||
} |
35 changes: 35 additions & 0 deletions
35
bosk-core/src/test/java/io/vena/bosk/SerializationPluginTest.java
This file contains 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,35 @@ | ||
package io.vena.bosk; | ||
|
||
import io.vena.bosk.annotations.Self; | ||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.Parameter; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static io.vena.bosk.ReferenceUtils.theOnlyConstructorFor; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
// TODO: This should aim for full coverage of SerializationPlugin | ||
class SerializationPluginTest { | ||
|
||
@Test | ||
void inheritedFieldAttribute_works() { | ||
Constructor<Child> childConstructor = theOnlyConstructorFor(Child.class); | ||
Parameter selfParameter = childConstructor.getParameters()[0]; | ||
assertTrue(SerializationPlugin.isSelfReference(Child.class, selfParameter)); | ||
} | ||
|
||
@RequiredArgsConstructor | ||
@Getter | ||
static class Parent { | ||
@Self final Parent self; | ||
} | ||
|
||
@Getter | ||
static class Child extends Parent { | ||
public Child(Parent self) { | ||
super(self); | ||
} | ||
} | ||
} |
Oops, something went wrong.