Java Code Examples for com.sun.source.tree.MethodInvocationTree#getMethodSelect()

The following examples show how to use com.sun.source.tree.MethodInvocationTree#getMethodSelect() . 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: OverridableMethodCallInConstructor.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static boolean invocationOnThis(MethodInvocationTree mit) {
    Tree select = mit.getMethodSelect();
    
    switch (select.getKind()) {
        case IDENTIFIER:
            return true;
        case MEMBER_SELECT:
            if (((MemberSelectTree) select).getExpression().getKind() == Kind.IDENTIFIER) {
                IdentifierTree ident = (IdentifierTree) ((MemberSelectTree) select).getExpression();

                return ident.getName().contentEquals("this");
            }
    }

    return false;
}
 
Example 2
Source File: XPFlagCleaner.java    From piranha with Apache License 2.0 6 votes vote down vote up
private API getXPAPI(ExpressionTree et) {
  et = ASTHelpers.stripParentheses(et);
  Kind k = et.getKind();
  if (k.equals(Tree.Kind.METHOD_INVOCATION)) {
    MethodInvocationTree mit = (MethodInvocationTree) et;
    if (!mit.getMethodSelect().getKind().equals(Kind.MEMBER_SELECT)) {
      return API.UNKNOWN;
    }
    MemberSelectTree mst = (MemberSelectTree) mit.getMethodSelect();
    String methodName = mst.getIdentifier().toString();
    if (!disabled && configMethodProperties.containsKey(methodName)) {
      return getXPAPI(mit, configMethodProperties.get(methodName));
    }
  }
  return API.UNKNOWN;
}
 
Example 3
Source File: Utilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Attempts to resolve a method or a constructor call with an altered argument tree.
 * 
 * @param ci the context
 * @param invPath path to the method invocation node
 * @param origPath path to the Tree within method's arguments which should be replaced
 * @param valPath the replacement tree
 * @return 
 */
public static boolean checkAlternativeInvocation(CompilationInfo ci, TreePath invPath, 
        TreePath origPath,
        TreePath valPath, String customPrefix) {
    Tree l = invPath.getLeaf();
    Tree sel;
    
    if (l.getKind() == Tree.Kind.NEW_CLASS) {
        NewClassTree nct = (NewClassTree)invPath.getLeaf();
        sel = nct.getIdentifier();
    } else if (l.getKind() == Tree.Kind.METHOD_INVOCATION) {
        MethodInvocationTree mit = (MethodInvocationTree)invPath.getLeaf();
        sel = mit.getMethodSelect();
    } else {
        return false;
    }
    
    return resolveAlternativeInvocation(ci, invPath, 
            origPath, sel, valPath, customPrefix);
}
 
Example 4
Source File: RxJavaMissingAutodisposeErrorChecker.java    From RIBs with Apache License 2.0 6 votes vote down vote up
@Override
public boolean matches(ExpressionTree tree, VisitorState state) {
  if (!(tree instanceof MethodInvocationTree)) {
    return false;
  }
  MethodInvocationTree invTree = (MethodInvocationTree) tree;

  final MemberSelectTree memberTree = (MemberSelectTree) invTree.getMethodSelect();
  if (!memberTree.getIdentifier().contentEquals(TO)) {
    return false;
  }

  for (MethodMatchers.MethodNameMatcher nameMatcher : METHOD_NAME_MATCHERS) {
    if (nameMatcher.matches(invTree, state)) {
      ExpressionTree arg = invTree.getArguments().get(0);
      final Type scoper = state.getTypeFromString("com.uber.autodispose.Scoper");
      return ASTHelpers.isSubtype(ASTHelpers.getType(arg), scoper, state);
    }
  }
  return false;
}
 
Example 5
Source File: FinalizeDoesNotCallSuper.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public Void visitMethodInvocation(MethodInvocationTree node, Void p) {
    if (!node.getArguments().isEmpty()) {
        return null;
    }
    final ExpressionTree et = node.getMethodSelect();
    if (et.getKind() != Tree.Kind.MEMBER_SELECT) {
        return null;
    }
    final MemberSelectTree mst = (MemberSelectTree) et;
    if (!FINALIZE.contentEquals(mst.getIdentifier())) {
        return null;
    }
    if (mst.getExpression().getKind() != Tree.Kind.IDENTIFIER) {
        return null;
    }
    if (!SUPER.contentEquals(((IdentifierTree)mst.getExpression()).getName())) {
        return null;
    }
    found = true;
    return null;
}
 
