Skip to content

Commit cef4b99

Browse files
committed
Minor tweaking about annotation collecting, trying to avoid processing JDK types
1 parent be2a3e6 commit cef4b99

File tree

6 files changed

+133
-68
lines changed

6 files changed

+133
-68
lines changed

src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedClass.java

+21-4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ public final class AnnotatedClass
7676
*/
7777
final protected Class<?> _primaryMixIn;
7878

79+
/**
80+
* @since 2.11
81+
*/
82+
final protected boolean _collectAnnotations;
83+
7984
/*
8085
/**********************************************************
8186
/* Gathered information
@@ -129,7 +134,8 @@ public final class AnnotatedClass
129134
*/
130135
AnnotatedClass(JavaType type, Class<?> rawType, List<JavaType> superTypes,
131136
Class<?> primaryMixIn, Annotations classAnnotations, TypeBindings bindings,
132-
AnnotationIntrospector aintr, MixInResolver mir, TypeFactory tf)
137+
AnnotationIntrospector aintr, MixInResolver mir, TypeFactory tf,
138+
boolean collectAnnotations)
133139
{
134140
_type = type;
135141
_class = rawType;
@@ -140,6 +146,16 @@ public final class AnnotatedClass
140146
_annotationIntrospector = aintr;
141147
_mixInResolver = mir;
142148
_typeFactory = tf;
149+
_collectAnnotations = collectAnnotations;
150+
}
151+
152+
@Deprecated // since 2.10
153+
AnnotatedClass(JavaType type, Class<?> rawType, List<JavaType> superTypes,
154+
Class<?> primaryMixIn, Annotations classAnnotations, TypeBindings bindings,
155+
AnnotationIntrospector aintr, MixInResolver mir, TypeFactory tf)
156+
{
157+
this(type, rawType, superTypes, primaryMixIn, classAnnotations, bindings,
158+
aintr, mir, tf, true);
143159
}
144160

145161
/**
@@ -158,6 +174,7 @@ public final class AnnotatedClass
158174
_annotationIntrospector = null;
159175
_mixInResolver = null;
160176
_typeFactory = null;
177+
_collectAnnotations = false;
161178
}
162179

163180
/**
@@ -347,7 +364,7 @@ private final List<AnnotatedField> _fields() {
347364
f = Collections.emptyList();
348365
} else {
349366
f = AnnotatedFieldCollector.collectFields(_annotationIntrospector,
350-
this, _mixInResolver, _typeFactory, _type);
367+
this, _mixInResolver, _typeFactory, _type, _collectAnnotations);
351368
}
352369
_fields = f;
353370
}
@@ -365,7 +382,7 @@ private final AnnotatedMethodMap _methods() {
365382
m = AnnotatedMethodCollector.collectMethods(_annotationIntrospector,
366383
this,
367384
_mixInResolver, _typeFactory,
368-
_type, _superTypes, _primaryMixIn);
385+
_type, _superTypes, _primaryMixIn, _collectAnnotations);
369386
}
370387
_memberMethods = m;
371388
}
@@ -379,7 +396,7 @@ private final Creators _creators() {
379396
c = NO_CREATORS;
380397
} else {
381398
c = AnnotatedCreatorCollector.collectCreators(_annotationIntrospector,
382-
this, _type, _primaryMixIn);
399+
this, _type, _primaryMixIn, _collectAnnotations);
383400
}
384401
_creators = c;
385402
}

src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedClassResolver.java

+41-14
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public class AnnotatedClassResolver
3333
private final Class<?> _class;
3434
private final Class<?> _primaryMixin;
3535

36+
/**
37+
* @since 2.11
38+
*/
39+
private final boolean _collectAnnotations;
40+
3641
AnnotatedClassResolver(MapperConfig<?> config, JavaType type, MixInResolver r) {
3742
_config = config;
3843
_type = type;
@@ -41,7 +46,10 @@ public class AnnotatedClassResolver
4146
_bindings = type.getBindings();
4247
_intr = config.isAnnotationProcessingEnabled()
4348
? config.getAnnotationIntrospector() : null;
44-
_primaryMixin = _config.findMixInClassFor(_class);
49+
_primaryMixin = (r == null) ? null : r.findMixInClassFor(_class);
50+
51+
// Also... JDK types do not have annotations that are of interest to us
52+
_collectAnnotations = (_intr != null) && !ClassUtil.isJDKClass(_class);
4553
}
4654

