Jackson Data Binding Source Code

Jackson is "the Java JSON library" or "the best JSON parser for Java". Or simply as "JSON for Java".

  • Jackson Data Binding module allows you to converts JSON to and from POJO (Plain Old Java Object) using property accessor or using annotations.
  • Jackson Databind Source Code files are provided in the source packge (jackson-databind-2.12.4-sources.jar). You can download it at Jackson Maven Website.

    You can also browse Jackson Databind Source Code below:

    ✍: FYIcenter.com

    com/fasterxml/jackson/databind/introspect/AnnotatedConstructor.java

    package com.fasterxml.jackson.databind.introspect;
    
    import java.lang.reflect.*;
    
    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;
    
        /**
         * Field that is used to make JDK serialization work with this
         * object.
         * 
         * @since 2.1
         */
        protected Serialization _serialization;
    
        /*
        /**********************************************************
        /* Life-cycle
        /**********************************************************
         */
    
        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;
        }
    
        /**
         * Method used for JDK serialization support
         * @since 2.1
         */
        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);
        }
        
        /*
        /**********************************************************
        /* Annotated impl
        /**********************************************************
         */
    
        @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();
        }
    
        /*
        /**********************************************************
        /* Extended API
        /**********************************************************
         */
    
        @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 // since 2.7
        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();
        }
    
        @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);
        }
        
        /*
        /**********************************************************
        /* AnnotatedMember impl
        /**********************************************************
         */
    
        @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());
        }
        
        /*
        /**********************************************************
        /* Extended API, specific annotations
        /**********************************************************
         */
    
        @Override
        public String toString() {
            // 03-Nov-2020 ckozak: This can use _constructor.getParameterCount() once java 8 is required.
            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;
            return ClassUtil.hasClass(o, getClass())
                    && (((AnnotatedConstructor) o)._constructor == _constructor);
        }
    
        /*
        /**********************************************************
        /* JDK serialization handling
        /**********************************************************
         */
    
        Object writeReplace() {
            return new AnnotatedConstructor(new Serialization(_constructor));
        }
    
        Object readResolve() {
            Class<?> clazz = _serialization.clazz;
            try {
                Constructor<?> ctor = clazz.getDeclaredConstructor(_serialization.args);
                // 06-Oct-2012, tatu: Has "lost" its security override, must force back
                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());
            }
        }
        
        /**
         * Helper class that is used as the workaround to persist
         * Field references. It basically just stores declaring class
         * and field name.
         */
        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();
            }
        }
    }
    

    com/fasterxml/jackson/databind/introspect/AnnotatedConstructor.java

     

    ⇒ Jackson Annotations Source Code

    ⇐ Download and Install Jackson Binary Package

    ⇑ Downloading and Reviewing jackson-*.jar

    ⇑⇑ Jackson - Java JSON library

    2022-03-29, 32073👍, 0💬