Skip to content

Commit 0b769e2

Browse files
committed
Work towards #4907: reduce introspection for "java.*" classes (and maybe others too)
1 parent ca578a0 commit 0b769e2

File tree

7 files changed

+122
-28
lines changed

7 files changed

+122
-28
lines changed

src/main/java/tools/jackson/databind/BeanDescription.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,8 @@ public static abstract class LazySupplier implements Supplier
331331
{
332332
protected final JavaType _type;
333333

334+
protected transient AnnotatedClass _classDesc;
335+
334336
protected transient BeanDescription _beanDesc;
335337

336338
protected LazySupplier(JavaType type) {
@@ -352,23 +354,28 @@ protected LazySupplier(JavaType type) {
352354

353355
@Override
354356
public Annotations getClassAnnotations() {
355-
return get().getClassAnnotations();
357+
return getClassInfo().getAnnotations();
356358
}
357359

358360
@Override
359361
public AnnotatedClass getClassInfo() {
360-
return get().getClassInfo();
362+
if (_classDesc == null) {
363+
364+
}
365+
return _classDesc;
361366
}
362367

363368
@Override
364369
public BeanDescription get() {
365370
if (_beanDesc == null) {
366-
_beanDesc = _construct(_type);
371+
_beanDesc = _construct(_type, getClassInfo());
367372
}
368373
return _beanDesc;
369374
}
370375

371-
protected abstract BeanDescription _construct(JavaType forType);
376+
protected abstract AnnotatedClass _introspect(JavaType forType);
377+
378+
protected abstract BeanDescription _construct(JavaType forType, AnnotatedClass ac);
372379
}
373380

374381
/**

src/main/java/tools/jackson/databind/DatabindContext.java

+15-4
Original file line numberDiff line numberDiff line change
@@ -307,18 +307,29 @@ protected abstract DatabindException invalidTypeIdException(JavaType baseType, S
307307
*/
308308

309309
/**
310-
* Convenience method for doing full "for serialization" introspection of specified
311-
* type; results may be cached during lifespan of this context as well.
310+
* Convenience method for doing full "for serialization or deserialization"
311+
* introspection of specified type; results may be cached for duration (lifespan)
312+
* of this context as well.
312313
*/
313-
public abstract BeanDescription introspectBeanDescription(JavaType type);
314+
public final BeanDescription introspectBeanDescription(JavaType type) {
315+
return introspectBeanDescription(type, introspectClassAnnotations(type));
316+
}
317+
318+
public abstract BeanDescription introspectBeanDescription(JavaType type,
319+
AnnotatedClass classDef);
314320

315321
public BeanDescription.Supplier lazyIntrospectBeanDescription(JavaType type) {
316322
return new BeanDescription.LazySupplier(type) {
317323
@Override
318-
public BeanDescription _construct(JavaType forType) {
324+
protected BeanDescription _construct(JavaType forType, AnnotatedClass ac) {
319325
// System.out.println("lazyIntrospectBeanDescription("+forType+")");
320326
return introspectBeanDescription(forType);
321327
}
328+
329+
@Override
330+
protected AnnotatedClass _introspect(JavaType forType) {
331+
return introspectClassAnnotations(forType);
332+
}
322333
};
323334
}
324335

src/main/java/tools/jackson/databind/DeserializationContext.java

+21-6
Original file line numberDiff line numberDiff line change
@@ -528,19 +528,29 @@ protected ClassIntrospector classIntrospector() {
528528
}
529529

530530
@Override
531-
public BeanDescription introspectBeanDescription(JavaType type) {
532-
return classIntrospector().introspectForDeserialization(type);
531+
public BeanDescription introspectBeanDescription(JavaType type, AnnotatedClass ac) {
532+
return classIntrospector().introspectForDeserialization(type, ac);
533533
}
534534

535535
public BeanDescription introspectBeanDescriptionForCreation(JavaType type) {
536-
return classIntrospector().introspectForCreation(type);
536+
return introspectBeanDescriptionForCreation(type,
537+
classIntrospector().introspectClassAnnotations(type));
538+
}
539+
540+
public BeanDescription introspectBeanDescriptionForCreation(JavaType type, AnnotatedClass ac) {
541+
return classIntrospector().introspectForCreation(type, ac);
537542
}
538543

539544
public BeanDescription.Supplier lazyIntrospectBeanDescriptionForCreation(JavaType type) {
540545
return new BeanDescription.LazySupplier(type) {
541546
@Override
542-
public BeanDescription _construct(JavaType forType) {
543-
return introspectBeanDescriptionForCreation(forType);
547+
protected BeanDescription _construct(JavaType forType, AnnotatedClass ac) {
548+
return introspectBeanDescriptionForCreation(forType, ac);
549+
}
550+
551+
@Override
552+
protected AnnotatedClass _introspect(JavaType forType) {
553+
return introspectClassAnnotations(forType);
544554
}
545555
};
546556
}
@@ -555,9 +565,14 @@ public BeanDescription.Supplier lazyIntrospectBeanDescriptionForBuilder(final Ja
555565
final BeanDescription valueTypeDesc) {
556566
return new BeanDescription.LazySupplier(builderType) {
557567
@Override
558-
public BeanDescription _construct(JavaType forType) {
568+
protected BeanDescription _construct(JavaType forType, AnnotatedClass ac) {
559569
return introspectBeanDescriptionForBuilder(forType, valueTypeDesc);
560570
}
571+
572+
@Override
573+
protected AnnotatedClass _introspect(JavaType forType) {
574+
return introspectClassAnnotations(forType);
575+
}
561576
};
562577
}
563578

src/main/java/tools/jackson/databind/SerializationContext.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,8 @@ protected ClassIntrospector classIntrospector() {
421421
}
422422

423423
@Override
424-
public BeanDescription introspectBeanDescription(JavaType type) {
425-
return classIntrospector().introspectForSerialization(type);
424+
public BeanDescription introspectBeanDescription(JavaType type, AnnotatedClass ac) {
425+
return classIntrospector().introspectForSerialization(type, ac);
426426
}
427427

428428
/*

src/main/java/tools/jackson/databind/introspect/BasicClassIntrospector.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ protected AnnotatedClass _resolveAnnotatedWithoutSuperTypes(JavaType type) {
147147
*/
148148

149149
@Override
150-
public BasicBeanDescription introspectForSerialization(JavaType type)
150+
public BasicBeanDescription introspectForSerialization(JavaType type,
151+
AnnotatedClass classDef)
151152
{
152153
// minor optimization: for some JDK types do minimal introspection
153154
BasicBeanDescription desc = _findStdTypeDesc(type);
@@ -165,16 +166,16 @@ public BasicBeanDescription introspectForSerialization(JavaType type)
165166
}
166167
}
167168
desc = BasicBeanDescription.forSerialization(collectProperties(type,
168-
introspectClassAnnotations(type),
169-
true, "set"));
169+
classDef, true, "set"));
170170
_resolvedSerBeanDescs.put(type, desc);
171171
}
172172
}
173173
return desc;
174174
}
175175

176176
@Override
177-
public BasicBeanDescription introspectForDeserialization(JavaType type)
177+
public BasicBeanDescription introspectForDeserialization(JavaType type,
178+
AnnotatedClass classDef)
178179
{
179180
// minor optimization: for some JDK types do minimal introspection
180181
BasicBeanDescription desc = _findStdTypeDesc(type);
@@ -192,8 +193,7 @@ public BasicBeanDescription introspectForDeserialization(JavaType type)
192193
}
193194
}
194195
desc = BasicBeanDescription.forDeserialization(collectProperties(type,
195-
introspectClassAnnotations(type),
196-
false, "set"));
196+
classDef, false, "set"));
197197
_resolvedDeserBeanDescs.put(type, desc);
198198
}
199199
}
@@ -211,7 +211,8 @@ public BasicBeanDescription introspectForDeserializationWithBuilder(JavaType typ
211211
}
212212

