Example usage for org.eclipse.jdt.core.dom MethodInvocation resolveMethodBinding

List of usage examples for org.eclipse.jdt.core.dom MethodInvocation resolveMethodBinding

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom MethodInvocation resolveMethodBinding.

Prototype

public IMethodBinding resolveMethodBinding() 

Source Link

Document

Resolves and returns the binding for the method invoked by this expression.

Usage

From source file:astview.ASTViewContentProvider.java

License:Open Source License

private Object[] getNodeChildren(ASTNode node) {
    ArrayList<Object> res = new ArrayList<>();

    if (node instanceof Expression) {
        Expression expression = (Expression) node;
        ITypeBinding expressionTypeBinding = expression.resolveTypeBinding();
        res.add(createExpressionTypeBinding(node, expressionTypeBinding));

        // expressions:
        if (expression instanceof Name) {
            IBinding binding = ((Name) expression).resolveBinding();
            if (binding != expressionTypeBinding)
                res.add(createBinding(expression, binding));
        } else if (expression instanceof MethodInvocation) {
            MethodInvocation methodInvocation = (MethodInvocation) expression;
            IMethodBinding binding = methodInvocation.resolveMethodBinding();
            res.add(createBinding(expression, binding));
            String inferred = String.valueOf(methodInvocation.isResolvedTypeInferredFromExpectedType());
            res.add(new GeneralAttribute(expression, "ResolvedTypeInferredFromExpectedType", inferred)); //$NON-NLS-1$
        } else if (expression instanceof SuperMethodInvocation) {
            SuperMethodInvocation superMethodInvocation = (SuperMethodInvocation) expression;
            IMethodBinding binding = superMethodInvocation.resolveMethodBinding();
            res.add(createBinding(expression, binding));
            String inferred = String.valueOf(superMethodInvocation.isResolvedTypeInferredFromExpectedType());
            res.add(new GeneralAttribute(expression, "ResolvedTypeInferredFromExpectedType", inferred)); //$NON-NLS-1$
        } else if (expression instanceof ClassInstanceCreation) {
            ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation) expression;
            IMethodBinding binding = classInstanceCreation.resolveConstructorBinding();
            res.add(createBinding(expression, binding));
            String inferred = String.valueOf(classInstanceCreation.isResolvedTypeInferredFromExpectedType());
            res.add(new GeneralAttribute(expression, "ResolvedTypeInferredFromExpectedType", inferred)); //$NON-NLS-1$
        } else if (expression instanceof FieldAccess) {
            IVariableBinding binding = ((FieldAccess) expression).resolveFieldBinding();
            res.add(createBinding(expression, binding));
        } else if (expression instanceof SuperFieldAccess) {
            IVariableBinding binding = ((SuperFieldAccess) expression).resolveFieldBinding();
            res.add(createBinding(expression, binding));
        } else if (expression instanceof Annotation) {
            IAnnotationBinding binding = ((Annotation) expression).resolveAnnotationBinding();
            res.add(createBinding(expression, binding));
        } else if (expression instanceof LambdaExpression) {
            ASTAttribute bindingAttribute;
            try {
                IMethodBinding binding = ((LambdaExpression) expression).resolveMethodBinding();
                bindingAttribute = createBinding(expression, binding);
            } catch (RuntimeException e) {
                bindingAttribute = new Error(res, ">binding: Error: " + e.getMessage(), e);
            }//from  w  ww  .j  av  a 2s.  c o  m
            res.add(bindingAttribute);
        } else if (expression instanceof MethodReference) {
            IMethodBinding binding = ((MethodReference) expression).resolveMethodBinding();
            res.add(createBinding(expression, binding));
        }
        // Expression attributes:
        res.add(new GeneralAttribute(expression,
                "Boxing: " + expression.resolveBoxing() + "; Unboxing: " + expression.resolveUnboxing())); //$NON-NLS-1$ //$NON-NLS-2$
        res.add(new GeneralAttribute(expression, "ConstantExpressionValue", //$NON-NLS-1$
                expression.resolveConstantExpressionValue()));

        // references:
    } else if (node instanceof ConstructorInvocation) {
        IMethodBinding binding = ((ConstructorInvocation) node).resolveConstructorBinding();
        res.add(createBinding(node, binding));
    } else if (node instanceof SuperConstructorInvocation) {
        IMethodBinding binding = ((SuperConstructorInvocation) node).resolveConstructorBinding();
        res.add(createBinding(node, binding));
    } else if (node instanceof MethodRef) {
        IBinding binding = ((MethodRef) node).resolveBinding();
        res.add(createBinding(node, binding));
    } else if (node instanceof MemberRef) {
        IBinding binding = ((MemberRef) node).resolveBinding();
        res.add(createBinding(node, binding));
    } else if (node instanceof Type) {
        IBinding binding = ((Type) node).resolveBinding();
        res.add(createBinding(node, binding));

        // declarations:
    } else if (node instanceof AbstractTypeDeclaration) {
        IBinding binding = ((AbstractTypeDeclaration) node).resolveBinding();
        res.add(createBinding(node, binding));
    } else if (node instanceof AnnotationTypeMemberDeclaration) {
        IBinding binding = ((AnnotationTypeMemberDeclaration) node).resolveBinding();
        res.add(createBinding(node, binding));
    } else if (node instanceof EnumConstantDeclaration) {
        IBinding binding = ((EnumConstantDeclaration) node).resolveVariable();
        res.add(createBinding(node, binding));
        IBinding binding2 = ((EnumConstantDeclaration) node).resolveConstructorBinding();
        res.add(createBinding(node, binding2));
    } else if (node instanceof MethodDeclaration) {
        IBinding binding = ((MethodDeclaration) node).resolveBinding();
        res.add(createBinding(node, binding));
    } else if (node instanceof VariableDeclaration) {
        IBinding binding = ((VariableDeclaration) node).resolveBinding();
        res.add(createBinding(node, binding));
    } else if (node instanceof AnonymousClassDeclaration) {
        IBinding binding = ((AnonymousClassDeclaration) node).resolveBinding();
        res.add(createBinding(node, binding));
    } else if (node instanceof ImportDeclaration) {
        IBinding binding = ((ImportDeclaration) node).resolveBinding();
        res.add(createBinding(node, binding));
    } else if (node instanceof PackageDeclaration) {
        IBinding binding = ((PackageDeclaration) node).resolveBinding();
        res.add(createBinding(node, binding));
    } else if (node instanceof TypeParameter) {
        IBinding binding = ((TypeParameter) node).resolveBinding();
        res.add(createBinding(node, binding));
    } else if (node instanceof MemberValuePair) {
        IBinding binding = ((MemberValuePair) node).resolveMemberValuePairBinding();
        res.add(createBinding(node, binding));
    }

    @SuppressWarnings("unchecked")
    List<StructuralPropertyDescriptor> list = node.structuralPropertiesForType();
    for (int i = 0; i < list.size(); i++) {
        StructuralPropertyDescriptor curr = list.get(i);
        res.add(new NodeProperty(node, curr));
    }

    return res.toArray();
}

