use of javax.lang.model.element.ElementKind.FIELD in project error-prone by google.
the class UnusedMethod method fixConstructors.
private void fixConstructors(ImmutableListMultimap<Symbol, TreePath> unusedConstructors, VisitorState state) {
for (Map.Entry<Symbol, List<TreePath>> entry : asMap(unusedConstructors).entrySet()) {
Symbol symbol = entry.getKey();
List<TreePath> trees = entry.getValue();
SuggestedFix.Builder fix = SuggestedFix.builder();
int constructorCount = size(scope(symbol.members()).getSymbols(Symbol::isConstructor));
int finalFields = size(scope(symbol.members()).getSymbols(s -> s.getKind().equals(FIELD) && s.getModifiers().contains(FINAL)));
boolean fixable;
if (constructorCount == trees.size()) {
fix.postfixWith(getLast(trees).getLeaf(), format("private %s() {}", symbol.getSimpleName()));
fixable = finalFields == 0;
} else {
fixable = true;
}
String message = String.format("Constructor '%s' is never used.", symbol.getSimpleName());
trees.forEach(t -> fix.merge(replaceIncludingComments(t, "", state)));
state.reportMatch(buildDescription(trees.get(0).getLeaf()).addFix(fixable ? fix.build() : emptyFix()).setMessage(message).build());
}
}
Aggregations