213213
@Override
214-
public BasicBeanDescription introspectForCreation(JavaType type)
214+
public BasicBeanDescription introspectForCreation(JavaType type,
215+
AnnotatedClass classDef)
215216
{
216217
BasicBeanDescription desc = _findStdTypeDesc(type);
217218
if (desc == null) {
@@ -220,8 +221,7 @@ public BasicBeanDescription introspectForCreation(JavaType type)
220221
desc = _findStdJdkCollectionDesc(type);
221222
if (desc == null) {
222223
desc = BasicBeanDescription.forDeserialization(collectProperties(type,
223-
introspectClassAnnotations(type),
224-
false, "set"));
224+
classDef, false, "set"));
225225
}
226226
}
227227
return desc;

src/main/java/tools/jackson/databind/introspect/ClassIntrospector.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,15 @@ protected ClassIntrospector() { }
6161
* Factory method that constructs an introspector that has all
6262
* information needed for serialization purposes.
6363
*/
64-
public abstract BeanDescription introspectForSerialization(JavaType type);
64+
public abstract BeanDescription introspectForSerialization(JavaType type,
65+
AnnotatedClass classDef);
6566

6667
/**
6768
* Factory method that constructs an introspector that has all
6869
* information needed for deserialization purposes.
6970
*/
70-
public abstract BeanDescription introspectForDeserialization(JavaType type);
71+
public abstract BeanDescription introspectForDeserialization(JavaType type,
72+
AnnotatedClass classDef);
7173

