@Override public void scanFile(JavaFileScannerContext context) { occurrences.clear(); constants.clear(); scan(context.getTree()); for (String entry : occurrences.keySet()) { Collection<LiteralTree> literalTrees = occurrences.get(entry); int literalOccurrence = literalTrees.size(); if (constants.containsKey(entry)) { VariableTree constant = constants.get(entry); List<LiteralTree> duplications = literalTrees.stream().filter(literal -> literal.parent() != constant).collect(Collectors.toList()); context.reportIssue(this, duplications.iterator().next(), "Use already-defined constant '" + constant.simpleName() + "' instead of duplicating its value here.", secondaryLocations(duplications.subList(1, duplications.size())), literalOccurrence); } else if (literalOccurrence >= threshold) { context.reportIssue( this, literalTrees.iterator().next(), "Define a constant instead of duplicating this literal " + entry + " " + literalOccurrence + " times.", secondaryLocations(literalTrees), literalOccurrence); } } }
@Override public void scanFile(JavaFileScannerContext context) { occurrences.clear(); constants.clear(); scan(context.getTree()); for (String entry : occurrences.keySet()) { Collection<LiteralTree> literalTrees = occurrences.get(entry); int literalOccurrence = literalTrees.size(); if (constants.containsKey(entry)) { VariableTree constant = constants.get(entry); List<LiteralTree> duplications = literalTrees.stream().filter(literal -> literal.parent() != constant).collect(Collectors.toList()); context.reportIssue(this, duplications.iterator().next(), "Use already-defined constant '" + constant.simpleName() + "' instead of duplicating its value here.", secondaryLocations(duplications.subList(1, duplications.size())), literalOccurrence); } else if (literalOccurrence >= threshold) { context.reportIssue( this, literalTrees.iterator().next(), "Define a constant instead of duplicating this literal " + entry + " " + literalOccurrence + " times.", secondaryLocations(literalTrees), literalOccurrence); } } }