Package com.sun.source.tree

Examples of com.sun.source.tree.ExpressionTree


    return Description.NO_MATCH;
  }

  @Override
  public Description matchVariable(VariableTree tree, VisitorState state) {
    ExpressionTree initializer = stripCheckNotNull(tree.getInitializer(), state);
    Tree parent = state.getPath().getParentPath().getLeaf();

    // must be a static class variable with member select initializer
    if (initializer == null || initializer.getKind() != MEMBER_SELECT || parent.getKind() != CLASS
        || !tree.getModifiers().getFlags().contains(STATIC)) {
      return Description.NO_MATCH;
    }

    MemberSelectTree rhs = (MemberSelectTree) initializer;
View Full Code Here


    Tree parent = state.getPath().getParentPath().getLeaf();

    // default fix is to delete assignment
    Fix fix = SuggestedFix.delete(parent);

    ExpressionTree lhs = assignmentTree.getVariable();
    ExpressionTree rhs = assignmentTree.getExpression();

    // if this is a method invocation, they must be calling checkNotNull()
    if (assignmentTree.getExpression().getKind() == METHOD_INVOCATION) {
      // change the default fix to be "checkNotNull(x)" instead of "x = checkNotNull(x)"
      fix = SuggestedFix.replace(assignmentTree, rhs.toString());
      // new rhs is first argument to checkNotNull()
      rhs = stripCheckNotNull(rhs, state);
    }


    if (lhs.getKind() == MEMBER_SELECT) {
      // find a method parameter of the same type and similar name and suggest it
      // as the rhs

      // rhs should be either identifier or field access
      assert(rhs.getKind() == IDENTIFIER || rhs.getKind() == MEMBER_SELECT);

      // get current name of rhs
      String rhsName = null;
      if (rhs.getKind() == IDENTIFIER) {
        rhsName = ((JCIdent) rhs).name.toString();
      } else if (rhs.getKind() == MEMBER_SELECT) {
        rhsName = ((JCFieldAccess) rhs).name.toString();
      }

      // find method parameters of the same type
      Type type = ((JCFieldAccess) lhs).type;
      TreePath path = state.getPath();
      while (path != null && path.getLeaf().getKind() != METHOD) {
        path = path.getParentPath();
      }
      JCMethodDecl method = (JCMethodDecl) path.getLeaf();
      int minEditDistance = Integer.MAX_VALUE;
      String replacement = null;
      for (JCVariableDecl var : method.params) {
        if (var.type == type) {
          int editDistance = EditDistance.getEditDistance(rhsName, var.name.toString());
          if (editDistance < minEditDistance) {
            // pick one with minimum edit distance
            minEditDistance = editDistance;
            replacement = var.name.toString();
          }
        }
      }
      if (replacement != null) {
        // suggest replacing rhs with the parameter
        fix = SuggestedFix.replace(rhs, replacement);
      }
    } else if (rhs.getKind() == IDENTIFIER) {
      // find a field of the same type and similar name and suggest it as the lhs

      // lhs should be identifier
      assert(lhs.getKind() == IDENTIFIER);
View Full Code Here

public class StringBuilderInitWithChar extends BugChecker implements NewClassTreeMatcher {
  @Override
  public Description matchNewClass(NewClassTree tree, VisitorState state) {
    if (Matchers.isSameType(Symtab.instance(state.context).stringBuilderType).matches(
        tree.getIdentifier(), state) && tree.getArguments().size() == 1) {
      ExpressionTree argument = tree.getArguments().get(0);
      Type type = ((JCTree) argument).type;
      if (type.getKind() == TypeKind.CHAR) {
        if (argument.getKind() == Kind.CHAR_LITERAL) {
          char ch = (Character) ((LiteralTree) argument).getValue();
          return describeMatch(tree,
              SuggestedFix.replace(argument, "\"" + Convert.quote(Character.toString(ch)) + "\""));
        } else {
          return describeMatch(tree, SuggestedFix.replace(tree,
View Full Code Here

    } else {
      return Description.NO_MATCH;
    }

    List<? extends ExpressionTree> allArgs = tree.getArguments();
    ExpressionTree formatExpression = allArgs.get(formatIndex);
    List<? extends ExpressionTree> formatArgs = allArgs.subList(formatIndex + 1, allArgs.size());
    // If there's a sole argument of array type, this can be a non-varargs form call. Ignoring.
    if (formatArgs.size() == 1
        && ((JCExpression) formatArgs.get(0)).type.getKind() == TypeKind.ARRAY) {
      return Description.NO_MATCH;
    }
    String formatString = null;
    if (formatExpression.getKind() == Kind.STRING_LITERAL) {
      formatString = ((JCLiteral) formatExpression).getValue().toString();
    } else {
      Symbol sym = ASTHelpers.getSymbol(formatExpression);
      if (sym instanceof VarSymbol) {
        formatString = (String) ((VarSymbol) sym).getConstValue();
View Full Code Here

      return Description.NO_MATCH;
    }

    // TODO: Suggest fixes for more situations.
    Description.Builder descriptionBuilder = Description.builder(methodInvocationTree, pattern);
    ExpressionTree arg = methodInvocationTree.getArguments().get(0);
    String value = (String) ((JCExpression) arg).type.constValue();
    String reasonInvalid = "";

    if (".".equals(value)) {
      descriptionBuilder.addFix(SuggestedFix.replace(arg, "\"\\\\.\""));
View Full Code Here

    return methodName.startsWith("get");
  }

  private static String getMethodName(ExpressionTree tree) {
    MethodInvocationTree method = (MethodInvocationTree) tree;
    ExpressionTree expressionTree = method.getMethodSelect();
    JCFieldAccess access = (JCFieldAccess) expressionTree;
    return access.sym.getQualifiedName().toString();
  }
View Full Code Here

        return false;
      }
      if (!returnsListMatcher.matches(method, state)) {
        return false;
      }
      ExpressionTree expressionTree = method.getMethodSelect();
      if (expressionTree instanceof JCFieldAccess) {
        JCFieldAccess access = (JCFieldAccess) expressionTree;
        String methodName = access.sym.getQualifiedName().toString();
        return isFieldGetMethod(methodName);
      }
View Full Code Here

        return false;
      }
      if (returnsListMatcher.matches(method, state)) {
        return false;
      }
      ExpressionTree expressionTree = method.getMethodSelect();
      if (expressionTree instanceof JCFieldAccess) {
        JCFieldAccess access = (JCFieldAccess) expressionTree;
        String methodName = access.sym.getQualifiedName().toString();
        return isFieldGetMethod(methodName);
      }
View Full Code Here

   * proto.getList() != null  --> !proto.getList().isEmpty()
   * <pre>
   * Also creates replacements for the Yoda style version of them.
   */
  private static String createReplacement(BinaryTree tree, VisitorState state) {
    ExpressionTree leftOperand = tree.getLeftOperand();
    ExpressionTree rightOperand = tree.getRightOperand();
    ExpressionTree methodInvocation;
    if (isNull(leftOperand)) {
      methodInvocation = rightOperand;
    } else {
      methodInvocation = leftOperand;
    }
    if (isGetMethodInvocation(methodInvocation, state)) {
      String methodName = getMethodName(methodInvocation);
      String hasMethod = methodName.replaceFirst("get", "has");
      String replacement = replaceLast(methodInvocation.toString(), methodName, hasMethod);
      replacement = tree.getKind() == Kind.EQUAL_TO ? "!" + replacement : replacement;
      return replacement;
    } else {
      String replacement = methodInvocation + ".isEmpty()";
      return tree.getKind() == Kind.EQUAL_TO ? replacement : "!" + replacement;
View Full Code Here

        String formalParamName = formalParam.getName().toString();
        Type formalParamType = getType(formalParam.getType());

        Type availableParamType = availableParams.get(formalParamName);

        ExpressionTree actualParam = invocation.getArguments().get(i);

        if (
            /*
             * The caller has no param of this type. (Or if it did, we couldn't determine the type.
             * Does that ever happen?) If the param doesn't exist, the caller can't be failing to
View Full Code Here

TOP

Related Classes of com.sun.source.tree.ExpressionTree

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.