From source file:ca.mcgill.cs.swevo.ppa.PPAASTUtil.java

License:Open Source License

/**
 * //from  w  ww.  j  a  v  a2 s .c o m
 * @param mi
 * @return True if this is a final method from Object
 */
private static boolean checkObjectFinalMethod(MethodInvocation mi) {
    boolean isObjectFinal = false;
    IMethodBinding mb = mi.resolveMethodBinding();

    if (mb != null) {
        isObjectFinal = Modifier.isFinal(mb.getModifiers())
                && mb.getDeclaringClass().getQualifiedName().equals(OBJECT_FQN);
    }

    return isObjectFinal;
}

From source file:ca.mcgill.cs.swevo.ppa.ui.NameBindingVisitor.java

License:Open Source License

@Override
public void postVisit(ASTNode node) {
    super.postVisit(node);

    if (node instanceof Expression) {
        Expression exp = (Expression) node;

        IBinding binding = null;//from w  w w . j  av  a2  s  . co  m
        if (exp instanceof Name) {
            Name name = (Name) exp;
            binding = name.resolveBinding();
        } else if (exp instanceof MethodInvocation) {
            MethodInvocation mi = (MethodInvocation) exp;
            binding = mi.resolveMethodBinding();
        } else if (exp instanceof ClassInstanceCreation) {
            ClassInstanceCreation cic = (ClassInstanceCreation) exp;
            binding = cic.resolveConstructorBinding();
        } else {
            return;
        }

        printer.println("Node: " + node.toString());
        ITypeBinding tBinding = exp.resolveTypeBinding();
        if (tBinding != null) {
            printer.println("  Type Binding: " + tBinding.getQualifiedName());
            printer.println("  isAnnotation?: " + tBinding.isAnnotation());
        }

        if (binding != null) {
            printer.println("  " + PPABindingsUtil.getBindingText(binding));
        }
        printer.flush();
    }
    monitor.worked(1);
}

