Search in sources :

Example 41 with ITypeBinding

use of org.eclipse.jdt.core.dom.ITypeBinding in project che by eclipse.

the class Bindings method findOverriddenMethodInHierarchy.

/**
     * Finds a method in the hierarchy of <code>type</code> that is overridden by </code>binding</code>.
     * Returns <code>null</code> if no such method exists. If the method is defined in more than one super type only the first match is
     * returned. First the super class is examined and then the implemented interfaces.
     * @param type The type to search the method in
     * @param binding The method that overrides
     * @return the method binding overridden the method
     */
public static IMethodBinding findOverriddenMethodInHierarchy(ITypeBinding type, IMethodBinding binding) {
    IMethodBinding method = findOverriddenMethodInType(type, binding);
    if (method != null)
        return method;
    ITypeBinding superClass = type.getSuperclass();
    if (superClass != null) {
        method = findOverriddenMethodInHierarchy(superClass, binding);
        if (method != null)
            return method;
    }
    ITypeBinding[] interfaces = type.getInterfaces();
    for (int i = 0; i < interfaces.length; i++) {
        method = findOverriddenMethodInHierarchy(interfaces[i], binding);
        if (method != null)
            return method;
    }
    return null;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Example 42 with ITypeBinding

use of org.eclipse.jdt.core.dom.ITypeBinding in project che by eclipse.

the class Bindings method getTopLevelType.

public static ITypeBinding getTopLevelType(ITypeBinding type) {
    ITypeBinding parent = type.getDeclaringClass();
    while (parent != null) {
        type = parent;
        parent = type.getDeclaringClass();
    }
    return type;
}
Also used : ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Example 43 with ITypeBinding

use of org.eclipse.jdt.core.dom.ITypeBinding in project che by eclipse.

the class Bindings method isSubsignature.

/**
     * @param overriding overriding method (m1)
     * @param overridden overridden method (m2)
     * @return <code>true</code> iff the method <code>m1</code> is a subsignature of the method <code>m2</code>.
     * 		This is one of the requirements for m1 to override m2.
     * 		Accessibility and return types are not taken into account.
     * 		Note that subsignature is <em>not</em> symmetric!
     */
public static boolean isSubsignature(IMethodBinding overriding, IMethodBinding overridden) {
    //TODO: use IMethodBinding#isSubsignature(..) once it is tested and fixed (only erasure of m1's parameter types, considering type variable counts, doing type variable substitution
    if (!overriding.getName().equals(overridden.getName()))
        return false;
    ITypeBinding[] m1Params = overriding.getParameterTypes();
    ITypeBinding[] m2Params = overridden.getParameterTypes();
    if (m1Params.length != m2Params.length)
        return false;
    ITypeBinding[] m1TypeParams = overriding.getTypeParameters();
    ITypeBinding[] m2TypeParams = overridden.getTypeParameters();
    if (m1TypeParams.length != m2TypeParams.length && //non-generic m1 can override a generic m2
    m1TypeParams.length != 0)
        return false;
    //m1TypeParameters.length == (m2TypeParameters.length || 0)
    if (m2TypeParams.length != 0) {
        //Compare type parameter bounds:
        for (int i = 0; i < m1TypeParams.length; i++) {
            // loop over m1TypeParams, which is either empty, or equally long as m2TypeParams
            Set<ITypeBinding> m1Bounds = getTypeBoundsForSubsignature(m1TypeParams[i]);
            Set<ITypeBinding> m2Bounds = getTypeBoundsForSubsignature(m2TypeParams[i]);
            if (!m1Bounds.equals(m2Bounds))
                return false;
        }
        //Compare parameter types:
        if (equals(m2Params, m1Params))
            return true;
        for (int i = 0; i < m1Params.length; i++) {
            ITypeBinding m1Param = m1Params[i];
            ITypeBinding m2Param = m2Params[i];
            if (containsTypeVariables(m1Param) || m1Param.isRawType())
                // try to achieve effect of "rename type variables"
                m1Param = m1Param.getErasure();
            if (!(equals(m1Param, m2Param) || equals(m1Param, m2Param.getErasure())))
                return false;
        }
        return true;
    } else {
        // m1TypeParams.length == m2TypeParams.length == 0
        if (equals(m1Params, m2Params))
            return true;
        for (int i = 0; i < m1Params.length; i++) {
            ITypeBinding m1Param = m1Params[i];
            ITypeBinding m2Param = m2Params[i];
            if (m1Param.isRawType())
                m1Param = m1Param.getTypeDeclaration();
            if (!(equals(m1Param, m2Param) || equals(m1Param, m2Param.getErasure())))
                return false;
        }
        return true;
    }
}
Also used : ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Example 44 with ITypeBinding

use of org.eclipse.jdt.core.dom.ITypeBinding in project che by eclipse.

the class Bindings method areSubTypeCompatible.

private static boolean areSubTypeCompatible(IMethodBinding overridden, IMethodBinding overridable) {
    if (overridden.getParameterTypes().length != overridable.getParameterTypes().length)
        return false;
    ITypeBinding overriddenReturn = overridden.getReturnType();
    ITypeBinding overridableReturn = overridable.getReturnType();
    if (overriddenReturn == null || overridableReturn == null)
        return false;
    if (!overriddenReturn.getErasure().isSubTypeCompatible(overridableReturn.getErasure()))
        return false;
    ITypeBinding[] overriddenTypes = overridden.getParameterTypes();
    ITypeBinding[] overridableTypes = overridable.getParameterTypes();
    Assert.isTrue(overriddenTypes.length == overridableTypes.length);
    for (int index = 0; index < overriddenTypes.length; index++) {
        final ITypeBinding overridableErasure = overridableTypes[index].getErasure();
        final ITypeBinding overriddenErasure = overriddenTypes[index].getErasure();
        if (!overridableErasure.isSubTypeCompatible(overriddenErasure) || !overridableErasure.getKey().equals(overriddenErasure.getKey()))
            return false;
    }
    ITypeBinding[] overriddenExceptions = overridden.getExceptionTypes();
    ITypeBinding[] overridableExceptions = overridable.getExceptionTypes();
    boolean checked = false;
    for (int index = 0; index < overriddenExceptions.length; index++) {
        checked = false;
        for (int offset = 0; offset < overridableExceptions.length; offset++) {
            if (overriddenExceptions[index].isSubTypeCompatible(overridableExceptions[offset]))
                checked = true;
        }
        if (!checked)
            return false;
    }
    return true;
}
Also used : ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Example 45 with ITypeBinding

use of org.eclipse.jdt.core.dom.ITypeBinding in project che by eclipse.

the class UnresolvedElementsSubProcessor method doEqualNumberOfParameters.

private static void doEqualNumberOfParameters(IInvocationContext context, ASTNode invocationNode, IProblemLocation problem, List<Expression> arguments, ITypeBinding[] argTypes, IMethodBinding methodBinding, Collection<ICommandAccess> proposals) throws CoreException {
    ITypeBinding[] paramTypes = methodBinding.getParameterTypes();
    int[] indexOfDiff = new int[paramTypes.length];
    int nDiffs = 0;
    for (int n = 0; n < argTypes.length; n++) {
        if (!canAssign(argTypes[n], paramTypes[n])) {
            indexOfDiff[nDiffs++] = n;
        }
    }
    ITypeBinding declaringTypeDecl = methodBinding.getDeclaringClass().getTypeDeclaration();
    ICompilationUnit cu = context.getCompilationUnit();
    CompilationUnit astRoot = context.getASTRoot();
    ASTNode nameNode = problem.getCoveringNode(astRoot);
    if (nameNode == null) {
        return;
    }
    if (nDiffs == 0) {
        if (nameNode.getParent() instanceof MethodInvocation) {
            MethodInvocation inv = (MethodInvocation) nameNode.getParent();
            if (inv.getExpression() == null) {
                addQualifierToOuterProposal(context, inv, methodBinding, proposals);
            }
        }
        return;
    }
    if (nDiffs == 1) {
        // one argument mismatching: try to fix
        int idx = indexOfDiff[0];
        Expression nodeToCast = arguments.get(idx);
        ITypeBinding castType = paramTypes[idx];
        castType = Bindings.normalizeTypeBinding(castType);
        if (castType.isWildcardType()) {
            castType = ASTResolving.normalizeWildcardType(castType, false, nodeToCast.getAST());
        }
        if (castType != null) {
            ITypeBinding binding = nodeToCast.resolveTypeBinding();
            ITypeBinding castFixType = null;
            if (binding == null || castType.isCastCompatible(binding)) {
                castFixType = castType;
            } else if (JavaModelUtil.is50OrHigher(cu.getJavaProject())) {
                ITypeBinding boxUnboxedTypeBinding = TypeMismatchSubProcessor.boxUnboxPrimitives(castType, binding, nodeToCast.getAST());
                if (boxUnboxedTypeBinding != castType && boxUnboxedTypeBinding.isCastCompatible(binding)) {
                    castFixType = boxUnboxedTypeBinding;
                }
            }
            if (castFixType != null) {
                ASTRewriteCorrectionProposal proposal = TypeMismatchSubProcessor.createCastProposal(context, castFixType, nodeToCast, IProposalRelevance.CAST_ARGUMENT_1);
                String castTypeName = BindingLabelProvider.getBindingLabel(castFixType, JavaElementLabels.ALL_DEFAULT);
                String[] arg = new String[] { getArgumentName(arguments, idx), castTypeName };
                proposal.setDisplayName(Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_addargumentcast_description, arg));
                proposals.add(proposal);
            }
            TypeMismatchSubProcessor.addChangeSenderTypeProposals(context, nodeToCast, castType, false, IProposalRelevance.CAST_ARGUMENT_2, proposals);
        }
    }
    if (nDiffs == 2) {
        // try to swap
        int idx1 = indexOfDiff[0];
        int idx2 = indexOfDiff[1];
        boolean canSwap = canAssign(argTypes[idx1], paramTypes[idx2]) && canAssign(argTypes[idx2], paramTypes[idx1]);
        if (canSwap) {
            Expression arg1 = arguments.get(idx1);
            Expression arg2 = arguments.get(idx2);
            ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST());
            rewrite.replace(arg1, rewrite.createCopyTarget(arg2), null);
            rewrite.replace(arg2, rewrite.createCopyTarget(arg1), null);
            {
                String[] arg = new String[] { getArgumentName(arguments, idx1), getArgumentName(arguments, idx2) };
                String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_swaparguments_description, arg);
                Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
                ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.SWAP_ARGUMENTS, image);
                proposals.add(proposal);
            }
            if (declaringTypeDecl.isFromSource()) {
                ICompilationUnit targetCU = ASTResolving.findCompilationUnitForBinding(cu, astRoot, declaringTypeDecl);
                if (targetCU != null) {
                    ChangeDescription[] changeDesc = new ChangeDescription[paramTypes.length];
                    for (int i = 0; i < nDiffs; i++) {
                        changeDesc[idx1] = new SwapDescription(idx2);
                    }
                    IMethodBinding methodDecl = methodBinding.getMethodDeclaration();
                    ITypeBinding[] declParamTypes = methodDecl.getParameterTypes();
                    ITypeBinding[] swappedTypes = new ITypeBinding[] { declParamTypes[idx1], declParamTypes[idx2] };
                    String[] args = new String[] { ASTResolving.getMethodSignature(methodDecl), getTypeNames(swappedTypes) };
                    String label;
                    if (methodDecl.isConstructor()) {
                        label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_swapparams_constr_description, args);
                    } else {
                        label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_swapparams_description, args);
                    }
                    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
                    ChangeMethodSignatureProposal proposal = new ChangeMethodSignatureProposal(label, targetCU, invocationNode, methodDecl, changeDesc, null, IProposalRelevance.CHANGE_METHOD_SWAP_PARAMETERS, image);
                    proposals.add(proposal);
                }
            }
            return;
        }
    }
    if (declaringTypeDecl.isFromSource()) {
        ICompilationUnit targetCU = ASTResolving.findCompilationUnitForBinding(cu, astRoot, declaringTypeDecl);
        if (targetCU != null) {
            ChangeDescription[] changeDesc = createSignatureChangeDescription(indexOfDiff, nDiffs, paramTypes, arguments, argTypes);
            if (changeDesc != null) {
                IMethodBinding methodDecl = methodBinding.getMethodDeclaration();
                ITypeBinding[] declParamTypes = methodDecl.getParameterTypes();
                ITypeBinding[] newParamTypes = new ITypeBinding[changeDesc.length];
                for (int i = 0; i < newParamTypes.length; i++) {
                    newParamTypes[i] = changeDesc[i] == null ? declParamTypes[i] : ((EditDescription) changeDesc[i]).type;
                }
                boolean isVarArgs = methodDecl.isVarargs() && newParamTypes.length > 0 && newParamTypes[newParamTypes.length - 1].isArray();
                String[] args = new String[] { ASTResolving.getMethodSignature(methodDecl), ASTResolving.getMethodSignature(methodDecl.getName(), newParamTypes, isVarArgs) };
                String label;
                if (methodDecl.isConstructor()) {
                    label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changeparamsignature_constr_description, args);
                } else {
                    label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changeparamsignature_description, args);
                }
                Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
                ChangeMethodSignatureProposal proposal = new ChangeMethodSignatureProposal(label, targetCU, invocationNode, methodDecl, changeDesc, null, IProposalRelevance.CHANGE_METHOD_SIGNATURE, image);
                proposals.add(proposal);
            }
        }
    }
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ChangeMethodSignatureProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeMethodSignatureProposal) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) Image(org.eclipse.swt.graphics.Image) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) SwapDescription(org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeMethodSignatureProposal.SwapDescription) EditDescription(org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeMethodSignatureProposal.EditDescription) 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) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ChangeDescription(org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeMethodSignatureProposal.ChangeDescription) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Aggregations

ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)388 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)110 ASTNode (org.eclipse.jdt.core.dom.ASTNode)72 Expression (org.eclipse.jdt.core.dom.Expression)69 Type (org.eclipse.jdt.core.dom.Type)55 SimpleName (org.eclipse.jdt.core.dom.SimpleName)52 ArrayList (java.util.ArrayList)50 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)49 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)49 AST (org.eclipse.jdt.core.dom.AST)43 IBinding (org.eclipse.jdt.core.dom.IBinding)41 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)40 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)36 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)35 CastExpression (org.eclipse.jdt.core.dom.CastExpression)33 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)32 Name (org.eclipse.jdt.core.dom.Name)31 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)29 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)29 ContextSensitiveImportRewriteContext (org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)26