Example 6
Source File: Tiny.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
protected void performRewrite(JavaFix.TransformationContext ctx) throws Exception {
    Tree t = ctx.getPath().getLeaf();
    if (t.getKind() != Tree.Kind.METHOD_INVOCATION) {
        return;
    }
    MethodInvocationTree mi = (MethodInvocationTree)t;
    if (mi.getMethodSelect().getKind() != Tree.Kind.MEMBER_SELECT) {
        return;
    }
    MemberSelectTree selector = ((MemberSelectTree)mi.getMethodSelect());
    TreeMaker maker = ctx.getWorkingCopy().getTreeMaker();
    ExpressionTree ms = maker.MemberSelect(maker.QualIdent("java.util.Arrays"), deep ? "deepHashCode" : "hashCode"); // NOI18N
    Tree nue = maker.MethodInvocation(
                    Collections.<ExpressionTree>emptyList(), 
                    ms, 
                    Collections.singletonList(selector.getExpression())
    );
    ctx.getWorkingCopy().rewrite(t, nue);
}
 
Example 7
Source File: Trees.java    From google-java-format with Apache License 2.0 5 votes vote down vote up
/** Returns the simple name of a (possibly qualified) method invocation expression. */
static Name getMethodName(MethodInvocationTree methodInvocation) {
  ExpressionTree select = methodInvocation.getMethodSelect();
  return select instanceof MemberSelectTree
      ? ((MemberSelectTree) select).getIdentifier()
      : ((IdentifierTree) select).getName();
}
 
Example 8
Source File: Trees.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns the simple name of a (possibly qualified) method invocation expression.
 */
static Name getMethodName(MethodInvocationTree methodInvocation) {
    ExpressionTree select = methodInvocation.getMethodSelect();
    return select instanceof MemberSelectTree
            ? ((MemberSelectTree) select).getIdentifier()
            : ((IdentifierTree) select).getName();
}
 
Example 9
Source File: StreamNullabilityPropagator.java    From NullAway with MIT License 5 votes vote down vote up
private void buildObservableCallChain(MethodInvocationTree tree) {
  ExpressionTree methodSelect = tree.getMethodSelect();
  if (methodSelect instanceof MemberSelectTree) {
    ExpressionTree receiverExpression = ((MemberSelectTree) methodSelect).getExpression();
    if (receiverExpression instanceof MethodInvocationTree) {
      observableOuterCallInChain.put((MethodInvocationTree) receiverExpression, tree);
    }
  } // ToDo: What else can be here? If there are other cases than MemberSelectTree, handle them.
}
 
Example 10
Source File: JavaInputAstVisitor.java    From google-java-format with Apache License 2.0 5 votes vote down vote up
private boolean fillFirstArgument(ExpressionTree e, List<ExpressionTree> items, Indent indent) {
  // is there a trailing dereference?
  if (items.size() < 2) {
    return false;
  }
  // don't special-case calls nested inside expressions
  if (e.getKind() != METHOD_INVOCATION) {
    return false;
  }
  MethodInvocationTree methodInvocation = (MethodInvocationTree) e;
  Name name = getMethodName(methodInvocation);
  if (!(methodInvocation.getMethodSelect() instanceof IdentifierTree)
      || name.length() > 4
      || !methodInvocation.getTypeArguments().isEmpty()
      || methodInvocation.getArguments().size() != 1) {
    return false;
  }
  builder.open(ZERO);
  builder.open(indent);
  visit(name);
  token("(");
  ExpressionTree arg = getOnlyElement(methodInvocation.getArguments());
  scan(arg, null);
  builder.close();
  token(")");
  builder.close();
  return true;
}
 