From source file:ca.uvic.chisel.diver.sequencediagrams.sc.java.model.ASTUTils.java

License:Open Source License

public static MethodDeclaration findDeclarationFor(MethodInvocation invocation) {
    IMethodBinding binding = invocation.resolveMethodBinding();
    if (binding != null) {
        IMethod method = (IMethod) binding.getJavaElement();
        IType declaringType = method.getDeclaringType();
        ASTNode typeNode = getASTFor(declaringType);
        if (typeNode != null) {
            return findMethodDeclaration(typeNode, method);
        }// w w w . jav  a2s. c  o m
    }
    return null;
}

From source file:ca.uvic.chisel.javasketch.internal.ast.ASTMessageFinder.java

License:Open Source License

@Override
public boolean visit(MethodInvocation node) {
    if (!(message instanceof ICall))
        return false;
    if (containsMessage(node)) {
        ICall call = (ICall) message;/*from w  w w. j av a  2s  . c o  m*/

        IMethodBinding binding = node.resolveMethodBinding();
        if (binding != null) {
            binding = binding.getMethodDeclaration();
            if (binding != null) {
                IJavaElement element = binding.getJavaElement();
                if (element instanceof IMethod) {
                    try {
                        IMethod jm = (IMethod) element;
                        //get the target method.
                        ITraceClassMethod am = call.getTarget().getActivation().getMethod();
                        String types[] = Signature.getParameterTypes(am.getSignature());
                        IMethod testMethod = jm.getDeclaringType().getMethod(am.getName(), types);
                        if (jm.isSimilar(testMethod)) {
                            this.node = node;
                            try {
                                if (document.getLineOfOffset(node.getStartPosition()) != (call.codeLine() - 1))
                                    //look for a better match.
                                    return true;
                            } catch (BadLocationException e) {
                            }
                            return false;
                        }
                    } catch (NullPointerException e) {
                        return false;
                    }
                }
            }
        }
        return true;
    }

    return false;
}

From source file:cc.kave.eclipse.namefactory.NodeFactory.java

License:Apache License

private static Name createMethodInvName(ASTNode node) {
    StringBuilder sb = new StringBuilder();
    MethodInvocation invocation = (MethodInvocation) node;
    IMethodBinding methodInvBinding = invocation.resolveMethodBinding();
    sb.append(methodNameHelper(null, methodInvBinding, true));
    return CsMethodName.newMethodName(sb.toString());
}

From source file:cc.kave.eclipse.namefactory.NodeFactory.java

License:Apache License

/**
 * //  w  w w . j  a va  2 s.c o  m
 * @param method
 *            Expects a MethodDeclaration, MethodInvocation or a
 *            SuperMethodInvocation node.
 * @return Returns an array of all parameterNames.
 */
protected static String[] createParameterNames(ASTNode method) {
    if (method instanceof MethodDeclaration) {
        MethodDeclaration methodDecl = (MethodDeclaration) method;
        return createParameterNames(methodDecl, methodDecl.resolveBinding());
    } else if (method instanceof MethodInvocation) {
        MethodInvocation methodInv = (MethodInvocation) method;
        return createParameterNames(null, methodInv.resolveMethodBinding());
    } else if (method instanceof SuperMethodInvocation) {
        SuperMethodInvocation superMethodInv = (SuperMethodInvocation) method;
        return createParameterNames(null, superMethodInv.resolveMethodBinding());
    }
    return null;
}

From source file:changetypes.ASTVisitorAtomicChange.java

License:Open Source License

