Search in sources :

Example 66 with IBinding

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

the class ConvertIterableLoopOperation method satisfiesPreconditions.

/**
	 * Is this proposal applicable?
	 *
	 * @return A status with severity <code>IStatus.Error</code> if not
	 *         applicable
	 */
@Override
public final IStatus satisfiesPreconditions() {
    IStatus resultStatus = StatusInfo.OK_STATUS;
    if (JavaModelUtil.is50OrHigher(getJavaProject())) {
        resultStatus = checkExpressionCondition();
        if (resultStatus.getSeverity() == IStatus.ERROR)
            return resultStatus;
        List<Expression> updateExpressions = getForStatement().updaters();
        if (updateExpressions.size() == 1) {
            resultStatus = new StatusInfo(IStatus.WARNING, Messages.format(FixMessages.ConvertIterableLoopOperation_RemoveUpdateExpression_Warning, BasicElementLabels.getJavaCodeString(updateExpressions.get(0).toString())));
        } else if (updateExpressions.size() > 1) {
            resultStatus = new StatusInfo(IStatus.WARNING, FixMessages.ConvertIterableLoopOperation_RemoveUpdateExpressions_Warning);
        }
        for (final Iterator<Expression> outer = getForStatement().initializers().iterator(); outer.hasNext(); ) {
            final Expression initializer = outer.next();
            if (initializer instanceof VariableDeclarationExpression) {
                final VariableDeclarationExpression declaration = (VariableDeclarationExpression) initializer;
                List<VariableDeclarationFragment> fragments = declaration.fragments();
                if (fragments.size() != 1) {
                    //$NON-NLS-1$
                    return new StatusInfo(IStatus.ERROR, "");
                } else {
                    final VariableDeclarationFragment fragment = fragments.get(0);
                    fragment.accept(new ASTVisitor() {

                        @Override
                        public final boolean visit(final MethodInvocation node) {
                            final IMethodBinding binding = node.resolveMethodBinding();
                            if (binding != null) {
                                final ITypeBinding type = binding.getReturnType();
                                if (type != null) {
                                    final String qualified = type.getQualifiedName();
                                    if (qualified.startsWith("java.util.Enumeration<") || qualified.startsWith("java.util.Iterator<")) {
                                        //$NON-NLS-1$ //$NON-NLS-2$
                                        final Expression qualifier = node.getExpression();
                                        if (qualifier != null) {
                                            final ITypeBinding resolved = qualifier.resolveTypeBinding();
                                            if (resolved != null) {
                                                //$NON-NLS-1$
                                                final ITypeBinding iterable = getSuperType(resolved, "java.lang.Iterable");
                                                if (iterable != null) {
                                                    fExpression = qualifier;
                                                    fIterable = resolved;
                                                }
                                            }
                                        } else {
                                            final ITypeBinding declaring = binding.getDeclaringClass();
                                            if (declaring != null) {
                                                //$NON-NLS-1$
                                                final ITypeBinding superBinding = getSuperType(declaring, "java.lang.Iterable");
                                                if (superBinding != null) {
                                                    fIterable = superBinding;
                                                    fThis = true;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            return true;
                        }

                        @Override
                        public final boolean visit(final VariableDeclarationFragment node) {
                            final IVariableBinding binding = node.resolveBinding();
                            if (binding != null) {
                                final ITypeBinding type = binding.getType();
                                if (type != null) {
                                    //$NON-NLS-1$
                                    ITypeBinding iterator = getSuperType(type, "java.util.Iterator");
                                    if (iterator != null)
                                        fIteratorVariable = binding;
                                    else {
                                        //$NON-NLS-1$
                                        iterator = getSuperType(type, "java.util.Enumeration");
                                        if (iterator != null)
                                            fIteratorVariable = binding;
                                    }
                                }
                            }
                            return true;
                        }
                    });
                }
            }
        }
        final Statement statement = getForStatement().getBody();
        final boolean[] otherInvocationThenNext = new boolean[] { false };
        final int[] nextInvocationCount = new int[] { 0 };
        if (statement != null && fIteratorVariable != null) {
            final ITypeBinding elementType = getElementType(fIteratorVariable.getType());
            statement.accept(new ASTVisitor() {

                @Override
                public boolean visit(SimpleName node) {
                    IBinding nodeBinding = node.resolveBinding();
                    if (fElementVariable != null && fElementVariable.equals(nodeBinding)) {
                        fMakeFinal = false;
                    }
                    if (nodeBinding == fIteratorVariable) {
                        if (node.getLocationInParent() == MethodInvocation.EXPRESSION_PROPERTY) {
                            MethodInvocation invocation = (MethodInvocation) node.getParent();
                            String name = invocation.getName().getIdentifier();
                            if (name.equals("next") || name.equals("nextElement")) {
                                //$NON-NLS-1$ //$NON-NLS-2$
                                nextInvocationCount[0]++;
                                Expression left = null;
                                if (invocation.getLocationInParent() == Assignment.RIGHT_HAND_SIDE_PROPERTY) {
                                    left = ((Assignment) invocation.getParent()).getLeftHandSide();
                                } else if (invocation.getLocationInParent() == VariableDeclarationFragment.INITIALIZER_PROPERTY) {
                                    left = ((VariableDeclarationFragment) invocation.getParent()).getName();
                                }
                                return visitElementVariable(left);
                            }
                        }
                        otherInvocationThenNext[0] = true;
                    }
                    return true;
                }

                private boolean visitElementVariable(final Expression node) {
                    if (node != null) {
                        final ITypeBinding binding = node.resolveTypeBinding();
                        if (binding != null && elementType.equals(binding)) {
                            if (node instanceof Name) {
                                final Name name = (Name) node;
                                final IBinding result = name.resolveBinding();
                                if (result != null) {
                                    fOccurrences.add(node);
                                    fElementVariable = result;
                                    return false;
                                }
                            } else if (node instanceof FieldAccess) {
                                final FieldAccess access = (FieldAccess) node;
                                final IBinding result = access.resolveFieldBinding();
                                if (result != null) {
                                    fOccurrences.add(node);
                                    fElementVariable = result;
                                    return false;
                                }
                            }
                        }
                    }
                    return true;
                }
            });
            if (otherInvocationThenNext[0])
                return ERROR_STATUS;
            if (nextInvocationCount[0] > 1)
                return ERROR_STATUS;
            if (fElementVariable != null) {
                statement.accept(new ASTVisitor() {

                    @Override
                    public final boolean visit(final VariableDeclarationFragment node) {
                        if (node.getInitializer() instanceof NullLiteral) {
                            SimpleName name = node.getName();
                            if (elementType.equals(name.resolveTypeBinding()) && fElementVariable.equals(name.resolveBinding())) {
                                fOccurrences.add(name);
                            }
                        }
                        return true;
                    }
                });
            }
        }
        final ASTNode root = getForStatement().getRoot();
        if (root != null) {
            root.accept(new ASTVisitor() {

                @Override
                public final boolean visit(final ForStatement node) {
                    return false;
                }

                @Override
                public final boolean visit(final SimpleName node) {
                    final IBinding binding = node.resolveBinding();
                    if (binding != null && binding.equals(fElementVariable))
                        fAssigned = true;
                    return false;
                }
            });
        }
    }
    if ((fExpression != null || fThis) && fIterable != null && fIteratorVariable != null && !fAssigned) {
        return resultStatus;
    } else {
        return ERROR_STATUS;
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) IStatus(org.eclipse.core.runtime.IStatus) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) Assignment(org.eclipse.jdt.core.dom.Assignment) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) Statement(org.eclipse.jdt.core.dom.Statement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) StatusInfo(org.eclipse.jdt.internal.ui.dialogs.StatusInfo) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) NullLiteral(org.eclipse.jdt.core.dom.NullLiteral)

Example 67 with IBinding

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

the class ConvertForLoopOperation method validateExpression.

/*
	 * Must be one of:
	 * <ul>
	 * <li>[indexBinding] < [result].length;</li>
	 * <li>[result].length > [indexBinding];</li>
	 * <li>[indexBinding] < [lengthBinding];</li>
	 * <li>[lengthBinding] > [indexBinding];</li>
	 * </ul>
	 */
private boolean validateExpression(ForStatement statement) {
    Expression expression = statement.getExpression();
    if (!(expression instanceof InfixExpression))
        return false;
    InfixExpression infix = (InfixExpression) expression;
    Expression left = infix.getLeftOperand();
    Expression right = infix.getRightOperand();
    if (left instanceof SimpleName && right instanceof SimpleName) {
        IVariableBinding lengthBinding = fLengthBinding;
        if (lengthBinding == null)
            return false;
        IBinding leftBinding = ((SimpleName) left).resolveBinding();
        IBinding righBinding = ((SimpleName) right).resolveBinding();
        if (fIndexBinding.equals(leftBinding)) {
            return lengthBinding.equals(righBinding);
        } else if (fIndexBinding.equals(righBinding)) {
            return lengthBinding.equals(leftBinding);
        }
        return false;
    } else if (left instanceof SimpleName) {
        if (!fIndexBinding.equals(((SimpleName) left).resolveBinding()))
            return false;
        if (!Operator.LESS.equals(infix.getOperator()))
            return false;
        return validateLengthQuery(right);
    } else if (right instanceof SimpleName) {
        if (!fIndexBinding.equals(((SimpleName) right).resolveBinding()))
            return false;
        if (!Operator.GREATER.equals(infix.getOperator()))
            return false;
        return validateLengthQuery(left);
    }
    return false;
}
Also used : PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 68 with IBinding

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

the class ConvertForLoopOperation method validateUpdaters.

/*
	 * Must be one of:
	 * <ul>
	 * <li>[indexBinding]++</li>
	 * <li>++[indexBinding]</li>
	 * <li>[indexBinding]+= 1</li>
	 * <li>[indexBinding]= [indexBinding] + 1</li>
	 * <li>[indexBinding]= 1 + [indexBinding]</li>
	 * <ul>
	 */
private boolean validateUpdaters(ForStatement statement) {
    List<Expression> updaters = statement.updaters();
    if (updaters.size() != 1)
        return false;
    Expression updater = updaters.get(0);
    if (updater instanceof PostfixExpression) {
        PostfixExpression postfix = (PostfixExpression) updater;
        if (!PostfixExpression.Operator.INCREMENT.equals(postfix.getOperator()))
            return false;
        IBinding binding = getBinding(postfix.getOperand());
        if (!fIndexBinding.equals(binding))
            return false;
        return true;
    } else if (updater instanceof PrefixExpression) {
        PrefixExpression prefix = (PrefixExpression) updater;
        if (!PrefixExpression.Operator.INCREMENT.equals(prefix.getOperator()))
            return false;
        IBinding binding = getBinding(prefix.getOperand());
        if (!fIndexBinding.equals(binding))
            return false;
        return true;
    } else if (updater instanceof Assignment) {
        Assignment assignment = (Assignment) updater;
        Expression left = assignment.getLeftHandSide();
        IBinding binding = getBinding(left);
        if (!fIndexBinding.equals(binding))
            return false;
        if (Assignment.Operator.PLUS_ASSIGN.equals(assignment.getOperator())) {
            return isOneLiteral(assignment.getRightHandSide());
        } else if (Assignment.Operator.ASSIGN.equals(assignment.getOperator())) {
            Expression right = assignment.getRightHandSide();
            if (!(right instanceof InfixExpression))
                return false;
            InfixExpression infixExpression = (InfixExpression) right;
            Expression leftOperand = infixExpression.getLeftOperand();
            IBinding leftBinding = getBinding(leftOperand);
            Expression rightOperand = infixExpression.getRightOperand();
            IBinding rightBinding = getBinding(rightOperand);
            if (fIndexBinding.equals(leftBinding)) {
                return isOneLiteral(rightOperand);
            } else if (fIndexBinding.equals(rightBinding)) {
                return isOneLiteral(leftOperand);
            }
        }
    }
    return false;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) IBinding(org.eclipse.jdt.core.dom.IBinding) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression)

Example 69 with IBinding

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

the class JdtASTMatcher method match.

@Override
public boolean match(SimpleName node, Object other) {
    boolean isomorphic = super.match(node, other);
    if (!isomorphic || !(other instanceof SimpleName))
        return false;
    SimpleName name = (SimpleName) other;
    IBinding nodeBinding = node.resolveBinding();
    IBinding otherBinding = name.resolveBinding();
    if (nodeBinding == null) {
        if (otherBinding != null) {
            return false;
        }
    } else {
        if (nodeBinding != otherBinding) {
            return false;
        }
    }
    if (node.resolveTypeBinding() != name.resolveTypeBinding())
        return false;
    return true;
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding)

Example 70 with IBinding

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

the class ScopeAnalyzer method getUsedVariableNames.

public Collection<String> getUsedVariableNames(int offset, int length) {
    HashSet<String> result = new HashSet<String>();
    IBinding[] bindingsBefore = getDeclarationsInScope(offset, VARIABLES);
    for (int i = 0; i < bindingsBefore.length; i++) {
        result.add(bindingsBefore[i].getName());
    }
    IBinding[] bindingsAfter = getDeclarationsAfter(offset + length, VARIABLES);
    for (int i = 0; i < bindingsAfter.length; i++) {
        result.add(bindingsAfter[i].getName());
    }
    List<ImportDeclaration> imports = fRoot.imports();
    for (int i = 0; i < imports.size(); i++) {
        ImportDeclaration decl = imports.get(i);
        if (decl.isStatic() && !decl.isOnDemand()) {
            result.add(ASTNodes.getSimpleNameIdentifier(decl.getName()));
        }
    }
    return result;
}
Also used : IBinding(org.eclipse.jdt.core.dom.IBinding) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) HashSet(java.util.HashSet)

Aggregations

IBinding (org.eclipse.jdt.core.dom.IBinding)103 SimpleName (org.eclipse.jdt.core.dom.SimpleName)59 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)48 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)41 ASTNode (org.eclipse.jdt.core.dom.ASTNode)40 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)25 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)25 Name (org.eclipse.jdt.core.dom.Name)20 Expression (org.eclipse.jdt.core.dom.Expression)19 ArrayList (java.util.ArrayList)16 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)14 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)13 CastExpression (org.eclipse.jdt.core.dom.CastExpression)12 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)11 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)11 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)11 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)11 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)11 AST (org.eclipse.jdt.core.dom.AST)10 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)10