Example usage for org.eclipse.jdt.core.dom ITypeBinding isCastCompatible

List of usage examples for org.eclipse.jdt.core.dom ITypeBinding isCastCompatible

Introduction

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

Prototype

public boolean isCastCompatible(ITypeBinding type);

Source Link

Document

Returns whether this type is cast compatible with the given type, as specified in section 5.5 of The Java Language Specification, Third Edition (JLS3).

Usage

From source file:cideplus.ui.astview.TrayContentProvider.java

License:Open Source License

private void addTypeBindingComparions(ArrayList result, Binding trayElement) {
    class IsSubTypeCompatibleProperty extends DynamicBindingProperty {
        public IsSubTypeCompatibleProperty(Binding parent) {
            super(parent);
        }//from   www.  j a v  a2s  .  c o m

        protected String getName() {
            return "*.isSubTypeCompatible(this): "; //$NON-NLS-1$
        }

        protected String executeQuery(IBinding viewerBinding, IBinding trayBinding) {
            if (viewerBinding instanceof ITypeBinding) {
                ITypeBinding viewerTB = (ITypeBinding) viewerBinding;
                ITypeBinding trayTB = (ITypeBinding) trayBinding;
                return Boolean.toString(viewerTB.isSubTypeCompatible(trayTB));
            } else {
                return "* not an ITypeBinding"; //$NON-NLS-1$
            }
        }
    }
    result.add(new IsSubTypeCompatibleProperty(trayElement));

    class IsCastCompatibleProperty extends DynamicBindingProperty {
        public IsCastCompatibleProperty(Binding parent) {
            super(parent);
        }

        protected String getName() {
            return "*.isCastCompatible(this): "; //$NON-NLS-1$
        }

        protected String executeQuery(IBinding viewerBinding, IBinding trayBinding) {
            if (viewerBinding instanceof ITypeBinding) {
                ITypeBinding viewerTB = (ITypeBinding) viewerBinding;
                ITypeBinding trayTB = (ITypeBinding) trayBinding;
                return Boolean.toString(viewerTB.isCastCompatible(trayTB));
            } else {
                return "* not an ITypeBinding"; //$NON-NLS-1$
            }
        }
    }
    result.add(new IsCastCompatibleProperty(trayElement));

    class IsAssignmentCompatibleProperty extends DynamicBindingProperty {
        public IsAssignmentCompatibleProperty(Binding parent) {
            super(parent);
        }

        protected String getName() {
            return "*.isAssignmentCompatible(this): "; //$NON-NLS-1$
        }

        protected String executeQuery(IBinding viewerBinding, IBinding trayBinding) {
            if (viewerBinding instanceof ITypeBinding) {
                ITypeBinding viewerTB = (ITypeBinding) viewerBinding;
                ITypeBinding trayTB = (ITypeBinding) trayBinding;
                return Boolean.toString(viewerTB.isAssignmentCompatible(trayTB));
            } else {
                return "* not an ITypeBinding"; //$NON-NLS-1$
            }
        }
    }
    result.add(new IsAssignmentCompatibleProperty(trayElement));
}

From source file:de.ovgu.cide.typing.jdt.checks.util.MethodPathItem.java

License:Open Source License

public Map<String, List<String>> getInheritedExceptionKeys(IMethodBinding sourceBinding) {

    if (exceptionKeys != null)
        return exceptionKeys;

    exceptionKeys = new HashMap<String, List<String>>();

    // add cast compatible exception 
    for (ITypeBinding tmpExBinding : sourceBinding.getExceptionTypes()) {

        ArrayList<String> tmpExceKeys = new ArrayList<String>();

        for (ITypeBinding tmpMethExBinding : binding.getExceptionTypes()) {

            if (tmpExBinding.isCastCompatible(tmpMethExBinding))
                tmpExceKeys.add(//from w ww. j av  a 2  s.  com
                        BindingProjectColorCache.getExceptionKey(binding.getKey(), tmpMethExBinding.getKey()));

        }

        exceptionKeys.put(tmpExBinding.getKey(), tmpExceKeys);

    }

    return exceptionKeys;

}

From source file:edu.cmu.cs.crystal.analysis.alias.MayAliasAnalysis.java

License:Open Source License

private Set<ObjectLabel> getAliasLabels(ITypeBinding typeBinding, TupleLatticeElement<Variable, AliasLE> le) {
    //TODO: Find a cheaper way of maintaining this context and have the ability
    //to query it at any node. Maybe a Contextual Lattice?
    Set<ObjectLabel> labels = new HashSet<ObjectLabel>();
    for (Variable var : le.getKeySet()) {
        Set<ObjectLabel> aliases = le.get(var).getLabels();

        for (ObjectLabel label : aliases) {
            if (typeBinding.isCastCompatible(label.getType()))
                labels.add(label);//from www .j av a 2  s. com
        }
    }
    return labels;
}

From source file:org.autorefactor.refactoring.ASTHelper.java

License:Open Source License

/**
 * Returns whether the two provided expressions are cast compatible.
 *
 * @param expr1 the first expression/*  w w  w. j  a  v a 2 s.  com*/
 * @param expr2 the second expression
 * @return {@code true} if the two provided expressions are cast compatible, {@code false} otherwise
 * @see ITypeBinding#isCastCompatible(ITypeBinding)
 */
