Example usage for org.eclipse.jdt.core.dom ClassInstanceCreation resolveConstructorBinding

List of usage examples for org.eclipse.jdt.core.dom ClassInstanceCreation resolveConstructorBinding

Introduction

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

Prototype

public IMethodBinding resolveConstructorBinding() 

Source Link

Document

Resolves and returns the binding for the constructor 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);
            }/* w ww  . j av  a  2 s. c om*/
            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.jayfx.ASTCrawler.java

License:Open Source License

@Override
public boolean visit(final ClassInstanceCreation pNode) {
    if (ASTCrawler.checkForNull(this.aCurrMethod))
        return false;

    final IMethodBinding lCBinding = pNode.resolveConstructorBinding();
    final ITypeBinding lTBinding = pNode.resolveTypeBinding();

    if (ASTCrawler.checkForNull(lCBinding))
        return false;
    if (ASTCrawler.checkForNull(lTBinding))
        return false;

    MethodElement lConstructor = null;//from  www . ja va  2  s. c  om

    if (lTBinding.isAnonymous()) {
        final IElement lDeclaringClass = ASTCrawler.convertBinding(lTBinding);
        // TODO HACK A bug in Eclipse occasionally causes binary names to
        // crap out.
        if (lDeclaringClass == null || lDeclaringClass.getId() == null)
            return false;

        lConstructor = (MethodElement) FlyweightElementFactory.getElement(Category.METHOD,
                lDeclaringClass.getId() + "." + ASTCrawler.aINIT_METHOD_NAME);
        this.aDB.addElement(lConstructor, ASTCrawler.aINIT_METHOD_MODIFIERS);
    } else
        lConstructor = (MethodElement) ASTCrawler.convertBinding(lCBinding);

    final IElement lClass = lConstructor.getDeclaringClass();

    // Register CALLS relationship to constructor
    this.addCallRelation(pNode, lCBinding, true);

    try {
        this.aDB.contains(lClass);
    } catch (final RuntimeException pException) {
        System.out.println(lClass.getId());
        System.out.println(lConstructor.getId());
        throw pException;
    }

    if (!this.aDB.contains(lClass)) {
        final ITypeBinding lType = lCBinding.getDeclaringClass();
        this.aDB.addElement(lClass, lType.getModifiers());
    }

    // Register CREATES relationship
    this.aDB.addRelationAndTranspose(this.aCurrMethod, Relation.CREATES, lClass);

    return true;
}

From source file:ca.mcgill.cs.swevo.ppa.inference.ConstructorInferenceStrategy.java

License:Open Source License

public boolean isSafe(ASTNode node) {
    boolean isSafe = hasDeclaration(node);

    if (!isSafe) {
        ClassInstanceCreation cic = (ClassInstanceCreation) node;
        IMethodBinding mBinding = (IMethodBinding) cic.resolveConstructorBinding();
        if (mBinding == null) {
            System.out.println("Hello!");
            cic.resolveConstructorBinding();
        }//from   ww w  .  j a v  a2  s  . c  om
        ITypeBinding container = mBinding.getDeclaringClass();
        ITypeBinding returnType = mBinding.getReturnType();
        isSafe = PPABindingsUtil.getSafetyValue(container) == PPABindingsUtil.FULL_TYPE
                && PPABindingsUtil.getSafetyValue(returnType) > PPABindingsUtil.UNKNOWN_TYPE;
    }

    return isSafe;
}

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;/* w  ww.j  a va 2s.c  om*/
        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.javasketch.internal.ast.ASTMessageFinder.java

License:Open Source License

public boolean visit(ClassInstanceCreation node) {
    if (!(message instanceof ICall))
        return false;
    if (containsMessage(node)) {
        ICall call = (ICall) message;//from w ww . j  av  a  2s.com
        IMethodBinding binding = node.resolveConstructorBinding();
        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();
                        if (JavaSearchUtils.getFullyQualifiedName(jm.getDeclaringType(), true)
                                .equals(am.getTraceClass().getName())) {
                            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 true;
                    }
                } else {
                    //try to match just on the class name
                    ITypeBinding typeBinding = binding.getDeclaringClass();
                    IJavaElement je = typeBinding.getJavaElement();
                    if (je instanceof IType) {
                        IType type = (IType) je;
                        try {
                            ITraceClassMethod am = call.getTarget().getActivation().getMethod();
                            if (JavaSearchUtils.getFullyQualifiedName(type, true)
                                    .equals(am.getTraceClass().getName())) {
                                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 true;
                        }
                    }
                }
            }
        }
        return true;
    }

    return false;
}

From source file:changetypes.ASTVisitorAtomicChange.java

License:Open Source License

public boolean visit(ClassInstanceCreation node) {
    IMethodBinding mmtb = node.resolveConstructorBinding();
    if (this.mtbStack.isEmpty()) {
        return true;
    }//from ww w.  ja  v a 2s .c  om
    try {
        this.facts.add(Fact.makeCallsFact(getQualifiedName((IMethodBinding) this.mtbStack.peek()),
                getQualifiedName(mmtb)));
    } catch (Exception localException) {
        System.err.println("Cannot resolve class instance creation \"" + node.getType().toString() + "\"");
    }
    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).
 *//*  w w w.  j a  va 2s  . 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).
 *//*  w  w  w .  j av a  2 s  .  c  o m*/
@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;
}

From source file:com.architexa.diagrams.jdt.extractors.CallHeirarchyExtractor.java

License:Open Source License

@Override
public boolean visit(ClassInstanceCreation invokedConstructor) {
    Resource invokedMethodRes = bindingToResource(invokedConstructor.resolveConstructorBinding());
    rdfModel.addStatement(getCallingRes(this), RJCore.calls, invokedMethodRes);
    return true;/*from   w ww .j av  a  2 s  . co m*/
}

From source file:com.codenvy.ide.ext.java.server.JavadocFinder.java

License:Open Source License

private static IBinding resolveBinding(ASTNode node) {
    if (node instanceof SimpleName) {
        SimpleName simpleName = (SimpleName) node;
        // workaround for https://bugs.eclipse.org/62605 (constructor name resolves to type, not method)
        ASTNode normalized = ASTNodes.getNormalizedNode(simpleName);
        if (normalized.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) {
            ClassInstanceCreation cic = (ClassInstanceCreation) normalized.getParent();
            IMethodBinding constructorBinding = cic.resolveConstructorBinding();
            if (constructorBinding == null)
                return null;
            ITypeBinding declaringClass = constructorBinding.getDeclaringClass();
            if (!declaringClass.isAnonymous())
                return constructorBinding;
            ITypeBinding superTypeDeclaration = declaringClass.getSuperclass().getTypeDeclaration();
            return resolveSuperclassConstructor(superTypeDeclaration, constructorBinding);
        }//  w  w  w. jav  a 2 s . c om
        return simpleName.resolveBinding();

    } else if (node instanceof SuperConstructorInvocation) {
        return ((SuperConstructorInvocation) node).resolveConstructorBinding();
    } else if (node instanceof ConstructorInvocation) {
        return ((ConstructorInvocation) node).resolveConstructorBinding();
    } else {
        return null;
    }
}