4755
AnnotatedClassResolver(MapperConfig<?> config, Class<?> cls, MixInResolver r) {
@@ -56,8 +64,10 @@ public class AnnotatedClassResolver
5664
} else {
5765
_intr = config.isAnnotationProcessingEnabled()
5866
? config.getAnnotationIntrospector() : null;
59-
_primaryMixin = _config.findMixInClassFor(_class);
67+
_primaryMixin = (r == null) ? null : r.findMixInClassFor(_class);
6068
}
69+
70+
_collectAnnotations = (_intr != null) && !ClassUtil.isJDKClass(_class);
6171
}
6272

6373
public static AnnotatedClass resolve(MapperConfig<?> config, JavaType forType,
@@ -116,15 +126,17 @@ AnnotatedClass resolveFully() {
116126
List<JavaType> superTypes = ClassUtil.findSuperTypes(_type, null, false);
117127
return new AnnotatedClass(_type, _class, superTypes, _primaryMixin,
118128
resolveClassAnnotations(superTypes),
119-
_bindings, _intr, _mixInResolver, _config.getTypeFactory());
129+
_bindings, _intr, _mixInResolver, _config.getTypeFactory(),
130+
_collectAnnotations);
120131

121132
}
122133

123134
AnnotatedClass resolveWithoutSuperTypes() {
124135
List<JavaType> superTypes = Collections.<JavaType>emptyList();
125136
return new AnnotatedClass(null, _class, superTypes, _primaryMixin,
126137
resolveClassAnnotations(superTypes),
127-
_bindings, _intr, _config, _config.getTypeFactory());
138+
_bindings, _intr, _mixInResolver, _config.getTypeFactory(),
139+
_collectAnnotations);
128140
}
129141

130142
/*
@@ -144,33 +156,48 @@ private Annotations resolveClassAnnotations(List<JavaType> superTypes)
144156
if (_intr == null) {
145157
return NO_ANNOTATIONS;
146158
}
159+
// Plus we may or may not have mix-ins to consider
160+
final boolean checkMixIns = (_mixInResolver != null)
161+
&& (!(_mixInResolver instanceof SimpleMixInResolver)
162+
|| ((SimpleMixInResolver) _mixInResolver).hasMixIns());
163+
164+
// also skip if there's nothing to do
165+
if (!checkMixIns && !_collectAnnotations) {
166+
return NO_ANNOTATIONS;
167+
}
168+
147169
AnnotationCollector resolvedCA = AnnotationCollector.emptyCollector();
148170
// add mix-in annotations first (overrides)
149171
if (_primaryMixin != null) {
150172
resolvedCA = _addClassMixIns(resolvedCA, _class, _primaryMixin);
151173
}
152174
// then annotations from the class itself:
153-
resolvedCA = _addAnnotationsIfNotPresent(resolvedCA,
154-
ClassUtil.findClassAnnotations(_class));
175+
// 06-Oct-2019, tatu: [databind#2464] Skip class annotations for JDK classes
176+
if (_collectAnnotations) {
177+
resolvedCA = _addAnnotationsIfNotPresent(resolvedCA,
178+
ClassUtil.findClassAnnotations(_class));
179+
}
155180

156181
// and then from super types
157182
for (JavaType type : superTypes) {
158183
// and mix mix-in annotations in-between
159-
if (_mixInResolver != null) {
184+
if (checkMixIns) {
160185
Class<?> cls = type.getRawClass();
161186
resolvedCA = _addClassMixIns(resolvedCA, cls,
162187
_mixInResolver.findMixInClassFor(cls));
163188
}
164-
resolvedCA = _addAnnotationsIfNotPresent(resolvedCA,
165-
ClassUtil.findClassAnnotations(type.getRawClass()));
189+
if (_collectAnnotations) {
190+
resolvedCA = _addAnnotationsIfNotPresent(resolvedCA,
191+
ClassUtil.findClassAnnotations(type.getRawClass()));
192+
}
166193
}
167-
/* and finally... any annotations there might be for plain
168-
* old Object.class: separate because for all other purposes
169-
* it is just ignored (not included in super types)
170-
*/
194+
195+
// and finally... any annotations there might be for plain old Object.class:
196+
// separate because otherwise it is just ignored (not included in super types)
197+
171198
// 12-Jul-2009, tatu: Should this be done for interfaces too?
172199
// For now, yes, seems useful for some cases, and not harmful for any?
173-
if (_mixInResolver != null) {
200+
if (checkMixIns) {
174201
resolvedCA = _addClassMixIns(resolvedCA, Object.class,
175202
_mixInResolver.findMixInClassFor(Object.class));
176203
}

src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedCreatorCollector.java

+33-22
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,32 @@ final class AnnotatedCreatorCollector
2727

2828
private final TypeResolutionContext _typeContext;
2929

30+
/**
31+
* @since 2.11
32+
*/
33+
private final boolean _collectAnnotations;
34+
3035
// // // Collected state
3136

3237
private AnnotatedConstructor _defaultConstructor;
3338

3439
AnnotatedCreatorCollector(AnnotationIntrospector intr,
35-
TypeResolutionContext tc)
40+
TypeResolutionContext tc, boolean collectAnnotations)
3641
{
3742
super(intr);
3843
_typeContext = tc;
44+
_collectAnnotations = collectAnnotations;
3945
}
4046