public static boolean isCastCompatible(Expression expr1, Expression expr2) {
    final ITypeBinding tb1 = expr1.resolveTypeBinding();
    final ITypeBinding tb2 = expr2.resolveTypeBinding();
    return tb1 != null && tb2 != null && tb1.isCastCompatible(tb2);
}

From source file:org.eclipse.ajdt.internal.ui.editor.quickfix.UnresolvedElementsSubProcessor.java

License:Open Source License

private static boolean useExistingParentCastProposal(ICompilationUnit cu, CastExpression expression,
        Expression accessExpression, SimpleName accessSelector, ITypeBinding[] paramTypes,
        Collection proposals) {/*from w w w  .ja  v a2s .co m*/
    ITypeBinding castType = expression.getType().resolveBinding();
    if (castType == null) {
        return false;
    }
    if (paramTypes != null) {
        if (Bindings.findMethodInHierarchy(castType, accessSelector.getIdentifier(), paramTypes) == null) {
            return false;
        }
    } else if (Bindings.findFieldInHierarchy(castType, accessSelector.getIdentifier()) == null) {
        return false;
    }
    ITypeBinding bindingToCast = accessExpression.resolveTypeBinding();
    if (bindingToCast != null && !bindingToCast.isCastCompatible(castType)) {
        return false;
    }

    IMethodBinding res = Bindings.findMethodInHierarchy(castType, accessSelector.getIdentifier(), paramTypes);
    if (res != null) {
        AST ast = expression.getAST();
        ASTRewrite rewrite = ASTRewrite.create(ast);
        CastExpression newCast = ast.newCastExpression();
        newCast.setType((Type) ASTNode.copySubtree(ast, expression.getType()));
        newCast.setExpression((Expression) rewrite.createCopyTarget(accessExpression));
        ParenthesizedExpression parents = ast.newParenthesizedExpression();
        parents.setExpression(newCast);

        ASTNode node = rewrite.createCopyTarget(expression.getExpression());
        rewrite.replace(expression, node, null);
        rewrite.replace(accessExpression, parents, null);

        String label = CorrectionMessages.UnresolvedElementsSubProcessor_missingcastbrackets_description;
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CAST);
        ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, 8, image);
        proposals.add(proposal);
        return true;
    }
    return false;
}

From source file:org.eclipse.ajdt.internal.ui.editor.quickfix.UnresolvedElementsSubProcessor.java

License:Open Source License

private static void doEqualNumberOfParameters(IInvocationContext context, ASTNode invocationNode,
        IProblemLocation problem, List arguments, ITypeBinding[] argTypes, IMethodBinding methodBinding,
        Collection 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;/*  ww w  . ja  v a 2 s .c  om*/
        }
    }
    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 = (Expression) 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();
            if (binding == null || binding.isCastCompatible(castType)) {
                ASTRewriteCorrectionProposal proposal = TypeMismatchSubProcessor.createCastProposal(context,
                        castType, nodeToCast, 6);
                String castTypeName = BindingLabelProvider.getBindingLabel(castType,
                        JavaElementLabels.ALL_DEFAULT);
                String[] arg = new String[] { getArgumentName(cu, arguments, idx), castTypeName };
                proposal.setDisplayName(Messages.format(
                        CorrectionMessages.UnresolvedElementsSubProcessor_addargumentcast_description, arg));
                proposals.add(proposal);
            }
            TypeMismatchSubProcessor.addChangeSenderTypeProposals(context, nodeToCast, castType, false, 5,
                    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 = (Expression) arguments.get(idx1);
            Expression arg2 = (Expression) 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(cu, arguments, idx1),
                        getArgumentName(cu, 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, 8, 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] };
                    /* AJDT 1.7 */
                    String[] args = new String[] { ASTResolving.getMethodSignature(methodDecl),
                            getTypeNames(swappedTypes) }; /* AJDT 1.7 */
                    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, 5, 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;
                }

                /* AJDT 1.7 */
                String[] args = new String[] { ASTResolving.getMethodSignature(methodDecl), ASTResolving
                        .getMethodSignature(methodDecl.getName(), newParamTypes, false) }; /* AJDT 1.7 */
                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, 7, image);
                proposals.add(proposal);
            }
        }
    }
}

From source file:org.spoofax.interpreter.library.ecj.ECJ_is_cast_compatible.java

License:LGPL

@Override
public boolean call(IContext env, Strategy[] svars, IStrategoTerm[] tvars) throws InterpreterException {

    if (!(tvars[0] instanceof WrappedITypeBinding))
        return true;
    if (!(tvars[1] instanceof WrappedITypeBinding))
        return true;

    WrappedITypeBinding wb0 = (WrappedITypeBinding) tvars[0];
    ITypeBinding b0 = wb0.getWrappee();

    WrappedITypeBinding wb1 = (WrappedITypeBinding) tvars[0];
    ITypeBinding b1 = wb1.getWrappee();//  ww w  .j a va 2  s . c  o  m

    return b0.isCastCompatible(b1);
}