
package org.neovera.jdiablo.internal;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

import org.neovera.jdiablo.annotation.Option;

import net.sf.cglib.proxy.CallbackFilter;

public class DiabloFilter implements CallbackFilter {

    private Set<String> _optionAnnotatedFieldNames = new HashSet<String>();

    public DiabloFilter(final Class<?> claz) {
        // Setup set of @Option annotated field names.
        Class<?> clz = claz;

        while (clz != null) {
            for (Field field : clz.getDeclaredFields()) {
                if (field.isAnnotationPresent(Option.class)) {
                    _optionAnnotatedFieldNames.add(field.getName().startsWith("_") ? field.getName().substring(1) : field.getName());
                }
            }
            clz = clz.getSuperclass();
        }
    }

    public int accept(Method method) {
        String propertyName = SpecializationUtil.getPropertyName(method);
        if (propertyName != null && _optionAnnotatedFieldNames.contains(propertyName)) {
            return 0;
        } else {
            return 1;
        }
    }

}
