<commit_before>////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2002  <NAME>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle;

import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.LocalizedMessages;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.api.FileContents;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Arrays;

/**
 * Responsible for walking an abstract syntax tree and notifying interested
 * checks at each each node.
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version 1.0
 */
class TreeWalker
{
    // TODO: really need to optimise the performance of this class.

    /** maps from token name to checks */
    private final Map mTokenToChecks = new HashMap();
    /** all the registered checks */
    private final Set mAllChecks = new HashSet();
    /** collects the error messages */
    private final LocalizedMessages mMessages;
    /** the tab width for error reporting */
    private final int mTabWidth;

    /**
     * Creates a new <code>TreeWalker</code> instance.
     *
     * @param aMessages used to collect messages
     * @param aTabWidth the tabwidth to use
     */
    public TreeWalker(LocalizedMessages aMessages, int aTabWidth)
    {
        mMessages = aMessages;
        mTabWidth = aTabWidth;
    }

    /**
     * Register a check for a given configuration.
     * @param aCheck the check to register
     * @param aConfig the configuration to use
     */
    void registerCheck(Check aCheck, CheckConfiguration aConfig)
    {
        aCheck.setMessages(mMessages);
        aCheck.setTabWidth(mTabWidth);
        if (!aConfig.getTokens().isEmpty()) {
            int acceptableTokens[] = aCheck.getAcceptableTokens();
            Arrays.sort(acceptableTokens);
            final Iterator it = aConfig.getTokens().iterator();
            while (it.hasNext()) {
                String token = (String) it.next();
                int tokenId = TokenTypes.getTokenId(token);
                if (Arrays.binarySearch(acceptableTokens, tokenId) >= 0) {
                    registerCheck(token, aCheck);
                }
                // TODO: else error message?
            }
        }
        else {
            final int[] tokens = aCheck.getDefaultTokens();
            for (int i = 0; i < tokens.length; i++) {
                registerCheck(tokens[i], aCheck);
            }
        }
        mAllChecks.add(aCheck);
    }

    /**
     * Register a check for a specified token id.
     * @param aTokenID the id of the token
     * @param aCheck the check to register
     */
    private void registerCheck(int aTokenID, Check aCheck)
    {
        registerCheck(TokenTypes.getTokenName(aTokenID), aCheck);
    }

    /**
     * Register a check for a specified token name
     * @param aToken the name of the token
     * @param aCheck the check to register
     */
    private void registerCheck(String aToken, Check aCheck)
    {
        ArrayList visitors = (ArrayList) mTokenToChecks.get(aToken);
        if (visitors == null) {
            visitors = new ArrayList();
            mTokenToChecks.put(aToken, visitors);
        }

        visitors.add(aCheck);
    }

    /**
     * Initiates the walk of an AST.
     * @param aAST the root AST
     * @param aContents the contents of the file the AST was generated from
     */
    void walk(DetailAST aAST, FileContents aContents)
    {
        mMessages.reset();
        notifyBegin(aContents);
        aAST.setParent(null);
        process(aAST);
        notifyEnd();
    }

    /**
     * Notify interested checks that about to begin walking a tree.
     * @param aContents the contents of the file the AST was generated from
     */
    private void notifyBegin(FileContents aContents)
    {
        // TODO: do not track Context properly for token
        final Iterator it = mAllChecks.iterator();
        while (it.hasNext()) {
            final Check check = (Check) it.next();
            final HashMap treeContext = new HashMap();
            check.setTreeContext(treeContext);<commit_msg>fixed NPE<commit_after>////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2002  <NAME>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle;

import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.LocalizedMessages;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.api.FileContents;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Arrays;

/**
 * Responsible for walking an abstract syntax tree and notifying interested
 * checks at each each node.
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version 1.0
 */
class TreeWalker
{
    // TODO: really need to optimise the performance of this class.

    /** maps from token name to checks */
    private final Map mTokenToChecks = new HashMap();
    /** all the registered checks */
    private final Set mAllChecks = new HashSet();
    /** collects the error messages */
    private final LocalizedMessages mMessages;
    /** the tab width for error reporting */
    private final int mTabWidth;

    /**
     * Creates a new <code>TreeWalker</code> instance.
     *
     * @param aMessages used to collect messages
     * @param aTabWidth the tabwidth to use
     */
    public TreeWalker(LocalizedMessages aMessages, int aTabWidth)
    {
        mMessages = aMessages;
        mTabWidth = aTabWidth;
    }

    /**
     * Register a check for a given configuration.
     * @param aCheck the check to register
     * @param aConfig the configuration to use
     */
    void registerCheck(Check aCheck, CheckConfiguration aConfig)
    {
        aCheck.setMessages(mMessages);
        aCheck.setTabWidth(mTabWidth);
        if (!aConfig.getTokens().isEmpty()) {
            int acceptableTokens[] = aCheck.getAcceptableTokens();
            Arrays.sort(acceptableTokens);
            final Iterator it = aConfig.getTokens().iterator();
            while (it.hasNext()) {
                String token = (String) it.next();
                int tokenId = TokenTypes.getTokenId(token);
                if (Arrays.binarySearch(acceptableTokens, tokenId) >= 0) {
                    registerCheck(token, aCheck);
                }
                // TODO: else error message?
            }
        }
        else {
            final int[] tokens = aCheck.getDefaultTokens();
            for (int i = 0; i < tokens.length; i++) {
                registerCheck(tokens[i], aCheck);
            }
        }
        mAllChecks.add(aCheck);
    }

    /**
     * Register a check for a specified token id.
     * @param aTokenID the id of the token
     * @param aCheck the check to register
     */
    private void registerCheck(int aTokenID, Check aCheck)
    {
        registerCheck(TokenTypes.getTokenName(aTokenID), aCheck);
    }

    /**
     * Register a check for a specified token name
     * @param aToken the name of the token
     * @param aCheck the check to register
     */
    private void registerCheck(String aToken, Check aCheck)
    {
        ArrayList visitors = (ArrayList) mTokenToChecks.get(aToken);
        if (visitors == null) {
            visitors = new ArrayList();
            mTokenToChecks.put(aToken, visitors);
        }

        visitors.add(aCheck);
    }

    /**
     * Initiates the walk of an AST.
     * @param aAST the root AST
     * @param aContents the contents of the file the AST was generated from
     */
    void walk(DetailAST aAST, FileContents aContents)
    {
        mMessages.reset();
        notifyBegin(aContents);

         // empty files are not flagged by javac, will yield aAST == null
        if (aAST != null) {
            aAST.setParent(null);
            process(aAST);
        }

        notifyEnd();
    }

    /**
     * Notify interested checks that about to begin walking a tree.
     * @param aContents the contents of the file the AST was generated from
     */
    private void notifyBegin(FileContents aContents)
    {
        // TODO: do not track Context properly for token
        final Iterator it = mAllChecks.iterator();
        while (it.hasNext()) {
            final Check check = (Check) it.next();
            final HashMap treeContext = new HashMap();
            check.setTreeContext(treeContext);
{'fraction_non_alphanumeric': 0.1197758358662614, 'fraction_numerical': 0.005319148936170213, 'mean_word_length': 2.212019524100061, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2767992', 'n_tokens_mistral': 2850, 'n_tokens_neox': 2760, 'n_words': 974}