Skip to content

Commit

Permalink
Added a new class TokenTypes that contains all the type constants. Th…
Browse files Browse the repository at this point in the history
…is class

breaks the circular dependencies - the cost is needing to maintain this class
when a new grammar is introduced.
  • Loading branch information
oburn committed Oct 19, 2002
1 parent 3f14872 commit 84eddc3
Show file tree
Hide file tree
Showing 25 changed files with 522 additions and 175 deletions.
59 changes: 9 additions & 50 deletions src/checkstyle/com/puppycrawl/tools/checkstyle/TreeWalker.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
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 java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
import java.util.ArrayList;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.Set;

/**
* Responsible for walking an abstract syntax tree and notifying interested
Expand All @@ -41,11 +41,6 @@ class TreeWalker
{
// TODO: really need to optimise the performance of this class.

/** maps from a token name to value */
private static final Map TOKEN_NAME_TO_VALUE = new HashMap();
/** maps from a token value to name */
private static final Map TOKEN_VALUE_TO_NAME = new HashMap();

/** maps from token name to checks */
private final Map mTokenToChecks = new HashMap();
/** all the registered checks */
Expand All @@ -55,30 +50,6 @@ class TreeWalker
/** the tab width for error reporting */
private final int mTabWidth;

// initialise the constants
static {
final Field[] fields = Java14TokenTypes.class.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
final Field f = fields[i];
final String name = f.getName();
try {
// this should NEVER fail (famous last words)
final Integer value = new Integer(f.getInt(name));
TOKEN_NAME_TO_VALUE.put(name, value);
TOKEN_VALUE_TO_NAME.put(value, name);
}
catch (IllegalArgumentException e) {
e.printStackTrace();
System.exit(1);
}
catch (IllegalAccessException e) {
e.printStackTrace();
System.exit(1);
}
}

}

/**
* Creates a new <code>TreeWalker</code> instance.
*
Expand All @@ -91,20 +62,6 @@ public TreeWalker(LocalizedMessages aMessages, int aTabWidth)
mTabWidth = aTabWidth;
}

/**
* Returns the name of a token for a given ID.
* @param aID the ID of the token name to get
* @return a token name
*/
static String getTokenName(int aID)
{
final String name = (String) TOKEN_VALUE_TO_NAME.get(new Integer(aID));
if (name == null) {
throw new IllegalArgumentException("given id " + aID);
}
return name;
}

/**
* Register a check for a given configuration.
* @param aCheck the check to register
Expand Down Expand Up @@ -136,7 +93,7 @@ void registerCheck(Check aCheck, CheckConfiguration aConfig)
*/
private void registerCheck(int aTokenID, Check aCheck)
{
registerCheck(getTokenName(aTokenID), aCheck);
registerCheck(TokenTypes.getTokenName(aTokenID), aCheck);
}

/**
Expand Down Expand Up @@ -236,7 +193,8 @@ private void process(DetailAST aAST)
private void notifyVisit(DetailAST aAST)
{
final ArrayList visitors =
(ArrayList) mTokenToChecks.get(getTokenName(aAST.getType()));
(ArrayList) mTokenToChecks.get(TokenTypes.getTokenName(
aAST.getType()));
if (visitors != null) {
final Map ctx = new HashMap();
for (int i = 0; i < visitors.size(); i++) {
Expand All @@ -254,7 +212,8 @@ private void notifyVisit(DetailAST aAST)
private void notifyLeave(DetailAST aAST)
{
final ArrayList visitors =
(ArrayList) mTokenToChecks.get(getTokenName(aAST.getType()));
(ArrayList) mTokenToChecks.get(TokenTypes.getTokenName(
aAST.getType()));
if (visitors != null) {
for (int i = 0; i < visitors.size(); i++) {
final Check check = (Check) visitors.get(i);
Expand Down

0 comments on commit 84eddc3

Please sign in to comment.