This is an automated email from the ASF dual-hosted git repository.
jlmonteiro pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/johnzon.git
The following commit(s) were added to refs/heads/master by this push:
new 1cff66fe feat(#419): filter out static methods from records
1cff66fe is described below
commit 1cff66fe75c5d0fac9d949cf8ecea8a47e610bda
Author: Jean-Louis Monteiro <[email protected]>
AuthorDate: Wed Jul 2 18:57:32 2025 +0200
feat(#419): filter out static methods from records
Signed-off-by: Jean-Louis Monteiro <[email protected]>
---
.../org/apache/johnzon/jsonb/JsonbRecordTest.java | 39 ++++++++++++++++++++++
.../johnzon/mapper/access/MethodAccessMode.java | 2 ++
2 files changed, 41 insertions(+)
diff --git
a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JsonbRecordTest.java
b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JsonbRecordTest.java
index 16397c8a..86847023 100644
--- a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JsonbRecordTest.java
+++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JsonbRecordTest.java
@@ -24,6 +24,8 @@ import org.junit.Rule;
import org.junit.Test;
import jakarta.json.bind.annotation.JsonbProperty;
+
+import java.time.LocalDate;
import java.util.Objects;
import static org.junit.Assert.assertEquals;
@@ -84,4 +86,41 @@ public class JsonbRecordTest {
return Objects.hash(age, name);
}
}
+
+ @Test
+ public void roundTripWithActualJavaRecord() {
+ final var ref = new Person("john", LocalDate.of(1904, 12, 25));
+ final String expectedJson =
"{\"age\":121,\"birthday\":\"1904-12-25\",\"name\":\"john\"}";
+ assertEquals(expectedJson, jsonb.toJson(ref));
+ assertEquals(ref, jsonb.fromJson(expectedJson, Person.class));
+ }
+
+ public record Person (String name, LocalDate birthday) {
+ public int age() {
+ return LocalDate.now().getYear() - birthday.getYear();
+ }
+
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ public static class Builder {
+ private String name;
+ private LocalDate birthday;
+
+ public Builder name(String name) {
+ this.name = name;
+ return this;
+ }
+
+ public Builder birthday(LocalDate birthday) {
+ this.birthday = birthday;
+ return this;
+ }
+
+ public Person build() {
+ return new Person(name, birthday);
+ }
+ }
+ }
}
diff --git
a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/access/MethodAccessMode.java
b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/access/MethodAccessMode.java
index 79e07acc..64bdcf31 100644
---
a/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/access/MethodAccessMode.java
+++
b/johnzon-mapper/src/main/java/org/apache/johnzon/mapper/access/MethodAccessMode.java
@@ -26,6 +26,7 @@ import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.HashMap;
@@ -54,6 +55,7 @@ public class MethodAccessMode extends BaseAccessMode {
readers.putAll(Stream.of(clazz.getMethods())
.filter(it -> it.getDeclaringClass() != Object.class &&
it.getParameterCount() == 0)
.filter(it -> !"toString".equals(it.getName()) &&
!"hashCode".equals(it.getName()))
+ .filter(it -> !Modifier.isStatic(it.getModifiers()))
.filter(it -> !isIgnored(it.getName()) &&
Meta.getAnnotation(it, JohnzonAny.class) == null)
.collect(toMap(m -> extractKey(m.getName(), m, null), it ->
new MethodReader(it, it.getGenericReturnType()))));
} else {