Skip to content

Commit

Permalink
Minor tweaking about annotation collecting, trying to avoid processin…
Browse files Browse the repository at this point in the history
…g JDK types
  • Loading branch information
cowtowncoder committed Oct 10, 2019
1 parent be2a3e6 commit cef4b99
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public final class AnnotatedClass
*/
final protected Class<?> _primaryMixIn;

/**
* @since 2.11
*/
final protected boolean _collectAnnotations;

/*
/**********************************************************
/* Gathered information
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}

/**
Expand All @@ -158,6 +174,7 @@ public final class AnnotatedClass
_annotationIntrospector = null;
_mixInResolver = null;
_typeFactory = null;
_collectAnnotations = false;
}

/**
Expand Down Expand Up @@ -347,7 +364,7 @@ private final List<AnnotatedField> _fields() {
f = Collections.emptyList();
} else {
f = AnnotatedFieldCollector.collectFields(_annotationIntrospector,
this, _mixInResolver, _typeFactory, _type);
this, _mixInResolver, _typeFactory, _type, _collectAnnotations);
}
_fields = f;
}
Expand All @@ -365,7 +382,7 @@ private final AnnotatedMethodMap _methods() {
m = AnnotatedMethodCollector.collectMethods(_annotationIntrospector,
this,
_mixInResolver, _typeFactory,
_type, _superTypes, _primaryMixIn);
_type, _superTypes, _primaryMixIn, _collectAnnotations);
}
_memberMethods = m;
}
Expand All @@ -379,7 +396,7 @@ private final Creators _creators() {
c = NO_CREATORS;
} else {
c = AnnotatedCreatorCollector.collectCreators(_annotationIntrospector,
this, _type, _primaryMixIn);
this, _type, _primaryMixIn, _collectAnnotations);
}
_creators = c;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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,
Expand Down Expand Up @@ -116,15 +126,17 @@ AnnotatedClass resolveFully() {
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);

}

AnnotatedClass resolveWithoutSuperTypes() {
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);
}

/*
Expand All @@ -144,33 +156,48 @@ private Annotations resolveClassAnnotations(List<JavaType> superTypes)
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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -60,7 +69,7 @@ Creators collect(JavaType type, Class<?> primaryMixIn)
* 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;
Expand Down Expand Up @@ -236,10 +245,6 @@ private List<AnnotatedMethod> _findPotentialFactories(JavaType type, Class<?> pr
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
Expand Down Expand Up @@ -320,27 +325,33 @@ protected AnnotatedMethod constructFactoryCreator(Method m, Method mixin)
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -75,7 +82,7 @@ private Map<String,FieldBuilder> _findFields(TypeResolutionContext tc,
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -118,7 +125,7 @@ private void _addMemberMethods(TypeResolutionContext tc,
: 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;
Expand Down
Loading

0 comments on commit cef4b99

Please sign in to comment.