Skip to content

Commit

Permalink
Merge branch '2.18'
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jun 20, 2024
2 parents 055dc46 + 32eb3e1 commit 9a23a53
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 95 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ on:
paths-ignore:
- "README.md"
- "release-notes/*"
permissions:
contents: read

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
java_version: ['8', '11', '17', '21']
os: ['ubuntu-20.04']
os: ['ubuntu-22.04']
env:
JAVA_OPTS: "-XX:+TieredCompilation -XX:TieredStopAtLevel=1"
steps:
Expand All @@ -44,7 +47,7 @@ jobs:
run: ./mvnw -B -q -ff -ntp verify
- name: Extract project Maven version
id: projectVersion
run: echo "version=$(./mvnw org.apache.maven.plugins:maven-help-plugin:3.3.0:evaluate -DforceStdout -Dexpression=project.version -q)" >> $GITHUB_OUTPUT
run: echo "version=$(./mvnw org.apache.maven.plugins:maven-help-plugin:3.4.1:evaluate -DforceStdout -Dexpression=project.version -q)" >> $GITHUB_OUTPUT
- name: Deploy snapshot
if: github.event_name != 'pull_request' && matrix.java_version == '8' && endsWith(steps.projectVersion.outputs.version, '-SNAPSHOT')
env:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ public class BeanConstructors

protected Constructor<?> _noArgsCtor;

// @since 2.18
protected Constructor<?> _recordCtor;

protected Constructor<?> _intCtor;
protected Constructor<?> _longCtor;
protected Constructor<?> _stringCtor;
Expand All @@ -30,12 +27,6 @@ public BeanConstructors addNoArgsConstructor(Constructor<?> ctor) {
return this;
}

// @since 2.18
public BeanConstructors addRecordConstructor(Constructor<?> ctor) {
_recordCtor = ctor;
return this;
}

