Package com.sun.source.tree

Examples of com.sun.source.tree.ExpressionTree


    if (!matcher.matches(methodInvocationTree, state)) {
      return Description.NO_MATCH;
    }

    List<? extends ExpressionTree> arguments = methodInvocationTree.getArguments();
    ExpressionTree stringLiteralValue = arguments.get(0);
    Fix fix;
    if (arguments.size() == 2) {
      fix = SuggestedFix.swap(arguments.get(0), arguments.get(1));
    } else {
      fix = SuggestedFix.delete(state.getPath().getParentPath().getLeaf());
View Full Code Here


  protected final Fix getSuggestedFix(AnnotationTree annotationTree) {
    List<String> values = new ArrayList<>();
    for (ExpressionTree argumentTree : annotationTree.getArguments()) {
      AssignmentTree assignmentTree = (AssignmentTree) argumentTree;
      if (assignmentTree.getVariable().toString().equals("value")) {
        ExpressionTree expressionTree = assignmentTree.getExpression();
        switch (expressionTree.getKind()) {
          case STRING_LITERAL:
            values.add(((String) ((JCTree.JCLiteral) expressionTree).value));
            break;
          case NEW_ARRAY:
            NewArrayTree newArrayTree = (NewArrayTree) expressionTree;
            for (ExpressionTree elementTree : newArrayTree.getInitializers()) {
              values.add((String) ((JCTree.JCLiteral) elementTree).value);
            }
            break;
          default:
            throw new AssertionError("Unknown kind: " + expressionTree.getKind());
        }
        processSuppressWarningsValues(values);
      } else {
        throw new AssertionError("SuppressWarnings has an element other than value=");
      }
View Full Code Here

    // Could we reorder a final, unreferenced exception?
    if (parameters.getAllowExceptionReordering()
        && referencedArguments.size() < formatArguments.size()
        && max(referencedArguments) < formatArguments.size() - 1) {

      ExpressionTree last = formatArguments.get(formatArguments.size() - 1);

      if (isThrowable.matches(last, state)) {
        leadingArguments.add(last);
        formatArguments.remove(formatArguments.size() - 1);
        errors.add("ignores the passed exception");
      } else if (last instanceof MethodInvocationTree
          && isThrowableMessage.matches((MethodInvocationTree) last, state)) {
        ExpressionTree target = getInvocationTarget((MethodInvocationTree) last);
        if (target != null) {
          leadingArguments.add(target);
          formatArguments.remove(formatArguments.size() - 1);
          errors.add("ignores the passed exception message");
        }
View Full Code Here

  // Check whether this is call is made variadically or through a final Object array parameter
  private boolean isVariadicInvocation(
      MethodInvocationTree tree, VisitorState state, FormatParameters params) {
    List<? extends ExpressionTree> arguments = tree.getArguments();
    if (arguments.size() == params.getFormatIndex() + 2) {
      ExpressionTree lastArgument = arguments.get(arguments.size() - 1);
      return !isArrayType().matches(lastArgument, state)
          || isPrimitiveArrayType().matches(lastArgument, state);
    }
    return true;
  }
View Full Code Here

  /** Return the enum values handled by the given switch statement's cases. */
  private static LinkedHashSet<String> collectEnumSwitchCases(SwitchTree tree) {
    LinkedHashSet<String> cases = new LinkedHashSet<>();
    for (CaseTree caseTree : tree.getCases()) {
      ExpressionTree pat = caseTree.getExpression();
      if (pat instanceof IdentifierTree) {
        cases.add(((IdentifierTree) pat).getName().toString());
      }
    }
    return cases;
View Full Code Here

     * Throwables.getStackTraceAsString(e).
     * Otherwise, replaces a.toString() with Arrays.toString(a).
     */
    Fix fix;

    ExpressionTree receiverTree = ASTHelpers.getReceiver(methodTree);
    if (receiverTree instanceof MethodInvocationTree &&
        getStackTraceMatcher.matches((MethodInvocationTree) receiverTree, state)) {
      String throwable = ASTHelpers.getReceiver(receiverTree).toString();
      fix = SuggestedFix.builder()
          .replace(methodTree, "Throwables.getStackTraceAsString(" + throwable + ")")
View Full Code Here

  public Description describe(MethodInvocationTree methodInvocationTree, VisitorState state) {

    // the statement that is the parent of the self-assignment expression
    Tree parent = state.getPath().getParentPath().getLeaf();

    ExpressionTree lhs = ASTHelpers.getReceiver(methodInvocationTree);
    ExpressionTree rhs = methodInvocationTree.getArguments().get(0);
   
    // default fix for methods
    Fix fix = SuggestedFix.delete(parent);
    if (methodSelect(instanceMethod(Matchers.<ExpressionTree>anything(), "removeAll"))
        .matches(methodInvocationTree, state)) {
      fix = SuggestedFix.replace(methodInvocationTree, lhs + ".clear()");
    }

    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 type "Collection"
      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 (variableType(isSubtypeOf("java.util.Collection")).matches(var, state)) {
          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

                + "identifier");
        }
      }

      // Choose argument to replace.
      ExpressionTree toReplace;
      if (args.get(1).getKind() == Kind.IDENTIFIER) {
        toReplace = args.get(1);
      } else if (args.get(0).getKind() == Kind.IDENTIFIER) {
        toReplace = args.get(0);
      } else {
        // If we don't have a good reason to replace one or the other, replace the second.
        toReplace = args.get(1);
      }
      // Find containing block
      TreePath path = state.getPath();
      while(path.getLeaf().getKind() != Kind.BLOCK) {
        path = path.getParentPath();
      }
      JCBlock block = (JCBlock)path.getLeaf();
      for (JCStatement jcStatement : block.getStatements()) {
        if (jcStatement.getKind() == Kind.VARIABLE) {
          JCVariableDecl declaration = (JCVariableDecl) jcStatement;
          TypeSymbol variableTypeSymbol = declaration.getType().type.tsym;

          if (ASTHelpers.getSymbol(toReplace).isMemberOf(variableTypeSymbol, state.getTypes())) {
            if (toReplace.getKind() == Kind.IDENTIFIER) {
              fix = SuggestedFix.prefixWith(toReplace, declaration.getName() + ".");
            } else {
              fix = SuggestedFix.replace(((JCFieldAccess) toReplace).getExpression(),
                  declaration.getName().toString());
            }
View Full Code Here

                                }
                                methodClassType = ElementUtilities.getBinaryName(te);
                            } else {
                                methodName = ((MemberSelectTree) identifier).getIdentifier().toString();
                                getStartPosFromMethodLength = true;
                                ExpressionTree exp = ((MemberSelectTree) identifier).getExpression();
                                TreePath expPath = TreePath.getPath(cu, exp);
                                TypeMirror type = trees.getTypeMirror(expPath);
                                if (type.getKind() == TypeKind.ERROR) {
                                    // There are errors, give it up.
                                    return null;
View Full Code Here

            }
        }

        if (instanceFound) {
            VariableTree tree = (VariableTree) trees.getTree(instanceElement);
            ExpressionTree initializer = tree.getInitializer();
            if (initializer == null) {
                lazy = true;
            } else {
                lazy = false; //default
            }
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.