Skip to content

Commit

Permalink
Issue #662: moved MultipleStringLiteralsExtendedCheck to log AST
Browse files Browse the repository at this point in the history
  • Loading branch information
rnveach authored and romani committed Jun 7, 2018
1 parent 2f9cfb2 commit 8a28e14
Showing 1 changed file with 6 additions and 57 deletions.
Expand Up @@ -49,7 +49,7 @@ public class MultipleStringLiteralsExtendedCheck extends AbstractCheck {
* The found strings and their positions. <String, ArrayList>, with
* the ArrayList containing StringInfo objects.
*/
private final Map<String, List<StringInfo>> stringMap = Maps.newHashMap();
private final Map<String, List<DetailAST>> stringMap = Maps.newHashMap();

/**
* Marks the TokenTypes where duplicate strings should be ignored.
Expand Down Expand Up @@ -152,14 +152,12 @@ public void visitToken(DetailAST ast) {
if (!isInIgnoreOccurrenceContext(ast)) {
final String currentString = ast.getText();
if ((pattern == null) || !pattern.matcher(currentString).find()) {
List<StringInfo> hitList = stringMap.get(currentString);
List<DetailAST> hitList = stringMap.get(currentString);
if (hitList == null) {
hitList = Lists.newArrayList();
stringMap.put(currentString, hitList);
}
final int line = ast.getLineNo();
final int col = ast.getColumnNo();
hitList.add(new StringInfo(line, col));
hitList.add(ast);
}
}
}
Expand Down Expand Up @@ -197,68 +195,19 @@ public void beginTree(DetailAST rootAST) {
public void finishTree(DetailAST rootAST) {
final Set<String> keys = stringMap.keySet();
for (String key : keys) {
final List<StringInfo> hits = stringMap.get(key);
final List<DetailAST> hits = stringMap.get(key);
if (hits.size() > allowedDuplicates) {
int hitsSize = 1;
if (highlightAllDuplicates) {
hitsSize = hits.size();
}
for (int index = 0; index < hitsSize; index++) {
final StringInfo firstFinding = hits.get(index);
final int line = firstFinding.getLine();
final int col = firstFinding.getCol();
log(line, col,
final DetailAST firstFinding = hits.get(index);
log(firstFinding,
MSG_KEY, key, hits.size());
}
}
}
}

/**
* This class contains information about where a string was found.
*/
private static final class StringInfo {

/**
* Line of finding.
*/
private final int line;
/**
* Column of finding.
*/
private final int col;

/**
* Creates information about a string position.
*
* @param line
* int
* @param col
* int
*/
private StringInfo(int line, int col) {
this.line = line;
this.col = col;
}

/**
* The line where a string was found.
*
* @return int Line of the string.
*/
private int getLine() {
return line;
}

/**
* The column where a string was found.
*
* @return int Column of the string.
*/
private int getCol() {
return col;
}

}

}

0 comments on commit 8a28e14

Please sign in to comment.