Java Code Examples for com.google.errorprone.VisitorState#getEndPosition()

The following examples show how to use com.google.errorprone.VisitorState#getEndPosition() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProtobufCheck.java    From startup-os with Apache License 2.0 6 votes vote down vote up
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {

  // check whether it's a newBuilder() method of any protobuf-generated message class
  if (NEW_BUILDER.matches(tree, state)) {
    // find next method invocation in chain (on obj.method1().method2() it would return method2)
    ExpressionTree methodInvocation =
        ASTHelpers.findEnclosingNode(state.getPath(), MethodInvocationTree.class);
    // next invoked message is a build() method of any protobuf-generated message builder class
    if (BUILDER_BUILD.matches(methodInvocation, state)) {

      // select "newBuilder().build()" part of tree
      int start = state.getEndPosition(tree) - "newBuilder()".length();
      int end = state.getEndPosition(methodInvocation);

      // suggest to replace it with "getDefaultInstance()"
      return describeMatch(
          methodInvocation,
          SuggestedFix.builder().replace(start, end, "getDefaultInstance()").build());
    }
  }
  return NO_MATCH;
}
 
Example 2
Source File: XPFlagCleaner.java    From piranha with Apache License 2.0 6 votes vote down vote up
private Description updateCode(
    Value v, ExpressionTree tree, ExpressionTree expr, VisitorState state) {
  boolean update = false;
  String replacementString = "";

  if (v.equals(Value.TRUE)) {
    update = true;
    replacementString = TRUE;
  } else if (v.equals(Value.FALSE)) {
    update = true;
    replacementString = FALSE;
  }

  if (update) {
    Description.Builder builder = buildDescription(tree);
    SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
    fixBuilder.replace(expr, replacementString);
    decrementAllSymbolUsages(expr, state, fixBuilder);
    builder.addFix(fixBuilder.build());
    endPos = state.getEndPosition(expr);
    return builder.build();
  }
  return Description.NO_MATCH;
}
 
Example 3
Source File: XPFlagCleaner.java    From piranha with Apache License 2.0 6 votes vote down vote up
@Override
public Description matchExpressionStatement(ExpressionStatementTree tree, VisitorState state) {
  if (overLaps(tree, state)) {
    return Description.NO_MATCH;
  }

  if (tree.getExpression().getKind().equals(Kind.METHOD_INVOCATION)) {
    MethodInvocationTree mit = (MethodInvocationTree) tree.getExpression();
    API api = getXPAPI(mit);
    if (api.equals(API.DELETE_METHOD)) {
      Description.Builder builder = buildDescription(tree);
      SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
      fixBuilder.delete(tree);
      decrementAllSymbolUsages(tree, state, fixBuilder);
      builder.addFix(fixBuilder.build());
      endPos = state.getEndPosition(tree);
      return builder.build();
    }
  }
  return Description.NO_MATCH;
}
 
Example 4
Source File: XPFlagCleaner.java    From piranha with Apache License 2.0 5 votes vote down vote up
private boolean overLaps(Tree t, VisitorState visitorState) {
  if (endPos != DONTCARE && visitorState.getEndPosition(t) <= endPos) {
    return true;
  } else {
    endPos = DONTCARE;
    return false;
  }
}
 
Example 5
Source File: XPFlagCleaner.java    From piranha with Apache License 2.0 5 votes vote down vote up
@Override
public Description matchReturn(ReturnTree tree, VisitorState state) {
  if (overLaps(tree, state)) {
    return Description.NO_MATCH;
  }
  ExpressionTree et = tree.getExpression();
  if (et != null && et.getKind().equals(Kind.BOOLEAN_LITERAL)) {
    return Description.NO_MATCH;
  }

  Value x = evalExpr(et, state);
  boolean update = false;
  String replacementString = EMPTY;

  if (x.equals(Value.TRUE)) {
    update = true;
    replacementString = TRUE;
  } else if (x.equals(Value.FALSE)) {
    update = true;
    replacementString = FALSE;
  }

  if (update) {
    Description.Builder builder = buildDescription(tree);
    SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
    fixBuilder.replace(et, replacementString);
    decrementAllSymbolUsages(et, state, fixBuilder);
    builder.addFix(fixBuilder.build());
    endPos = state.getEndPosition(tree);
    return builder.build();
  }
  return Description.NO_MATCH;
}
 
Example 6
Source File: XPFlagCleaner.java    From piranha with Apache License 2.0 5 votes vote down vote up
@Override
public Description matchConditionalExpression(
    ConditionalExpressionTree tree, VisitorState state) {
  if (overLaps(tree, state)) {
    return Description.NO_MATCH;
  }
  ExpressionTree et = tree.getCondition();
  Value x = evalExpr(et, state);
  String replacementString = EMPTY;

  boolean update = false;
  ExpressionTree removedBranch = null;
  if (x.equals(Value.TRUE)) {
    update = true;
    replacementString = state.getSourceForNode(tree.getTrueExpression());
    removedBranch = tree.getFalseExpression();
  } else if (x.equals(Value.FALSE)) {
    update = true;
    replacementString = state.getSourceForNode(tree.getFalseExpression());
    removedBranch = tree.getTrueExpression();
  }

  if (update) {
    Preconditions.checkNotNull(removedBranch, "update => removedBranch != null here.");
    Description.Builder builder = buildDescription(tree);
    SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
    fixBuilder.replace(tree, stripBraces(replacementString));
    decrementAllSymbolUsages(et, state, fixBuilder);
    decrementAllSymbolUsages(removedBranch, state, fixBuilder);
    builder.addFix(fixBuilder.build());
    endPos = state.getEndPosition(tree);
    return builder.build();
  }
  return Description.NO_MATCH;
}
 
Example 7
Source File: XPFlagCleaner.java    From piranha with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("TreeToString")
@Override
public Description matchBinary(BinaryTree tree, VisitorState state) {
  if (overLaps(tree, state)) {
    return Description.NO_MATCH;
  }

  Value x = evalExpr(tree, state);
  Description d = updateCode(x, tree, tree, state);
  if (!d.equals(Description.NO_MATCH)) {
    return d;
  }

  ExpressionTree deletedSubTree = null;
  ExpressionTree remainingSubTree = null;
  Value l = evalExpr(tree.getLeftOperand(), state);
  Value r = evalExpr(tree.getRightOperand(), state);
  if (tree.getKind().equals(Kind.CONDITIONAL_AND)) {
    if (l.equals(Value.TRUE)) {
      deletedSubTree = tree.getLeftOperand();
      remainingSubTree = tree.getRightOperand();
    } else if (r.equals(Value.TRUE)) {
      deletedSubTree = tree.getRightOperand();
      remainingSubTree = tree.getLeftOperand();
    }
  } else if (tree.getKind().equals(Kind.CONDITIONAL_OR)) {
    if (l.equals(Value.FALSE)) {
      deletedSubTree = tree.getLeftOperand();
      remainingSubTree = tree.getRightOperand();
    } else if (r.equals(Value.FALSE)) {
      deletedSubTree = tree.getRightOperand();
      remainingSubTree = tree.getLeftOperand();
    }
  }

  if (deletedSubTree != null) {
    Preconditions.checkNotNull(
        remainingSubTree, "deletedSubTree != null => remainingSubTree !=null here.");
    Description.Builder builder = buildDescription(tree);
    SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
    fixBuilder.replace(tree, remainingSubTree.toString());
    decrementAllSymbolUsages(deletedSubTree, state, fixBuilder);
    builder.addFix(fixBuilder.build());

    endPos = state.getEndPosition(tree);
    return builder.build();
  }

  return Description.NO_MATCH;
}