package com.fasterxml.jackson.databind.introspect;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.introspect.AnnotatedClass.Creators;
import com.fasterxml.jackson.databind.util.ClassUtil;
final class AnnotatedCreatorCollector
extends CollectorBase
{
private final TypeResolutionContext _typeContext;
private AnnotatedConstructor _defaultConstructor;
AnnotatedCreatorCollector(AnnotationIntrospector intr,
TypeResolutionContext tc)
{
super(intr);
_typeContext = tc;
}
public static Creators collectCreators(AnnotationIntrospector intr,
TypeResolutionContext tc,
JavaType type, Class<?> primaryMixIn)
{
return new AnnotatedCreatorCollector(intr, tc)
.collect(type, primaryMixIn);
}
Creators collect(JavaType type, Class<?> primaryMixIn)
{
List<AnnotatedConstructor> constructors = _findPotentialConstructors(type, primaryMixIn);
List<AnnotatedMethod> factories = _findPotentialFactories(type, primaryMixIn);
if (_intr != null) {
if (_defaultConstructor != null) {
if (_intr.hasIgnoreMarker(_defaultConstructor)) {
_defaultConstructor = null;
}
}
for (int i = constructors.size(); --i >= 0; ) {
if (_intr.hasIgnoreMarker(constructors.get(i))) {
constructors.remove(i);
}
}
for (int i = factories.size(); --i >= 0; ) {
if (_intr.hasIgnoreMarker(factories.get(i))) {
factories.remove(i);
}
}
}
return new AnnotatedClass.Creators(_defaultConstructor, constructors, factories);
}
private List<AnnotatedConstructor> _findPotentialConstructors(JavaType type,
Class<?> primaryMixIn)
{
ClassUtil.Ctor defaultCtor = null;
List<ClassUtil.Ctor> ctors = null;
if (!type.isEnumType()) {
ClassUtil.Ctor[] declaredCtors = ClassUtil.getConstructors(type.getRawClass());
for (ClassUtil.Ctor ctor : declaredCtors) {
if (!isIncludableConstructor(ctor.getConstructor())) {
continue;
}
if (ctor.getParamCount() == 0) {
defaultCtor = ctor;
} else {
if (ctors == null) {
ctors = new ArrayList<>();
}
ctors.add(ctor);
}
}
}
List<AnnotatedConstructor> result;
int ctorCount;
if (ctors == null) {
result = Collections.emptyList();
if (defaultCtor == null) {
return result;
}
ctorCount = 0;
} else {
ctorCount = ctors.size();
result = new ArrayList<>(ctorCount);
for (int i = 0; i < ctorCount; ++i) {
result.add(null);
}
}
if (primaryMixIn != null) {
MemberKey[] ctorKeys = null;
for (ClassUtil.Ctor mixinCtor : ClassUtil.getConstructors(primaryMixIn)) {
if (mixinCtor.getParamCount() == 0) {
if (defaultCtor != null) {
_defaultConstructor = constructDefaultConstructor(defaultCtor, mixinCtor);
defaultCtor = null;
}
continue;
}
if (ctors != null) {
if (ctorKeys == null) {
ctorKeys = new MemberKey[ctorCount];
for (int i = 0; i < ctorCount; ++i) {
ctorKeys[i] = new MemberKey(ctors.get(i).getConstructor());
}
}
MemberKey key = new MemberKey(mixinCtor.getConstructor());
for (int i = 0; i < ctorCount; ++i) {
if (key.equals(ctorKeys[i])) {
result.set(i,
constructNonDefaultConstructor(ctors.get(i), mixinCtor));
break;
}
}
}
}
}
if (defaultCtor != null) {
_defaultConstructor = constructDefaultConstructor(defaultCtor, null);
}
for (int i = 0; i < ctorCount; ++i) {
AnnotatedConstructor ctor = result.get(i);
if (ctor == null) {
result.set(i,
constructNonDefaultConstructor(ctors.get(i), null));
}
}
return result;
}
private List<AnnotatedMethod> _findPotentialFactories(JavaType type, Class<?> primaryMixIn)
{
List<Method> candidates = null;
for (Method m : ClassUtil.getClassMethods(type.getRawClass())) {
if (!Modifier.isStatic(m.getModifiers())) {
continue;
}
if (candidates == null) {
candidates = new ArrayList<>();
}
candidates.add(m);
}
if (candidates == null) {
return Collections.emptyList();
}
int factoryCount = candidates.size();
List<AnnotatedMethod> result = new ArrayList<>(factoryCount);
for (int i = 0; i < factoryCount; ++i) {
result.add(null);
}
if (primaryMixIn != null) {
MemberKey[] methodKeys = null;
for (Method mixinFactory : ClassUtil.getDeclaredMethods(primaryMixIn)) {
if (!Modifier.isStatic(mixinFactory.getModifiers())) {
continue;
}
if (methodKeys == null) {
methodKeys = new MemberKey[factoryCount];
for (int i = 0; i < factoryCount; ++i) {
methodKeys[i] = new MemberKey(candidates.get(i));
}
}
MemberKey key = new MemberKey(mixinFactory);
for (int i = 0; i < factoryCount; ++i) {
if (key.equals(methodKeys[i])) {
result.set(i,
constructFactoryCreator(candidates.get(i), mixinFactory));
break;
}
}
}
}
for (int i = 0; i < factoryCount; ++i) {
AnnotatedMethod factory = result.get(i);
if (factory == null) {
result.set(i,
constructFactoryCreator(candidates.get(i), null));
}
}
return result;
}
protected AnnotatedConstructor constructDefaultConstructor(ClassUtil.Ctor ctor,
ClassUtil.Ctor mixin)
{
if (_intr == null) {
return new AnnotatedConstructor(_typeContext, ctor.getConstructor(),
_emptyAnnotationMap(), NO_ANNOTATION_MAPS);
}
return new AnnotatedConstructor(_typeContext, ctor.getConstructor(),
collectAnnotations(ctor, mixin),
collectAnnotations(ctor.getConstructor().getParameterAnnotations(),
(mixin == null) ? null : mixin.getConstructor().getParameterAnnotations()));
}
protected AnnotatedConstructor constructNonDefaultConstructor(ClassUtil.Ctor ctor,
ClassUtil.Ctor mixin)
{
final int paramCount = ctor.getParamCount();
if (_intr == null) {
return new AnnotatedConstructor(_typeContext, ctor.getConstructor(),
_emptyAnnotationMap(), _emptyAnnotationMaps(paramCount));
}
if (paramCount == 0) {
return new AnnotatedConstructor(_typeContext, ctor.getConstructor(),
collectAnnotations(ctor, mixin),
NO_ANNOTATION_MAPS);
}
AnnotationMap[] resolvedAnnotations;
Annotation[][] paramAnns = ctor.getParameterAnnotations();
if (paramCount != paramAnns.length) {
resolvedAnnotations = null;
Class<?> dc = ctor.getDeclaringClass();
if (dc.isEnum() && (paramCount == paramAnns.length + 2)) {
Annotation[][] old = paramAnns;
paramAnns = new Annotation[old.length+2][];
System.arraycopy(old, 0, paramAnns, 2, old.length);
resolvedAnnotations = collectAnnotations(paramAnns, null);
} else if (dc.isMemberClass()) {
if (paramCount == (paramAnns.length + 1)) {
Annotation[][] old = paramAnns;
paramAnns = new Annotation[old.length+1][];
System.arraycopy(old, 0, paramAnns, 1, old.length);
paramAnns[0] = NO_ANNOTATIONS;
resolvedAnnotations = collectAnnotations(paramAnns, null);
}
}
if (resolvedAnnotations == null) {
throw new IllegalStateException(String.format(
"Internal error: constructor for %s has mismatch: %d parameters; %d sets of annotations",
ctor.getDeclaringClass().getName(), paramCount, paramAnns.length));
}
} else {
resolvedAnnotations = collectAnnotations(paramAnns,
(mixin == null) ? null : mixin.getParameterAnnotations());
}
return new AnnotatedConstructor(_typeContext, ctor.getConstructor(),
collectAnnotations(ctor, mixin), resolvedAnnotations);
}
protected AnnotatedMethod constructFactoryCreator(Method m, Method mixin)
{
final int paramCount = m.getParameterTypes().length;
if (_intr == null) {
return new AnnotatedMethod(_typeContext, m, _emptyAnnotationMap(),
_emptyAnnotationMaps(paramCount));
}
if (paramCount == 0) {
return new AnnotatedMethod(_typeContext, m, collectAnnotations(m, mixin),
NO_ANNOTATION_MAPS);
}
return new AnnotatedMethod(_typeContext, m, collectAnnotations(m, mixin),
collectAnnotations(m.getParameterAnnotations(),
(mixin == null) ? null : mixin.getParameterAnnotations()));
}
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]);
}
result[i] = c.asAnnotationMap();
}
return result;
}
private AnnotationMap collectAnnotations(ClassUtil.Ctor main, ClassUtil.Ctor mixin) {
AnnotationCollector c = collectAnnotations(main.getConstructor().getDeclaredAnnotations());
if (mixin != null) {
c = collectAnnotations(c, mixin.getConstructor().getDeclaredAnnotations());
}
return c.asAnnotationMap();
}
private final AnnotationMap collectAnnotations(AnnotatedElement main, AnnotatedElement mixin) {
AnnotationCollector c = collectAnnotations(main.getDeclaredAnnotations());
if (mixin != null) {
c = collectAnnotations(c, mixin.getDeclaredAnnotations());
}
return c.asAnnotationMap();
}
private static boolean isIncludableConstructor(Constructor<?> c) {
return !c.isSynthetic();
}
}