7274
/**
7375
* Factory method that constructs an introspector that has all
@@ -83,5 +85,6 @@ public abstract BeanDescription introspectForDeserializationWithBuilder(JavaType
8385
* class ("creator"), as well as class annotations, but
8486
* no information on member methods
8587
*/
86-
public abstract BeanDescription introspectForCreation(JavaType type);
88+
public abstract BeanDescription introspectForCreation(JavaType type,
89+
AnnotatedClass classDef);
8790
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package tools.jackson.databind.misc;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import tools.jackson.databind.*;
6+
import tools.jackson.databind.testutil.DatabindTestUtil;
7+
8+
import static org.junit.jupiter.api.Assertions.assertNotNull;
9+
10+
public class Reflection4907Jackson3Test extends DatabindTestUtil
11+
{
12+
static class SqlDatePojo {
13+
public String name;
14+
public java.sql.Date date;
15+
16+
public SqlDatePojo() {
17+
}
18+
19+
public SqlDatePojo(String name, java.sql.Date date) {
20+
this.name = name;
21+
this.date = date;
22+
}
23+
24+
public SqlDatePojo(java.sql.Date date) {
25+
this.date = date;
26+
}
27+
28+
public java.sql.Date getDate() {
29+
return date;
30+
}
31+
32+
public void setDate(java.sql.Date date) {
33+
this.date = date;
34+
}
35+
}
36+
37+
private final ObjectMapper MAPPER = newJsonMapper();
38+
39+
// [databind#4907]
40+
@Test
41+
public void test4907Read() throws Exception {
42+
System.err.println("<testRead>");
43+
SqlDatePojo pojo = MAPPER.readValue(a2q("{'date':'2000-01-01', 'name':'foo'}"),
44+
SqlDatePojo.class);
45+
System.err.println("</testRead>");
46+
assertNotNull(pojo);
47+
}
48+
49+
// [databind#4907]
50+
@Test
51+
public void test4907Write() throws Exception {
52+
System.err.println("<testWrite>");
53+
String json = MAPPER.writeValueAsString(new SqlDatePojo("foobar",
54+
java.sql.Date.valueOf("2000-01-01")));
55+
System.err.println("</testWrite>");
56+
assertNotNull(json);
57+
}
58+
}

0 commit comments

Comments
 (0)