Example 11
Source File: ConvertToLambdaPreconditionChecker.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Tree visitMethodInvocation(MethodInvocationTree methodInvocationTree, Trees trees) {
    String nameSuggestion = org.netbeans.modules.editor.java.Utilities.varNameSuggestion(methodInvocationTree.getMethodSelect());
    //check for recursion
    if (nameSuggestion != null && lambdaMethodTree.getName().contentEquals(nameSuggestion)) {
        ExpressionTree selector = getSelector(methodInvocationTree);
        if (selector == null || (org.netbeans.modules.editor.java.Utilities.varNameSuggestion(selector) != null
                && org.netbeans.modules.editor.java.Utilities.varNameSuggestion(selector).contentEquals("this"))) {
            foundRecursiveCall = true;
        }
    }
    if (singleStatementLambdaMethodBody == getCurrentPath().getParentPath().getParentPath().getLeaf()) {
        Tree parent = getCurrentPath().getParentPath().getLeaf();
        if (parent.getKind() == Tree.Kind.EXPRESSION_STATEMENT || parent.getKind() == Tree.Kind.RETURN) {
            boolean check = true;
            Iterator<? extends VariableTree> paramsIt = lambdaMethodTree.getParameters().iterator();
            ExpressionTree methodSelect = methodInvocationTree.getMethodSelect();
            if (paramsIt.hasNext() && methodSelect.getKind() == Tree.Kind.MEMBER_SELECT) {
                ExpressionTree expr = ((MemberSelectTree) methodSelect).getExpression();
                if (expr.getKind() == Tree.Kind.IDENTIFIER) {
                    if (!((IdentifierTree)expr).getName().contentEquals(paramsIt.next().getName())) {
                        paramsIt = lambdaMethodTree.getParameters().iterator();
                    }
                }
            }
            Iterator<? extends ExpressionTree> argsIt = methodInvocationTree.getArguments().iterator();
            while (check && argsIt.hasNext() && paramsIt.hasNext()) {
                ExpressionTree arg = argsIt.next();
                if (arg.getKind() != Tree.Kind.IDENTIFIER || !paramsIt.next().getName().contentEquals(((IdentifierTree)arg).getName())) {
                    check = false;
                }
            }
            if (check && !paramsIt.hasNext() && !argsIt.hasNext()) {
                foundMemberReferenceCandidate = true;
            }
        }
    }
    return super.visitMethodInvocation(methodInvocationTree, trees);
}
 
Example 12
Source File: Utilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static boolean isSynthetic(CompilationUnitTree cut, Tree leaf) throws NullPointerException {
    JCTree tree = (JCTree) leaf;

    if (tree.pos == (-1))
        return true;

    if (leaf.getKind() == Kind.METHOD) {
        //check for synthetic constructor:
        return (((JCMethodDecl)leaf).mods.flags & Flags.GENERATEDCONSTR) != 0L;
    }

    //check for synthetic superconstructor call:
    if (cut != null && leaf.getKind() == Kind.EXPRESSION_STATEMENT) {
        ExpressionStatementTree est = (ExpressionStatementTree) leaf;

        if (est.getExpression().getKind() == Kind.METHOD_INVOCATION) {
            MethodInvocationTree mit = (MethodInvocationTree) est.getExpression();

            if (mit.getMethodSelect().getKind() == Kind.IDENTIFIER) {
                IdentifierTree it = (IdentifierTree) mit.getMethodSelect();

                if ("super".equals(it.getName().toString())) {
                    return ((JCCompilationUnit) cut).endPositions.getEndPos(tree) == (-1);
                }
            }
        }
    }

    return false;
}
 
Example 13
Source File: JavaInputAstVisitor.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private boolean fillFirstArgument(
        ExpressionTree e,
        List<ExpressionTree> items,
        Indent indent) {
    // is there a trailing dereference?
    if (items.size() < 2) {
        return false;
    }
    // don't special-case calls nested inside expressions
    if (e.getKind() != METHOD_INVOCATION) {
        return false;
    }
    MethodInvocationTree methodInvocation = (MethodInvocationTree) e;
    Name name = getMethodName(methodInvocation);
    if (!(methodInvocation.getMethodSelect() instanceof IdentifierTree)
            || name.length() > 4
            || !methodInvocation.getTypeArguments().isEmpty()
            || methodInvocation.getArguments().size() != 1) {
        return false;
    }
    builder.open(ZERO);
    builder.open(indent);
    visit(name);
    token("(");
    ExpressionTree arg = getOnlyElement(methodInvocation.getArguments());
    scan(arg, null);
    builder.close();
    token(")");
    builder.close();
    return true;
}
 
