diff options
author | Tatu Saloranta <tatu.saloranta@iki.fi> | 2019-10-09 22:50:44 -0700 |
---|---|---|
committer | Tatu Saloranta <tatu.saloranta@iki.fi> | 2019-10-09 22:50:44 -0700 |
commit | cef4b99644f360767d6d89df2fe5567e0b57b842 (patch) | |
tree | 0ed2f4f8f1d55ba500d4b65411dc871e75f84f8a /src/main/java/com/fasterxml/jackson/databind/introspect | |
parent | be2a3e6d28e64bbd393dad603a3d75b479f13b71 (diff) | |
download | jackson-databind-cef4b99644f360767d6d89df2fe5567e0b57b842.tar.gz |
Minor tweaking about annotation collecting, trying to avoid processing JDK types
Diffstat (limited to 'src/main/java/com/fasterxml/jackson/databind/introspect')
5 files changed, 117 insertions, 48 deletions
diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedClass.java b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedClass.java index 2c8b733e8..4f8a933ba 100644 --- a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedClass.java +++ b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedClass.java @@ -76,6 +76,11 @@ public final class AnnotatedClass */ final protected Class<?> _primaryMixIn; + /** + * @since 2.11 + */ + final protected boolean _collectAnnotations; + /* /********************************************************** /* Gathered information @@ -129,7 +134,8 @@ public final class AnnotatedClass */ AnnotatedClass(JavaType type, Class<?> rawType, List<JavaType> superTypes, Class<?> primaryMixIn, Annotations classAnnotations, TypeBindings bindings, - AnnotationIntrospector aintr, MixInResolver mir, TypeFactory tf) + AnnotationIntrospector aintr, MixInResolver mir, TypeFactory tf, + boolean collectAnnotations) { _type = type; _class = rawType; @@ -140,6 +146,16 @@ public final class AnnotatedClass _annotationIntrospector = aintr; _mixInResolver = mir; _typeFactory = tf; + _collectAnnotations = collectAnnotations; + } + + @Deprecated // since 2.10 + AnnotatedClass(JavaType type, Class<?> rawType, List<JavaType> superTypes, + Class<?> primaryMixIn, Annotations classAnnotations, TypeBindings bindings, + AnnotationIntrospector aintr, MixInResolver mir, TypeFactory tf) + { + this(type, rawType, superTypes, primaryMixIn, classAnnotations, bindings, + aintr, mir, tf, true); } /** @@ -158,6 +174,7 @@ public final class AnnotatedClass _annotationIntrospector = null; _mixInResolver = null; _typeFactory = null; + _collectAnnotations = false; } /** @@ -347,7 +364,7 @@ public final class AnnotatedClass f = Collections.emptyList(); } else { f = AnnotatedFieldCollector.collectFields(_annotationIntrospector, - this, _mixInResolver, _typeFactory, _type); + this, _mixInResolver, _typeFactory, _type, _collectAnnotations); } _fields = f; } @@ -365,7 +382,7 @@ public final class AnnotatedClass m = AnnotatedMethodCollector.collectMethods(_annotationIntrospector, this, _mixInResolver, _typeFactory, - _type, _superTypes, _primaryMixIn); + _type, _superTypes, _primaryMixIn, _collectAnnotations); } _memberMethods = m; } @@ -379,7 +396,7 @@ public final class AnnotatedClass c = NO_CREATORS; } else { c = AnnotatedCreatorCollector.collectCreators(_annotationIntrospector, - this, _type, _primaryMixIn); + this, _type, _primaryMixIn, _collectAnnotations); } _creators = c; } diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedClassResolver.java b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedClassResolver.java index 65326e23b..bdf7c2a3f 100644 --- a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedClassResolver.java +++ b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedClassResolver.java @@ -33,6 +33,11 @@ public class AnnotatedClassResolver private final Class<?> _class; private final Class<?> _primaryMixin; + /** + * @since 2.11 + */ + private final boolean _collectAnnotations; + AnnotatedClassResolver(MapperConfig<?> config, JavaType type, MixInResolver r) { _config = config; _type = type; @@ -41,7 +46,10 @@ public class AnnotatedClassResolver _bindings = type.getBindings(); _intr = config.isAnnotationProcessingEnabled() ? config.getAnnotationIntrospector() : null; - _primaryMixin = _config.findMixInClassFor(_class); + _primaryMixin = (r == null) ? null : r.findMixInClassFor(_class); + + // Also... JDK types do not have annotations that are of interest to us + _collectAnnotations = (_intr != null) && !ClassUtil.isJDKClass(_class); } AnnotatedClassResolver(MapperConfig<?> config, Class<?> cls, MixInResolver r) { @@ -56,8 +64,10 @@ public class AnnotatedClassResolver } else { _intr = config.isAnnotationProcessingEnabled() ? config.getAnnotationIntrospector() : null; - _primaryMixin = _config.findMixInClassFor(_class); + _primaryMixin = (r == null) ? null : r.findMixInClassFor(_class); } + + _collectAnnotations = (_intr != null) && !ClassUtil.isJDKClass(_class); } public static AnnotatedClass resolve(MapperConfig<?> config, JavaType forType, @@ -116,7 +126,8 @@ public class AnnotatedClassResolver List<JavaType> superTypes = ClassUtil.findSuperTypes(_type, null, false); return new AnnotatedClass(_type, _class, superTypes, _primaryMixin, resolveClassAnnotations(superTypes), - _bindings, _intr, _mixInResolver, _config.getTypeFactory()); + _bindings, _intr, _mixInResolver, _config.getTypeFactory(), + _collectAnnotations); } @@ -124,7 +135,8 @@ public class AnnotatedClassResolver List<JavaType> superTypes = Collections.<JavaType>emptyList(); return new AnnotatedClass(null, _class, superTypes, _primaryMixin, resolveClassAnnotations(superTypes), - _bindings, _intr, _config, _config.getTypeFactory()); + _bindings, _intr, _mixInResolver, _config.getTypeFactory(), + _collectAnnotations); } /* @@ -144,33 +156,48 @@ public class AnnotatedClassResolver if (_intr == null) { return NO_ANNOTATIONS; } + // Plus we may or may not have mix-ins to consider + final boolean checkMixIns = (_mixInResolver != null) + && (!(_mixInResolver instanceof SimpleMixInResolver) + || ((SimpleMixInResolver) _mixInResolver).hasMixIns()); + + // also skip if there's nothing to do + if (!checkMixIns && !_collectAnnotations) { + return NO_ANNOTATIONS; + } + AnnotationCollector resolvedCA = AnnotationCollector.emptyCollector(); // add mix-in annotations first (overrides) if (_primaryMixin != null) { resolvedCA = _addClassMixIns(resolvedCA, _class, _primaryMixin); } // then annotations from the class itself: - resolvedCA = _addAnnotationsIfNotPresent(resolvedCA, - ClassUtil.findClassAnnotations(_class)); + // 06-Oct-2019, tatu: [databind#2464] Skip class annotations for JDK classes + if (_collectAnnotations) { + resolvedCA = _addAnnotationsIfNotPresent(resolvedCA, + ClassUtil.findClassAnnotations(_class)); + } // and then from super types for (JavaType type : superTypes) { // and mix mix-in annotations in-between - if (_mixInResolver != null) { + if (checkMixIns) { Class<?> cls = type.getRawClass(); resolvedCA = _addClassMixIns(resolvedCA, cls, _mixInResolver.findMixInClassFor(cls)); } - resolvedCA = _addAnnotationsIfNotPresent(resolvedCA, - ClassUtil.findClassAnnotations(type.getRawClass())); + if (_collectAnnotations) { + resolvedCA = _addAnnotationsIfNotPresent(resolvedCA, + ClassUtil.findClassAnnotations(type.getRawClass())); + } } - /* and finally... any annotations there might be for plain - * old Object.class: separate because for all other purposes - * it is just ignored (not included in super types) - */ + + // and finally... any annotations there might be for plain old Object.class: + // separate because otherwise it is just ignored (not included in super types) + // 12-Jul-2009, tatu: Should this be done for interfaces too? // For now, yes, seems useful for some cases, and not harmful for any? - if (_mixInResolver != null) { + if (checkMixIns) { resolvedCA = _addClassMixIns(resolvedCA, Object.class, _mixInResolver.findMixInClassFor(Object.class)); } diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedCreatorCollector.java b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedCreatorCollector.java index d4bbce56f..c113d7278 100644 --- a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedCreatorCollector.java +++ b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedCreatorCollector.java @@ -27,23 +27,32 @@ final class AnnotatedCreatorCollector private final TypeResolutionContext _typeContext; + /** + * @since 2.11 + */ + private final boolean _collectAnnotations; + // // // Collected state private AnnotatedConstructor _defaultConstructor; AnnotatedCreatorCollector(AnnotationIntrospector intr, - TypeResolutionContext tc) + TypeResolutionContext tc, boolean collectAnnotations) { super(intr); _typeContext = tc; + _collectAnnotations = collectAnnotations; } public static Creators collectCreators(AnnotationIntrospector intr, TypeResolutionContext tc, - JavaType type, Class<?> primaryMixIn) + JavaType type, Class<?> primaryMixIn, boolean collectAnnotations) { + final boolean checkClassAnnotations = (intr != null) + && !ClassUtil.isJDKClass(type.getRawClass()); + // Constructor also always members of resolved class, parent == resolution context - return new AnnotatedCreatorCollector(intr, tc) + return new AnnotatedCreatorCollector(intr, tc, checkClassAnnotations) .collect(type, primaryMixIn); } @@ -60,7 +69,7 @@ final class AnnotatedCreatorCollector * ignorable after all annotations have been properly collapsed. */ // AnnotationIntrospector is null if annotations not enabled; if so, can skip: - if (_intr != null) { + if (_collectAnnotations) { if (_defaultConstructor != null) { if (_intr.hasIgnoreMarker(_defaultConstructor)) { _defaultConstructor = null; @@ -236,10 +245,6 @@ final class AnnotatedCreatorCollector protected AnnotatedConstructor constructDefaultConstructor(ClassUtil.Ctor ctor, ClassUtil.Ctor mixin) { - if (_intr == null) { // when annotation processing is disabled - return new AnnotatedConstructor(_typeContext, ctor.getConstructor(), - _emptyAnnotationMap(), NO_ANNOTATION_MAPS); - } return new AnnotatedConstructor(_typeContext, ctor.getConstructor(), collectAnnotations(ctor, mixin), // 16-Jun-2019, tatu: default is zero-args, so can't have parameter annotations @@ -320,27 +325,33 @@ ctor.getDeclaringClass().getName(), paramCount, paramAnns.length)); } private AnnotationMap[] collectAnnotations(Annotation[][] mainAnns, Annotation[][] mixinAnns) { - final int count = mainAnns.length; - AnnotationMap[] result = new AnnotationMap[count]; - for (int i = 0; i < count; ++i) { - AnnotationCollector c = collectAnnotations(AnnotationCollector.emptyCollector(), - mainAnns[i]); - if (mixinAnns != null) { - c = collectAnnotations(c, mixinAnns[i]); + if (_collectAnnotations) { + final int count = mainAnns.length; + AnnotationMap[] result = new AnnotationMap[count]; + for (int i = 0; i < count; ++i) { + AnnotationCollector c = collectAnnotations(AnnotationCollector.emptyCollector(), + mainAnns[i]); + if (mixinAnns != null) { + c = collectAnnotations(c, mixinAnns[i]); + } + result[i] = c.asAnnotationMap(); } - result[i] = c.asAnnotationMap(); + return result; } - return result; + return NO_ANNOTATION_MAPS; } // // NOTE: these are only called when we know we have AnnotationIntrospector - + private AnnotationMap collectAnnotations(ClassUtil.Ctor main, ClassUtil.Ctor mixin) { - AnnotationCollector c = collectAnnotations(main.getConstructor().getDeclaredAnnotations()); - if (mixin != null) { - c = collectAnnotations(c, mixin.getConstructor().getDeclaredAnnotations()); + if (_collectAnnotations) { + AnnotationCollector c = collectAnnotations(main.getConstructor().getDeclaredAnnotations()); + if (mixin != null) { + c = collectAnnotations(c, mixin.getConstructor().getDeclaredAnnotations()); + } + return c.asAnnotationMap(); } - return c.asAnnotationMap(); + return _emptyAnnotationMap(); } private final AnnotationMap collectAnnotations(AnnotatedElement main, AnnotatedElement mixin) { diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedFieldCollector.java b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedFieldCollector.java index 0dba88bba..d50aa534c 100644 --- a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedFieldCollector.java +++ b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedFieldCollector.java @@ -18,22 +18,29 @@ public class AnnotatedFieldCollector private final TypeFactory _typeFactory; private final MixInResolver _mixInResolver; + /** + * @since 2.11 + */ + private final boolean _collectAnnotations; + // // // Collected state AnnotatedFieldCollector(AnnotationIntrospector intr, - TypeFactory types, MixInResolver mixins) + TypeFactory types, MixInResolver mixins, boolean collectAnnotations) { super(intr); _typeFactory = types; _mixInResolver = (intr == null) ? null : mixins; + _collectAnnotations = collectAnnotations; } public static List<AnnotatedField> collectFields(AnnotationIntrospector intr, TypeResolutionContext tc, MixInResolver mixins, TypeFactory types, - JavaType type) + JavaType type, boolean collectAnnotations) { - return new AnnotatedFieldCollector(intr, types, mixins).collect(tc, type); + return new AnnotatedFieldCollector(intr, types, mixins, collectAnnotations) + .collect(tc, type); } List<AnnotatedField> collect(TypeResolutionContext tc, JavaType type) @@ -75,7 +82,7 @@ public class AnnotatedFieldCollector fields = new LinkedHashMap<>(); } FieldBuilder b = new FieldBuilder(tc, f); - if (_intr != null) { + if (_collectAnnotations) { b.annotations = collectAnnotations(b.annotations, f.getDeclaredAnnotations()); } fields.put(f.getName(), b); diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedMethodCollector.java b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedMethodCollector.java index 0341e3a2a..96077af7f 100644 --- a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedMethodCollector.java +++ b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedMethodCollector.java @@ -16,20 +16,27 @@ public class AnnotatedMethodCollector { private final MixInResolver _mixInResolver; + /** + * @since 2.11 + */ + private final boolean _collectAnnotations; + AnnotatedMethodCollector(AnnotationIntrospector intr, - MixInResolver mixins) + MixInResolver mixins, boolean collectAnnotations) { super(intr); _mixInResolver = (intr == null) ? null : mixins; + _collectAnnotations = collectAnnotations; } public static AnnotatedMethodMap collectMethods(AnnotationIntrospector intr, TypeResolutionContext tc, MixInResolver mixins, TypeFactory types, - JavaType type, List<JavaType> superTypes, Class<?> primaryMixIn) + JavaType type, List<JavaType> superTypes, Class<?> primaryMixIn, + boolean collectAnnotations) { // Constructor also always members of resolved class, parent == resolution context - return new AnnotatedMethodCollector(intr, mixins) + return new AnnotatedMethodCollector(intr, mixins, collectAnnotations) .collect(types, tc, type, superTypes, primaryMixIn); } @@ -118,7 +125,7 @@ public class AnnotatedMethodCollector : collectAnnotations(m.getDeclaredAnnotations()); methods.put(key, new MethodBuilder(tc, m, c)); } else { - if (_intr != null) { + if (_collectAnnotations) { b.annotations = collectDefaultAnnotations(b.annotations, m.getDeclaredAnnotations()); } Method old = b.method; |