use of com.google.errorprone.util.ErrorProneToken in project error-prone by google.
the class MixedArrayDimensions method checkArrayDimensions.
private Description checkArrayDimensions(Tree tree, Tree type, VisitorState state) {
if (!(type instanceof ArrayTypeTree)) {
return NO_MATCH;
}
CharSequence source = state.getSourceCode();
for (; type instanceof ArrayTypeTree; type = ((ArrayTypeTree) type).getType()) {
Tree elemType = ((ArrayTypeTree) type).getType();
int start = state.getEndPosition(elemType);
int end = state.getEndPosition(type);
if (start >= end) {
continue;
}
String dim = source.subSequence(start, end).toString();
if (dim.isEmpty()) {
continue;
}
ImmutableList<ErrorProneToken> tokens = ErrorProneTokens.getTokens(dim.trim(), state.context);
if (tokens.size() > 2 && tokens.get(0).kind() == TokenKind.IDENTIFIER) {
int nonWhitespace = CharMatcher.isNot(' ').indexIn(dim);
int idx = dim.indexOf("[]", nonWhitespace);
if (idx > nonWhitespace) {
String replacement = dim.substring(idx, dim.length()) + dim.substring(0, idx);
return describeMatch(tree, SuggestedFix.replace(start, end, replacement));
}
}
}
return NO_MATCH;
}
use of com.google.errorprone.util.ErrorProneToken in project error-prone by google.
the class BooleanParameter method matchMethodInvocation.
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
List<? extends ExpressionTree> arguments = tree.getArguments();
if (arguments.size() < 2) {
// single-argument methods are often self-documentating
return NO_MATCH;
}
if (arguments.stream().noneMatch(BooleanParameter::isBooleanLiteral)) {
return NO_MATCH;
}
MethodSymbol sym = ASTHelpers.getSymbol(tree);
if (NamedParameterComment.containsSyntheticParameterName(sym)) {
return NO_MATCH;
}
int start = ((JCTree) tree).getStartPosition();
int end = state.getEndPosition(getLast(tree.getArguments()));
String source = state.getSourceCode().subSequence(start, end).toString();
Deque<ErrorProneToken> tokens = new ArrayDeque<>(ErrorProneTokens.getTokens(source, state.context));
forEachPair(sym.getParameters().stream(), arguments.stream(), (p, c) -> checkParameter(p, c, start, tokens, state));
return NO_MATCH;
}
use of com.google.errorprone.util.ErrorProneToken in project error-prone by google.
the class SuggestedFixes method addModifiers.
/**
* Adds modifiers to the given class, method, or field declaration.
*/
public static Optional<SuggestedFix> addModifiers(Tree tree, VisitorState state, Modifier... modifiers) {
ModifiersTree originalModifiers = getModifiers(tree);
if (originalModifiers == null) {
return Optional.empty();
}
Set<Modifier> toAdd = Sets.difference(new TreeSet<>(Arrays.asList(modifiers)), originalModifiers.getFlags());
if (originalModifiers.getFlags().isEmpty()) {
int pos = state.getEndPosition(originalModifiers) != Position.NOPOS ? state.getEndPosition(originalModifiers) + 1 : ((JCTree) tree).getStartPosition();
int base = ((JCTree) tree).getStartPosition();
Optional<Integer> insert = state.getTokensForNode(tree).stream().map(token -> token.pos() + base).filter(thisPos -> thisPos >= pos).findFirst();
// shouldn't ever be able to get to the else
int insertPos = insert.orElse(pos);
return Optional.of(SuggestedFix.replace(insertPos, insertPos, Joiner.on(' ').join(toAdd) + " "));
}
// a map from modifiers to modifier position (or -1 if the modifier is being added)
// modifiers are sorted in Google Java Style order
Map<Modifier, Integer> modifierPositions = new TreeMap<>();
for (Modifier mod : toAdd) {
modifierPositions.put(mod, -1);
}
List<ErrorProneToken> tokens = state.getTokensForNode(originalModifiers);
int base = ((JCTree) originalModifiers).getStartPosition();
for (ErrorProneToken tok : tokens) {
Modifier mod = getTokModifierKind(tok);
if (mod != null) {
modifierPositions.put(mod, base + tok.pos());
}
}
SuggestedFix.Builder fix = SuggestedFix.builder();
// walk the map of all modifiers, and accumulate a list of new modifiers to insert
// beside an existing modifier
List<Modifier> modifiersToWrite = new ArrayList<>();
for (Modifier mod : modifierPositions.keySet()) {
int p = modifierPositions.get(mod);
if (p == -1) {
modifiersToWrite.add(mod);
} else if (!modifiersToWrite.isEmpty()) {
fix.replace(p, p, Joiner.on(' ').join(modifiersToWrite) + " ");
modifiersToWrite.clear();
}
}
if (!modifiersToWrite.isEmpty()) {
fix.postfixWith(originalModifiers, " " + Joiner.on(' ').join(modifiersToWrite));
}
return Optional.of(fix.build());
}
use of com.google.errorprone.util.ErrorProneToken in project error-prone by google.
the class SuggestedFixes method renameMethod.
/**
* Be warned, only changes method name at the declaration.
*/
public static SuggestedFix renameMethod(MethodTree tree, final String replacement, VisitorState state) {
// Search tokens from beginning of method tree to beginning of method body.
int basePos = ((JCTree) tree).getStartPosition();
int endPos = tree.getBody() != null ? ((JCTree) tree.getBody()).getStartPosition() : state.getEndPosition(tree);
List<ErrorProneToken> methodTokens = ErrorProneTokens.getTokens(state.getSourceCode().subSequence(basePos, endPos).toString(), state.context);
for (ErrorProneToken token : methodTokens) {
if (token.kind() == TokenKind.IDENTIFIER && token.name().equals(tree.getName())) {
int nameStartPosition = basePos + token.pos();
int nameEndPosition = basePos + token.endPos();
return SuggestedFix.builder().replace(nameStartPosition, nameEndPosition, replacement).build();
}
}
// Method name not found.
throw new AssertionError();
}
use of com.google.errorprone.util.ErrorProneToken in project error-prone by google.
the class SuggestedFixes method removeModifiers.
/**
* Remove modifiers from the given class, method, or field declaration.
*/
public static Optional<SuggestedFix> removeModifiers(Tree tree, VisitorState state, Modifier... modifiers) {
Set<Modifier> toRemove = ImmutableSet.copyOf(modifiers);
ModifiersTree originalModifiers = getModifiers(tree);
if (originalModifiers == null) {
return Optional.empty();
}
SuggestedFix.Builder fix = SuggestedFix.builder();
List<ErrorProneToken> tokens = state.getTokensForNode(originalModifiers);
int basePos = ((JCTree) originalModifiers).getStartPosition();
boolean empty = true;
for (ErrorProneToken tok : tokens) {
Modifier mod = getTokModifierKind(tok);
if (toRemove.contains(mod)) {
empty = false;
fix.replace(basePos + tok.pos(), basePos + tok.endPos() + 1, "");
break;
}
}
if (empty) {
return Optional.empty();
}
return Optional.of(fix.build());
}
Aggregations