/*
 * Copyright 2015, Yahoo Inc.
 * Copyrights licensed under the Apache 2.0 License.
 * See the accompanying LICENSE file for terms.
 */
package com.yahoo.squidb.jackson;

import com.yahoo.aptutils.model.DeclaredTypeName;
import com.yahoo.squidb.processor.data.ModelSpec;
import com.yahoo.squidb.processor.plugins.PluginEnvironment;
import com.yahoo.squidb.processor.plugins.defaults.properties.TableModelSpecFieldPlugin;
import com.yahoo.squidb.processor.plugins.defaults.properties.generators.PropertyGenerator;

import javax.lang.model.element.VariableElement;

public class JacksonFieldPlugin extends TableModelSpecFieldPlugin {

    public JacksonFieldPlugin(ModelSpec<?> modelSpec, PluginEnvironment pluginEnv) {
        super(modelSpec, pluginEnv);
    }

    @Override
    public boolean processVariableElement(VariableElement field, DeclaredTypeName fieldType) {
        if (field.getAnnotation(JacksonProperty.class) == null) {
            return false;
        }
        return super.processVariableElement(field, fieldType);
    }

    @Override
    protected boolean hasPropertyGeneratorForField(VariableElement field, DeclaredTypeName fieldType) {
        if (fieldType.equals(JacksonTypeConstants.MAP)) {
            return fieldType.getTypeArgs().get(0) instanceof DeclaredTypeName &&
                    fieldType.getTypeArgs().get(1) instanceof DeclaredTypeName;
        } else if (fieldType.equals(JacksonTypeConstants.LIST)) {
            return fieldType.getTypeArgs().get(0) instanceof DeclaredTypeName;
        }
        return false;
    }

    @Override
    protected PropertyGenerator getPropertyGenerator(VariableElement field, DeclaredTypeName fieldType) {
        if (fieldType.equals(JacksonTypeConstants.MAP)) {
            return new JacksonMapPropertyGenerator(modelSpec, field, fieldType, utils);
        } else if (fieldType.equals(JacksonTypeConstants.LIST)) {
            return new JacksonListPropertyGenerator(modelSpec, field, fieldType, utils);
        }
        return null;
    }

}

@@ -776,21 +776,24 @@
 de%0A    p
-ublic
+rotected
  boolean
@@ -793,38 +793,44 @@
 boolean 
-processVariableElement
+hasPropertyGeneratorForField
 (Variabl
@@ -981,196 +981,8 @@
   %7D%0A
-        return super.processVariableElement(field, fieldType);%0A    %7D%0A%0A    @Override%0A    protected boolean hasPropertyGeneratorForField(VariableElement field, DeclaredTypeName fieldType) %7B%0A
     

Simplify JacksonFieldPlugin using new shared field plugin logic