public boolean visit(MethodInvocation node) {
    IMethodBinding mmtb = node.resolveMethodBinding();
    if (this.mtbStack.isEmpty()) {
        return true;
    }/*from w w w  . java 2s . co  m*/
    try {
        if (node.getExpression() != null) {
            if (mmtb.getDeclaringClass().getQualifiedName().startsWith("java.awt.geom.Path2D")) {
                Expression e = node.getExpression();
                ITypeBinding itb = e.resolveTypeBinding();
                this.facts.add(Fact.makeCallsFact(getQualifiedName((IMethodBinding) this.mtbStack.peek()),
                        getQualifiedName(itb) + "#" + getSimpleName(mmtb)));
                break label179;
            }
        }
        this.facts.add(Fact.makeCallsFact(getQualifiedName((IMethodBinding) this.mtbStack.peek()),
                getQualifiedName(mmtb)));
    } catch (Exception localException) {
        System.err.println("Cannot resolve method invocation \"" + node.getName().toString() + "\"");
    }
    label179: return true;
}

From source file:com.android.ide.eclipse.adt.internal.refactorings.extractstring.ReplaceStringsVisitor.java

License:Open Source License

/**
 * If the expression is part of a method invocation (aka a function call) or a
 * class instance creation (aka a "new SomeClass" constructor call), we try to
 * find the type of the argument being used. If it is a String (most likely), we
 * want to return true (to generate a getString() call). However if there might
 * be a similar method that takes an int, in which case we don't want to do that.
 *
 * This covers the case of Activity.setTitle(int resId) vs setTitle(String str).
 */// ww  w . j  a  v a2 s  .c o m
@SuppressWarnings("rawtypes")
private boolean examineMethodInvocation(StringLiteral node) {

    ASTNode parent = null;
    List arguments = null;
    IMethodBinding methodBinding = null;

    MethodInvocation invoke = findParentClass(node, MethodInvocation.class);
    if (invoke != null) {
        parent = invoke;
        arguments = invoke.arguments();
        methodBinding = invoke.resolveMethodBinding();
    } else {
        ClassInstanceCreation newclass = findParentClass(node, ClassInstanceCreation.class);
        if (newclass != null) {
            parent = newclass;
            arguments = newclass.arguments();
            methodBinding = newclass.resolveConstructorBinding();
        }
    }

    if (parent != null && arguments != null && methodBinding != null) {
        // We want to know which argument this is.
        // Walk up the hierarchy again to find the immediate child of the parent,
        // which should turn out to be one of the invocation arguments.
        ASTNode child = null;
        for (ASTNode n = node; n != parent;) {
            ASTNode p = n.getParent();
            if (p == parent) {
                child = n;
                break;
            }
            n = p;
        }
        if (child == null) {
            // This can't happen: a parent of 'node' must be the child of 'parent'.
            return false;
        }

        // Find the index
        int index = 0;
        for (Object arg : arguments) {
            if (arg == child) {
                break;
            }
            index++;
        }

        if (index == arguments.size()) {
            // This can't happen: one of the arguments of 'invoke' must be 'child'.
            return false;
        }

        // Eventually we want to determine if the parameter is a string type,
        // in which case a Context.getString() call must be generated.
        boolean useStringType = false;

        // Find the type of that argument
        ITypeBinding[] types = methodBinding.getParameterTypes();
        if (index < types.length) {
            ITypeBinding type = types[index];
            useStringType = isJavaString(type);
        }

        // Now that we know that this method takes a String parameter, can we find
        // a variant that would accept an int for the same parameter position?
        if (useStringType) {
            String name = methodBinding.getName();
            ITypeBinding clazz = methodBinding.getDeclaringClass();
            nextMethod: for (IMethodBinding mb2 : clazz.getDeclaredMethods()) {
                if (methodBinding == mb2 || !mb2.getName().equals(name)) {
                    continue;
                }
                // We found a method with the same name. We want the same parameters
                // except that the one at 'index' must be an int type.
                ITypeBinding[] types2 = mb2.getParameterTypes();
                int len2 = types2.length;
                if (types.length == len2) {
                    for (int i = 0; i < len2; i++) {
                        if (i == index) {
                            ITypeBinding type2 = types2[i];
                            if (!("int".equals(type2.getQualifiedName()))) { //$NON-NLS-1$
                                // The argument at 'index' is not an int.
                                continue nextMethod;
                            }
                        } else if (!types[i].equals(types2[i])) {
                            // One of the other arguments do not match our original method
                            continue nextMethod;
                        }
                    }
                    // If we got here, we found a perfect match: a method with the same
                    // arguments except the one at 'index' is an int. In this case we
                    // don't need to convert our R.id into a string.
                    useStringType = false;
                    break;
                }
            }
        }

        return useStringType;
    }
    return false;
}