Example 14
Source File: JavaInputAstVisitor.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
private boolean fillFirstArgument(
        ExpressionTree e,
        List<ExpressionTree> items,
        Indent indent) {
    // is there a trailing dereference?
    if (items.size() < 2) {
        return false;
    }
    // don't special-case calls nested inside expressions
    if (e.getKind() != METHOD_INVOCATION) {
        return false;
    }
    MethodInvocationTree methodInvocation = (MethodInvocationTree) e;
    Name name = getMethodName(methodInvocation);
    if (!(methodInvocation.getMethodSelect() instanceof IdentifierTree)
            || name.length() > 4
            || !methodInvocation.getTypeArguments().isEmpty()
            || methodInvocation.getArguments().size() != 1) {
        return false;
    }
    builder.open(ZERO);
    builder.open(indent);
    visit(name);
    token("(");
    ExpressionTree arg = getOnlyElement(methodInvocation.getArguments());
    scan(arg, null);
    builder.close();
    token(")");
    builder.close();
    return true;
}
 
Example 15
Source File: Trees.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the simple name of a (possibly qualified) method invocation expression.
 */
static Name getMethodName(MethodInvocationTree methodInvocation) {
    ExpressionTree select = methodInvocation.getMethodSelect();
    return select instanceof MemberSelectTree
            ? ((MemberSelectTree) select).getIdentifier()
            : ((IdentifierTree) select).getName();
}
 
Example 16
Source File: XPFlagCleaner.java    From piranha with Apache License 2.0 4 votes vote down vote up
private API getXPAPI(
    MethodInvocationTree mit, ImmutableCollection<PiranhaMethodRecord> methodRecordsForName) {
  for (PiranhaMethodRecord methodRecord : methodRecordsForName) {
    // when argumentIndex is specified, if mit's argument at argIndex doesn't match xpFlagName,
    // skip to next method property map
    Optional<Integer> optionalArgumentIdx = methodRecord.getArgumentIdx();
    if (optionalArgumentIdx.isPresent()) {
      int argumentIndex = optionalArgumentIdx.get().intValue();
      if (argumentIndex < mit.getArguments().size()) {
        ExpressionTree argTree = mit.getArguments().get(argumentIndex);
        Symbol argSym = ASTHelpers.getSymbol(argTree);
        if (!isArgumentMatchesFlagName(argTree, argSym)) {
          continue;
        }
      } else {
        continue;
      }
    }
    MemberSelectTree mst = ((MemberSelectTree) mit.getMethodSelect());
    // when returnType is specified, check if mst's return type matches it
    // if it's not a match, skip to next method property map
    Optional<String> optionalReturnType = methodRecord.getReturnType();
    if (optionalReturnType.isPresent()) {
      String mReturn = ASTHelpers.getReturnType(mst).toString();
      if (!optionalReturnType.get().equals(mReturn)) {
        continue;
      }
    }
    // when receiverType is specified, check if mst's receiver type matches it
    // if it's not a match, skip to next method property map
    Optional<String> optionalReceiverType = methodRecord.getReceiverType();
    if (optionalReceiverType.isPresent()) {
      String mReceive = ASTHelpers.getReceiverType(mst).toString();
      if (!optionalReceiverType.get().equals(mReceive)) {
        continue;
      }
    }
    // The record matches the checks so far, so return its API type as the type of mit
    return methodRecord.getApiType();
  }
  return API.UNKNOWN;
}
 
Example 17
Source File: JavaInputAstVisitor.java    From google-java-format with Apache License 2.0 4 votes vote down vote up
private ExpressionTree getMethodReceiver(MethodInvocationTree methodInvocation) {
  ExpressionTree select = methodInvocation.getMethodSelect();
  return select instanceof MemberSelectTree ? ((MemberSelectTree) select).getExpression() : null;
}
 
Example 18
Source File: Trees.java    From google-java-format with Apache License 2.0 4 votes vote down vote up
/** Returns the receiver of a qualified method invocation expression, or {@code null}. */
static ExpressionTree getMethodReceiver(MethodInvocationTree methodInvocation) {
  ExpressionTree select = methodInvocation.getMethodSelect();
  return select instanceof MemberSelectTree ? ((MemberSelectTree) select).getExpression() : null;
}
 
Example 19
Source File: JavaInputAstVisitor.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
private ExpressionTree getMethodReceiver(MethodInvocationTree methodInvocation) {
    ExpressionTree select = methodInvocation.getMethodSelect();
    return select instanceof MemberSelectTree ? ((MemberSelectTree) select).getExpression() : null;
}
 
Example 20
Source File: Trees.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Returns the receiver of a qualified method invocation expression, or {@code null}.
 */
static ExpressionTree getMethodReceiver(MethodInvocationTree methodInvocation) {
    ExpressionTree select = methodInvocation.getMethodSelect();
    return select instanceof MemberSelectTree ? ((MemberSelectTree) select).getExpression() : null;
}