public BeanConstructors addIntConstructor(Constructor<?> ctor) {
_intCtor = ctor;
return this;
Expand All @@ -55,9 +46,6 @@ public void forceAccess() {
if (_noArgsCtor != null) {
_noArgsCtor.setAccessible(true);
}
if (_recordCtor != null) {
_recordCtor.setAccessible(true);
}
if (_intCtor != null) {
_intCtor.setAccessible(true);
}
Expand All @@ -76,14 +64,6 @@ protected Object create() throws Exception {
return _noArgsCtor.newInstance((Object[]) null);
}

// @since 2.18
protected Object createRecord(Object[] components) throws Exception {
if (_recordCtor == null) {
throw new IllegalStateException("Class "+_valueType.getName()+" does not have record constructor to use");
}
return _recordCtor.newInstance(components);
}

protected Object create(String str) throws Exception {
if (_stringCtor == null) {
throw new IllegalStateException("Class "+_valueType.getName()+" does not have single-String constructor to use");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ private POJODefinition _introspectDefinition(Class<?> beanType,
} else if (argType == Long.class || argType == Long.TYPE) {
constructors.addLongConstructor(ctor);
}
} else if (RecordsHelpers.isRecordConstructor(beanType, ctor, propsByName)) {
constructors.addRecordConstructor(ctor);
}
}
}
Expand Down
27 changes: 0 additions & 27 deletions jr-objects/src/main/java/tools/jackson/jr/ob/impl/BeanReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ public class BeanReader

protected final BeanConstructors _constructors;

// @since 2.18
protected final boolean _isRecordType;

// // 13-Dec-2017, tatu: NOTE! These will be constructed right after construction, but
// // not during it (due to need to resolve possible cyclic deps). So they are
// // non-final due to this but never `null` before use.
Expand All @@ -55,7 +52,6 @@ protected BeanReader(Class<?> type, Map<String,BeanPropertyReader> propsByName,
_ignorableNames = ignorableNames;
_aliasMapping = aliasMapping;
_caseInsensitive = caseInsensitive;
_isRecordType = RecordsHelpers.isRecordType(type);
}

/**
Expand Down Expand Up @@ -140,29 +136,6 @@ public BeanPropertyReader findProperty(String name) {
public Object read(JSONReader r, JsonParser p) throws JacksonException
{
if (p.isExpectedStartObjectToken()) {
// [jackson-jr#148] Record deser support (2.18)
if (_isRecordType) {
final List<Object> values = new ArrayList<>();

String propName;

// 13-Jun-2024, tatu: Should optimize the way _readBean()
// optimizes regular case...
for (; (propName = p.nextName()) != null;) {
BeanPropertyReader prop = findProperty(propName);
if (prop == null) {
handleUnknown(r, p, propName);
continue;
}
values.add(prop.getReader().readNext(r, p));
}
try {
return _constructors.createRecord(values.toArray());
} catch (Exception e) {
return _reportFailureToCreate(p, e);
}
}

final Object bean;
try {
bean = _constructors.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,25 +483,16 @@ protected BeanReader _resolveBeanForDeser(Class<?> raw, POJODefinition beanDef)
setter = null;
}
}
if (RecordsHelpers.isRecordType(raw)) {
try {
field = raw.getDeclaredField(rawProp.name);
} catch (NoSuchFieldException e) {
throw new IllegalStateException("Cannot access field '" + rawProp.name
+ "' of record class `" + raw.getName() + "`", e);
// if no setter, field would do as well
if (setter == null) {
if (field == null) {
continue;
}
} else {
// if no setter, field would do as well
if (setter == null) {
if (field == null) {
continue;
}
// fields should always be public, but let's just double-check
if (forceAccess) {
field.setAccessible(true);
} else if (!Modifier.isPublic(field.getModifiers())) {
continue;
}
// fields should always be public, but let's just double-check
if (forceAccess) {
field.setAccessible(true);
} else if (!Modifier.isPublic(field.getModifiers())) {
continue;
}
}

Expand Down
22 changes: 3 additions & 19 deletions jr-record-test/src/test-jdk17/java/jr/Java17RecordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,9 @@ public class Java17RecordTest extends TestCase
public record Cow(String message, Map<String, String> object) {
}

// [jackson-jr#94]
// [jackson-jr#94]: Record serialization
public void testJava14RecordSerialization() throws Exception {
// 13-Jun-2024, tatu: why is this explicitly needed?
JSON json = JSON.std;
var expectedDoc = "{\"message\":\"MOO\",\"object\":{\"Foo\":\"Bar\"}}";
Cow input = new Cow("MOO", Map.of("Foo", "Bar"));

assertEquals(expectedDoc, json.asString(input));
}

// [jackson-jr#148]
public void testJava14RecordDeserialization() throws Exception {
// 13-Jun-2024, tatu: why is this explicitly needed?
JSON json = JSON.std;
String inputDoc = "{\"message\":\"MOO\",\"object\":{\"Foo\":\"Bar\"}}";

Cow expected = new Cow("MOO", Map.of("Foo", "Bar"));

Cow actual = json.beanFrom(Cow.class, inputDoc);
assertEquals(expected, actual);
assertEquals("{\"message\":\"MOO\",\"object\":{\"Foo\":\"Bar\"}}",
JSON.std.asString(new Cow("MOO", Map.of("Foo", "Bar"))));
}
}
5 changes: 0 additions & 5 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,3 @@ Julian Honnen (@jhonnen)
* Contributed fix for #90: `USE_BIG_DECIMAL_FOR_FLOATS` feature not working
when using `JSON.treeFrom()`
(2.17.1)

Tomasz Gawęda (@TomaszGaweda)

* Contributed #148: Add support for Java Record deserialization
(2.18.0)
3 changes: 1 addition & 2 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ Modules:

2.18.0 (not yet released)

#148: Add support for Java Record deserialization
(contributed by Tomasz G)
-

2.17.1 (04-May-2024)

Expand Down

0 comments on commit 9a23a53

Please sign in to comment.