From source file:com.android.ide.eclipse.auidt.internal.refactorings.extractstring.ReplaceStringsVisitor.java

License:Open Source License

/**
 * If the expression is part of a method invocation (aka a function call) or a
 * class instance creation (aka a "new SomeClass" constructor call), we try to
 * find the type of the argument being used. If it is a String (most likely), we
 * want to return true (to generate a getString() call). However if there might
 * be a similar method that takes an int, in which case we don't want to do that.
 *
 * This covers the case of Activity.setTitle(int resId) vs setTitle(String str).
 *///from w  w w.  j  ava  2 s  . c  om
@SuppressWarnings("unchecked")
private boolean examineMethodInvocation(StringLiteral node) {

    ASTNode parent = null;
    List arguments = null;
    IMethodBinding methodBinding = null;

    MethodInvocation invoke = findParentClass(node, MethodInvocation.class);
    if (invoke != null) {
        parent = invoke;
        arguments = invoke.arguments();
        methodBinding = invoke.resolveMethodBinding();
    } else {
        ClassInstanceCreation newclass = findParentClass(node, ClassInstanceCreation.class);
        if (newclass != null) {
            parent = newclass;
            arguments = newclass.arguments();
            methodBinding = newclass.resolveConstructorBinding();
        }
    }

    if (parent != null && arguments != null && methodBinding != null) {
        // We want to know which argument this is.
        // Walk up the hierarchy again to find the immediate child of the parent,
        // which should turn out to be one of the invocation arguments.
        ASTNode child = null;
        for (ASTNode n = node; n != parent;) {
            ASTNode p = n.getParent();
            if (p == parent) {
                child = n;
                break;
            }
            n = p;
        }
        if (child == null) {
            // This can't happen: a parent of 'node' must be the child of 'parent'.
            return false;
        }

        // Find the index
        int index = 0;
        for (Object arg : arguments) {
            if (arg == child) {
                break;
            }
            index++;
        }

        if (index == arguments.size()) {
            // This can't happen: one of the arguments of 'invoke' must be 'child'.
            return false;
        }

        // Eventually we want to determine if the parameter is a string type,
        // in which case a Context.getString() call must be generated.
        boolean useStringType = false;

        // Find the type of that argument
        ITypeBinding[] types = methodBinding.getParameterTypes();
        if (index < types.length) {
            ITypeBinding type = types[index];
            useStringType = isJavaString(type);
        }

        // Now that we know that this method takes a String parameter, can we find
        // a variant that would accept an int for the same parameter position?
        if (useStringType) {
            String name = methodBinding.getName();
            ITypeBinding clazz = methodBinding.getDeclaringClass();
            nextMethod: for (IMethodBinding mb2 : clazz.getDeclaredMethods()) {
                if (methodBinding == mb2 || !mb2.getName().equals(name)) {
                    continue;
                }
                // We found a method with the same name. We want the same parameters
                // except that the one at 'index' must be an int type.
                ITypeBinding[] types2 = mb2.getParameterTypes();
                int len2 = types2.length;
                if (types.length == len2) {
                    for (int i = 0; i < len2; i++) {
                        if (i == index) {
                            ITypeBinding type2 = types2[i];
                            if (!("int".equals(type2.getQualifiedName()))) { //$NON-NLS-1$
                                // The argument at 'index' is not an int.
                                continue nextMethod;
                            }
                        } else if (!types[i].equals(types2[i])) {
                            // One of the other arguments do not match our original method
                            continue nextMethod;
                        }
                    }
                    // If we got here, we found a perfect match: a method with the same
                    // arguments except the one at 'index' is an int. In this case we
                    // don't need to convert our R.id into a string.
                    useStringType = false;
                    break;
                }
            }
        }

        return useStringType;
    }
    return false;
}