下面列出了com.sun.source.tree.MethodInvocationTree#getMethodSelect ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
@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);
}
@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;
}
@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;
}
/**
* 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);
}
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;
}
/**
* 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();
}
/** 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();
}
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;
}
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;
}
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;
}
@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);
}
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;
}
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.
}
/**
* 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();
}
private ExpressionTree getMethodReceiver(MethodInvocationTree methodInvocation) {
ExpressionTree select = methodInvocation.getMethodSelect();
return select instanceof MemberSelectTree ? ((MemberSelectTree) select).getExpression() : null;
}
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;
}
private ExpressionTree getMethodReceiver(MethodInvocationTree methodInvocation) {
ExpressionTree select = methodInvocation.getMethodSelect();
return select instanceof MemberSelectTree ? ((MemberSelectTree) select).getExpression() : null;
}
/** 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;
}
/**
* 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;
}