-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Support for Java 14 records (#2709) #2714
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
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
072189b
Support for Java 14 records (#2709)
youribonnaffe a1b9a76
Test record with @JsonProperty
youribonnaffe 3599e37
Some review comments on records support
ChrisHegarty 20684d7
Merge pull request #1 from ChrisHegarty/java-14-records-1
youribonnaffe 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
103 changes: 103 additions & 0 deletions
103
src/main/java/com/fasterxml/jackson/databind/util/RecordUtil.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,103 @@ | ||
package com.fasterxml.jackson.databind.util; | ||
|
||
import com.fasterxml.jackson.databind.introspect.AnnotatedClass; | ||
import com.fasterxml.jackson.databind.introspect.AnnotatedConstructor; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.Arrays; | ||
|
||
/** | ||
* Helper class to detect Java records without Java 14 as Jackson targets is Java 8. | ||
* <p> | ||
* See <a href="https://openjdk.java.net/jeps/359">JEP 359</a> | ||
*/ | ||
public final class RecordUtil { | ||
|
||
private static final Method IS_RECORD; | ||
private static final Method GET_RECORD_COMPONENTS; | ||
private static final Method GET_NAME; | ||
private static final Method GET_TYPE; | ||
|
||
static { | ||
Method isRecord; | ||
Method getRecordComponents; | ||
Method getName; | ||
Method getType; | ||
try { | ||
isRecord = Class.class.getDeclaredMethod("isRecord"); | ||
getRecordComponents = Class.class.getMethod("getRecordComponents"); | ||
Class c = Class.forName("java.lang.reflect.RecordComponent"); | ||
getName = c.getMethod("getName"); | ||
getType = c.getMethod("getType"); | ||
} catch (ClassNotFoundException| NoSuchMethodException e) { | ||
// pre-Java-14 | ||
isRecord = null; | ||
getRecordComponents = null; | ||
getName = null; | ||
getType = null; | ||
} | ||
IS_RECORD = isRecord; | ||
GET_RECORD_COMPONENTS = getRecordComponents; | ||
GET_NAME = getName; | ||
GET_TYPE = getType; | ||
} | ||
|
||
public static boolean isRecord(Class<?> aClass) { | ||
try { | ||
return IS_RECORD == null ? false : (boolean) IS_RECORD.invoke(aClass); | ||
} catch (IllegalAccessException | InvocationTargetException e) { | ||
throw new AssertionError(); | ||
} | ||
} | ||
|
||
/** | ||
* @return Record component's names, ordering is preserved. | ||
*/ | ||
public static String[] getRecordComponents(Class<?> aRecord) { | ||
if (!isRecord(aRecord)) { | ||
return new String[0]; | ||
} | ||
|
||
try { | ||
Object[] components = (Object[]) GET_RECORD_COMPONENTS.invoke(aRecord); | ||
String[] names = new String[components.length]; | ||
for (int i = 0; i < components.length; i++) { | ||
Object component = components[i]; | ||
names[i] = (String) GET_NAME.invoke(component); | ||
} | ||
return names; | ||
} catch (Throwable e) { | ||
return new String[0]; | ||
} | ||
} | ||
|
||
public static AnnotatedConstructor getCanonicalConstructor(AnnotatedClass aRecord) { | ||
if (!isRecord(aRecord.getAnnotated())) { | ||
return null; | ||
} | ||
|
||
Class<?>[] paramTypes = getRecordComponentTypes(aRecord.getAnnotated()); | ||
for (AnnotatedConstructor constructor : aRecord.getConstructors()) { | ||
if (Arrays.equals(constructor.getAnnotated().getParameterTypes(), paramTypes)) { | ||
return constructor; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private static Class<?>[] getRecordComponentTypes(Class<?> aRecord) { | ||
try { | ||
Object[] components = (Object[]) GET_RECORD_COMPONENTS.invoke(aRecord); | ||
Class<?>[] types = new Class[components.length]; | ||
for (int i = 0; i < components.length; i++) { | ||
Object component = components[i]; | ||
types[i] = (Class<?>) GET_TYPE.invoke(component); | ||
} | ||
return types; | ||
} catch (Throwable e) { | ||
return new Class[0]; | ||
} | ||
} | ||
} | ||
|
105 changes: 105 additions & 0 deletions
105
src/test-jdk14/java/com/fasterxml/jackson/databind/RecordTest.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,105 @@ | ||
package com.fasterxml.jackson.databind; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.json.JsonMapper; | ||
|
||
import java.io.IOException; | ||
|
||
public class RecordTest extends BaseMapTest { | ||
|
||
private JsonMapper jsonMapper; | ||
|
||
public void setUp() { | ||
jsonMapper = new JsonMapper(); | ||
} | ||
|
||
public record SimpleRecord(int id, String name) { | ||
} | ||
|
||
public void testSerializeSimpleRecord() throws JsonProcessingException { | ||
SimpleRecord record = new SimpleRecord(123, "Bob"); | ||
|
||
String json = jsonMapper.writeValueAsString(record); | ||
|
||
assertEquals("{\"id\":123,\"name\":\"Bob\"}", json); | ||
} | ||
|
||
public void testDeserializeSimpleRecord() throws IOException { | ||
SimpleRecord value = jsonMapper.readValue("{\"id\":123,\"name\":\"Bob\"}", SimpleRecord.class); | ||
|
||
assertEquals(new SimpleRecord(123, "Bob"), value); | ||
} | ||
|
||
public void testSerializeSimpleRecord_DisableAnnotationIntrospector() throws JsonProcessingException { | ||
SimpleRecord record = new SimpleRecord(123, "Bob"); | ||
|
||
JsonMapper mapper = JsonMapper.builder() | ||
.configure(MapperFeature.USE_ANNOTATIONS, false) | ||
.build(); | ||
String json = mapper.writeValueAsString(record); | ||
|
||
assertEquals("{\"id\":123,\"name\":\"Bob\"}", json); | ||
} | ||
|
||
public void testDeserializeSimpleRecord_DisableAnnotationIntrospector() throws IOException { | ||
JsonMapper mapper = JsonMapper.builder() | ||
.configure(MapperFeature.USE_ANNOTATIONS, false) | ||
.build(); | ||
SimpleRecord value = mapper.readValue("{\"id\":123,\"name\":\"Bob\"}", SimpleRecord.class); | ||
|
||
assertEquals(new SimpleRecord(123, "Bob"), value); | ||
} | ||
|
||
public record RecordOfRecord(SimpleRecord record) { | ||
} | ||
|
||
public void testSerializeRecordOfRecord() throws JsonProcessingException { | ||
RecordOfRecord record = new RecordOfRecord(new SimpleRecord(123, "Bob")); | ||
|
||
String json = jsonMapper.writeValueAsString(record); | ||
|
||
assertEquals("{\"record\":{\"id\":123,\"name\":\"Bob\"}}", json); | ||
} | ||
|
||
public record JsonIgnoreRecord(int id, @JsonIgnore String name) { | ||
} | ||
|
||
public void testSerializeJsonIgnoreRecord() throws JsonProcessingException { | ||
JsonIgnoreRecord record = new JsonIgnoreRecord(123, "Bob"); | ||
|
||
String json = jsonMapper.writeValueAsString(record); | ||
|
||
assertEquals("{\"id\":123}", json); | ||
} | ||
|
||
public record RecordWithConstructor(int id, String name) { | ||
public RecordWithConstructor(int id) { | ||
this(id, "name"); | ||
} | ||
} | ||
|
||
public void testDeserializeRecordWithConstructor() throws IOException { | ||
RecordWithConstructor value = jsonMapper.readValue("{\"id\":123,\"name\":\"Bob\"}", RecordWithConstructor.class); | ||
|
||
assertEquals(new RecordWithConstructor(123, "Bob"), value); | ||
} | ||
|
||
public record JsonPropertyRenameRecord(int id, @JsonProperty("rename")String name) { | ||
} | ||
|
||
public void testSerializeJsonRenameRecord() throws JsonProcessingException { | ||
JsonPropertyRenameRecord record = new JsonPropertyRenameRecord(123, "Bob"); | ||
|
||
String json = jsonMapper.writeValueAsString(record); | ||
|
||
assertEquals("{\"id\":123,\"rename\":\"Bob\"}", json); | ||
} | ||
|
||
public void testDeserializeJsonRenameRecord() throws IOException { | ||
JsonPropertyRenameRecord value = jsonMapper.readValue("{\"id\":123,\"rename\":\"Bob\"}", JsonPropertyRenameRecord.class); | ||
|
||
assertEquals(new JsonPropertyRenameRecord(123, "Bob"), value); | ||
} | ||
} |
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.
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.
Definitely needs javadocs to explain why is this needed.