<?xml version="1.0" encoding="UTF-8"?>
<test-data
    xmlns="http://pmd.sourceforge.net/rule-tests"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd">

    <code-fragment id="full-example"><![CDATA[
public class StatementAndBraceFinder extends JavaParserVisitorAdapter {
    private static final Logger LOGGER = Logger.getLogger(StatementAndBraceFinder.class.getName());

    private final DataFlowHandler dataFlowHandler;
    private Structure dataFlow;

    public StatementAndBraceFinder(DataFlowHandler dataFlowHandler) {
        this.dataFlowHandler = dataFlowHandler;
    }

    public void buildDataFlowFor(JavaNode node) {
        if (!(node instanceof ASTMethodDeclaration) && !(node instanceof ASTConstructorDeclaration)) {
            throw new RuntimeException("Can't build a data flow for anything other than a method or a constructor");
        }

        this.dataFlow = new Structure(dataFlowHandler);
        this.dataFlow.createStartNode(node.getBeginLine());
        this.dataFlow.createNewNode(node);

        node.jjtAccept(this, dataFlow);

        this.dataFlow.createEndNode(node.getEndLine());
        if (LOGGER.isLoggable(Level.FINE)) {
            // TODO SRT Remove after development
            LOGGER.fine("DataFlow is " + this.dataFlow.dump());
        }
        Linker linker = new Linker(dataFlowHandler, dataFlow.getBraceStack(), dataFlow.getContinueBreakReturnStack());
        try {
            linker.computePaths();
        } catch (SequenceException | LinkerException e) {
            e.printStackTrace();
        }
    }


    private void tryToLog(String tag, NodeType type, Node node) {
        if (LOGGER.isLoggable(Level.FINEST)) {
            LOGGER.finest("pushOnStack " + tag + " " + type + ": line " + node.getBeginLine()
                + ", column " + node.getBeginColumn());
        }
    }

    private void tryToLog(NodeType type, Node node) {
        tryToLog("", type, node);
    }

    @Override
    public Object visit(ASTStatementExpression node, Object data) {
        if (!(data instanceof Structure)) {
            return data;
        }
        Structure dataFlow = (Structure) data;
        if (LOGGER.isLoggable(Level.FINEST)) {
            LOGGER.finest("createNewNode ASTStatementExpression: line " + node.getBeginLine() + ", column "
                + node.getBeginColumn());
        }
        dataFlow.createNewNode(node);
        return super.visit(node, data);
    }

    @Override
    public Object visit(ASTVariableDeclarator node, Object data) {
        if (!(data instanceof Structure)) {
            return data;
        }
        Structure dataFlow = (Structure) data;
        if (LOGGER.isLoggable(Level.FINEST)) {
            LOGGER.finest("createNewNode ASTVariableDeclarator: line " + node.getBeginLine() + ", column "
                + node.getBeginColumn());
        }
        dataFlow.createNewNode(node);
        return super.visit(node, data);
    }

    @Override
    public Object visit(ASTExpression node, Object data) {
        if (!(data instanceof Structure)) {
            return data;
        }
        Structure dataFlow = (Structure) data;

        String loggerTag = "parent";

        Node parent = node.jjtGetParent();

        // TODO what about throw stmts?
        if (parent instanceof ASTIfStatement) {
            dataFlow.createNewNode(node); // START IF
            dataFlow.pushOnStack(NodeType.IF_EXPR, dataFlow.getLast());
            tryToLog(loggerTag, NodeType.IF_EXPR, node);
        } else if (parent instanceof ASTWhileStatement) {
            dataFlow.createNewNode(node); // START WHILE
            dataFlow.pushOnStack(NodeType.WHILE_EXPR, dataFlow.getLast());
            tryToLog(loggerTag, NodeType.WHILE_EXPR, node);
        } else if (parent instanceof ASTSwitchStatement) {
            dataFlow.createNewNode(node); // START SWITCH
            dataFlow.pushOnStack(NodeType.SWITCH_START, dataFlow.getLast());
            tryToLog(loggerTag, NodeType.SWITCH_START, node);
        } else if (parent instanceof ASTForStatement) {
            dataFlow.createNewNode(node); // FOR EXPR
            dataFlow.pushOnStack(NodeType.FOR_EXPR, dataFlow.getLast());
            tryToLog(loggerTag, NodeType.FOR_EXPR, node);
        } else if (parent instanceof ASTDoStatement) {
            dataFlow.createNewNode(node); // DO EXPR
            dataFlow.pushOnStack(NodeType.DO_EXPR, dataFlow.getLast());
            tryToLog(loggerTag, NodeType.DO_EXPR, node);
        } else if (parent instanceof ASTAssertStatement) {
            dataFlow.createNewNode(node);
            dataFlow.pushOnStack(NodeType.ASSERT_STATEMENT, dataFlow.getLast());
            tryToLog(loggerTag, NodeType.ASSERT_STATEMENT, node);
        }

        return super.visit(node, data);
    }

}

     ]]></code-fragment>


    <test-code>
        <description>Full example</description> <!-- TODO issues w/ that-->
        <expected-problems>7</expected-problems>
        <expected-messages>
            <message>'.StatementAndBraceFinder' has value 10 highest 6.</message>
            <message>'.StatementAndBraceFinder#buildDataFlowFor(JavaNode)' has value 6.</message>
            <message>'.StatementAndBraceFinder#tryToLog(String, NodeType, Node)' has value 4.</message>
            <message>'.StatementAndBraceFinder#tryToLog(NodeType, Node)' has value 0.</message>
            <message>'.StatementAndBraceFinder#visit(ASTStatementExpression, Object)' has value 4.</message>
            <message>'.StatementAndBraceFinder#visit(ASTVariableDeclarator, Object)' has value 4.</message>
            <message>'.StatementAndBraceFinder#visit(ASTExpression, Object)' has value 18.</message>
        </expected-messages>
        <code-ref id="full-example"/>
    </test-code>

    <test-code regressionTest="false">
        <description>TODO: known limitation, should report 1</description>
        <rule-property name="reportClasses">false</rule-property>
        <expected-problems>1</expected-problems>
        <expected-messages>
            <message>'.Foo#bar()' has value 1.</message>
        </expected-messages>
        <code><![CDATA[
        public class Foo {
          Type a;
          void bar() {
            a.b.foo();
          }
        }
        ]]></code>
    </test-code>

    <test-code>
        <description>Test empty class</description>

        <expected-problems>1</expected-problems>
        <expected-messages>
            <message>'.Foo' has value 0 highest 0.</message>
        </expected-messages>
        <code><![CDATA[
        public class Foo {



        }
        ]]></code>
    </test-code>

    <test-code>
        <description>ATFD doesn't support interfaces or annotations</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
        public interface Foo {

           public final int h;

           @interface Tag {
              public static final int num = 0;
           }
        }
        ]]></code>
    </test-code>

</test-data>