package com.fasterxml.jackson.module.paramnames;

import java.lang.reflect.MalformedParametersException;
import java.lang.reflect.Parameter;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.introspect.*;

/**
 * Introspector that uses parameter name information provided by the Java Reflection API additions in Java 8 to
 * determine the parameter name for methods and constructors.
 *
 * @author Lovro Pandzic
 * @see AnnotationIntrospector
 * @see Parameter
 */
public class ParameterNamesAnnotationIntrospector extends NopAnnotationIntrospector {
    private static final long serialVersionUID = 1L;

    private final JsonCreator.Mode creatorBinding;
    private final ParameterExtractor parameterExtractor;

    ParameterNamesAnnotationIntrospector(JsonCreator.Mode creatorBinding, ParameterExtractor parameterExtractor)
    {
        this.creatorBinding = creatorBinding;
        this.parameterExtractor = parameterExtractor;
    }

    @Override
    public String findImplicitPropertyName(AnnotatedMember m) {
        if (m instanceof AnnotatedParameter) {
            return findParameterName((AnnotatedParameter) m);
        }
        return null;
    }

    private String findParameterName(AnnotatedParameter annotatedParameter) {

        Parameter[] params;
        try {
            params = getParameters(annotatedParameter.getOwner());
        } catch (MalformedParametersException e) {
            return null;
        }

        Parameter p = params[annotatedParameter.getIndex()];
        return p.isNamePresent() ? p.getName() : null;
    }

    private Parameter[] getParameters(AnnotatedWithParams owner) {
        if (owner instanceof AnnotatedConstructor) {
            return parameterExtractor.getParameters(((AnnotatedConstructor) owner).getAnnotated());
        }
        if (owner instanceof AnnotatedMethod) {
            return parameterExtractor.getParameters(((AnnotatedMethod) owner).getAnnotated());
        }

        return null;
    }

    /*
    /**********************************************************
    /* Creator information handling
    /**********************************************************
     */

    @Override
    public JsonCreator.Mode findCreatorAnnotation(MapperConfig<?> config, Annotated a) {
        JsonCreator ann = _findAnnotation(a, JsonCreator.class);
        if (ann != null) {
            JsonCreator.Mode mode = ann.mode();
            // but keep in mind that there may be explicit default for this module
            if ((creatorBinding != null)
                    && (mode == JsonCreator.Mode.DEFAULT)) {
                mode = creatorBinding;
            }
            return mode;
        }
        return null;
    }

    @Override
    @Deprecated // remove AFTER 2.9
    public JsonCreator.Mode findCreatorBinding(Annotated a) {
        JsonCreator ann = _findAnnotation(a, JsonCreator.class);
        if (ann != null) {
            JsonCreator.Mode mode = ann.mode();
            if ((creatorBinding != null)
                    && (mode == JsonCreator.Mode.DEFAULT)) {
                mode = creatorBinding;
            }
            return mode;
        }
        return creatorBinding;
    }

    @Override
    @Deprecated // since 2.9
    public boolean hasCreatorAnnotation(Annotated a)
    {
        // 02-Mar-2017, tatu: Copied from base AnnotationIntrospector
        JsonCreator ann = _findAnnotation(a, JsonCreator.class);
        if (ann != null) {
            return (ann.mode() != JsonCreator.Mode.DISABLED);
        }
        return false;
    }
}

@@ -2854,852 +2854,7 @@
    %7D
-%0A%0A    @Override%0A    @Deprecated // remove AFTER 2.9%0A    public JsonCreator.Mode findCreatorBinding(Annotated a) %7B%0A        JsonCreator ann = _findAnnotation(a, JsonCreator.class);%0A        if (ann != null) %7B%0A            JsonCreator.Mode mode = ann.mode();%0A            if ((creatorBinding != null)%0A                    && (mode == JsonCreator.Mode.DEFAULT)) %7B%0A                mode = creatorBinding;%0A            %7D%0A            return mode;%0A        %7D%0A        return creatorBinding;%0A    %7D%0A%0A    @Override%0A    @Deprecated // since 2.9%0A    public boolean hasCreatorAnnotation(Annotated a)%0A    %7B%0A        // 02-Mar-2017, tatu: Copied from base AnnotationIntrospector%0A        JsonCreator ann = _findAnnotation(a, JsonCreator.class);%0A        if (ann != null) %7B%0A            return (ann.mode() != JsonCreator.Mode.DISABLED);%0A        %7D%0A        return false;%0A    %7D
 %0A%7D%0A

Remove unnecessary legacy method implementations