4147
public static Creators collectCreators(AnnotationIntrospector intr,
4248
TypeResolutionContext tc,
43-
JavaType type, Class<?> primaryMixIn)
49+
JavaType type, Class<?> primaryMixIn, boolean collectAnnotations)
4450
{
51+
final boolean checkClassAnnotations = (intr != null)
52+
&& !ClassUtil.isJDKClass(type.getRawClass());
53+
4554
// Constructor also always members of resolved class, parent == resolution context
46-
return new AnnotatedCreatorCollector(intr, tc)
55+
return new AnnotatedCreatorCollector(intr, tc, checkClassAnnotations)
4756
.collect(type, primaryMixIn);
4857
}
4958

@@ -60,7 +69,7 @@ Creators collect(JavaType type, Class<?> primaryMixIn)
6069
* ignorable after all annotations have been properly collapsed.
6170
*/
6271
// AnnotationIntrospector is null if annotations not enabled; if so, can skip:
63-
if (_intr != null) {
72+
if (_collectAnnotations) {
6473
if (_defaultConstructor != null) {
6574
if (_intr.hasIgnoreMarker(_defaultConstructor)) {
6675
_defaultConstructor = null;
@@ -236,10 +245,6 @@ private List<AnnotatedMethod> _findPotentialFactories(JavaType type, Class<?> pr
236245
protected AnnotatedConstructor constructDefaultConstructor(ClassUtil.Ctor ctor,
237246
ClassUtil.Ctor mixin)
238247
{
239-
if (_intr == null) { // when annotation processing is disabled
240-
return new AnnotatedConstructor(_typeContext, ctor.getConstructor(),
241-
_emptyAnnotationMap(), NO_ANNOTATION_MAPS);
242-
}
243248
return new AnnotatedConstructor(_typeContext, ctor.getConstructor(),
244249
collectAnnotations(ctor, mixin),
245250
// 16-Jun-2019, tatu: default is zero-args, so can't have parameter annotations
@@ -320,27 +325,33 @@ protected AnnotatedMethod constructFactoryCreator(Method m, Method mixin)
320325
}
321326

322327
private AnnotationMap[] collectAnnotations(Annotation[][] mainAnns, Annotation[][] mixinAnns) {
323-
final int count = mainAnns.length;
324-
AnnotationMap[] result = new AnnotationMap[count];
325-
for (int i = 0; i < count; ++i) {
326-
AnnotationCollector c = collectAnnotations(AnnotationCollector.emptyCollector(),
327-
mainAnns[i]);
328-
if (mixinAnns != null) {
329-
c = collectAnnotations(c, mixinAnns[i]);
328+
if (_collectAnnotations) {
329+
final int count = mainAnns.length;
330+
AnnotationMap[] result = new AnnotationMap[count];
331+
for (int i = 0; i < count; ++i) {
332+
AnnotationCollector c = collectAnnotations(AnnotationCollector.emptyCollector(),
333+
mainAnns[i]);
334+
if (mixinAnns != null) {
335+
c = collectAnnotations(c, mixinAnns[i]);
336+
}
337+
result[i] = c.asAnnotationMap();
330338
}
331-
result[i] = c.asAnnotationMap();
339+
return result;
332340
}
333-
return result;
341+
return NO_ANNOTATION_MAPS;
334342
}
335343

336344
// // NOTE: these are only called when we know we have AnnotationIntrospector
337-
345+
338346
private AnnotationMap collectAnnotations(ClassUtil.Ctor main, ClassUtil.Ctor mixin) {
339-
AnnotationCollector c = collectAnnotations(main.getConstructor().getDeclaredAnnotations());
340-
if (mixin != null) {
341-
c = collectAnnotations(c, mixin.getConstructor().getDeclaredAnnotations());
347+
if (_collectAnnotations) {
348+
AnnotationCollector c = collectAnnotations(main.getConstructor().getDeclaredAnnotations());
349+
if (mixin != null) {
350+
c = collectAnnotations(c, mixin.getConstructor().getDeclaredAnnotations());
351+
}
352+
return c.asAnnotationMap();
342353
}
343-
return c.asAnnotationMap();
354+
return _emptyAnnotationMap();
344355
}
345356

346357
private final AnnotationMap collectAnnotations(AnnotatedElement main, AnnotatedElement mixin) {

src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedFieldCollector.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,29 @@ public class AnnotatedFieldCollector
1818
private final TypeFactory _typeFactory;
1919
private final MixInResolver _mixInResolver;
2020

21+
/**
22+
* @since 2.11
23+
*/
24+
private final boolean _collectAnnotations;
25+
2126
// // // Collected state
2227

2328
AnnotatedFieldCollector(AnnotationIntrospector intr,
24-
TypeFactory types, MixInResolver mixins)
29+
TypeFactory types, MixInResolver mixins, boolean collectAnnotations)
2530
{
2631
super(intr);
2732
_typeFactory = types;
2833
_mixInResolver = (intr == null) ? null : mixins;
34+
_collectAnnotations = collectAnnotations;
2935
}
3036

3137
public static List<AnnotatedField> collectFields(AnnotationIntrospector intr,
3238
TypeResolutionContext tc,
3339
MixInResolver mixins, TypeFactory types,
34-
JavaType type)
40+
JavaType type, boolean collectAnnotations)
3541
{
36-
return new AnnotatedFieldCollector(intr, types, mixins).collect(tc, type);
42+
return new AnnotatedFieldCollector(intr, types, mixins, collectAnnotations)
43+
.collect(tc, type);
3744
}
3845

3946
List<AnnotatedField> collect(TypeResolutionContext tc, JavaType type)
@@ -75,7 +82,7 @@ private Map<String,FieldBuilder> _findFields(TypeResolutionContext tc,
7582
fields = new LinkedHashMap<>();
7683
}
7784
FieldBuilder b = new FieldBuilder(tc, f);
78-
if (_intr != null) {
85+
if (_collectAnnotations) {
7986
b.annotations = collectAnnotations(b.annotations, f.getDeclaredAnnotations());
8087
}
8188
fields.put(f.getName(), b);

src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedMethodCollector.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,27 @@ public class AnnotatedMethodCollector
1616
{
1717
private final MixInResolver _mixInResolver;
1818

19+
/**
20+
* @since 2.11
21+
*/
22+
private final boolean _collectAnnotations;
23+
1924
AnnotatedMethodCollector(AnnotationIntrospector intr,
20-
MixInResolver mixins)
25+
MixInResolver mixins, boolean collectAnnotations)
2126
{
2227
super(intr);
2328
_mixInResolver = (intr == null) ? null : mixins;
29+
_collectAnnotations = collectAnnotations;
2430
}
2531

2632
public static AnnotatedMethodMap collectMethods(AnnotationIntrospector intr,
2733
TypeResolutionContext tc,
2834
MixInResolver mixins, TypeFactory types,
29-
JavaType type, List<JavaType> superTypes, Class<?> primaryMixIn)
35+
JavaType type, List<JavaType> superTypes, Class<?> primaryMixIn,
36+
boolean collectAnnotations)
3037
{
3138
// Constructor also always members of resolved class, parent == resolution context
32-
return new AnnotatedMethodCollector(intr, mixins)
39+
return new AnnotatedMethodCollector(intr, mixins, collectAnnotations)
3340
.collect(types, tc, type, superTypes, primaryMixIn);
3441
}
3542

@@ -118,7 +125,7 @@ private void _addMemberMethods(TypeResolutionContext tc,
118125
: collectAnnotations(m.getDeclaredAnnotations());
119126
methods.put(key, new MethodBuilder(tc, m, c));
120127
} else {
121-
if (_intr != null) {
128+
if (_collectAnnotations) {
122129
b.annotations = collectDefaultAnnotations(b.annotations, m.getDeclaredAnnotations());
123130
}
124131
Method old = b.method;

0 commit comments

Comments
 (0)