package net.ranides.assira.reflection.asm;

import net.ranides.assira.reflection.IClass;

import java.util.HashSet;
import java.util.Set;

public class AsmInnerScanner {

    private final Set<IClass<?>> inner;

    public AsmInnerScanner() {
        this.inner = new HashSet<>();
    }

    /**
     * Returns all inner classes used by scanned classes.
     *
     * @return classes
     */
    public Set<IClass<?>> result() {
        return inner;
    }

    public AsmInnerScanner scan(IClass<?> type) {
        add(type);

        type.parents().each(this::scan);

        type.interfaces().discard(v -> v == type).each(this::scan);

        type.methods().each(v -> {
            add(v.returns());
            v.arguments().types().each(this::add);
        });

        return this;
    }

    private void add(IClass<?> type) {
        if(type.outer() != IClass.NULL) {
            inner.add(type);
        }
    }

}
