Search in sources :

Example 1 with ArrayTypeTree

use of com.sun.source.tree.ArrayTypeTree 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;
}
Also used : ErrorProneToken(com.google.errorprone.util.ErrorProneToken) ArrayTypeTree(com.sun.source.tree.ArrayTypeTree) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) Tree(com.sun.source.tree.Tree) ArrayTypeTree(com.sun.source.tree.ArrayTypeTree)

Aggregations

ErrorProneToken (com.google.errorprone.util.ErrorProneToken)1 ArrayTypeTree (com.sun.source.tree.ArrayTypeTree)1 MethodTree (com.sun.source.tree.MethodTree)1 Tree (com.sun.source.tree.Tree)1 VariableTree (com.sun.source.tree.VariableTree)1