package com.fasterxml.jackson.databind.introspect;
import java.lang.reflect.Constructor;
import java.lang.reflect.Member;
import java.lang.reflect.Type;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.util.ClassUtil;
public final class AnnotatedConstructor
extends AnnotatedWithParams
{
private static final long serialVersionUID = 1L;
protected final Constructor<?> _constructor;
protected Serialization _serialization;
public AnnotatedConstructor(TypeResolutionContext ctxt, Constructor<?> constructor,
AnnotationMap classAnn, AnnotationMap[] paramAnn)
{
super(ctxt, classAnn, paramAnn);
if (constructor == null) {
throw new IllegalArgumentException("Null constructor not allowed");
}
_constructor = constructor;
}
protected AnnotatedConstructor(Serialization ser)
{
super(null, null, null);
_constructor = null;
_serialization = ser;
}
@Override
public AnnotatedConstructor withAnnotations(AnnotationMap ann) {
return new AnnotatedConstructor(_typeContext, _constructor, ann, _paramAnnotations);
}
@Override
public Constructor<?> getAnnotated() { return _constructor; }
@Override
public int getModifiers() { return _constructor.getModifiers(); }
@Override
public String getName() { return _constructor.getName(); }
@Override
public JavaType getType() {
return _typeContext.resolveType(getRawType());
}
@Override
public Class<?> getRawType() {
return _constructor.getDeclaringClass();
}
@Override
public int getParameterCount() {
return _constructor.getParameterTypes().length;
}
@Override
public Class<?> getRawParameterType(int index)
{
Class<?>[] types = _constructor.getParameterTypes();
return (index >= types.length) ? null : types[index];
}
@Override
public JavaType getParameterType(int index) {
Type[] types = _constructor.getGenericParameterTypes();
if (index >= types.length) {
return null;
}
return _typeContext.resolveType(types[index]);
}
@Override
@Deprecated
public Type getGenericParameterType(int index) {
Type[] types = _constructor.getGenericParameterTypes();
if (index >= types.length) {
return null;
}
return types[index];
}
@Override
public final Object call() throws Exception {
return _constructor.newInstance((Object[]) null);
}
@Override
public final Object call(Object[] args) throws Exception {
return _constructor.newInstance(args);
}
@Override
public final Object call1(Object arg) throws Exception {
return _constructor.newInstance(arg);
}
@Override
public Class<?> getDeclaringClass() { return _constructor.getDeclaringClass(); }
@Override
public Member getMember() { return _constructor; }
@Override
public void setValue(Object pojo, Object value)
throws UnsupportedOperationException
{
throw new UnsupportedOperationException("Cannot call setValue() on constructor of "
+getDeclaringClass().getName());
}
@Override
public Object getValue(Object pojo)
throws UnsupportedOperationException
{
throw new UnsupportedOperationException("Cannot call getValue() on constructor of "
+getDeclaringClass().getName());
}
@Override
public String toString() {
final int argCount = _constructor.getParameterTypes().length;
return String.format("[constructor for %s (%d arg%s), annotations: %s",
ClassUtil.nameOf(_constructor.getDeclaringClass()), argCount,
(argCount == 1) ? "" : "s", _annotations);
}
@Override
public int hashCode() {
return _constructor.getName().hashCode();
}
@Override
public boolean equals(Object o) {
if (o == this) return true;
if (!ClassUtil.hasClass(o, getClass())) {
return false;
}
AnnotatedConstructor other = (AnnotatedConstructor) o;
if (other._constructor == null) {
return _constructor == null;
} else {
return other._constructor.equals(_constructor);
}
}
Object writeReplace() {
return new AnnotatedConstructor(new Serialization(_constructor));
}
Object readResolve() {
Class<?> clazz = _serialization.clazz;
try {
Constructor<?> ctor = clazz.getDeclaredConstructor(_serialization.args);
if (!ctor.isAccessible()) {
ClassUtil.checkAndFixAccess(ctor, false);
}
return new AnnotatedConstructor(null, ctor, null, null);
} catch (Exception e) {
throw new IllegalArgumentException("Could not find constructor with "
+_serialization.args.length+" args from Class '"+clazz.getName());
}
}
private final static class Serialization
implements java.io.Serializable
{
private static final long serialVersionUID = 1L;
protected Class<?> clazz;
protected Class<?>[] args;
public Serialization(Constructor<?> ctor) {
clazz = ctor.getDeclaringClass();
args = ctor.getParameterTypes();
}
}
}