Search in sources :

Example 1 with TypeBindingVisitor

use of org.eclipse.jdt.internal.corext.dom.TypeBindingVisitor in project che by eclipse.

the class ASTResolving method getQualifierGuess.

/**
   	 * Finds all type bindings that contain a method of a given signature
   	 * @param searchRoot the ast node to start the search from
   	 * @param selector the method name
   	 * @param arguments the method arguments
   	 * @param context the context in which the method would be called
   	 * @return returns all types known in the AST that have a method with a given name
   	 */
public static ITypeBinding[] getQualifierGuess(ASTNode searchRoot, final String selector, List<Expression> arguments, final IBinding context) {
    final int nArgs = arguments.size();
    final ArrayList<ITypeBinding> result = new ArrayList<ITypeBinding>();
    // test if selector is a object method
    //$NON-NLS-1$
    ITypeBinding binding = searchRoot.getAST().resolveWellKnownType("java.lang.Object");
    IMethodBinding[] objectMethods = binding.getDeclaredMethods();
    for (int i = 0; i < objectMethods.length; i++) {
        IMethodBinding meth = objectMethods[i];
        if (meth.getName().equals(selector) && meth.getParameterTypes().length == nArgs) {
            return new ITypeBinding[] { binding };
        }
    }
    visitAllBindings(searchRoot, new TypeBindingVisitor() {

        private HashSet<String> fVisitedBindings = new HashSet<String>(100);

        public boolean visit(ITypeBinding node) {
            node = Bindings.normalizeTypeBinding(node);
            if (node == null) {
                return true;
            }
            if (!fVisitedBindings.add(node.getKey())) {
                return true;
            }
            if (node.isGenericType()) {
                // only look at  parameterized types
                return true;
            }
            if (context != null && !isUseableTypeInContext(node, context, false)) {
                return true;
            }
            IMethodBinding[] methods = node.getDeclaredMethods();
            for (int i = 0; i < methods.length; i++) {
                IMethodBinding meth = methods[i];
                if (meth.getName().equals(selector) && meth.getParameterTypes().length == nArgs) {
                    result.add(node);
                }
            }
            return true;
        }
    });
    return result.toArray(new ITypeBinding[result.size()]);
}
Also used : ArrayList(java.util.ArrayList) TypeBindingVisitor(org.eclipse.jdt.internal.corext.dom.TypeBindingVisitor) HashSet(java.util.HashSet)

Example 2 with TypeBindingVisitor

use of org.eclipse.jdt.internal.corext.dom.TypeBindingVisitor in project che by eclipse.

the class CallInliner method needsExplicitCast.

/**
	 * @param status the status
	 * @return <code>true</code> if explicit cast is needed otherwise <code>false</code>
	 */
private boolean needsExplicitCast(RefactoringStatus status) {
    // returned expression then we don't need an explicit cast.
    if (fSourceProvider.returnTypeMatchesReturnExpressions())
        return false;
    List<Expression> returnExprs = fSourceProvider.getReturnExpressions();
    // method invocations
    if (returnExprs.size() != 1)
        return false;
    if (fTargetNode.getLocationInParent() == MethodInvocation.ARGUMENTS_PROPERTY) {
        MethodInvocation methodInvocation = (MethodInvocation) fTargetNode.getParent();
        if (methodInvocation.getExpression() == fTargetNode)
            return false;
        IMethodBinding method = methodInvocation.resolveMethodBinding();
        if (method == null) {
            status.addError(RefactoringCoreMessages.CallInliner_cast_analysis_error, JavaStatusContext.create(fCUnit, methodInvocation));
            return false;
        }
        ITypeBinding[] parameters = method.getParameterTypes();
        int argumentIndex = methodInvocation.arguments().indexOf(fInvocation);
        ITypeBinding parameterType = returnExprs.get(0).resolveTypeBinding();
        if (method.isVarargs() && argumentIndex >= parameters.length - 1) {
            argumentIndex = parameters.length - 1;
            parameterType = parameterType.createArrayType(1);
        }
        parameters[argumentIndex] = parameterType;
        ITypeBinding type = ASTNodes.getReceiverTypeBinding(methodInvocation);
        TypeBindingVisitor visitor = new AmbiguousMethodAnalyzer(fTypeEnvironment, method, fTypeEnvironment.create(parameters));
        if (!visitor.visit(type)) {
            return true;
        } else if (type.isInterface()) {
            return !Bindings.visitInterfaces(type, visitor);
        } else if (Modifier.isAbstract(type.getModifiers())) {
            return !Bindings.visitHierarchy(type, visitor);
        } else {
            // it is not needed to visit interfaces if receiver is a concrete class
            return !Bindings.visitSuperclasses(type, visitor);
        }
    } else {
        ITypeBinding explicitCast = ASTNodes.getExplicitCast(returnExprs.get(0), (Expression) fTargetNode);
        return explicitCast != null;
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) TypeBindingVisitor(org.eclipse.jdt.internal.corext.dom.TypeBindingVisitor) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 3 with TypeBindingVisitor

use of org.eclipse.jdt.internal.corext.dom.TypeBindingVisitor in project flux by eclipse.

the class ASTResolving method getQualifierGuess.

/**
   	 * Finds all type bindings that contain a method of a given signature
   	 * @param searchRoot the ast node to start the search from
   	 * @param selector the method name
   	 * @param arguments the method arguments
   	 * @param context the context in which the method would be called
   	 * @return returns all types known in the AST that have a method with a given name
   	 */
public static ITypeBinding[] getQualifierGuess(ASTNode searchRoot, final String selector, List<Expression> arguments, final IBinding context) {
    final int nArgs = arguments.size();
    final ArrayList<ITypeBinding> result = new ArrayList<ITypeBinding>();
    // test if selector is a object method
    //$NON-NLS-1$
    ITypeBinding binding = searchRoot.getAST().resolveWellKnownType("java.lang.Object");
    IMethodBinding[] objectMethods = binding.getDeclaredMethods();
    for (int i = 0; i < objectMethods.length; i++) {
        IMethodBinding meth = objectMethods[i];
        if (meth.getName().equals(selector) && meth.getParameterTypes().length == nArgs) {
            return new ITypeBinding[] { binding };
        }
    }
    visitAllBindings(searchRoot, new TypeBindingVisitor() {

        private HashSet<String> fVisitedBindings = new HashSet<String>(100);

        public boolean visit(ITypeBinding node) {
            node = Bindings.normalizeTypeBinding(node);
            if (node == null) {
                return true;
            }
            if (!fVisitedBindings.add(node.getKey())) {
                return true;
            }
            if (node.isGenericType()) {
                // only look at  parameterized types
                return true;
            }
            if (context != null && !isUseableTypeInContext(node, context, false)) {
                return true;
            }
            IMethodBinding[] methods = node.getDeclaredMethods();
            for (int i = 0; i < methods.length; i++) {
                IMethodBinding meth = methods[i];
                if (meth.getName().equals(selector) && meth.getParameterTypes().length == nArgs) {
                    result.add(node);
                }
            }
            return true;
        }
    });
    return result.toArray(new ITypeBinding[result.size()]);
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ArrayList(java.util.ArrayList) TypeBindingVisitor(org.eclipse.jdt.internal.corext.dom.TypeBindingVisitor) HashSet(java.util.HashSet)

Aggregations

TypeBindingVisitor (org.eclipse.jdt.internal.corext.dom.TypeBindingVisitor)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)2 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)2 CastExpression (org.eclipse.jdt.core.dom.CastExpression)1 Expression (org.eclipse.jdt.core.dom.Expression)1 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)1 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)1 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)1