package com.github.therapi.jackson.enums;

import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector;
import com.fasterxml.jackson.databind.module.SimpleModule;

/**
 * Customizes the way Jackson serializes enums.
 */
public abstract class EnumRenamingModule extends SimpleModule {
    public EnumRenamingModule() {
        super("therapi-enum-renaming");
    }

    @Override
    public void setupModule(Module.SetupContext context) {
        super.setupModule(context);
        context.insertAnnotationIntrospector(new EnumNamingAnnotationIntrospector());
    }

    private class EnumNamingAnnotationIntrospector extends NopAnnotationIntrospector {
        @Override
        public String findEnumValue(Enum<?> value) {
            return EnumRenamingModule.this.getName(value);
        }
    }

    /**
     * @param value the enum value to inspect
     * @return the JSON name for the enum value, or {@code null} to delegate to the next introspector
     */
    protected abstract String getName(Enum<?> value);
}

@@ -338,16 +338,60 @@
 odule %7B%0A
+    private boolean overrideExistingNames;%0A%0A
     publ
@@ -459,24 +459,272 @@
 g%22);%0A    %7D%0A%0A
+    /**%0A     * Configures the module to clobber any enum names set by%0A     * a previous annotation introspector.%0A     */%0A    public EnumRenamingModule overrideExistingNames() %7B%0A        this.overrideExistingNames = true;%0A        return this;%0A    %7D%0A%0A
     @Overrid
@@ -1004,26 +1004,8 @@
 r %7B%0A
-        @Override%0A
     
@@ -1021,16 +1021,18 @@
 c String
+%5B%5D
  findEnu
@@ -1041,83 +1041,327 @@
 alue
-(Enum%3C?%3E value) %7B%0A            return EnumRenamingModule.this.getName(value)
+s(Class%3C?%3E enumType, Enum%3C?%3E%5B%5D enumValues, String%5B%5D names) %7B%0A            for (int i = 0; i %3C enumValues.length; i++) %7B%0A                if (names%5Bi%5D == null %7C%7C overrideExistingNames) %7B%0A                    names%5Bi%5D = EnumRenamingModule.this.getName(enumValues%5Bi%5D);%0A                %7D%0A            %7D%0A            return names
 ;%0A  

update for Jackson 2.7+