Skip to content

(WIP) Reflection testing wrt #4907 #5119

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

Draft
wants to merge 13 commits into
base: 2.x
Choose a base branch
from
Draft
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ public final class AnnotatedClass
*/
protected final boolean _collectAnnotations;

/**
* Flag that indicates whether member (Field, Method, Constructor}
* detection should occur: starting with 2.19 is disabled for <b>core</b> JDK
* types {@link ClassUtil#isJDKCoreClass(Class)}).
*
* @since 2.20
*/
protected final boolean _collectMembers;

/*
/**********************************************************
/* Gathered information
Expand Down Expand Up @@ -152,6 +161,19 @@ public final class AnnotatedClass
_mixInResolver = mir;
_typeFactory = tf;
_collectAnnotations = collectAnnotations;
// But need to collect for some JDK types:
//
// - Throwables
_collectMembers = (type != null)
&& (!ClassUtil.isJDKCoreClass(rawType)
|| type.hasRawClass(Optional.class)
|| type.hasRawClass(StackTraceElement.class)
|| Throwable.class.isAssignableFrom(rawType)
|| type.hasRawClass(Thread.class)
|| type.hasRawClass(ThreadGroup.class)
);

System.out.println(" AnnotatedClass("+_type+"), coll anno? "+collectAnnotations+" coll mem? "+_collectMembers);
}

/**
Expand All @@ -171,6 +193,7 @@ public final class AnnotatedClass
_mixInResolver = null;
_typeFactory = null;
_collectAnnotations = false;
_collectMembers = false;
}

/**
Expand Down Expand Up @@ -327,9 +350,12 @@ private final List<AnnotatedField> _fields() {
List<AnnotatedField> f = _fields;
if (f == null) {
// 09-Jun-2017, tatu: _type only null for primordial, placeholder array types.
if (_type == null) {
// 26-Apr-2025, tatu: [databind#4907] Less introspection, skip for core JDK types
if (_type == null || !_collectMembers) {
System.out.println("SKIP Collecting _fields() for "+_type);
f = Collections.emptyList();
} else {
System.out.println("Collecting _fields() for "+_type);
f = AnnotatedFieldCollector.collectFields(_annotationIntrospector,
this, _mixInResolver, _typeFactory, _type, _collectAnnotations);
}
Expand All @@ -343,9 +369,11 @@ private final AnnotatedMethodMap _methods() {
if (m == null) {
// 09-Jun-2017, tatu: _type only null for primordial, placeholder array types.
// NOTE: would be great to have light-weight shareable maps; no such impl exists for now
if (_type == null) {
if (_type == null) { // || !_collectMembers) {
System.out.println("SKIP Collecting _methods() for: "+_type);
m = new AnnotatedMethodMap();
} else {
System.out.println("Collecting _methods() for "+_type);
m = AnnotatedMethodCollector.collectMethods(_annotationIntrospector,
this,
_mixInResolver, _typeFactory,
Expand All @@ -359,9 +387,12 @@ private final AnnotatedMethodMap _methods() {
private final Creators _creators() {
Creators c = _creators;
if (c == null) {
if (_type == null) {
// 26-Apr-2025, tatu: [databind#4907] Less introspection, skip for core JDK types
if (_type == null || !_collectMembers) {
System.out.println("SKIP Collecting _creators() for: "+_type);
c = NO_CREATORS;
} else {
System.out.println("Collecting _creators() for "+_type);
c = AnnotatedCreatorCollector.collectCreators(_annotationIntrospector,
_typeFactory,
this, _type, _primaryMixIn, _collectAnnotations);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ static AnnotatedClass createArrayType(MapperConfig<?> config, Class<?> raw) {
}

AnnotatedClass resolveFully() {
System.out.println(" AnnotatedClassResolver.resolveFully() for "+_type);

List<JavaType> superTypes = new ArrayList<>(8);
if (!_type.hasRawClass(Object.class)) {
if (_type.isInterface()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public BasicBeanDescription forSerialization(SerializationConfig config,
// structured types as well
desc = _findStdJdkCollectionDesc(config, type);
if (desc == null) {
System.out.println("BasicBeanDescription.forSerialization() for "+type);
desc = BasicBeanDescription.forSerialization(collectProperties(config,
type, r, true));
}
Expand All @@ -101,6 +102,7 @@ public BasicBeanDescription forDeserialization(DeserializationConfig config,
// structured types as well
desc = _findStdJdkCollectionDesc(config, type);
if (desc == null) {
System.out.println("BasicBeanDescription.forDeserialization() for "+type);
desc = BasicBeanDescription.forDeserialization(collectProperties(config,
type, r, false));
}
Expand All @@ -127,6 +129,7 @@ public BasicBeanDescription forCreation(DeserializationConfig config,
// structured types as well
desc = _findStdJdkCollectionDesc(config, type);
if (desc == null) {
System.out.println("BasicBeanDescription.forCreation() for "+type);
desc = BasicBeanDescription.forDeserialization(
collectProperties(config, type, r, false));
}
Expand Down Expand Up @@ -170,6 +173,7 @@ public BasicBeanDescription forDirectClassAnnotations(MapperConfig<?> config,
protected POJOPropertiesCollector collectProperties(MapperConfig<?> config,
JavaType type, MixInResolver r, boolean forSerialization)
{
System.out.println(" collectProperties() for "+type);
final AnnotatedClass classDef = _resolveAnnotatedClass(config, type, r);
final AccessorNamingStrategy accNaming = type.isRecordType()
? config.getAccessorNaming().forRecord(config, classDef)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.fasterxml.jackson.databind.misc;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;

import static org.junit.jupiter.api.Assertions.assertNotNull;

public class Reflection4907Test extends DatabindTestUtil
{
static class SqlDatePojo {
public String name;
public java.sql.Date date;

public SqlDatePojo() {
}

public SqlDatePojo(String name, java.sql.Date date) {
this.name = name;
this.date = date;
}

public SqlDatePojo(java.sql.Date date) {
this.date = date;
}

public java.sql.Date getDate() {
return date;
}

public void setDate(java.sql.Date date) {
this.date = date;
}
}

private final ObjectMapper MAPPER = newJsonMapper();

// [databind#4907]
@Test
public void test4907Read() throws Exception {
System.err.println("<testRead>");
SqlDatePojo pojo = MAPPER.readValue(a2q("{'date':'2000-01-01', 'name':'foo'}"),
SqlDatePojo.class);
System.err.println("</testRead>");
assertNotNull(pojo);
}

// [databind#4907]
@Test
public void test4907Write() throws Exception {
System.err.println("<testWrite>");
String json = MAPPER.writeValueAsString(new SqlDatePojo("foobar",
java.sql.Date.valueOf("2000-01-01")));
System.err.println("</testWrite>");
assertNotNull(json);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ public void testNonStandardProperties() throws Exception
public void testThreadSerialization() throws Exception
{
final Thread input = Thread.currentThread();
// String json = MAPPER.writerWithDefaultPrettyPrinter()
// .writeValueAsString(input);
// System.err.println("Thread -> "+MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(input));

Map<?,?> asMap = MAPPER.convertValue(input, Map.class);
// System.err.println("PROPS -> "+asMap.keySet());

Expand Down
Loading