-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add streamlined sealed class polymorphic de/serialization #3549
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
Closed
sigpwned
wants to merge
15
commits into
FasterXML:2.14
from
sigpwned:sealed-classes-polymorphic-serialization-61
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2d6fbcc
add support for inferred polymorphic serialization of sealed classes …
sigpwned d57f5a9
small logic fix
sigpwned cfb8372
add sealed classes tests for JDK17
sigpwned 63a6250
fix build on jdk17
sigpwned f0af3bb
rename sealed class mapper feature
sigpwned 48c6118
add deprecation metadata
sigpwned 4417418
spacing
sigpwned 02c36a1
fix imports
sigpwned 07c9df0
fix @Deprecated compile error
sigpwned bb3ce82
tweak deprecation
sigpwned 821a467
move failing tests into failing package
sigpwned e8f808a
fix import warning
sigpwned cd9acc7
remove unneeded record types in test
sigpwned f8139c4
remove JDK16 idiom
sigpwned f82de3f
Merge branch '2.14' into sealed-classes-polymorphic-serialization-61
sigpwned 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
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
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
85 changes: 85 additions & 0 deletions
85
src/main/java/com/fasterxml/jackson/databind/jdk17/JDK17Util.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,85 @@ | ||
package com.fasterxml.jackson.databind.jdk17; | ||
|
||
import java.lang.reflect.Method; | ||
import com.fasterxml.jackson.databind.util.ClassUtil; | ||
import com.fasterxml.jackson.databind.util.NativeImageUtil; | ||
|
||
/** | ||
* Helper class to support some of JDK 17 (and later) features without Jackson itself being run on | ||
* (or even built with) Java 17. In particular allows better support of sealed class types (see | ||
* <a href="https://openjdk.java.net/jeps/409">JEP 409</a>). | ||
* | ||
* @since 2.14 | ||
*/ | ||
public class JDK17Util { | ||
public static Boolean isSealed(Class<?> type) { | ||
return SealedClassAccessor.instance().isSealed(type); | ||
} | ||
|
||
public static Class<?>[] getPermittedSubclasses(Class<?> sealedType) { | ||
return SealedClassAccessor.instance().getPermittedSubclasses(sealedType); | ||
} | ||
|
||
static class SealedClassAccessor { | ||
private final Method SEALED_IS_SEALED; | ||
private final Method SEALED_GET_PERMITTED_SUBCLASSES; | ||
|
||
private final static SealedClassAccessor INSTANCE; | ||
private final static RuntimeException PROBLEM; | ||
|
||
static { | ||
RuntimeException prob = null; | ||
SealedClassAccessor inst = null; | ||
try { | ||
inst = new SealedClassAccessor(); | ||
} catch (RuntimeException e) { | ||
prob = e; | ||
} | ||
INSTANCE = inst; | ||
PROBLEM = prob; | ||
} | ||
|
||
private SealedClassAccessor() throws RuntimeException { | ||
try { | ||
SEALED_IS_SEALED = Class.class.getMethod("isSealed"); | ||
SEALED_GET_PERMITTED_SUBCLASSES = Class.class.getMethod("getPermittedSubclasses"); | ||
} catch (Exception e) { | ||
throw new RuntimeException( | ||
String.format("Failed to access Methods needed to support sealed classes: (%s) %s", | ||
e.getClass().getName(), e.getMessage()), | ||
e); | ||
} | ||
} | ||
|
||
public static SealedClassAccessor instance() { | ||
if (PROBLEM != null) { | ||
throw PROBLEM; | ||
} | ||
return INSTANCE; | ||
} | ||
|
||
public Boolean isSealed(Class<?> type) throws IllegalArgumentException { | ||
try { | ||
return (Boolean) SEALED_IS_SEALED.invoke(type); | ||
} catch (Exception e) { | ||
if (NativeImageUtil.isUnsupportedFeatureError(e)) { | ||
return null; | ||
} | ||
throw new IllegalArgumentException( | ||
"Failed to access sealedness of type " + ClassUtil.nameOf(type)); | ||
} | ||
} | ||
|
||
public Class<?>[] getPermittedSubclasses(Class<?> sealedType) throws IllegalArgumentException { | ||
try { | ||
return (Class<?>[]) SEALED_GET_PERMITTED_SUBCLASSES.invoke(sealedType); | ||
} catch (Exception e) { | ||
if (NativeImageUtil.isUnsupportedFeatureError(e)) { | ||
return null; | ||
} | ||
throw new IllegalArgumentException( | ||
"Failed to access permitted subclasses of type " + ClassUtil.nameOf(sealedType)); | ||
} | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/fasterxml/jackson/databind/jdk17/package-info.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,6 @@ | ||
/** | ||
Contains helper class(es) needed to support some of JDK17+ | ||
features without requiring running or building using JDK 17. | ||
*/ | ||
|
||
package com.fasterxml.jackson.databind.jdk17; |
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
35 changes: 35 additions & 0 deletions
35
src/test-jdk14/java/com/fasterxml/jackson/databind/failing/RecordBasicsTestFailing.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,35 @@ | ||
package com.fasterxml.jackson.databind.failing; | ||
|
||
import com.fasterxml.jackson.databind.BaseMapTest; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
import com.fasterxml.jackson.databind.records.RecordBasicsTest; | ||
|
||
/** | ||
* Tests in this class were moved from {@link RecordBasicsTest}. | ||
*/ | ||
public class RecordBasicsTestFailing extends BaseMapTest { | ||
// [databind#2992] | ||
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) | ||
record SnakeRecord(String myId, String myValue){} | ||
|
||
private final ObjectMapper MAPPER = newJsonMapper(); | ||
|
||
/* | ||
/********************************************************************** | ||
/* Test methods, naming strategy | ||
/********************************************************************** | ||
*/ | ||
|
||
// [databind#2992] | ||
// [databind#3102]: fails on JDK 16 which finally blocks mutation | ||
// of Record fields. | ||
public void testNamingStrategy() throws Exception | ||
{ | ||
SnakeRecord input = new SnakeRecord("123", "value"); | ||
String json = MAPPER.writeValueAsString(input); | ||
SnakeRecord output = MAPPER.readValue(json, SnakeRecord.class); | ||
assertEquals(input, output); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/test-jdk14/java/com/fasterxml/jackson/databind/failing/RecordUpdate3079TestFailing.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 @@ | ||
package com.fasterxml.jackson.databind.failing; | ||
|
||
import java.util.Collections; | ||
import com.fasterxml.jackson.databind.BaseMapTest; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.records.RecordUpdate3079Test; | ||
|
||
/** | ||
* Tests in this class were moved from {@link RecordUpdate3079Test}. | ||
*/ | ||
public class RecordUpdate3079TestFailing extends BaseMapTest { | ||
record IdNameRecord(int id, String name) { } | ||
|
||
static class IdNameWrapper { | ||
public IdNameRecord value; | ||
|
||
protected IdNameWrapper() { } | ||
public IdNameWrapper(IdNameRecord v) { value = v; } | ||
} | ||
|
||
private final ObjectMapper MAPPER = newJsonMapper(); | ||
|
||
/** | ||
*This test no longer works as of JDK 15 for records. Maybe Unsafe will work? | ||
* | ||
* https://medium.com/@atdsqdjyfkyziqkezu/java-15-breaks-deserialization-of-records-902fcc81253d | ||
* https://stackoverflow.com/questions/61141836/change-static-final-field-in-java-12 | ||
*/ | ||
// [databind#3079]: Should be able to Record value directly | ||
// [databind#3102]: fails on JDK 16 which finally blocks mutation | ||
// of Record fields. | ||
public void testDirectRecordUpdate() throws Exception | ||
{ | ||
IdNameRecord orig = new IdNameRecord(123, "Bob"); | ||
IdNameRecord result = MAPPER.updateValue(orig, | ||
Collections.singletonMap("id", 137)); | ||
assertNotNull(result); | ||
assertEquals(137, result.id()); | ||
assertEquals("Bob", result.name()); | ||
assertNotSame(orig, result); | ||
} | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.