Search in sources :

Example 16 with VariableDeclarationExpression

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

the class GenerateForLoopAssistProposal method getIteratorBasedForBodyAssignment.

/**
	 * Generates the Assignment in an iterator based for, used in the first statement of an iterator
	 * based <code>for</code> loop body, to retrieve the next element of the {@link Iterable}
	 * instance.
	 * 
	 * @param rewrite the current instance of {@link ASTRewrite}
	 * @param loopOverType the {@link ITypeBinding} of the loop variable
	 * @param loopVariableName the name of the loop variable
	 * @return an {@link Assignment}, which retrieves the next element of the {@link Iterable} using
	 *         the active {@link Iterator}
	 */
private Assignment getIteratorBasedForBodyAssignment(ASTRewrite rewrite, ITypeBinding loopOverType, SimpleName loopVariableName) {
    AST ast = rewrite.getAST();
    Assignment assignResolvedVariable = ast.newAssignment();
    // left hand side
    SimpleName resolvedVariableName = resolveLinkedVariableNameWithProposals(rewrite, loopOverType.getName(), loopVariableName.getIdentifier(), false);
    VariableDeclarationFragment resolvedVariableDeclarationFragment = ast.newVariableDeclarationFragment();
    resolvedVariableDeclarationFragment.setName(resolvedVariableName);
    VariableDeclarationExpression resolvedVariableDeclaration = ast.newVariableDeclarationExpression(resolvedVariableDeclarationFragment);
    resolvedVariableDeclaration.setType(getImportRewrite().addImport(loopOverType, ast, new ContextSensitiveImportRewriteContext(fCurrentNode, getImportRewrite())));
    assignResolvedVariable.setLeftHandSide(resolvedVariableDeclaration);
    // right hand side
    MethodInvocation invokeIteratorNextExpression = ast.newMethodInvocation();
    //$NON-NLS-1$
    invokeIteratorNextExpression.setName(ast.newSimpleName("next"));
    SimpleName currentElementName = ast.newSimpleName(loopVariableName.getIdentifier());
    addLinkedPosition(rewrite.track(currentElementName), LinkedPositionGroup.NO_STOP, currentElementName.getIdentifier());
    invokeIteratorNextExpression.setExpression(currentElementName);
    assignResolvedVariable.setRightHandSide(invokeIteratorNextExpression);
    assignResolvedVariable.setOperator(Assignment.Operator.ASSIGN);
    return assignResolvedVariable;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) AST(org.eclipse.jdt.core.dom.AST) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) SimpleName(org.eclipse.jdt.core.dom.SimpleName) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 17 with VariableDeclarationExpression

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

the class ConvertForLoopOperation method validateInitializers.

/*
	 * Must be one of:
	 * <ul>
	 * <li>int [result]= 0;</li>
	 * <li>int [result]= 0, [lengthBinding]= [arrayBinding].length;</li>
	 * <li>int , [result]= 0;</li>
	 * </ul>
	 */
private boolean validateInitializers(ForStatement statement) {
    List<Expression> initializers = statement.initializers();
    if (initializers.size() != 1)
        return false;
    Expression expression = initializers.get(0);
    if (!(expression instanceof VariableDeclarationExpression))
        return false;
    VariableDeclarationExpression declaration = (VariableDeclarationExpression) expression;
    ITypeBinding declarationBinding = declaration.resolveTypeBinding();
    if (declarationBinding == null)
        return false;
    if (!declarationBinding.isPrimitive())
        return false;
    if (!PrimitiveType.INT.toString().equals(declarationBinding.getQualifiedName()))
        return false;
    List<VariableDeclarationFragment> fragments = declaration.fragments();
    if (fragments.size() == 1) {
        IVariableBinding indexBinding = getIndexBindingFromFragment(fragments.get(0));
        if (indexBinding == null)
            return false;
        fIndexBinding = indexBinding;
        return true;
    } else if (fragments.size() == 2) {
        IVariableBinding indexBinding = getIndexBindingFromFragment(fragments.get(0));
        if (indexBinding == null) {
            indexBinding = getIndexBindingFromFragment(fragments.get(1));
            if (indexBinding == null)
                return false;
            if (!validateLengthFragment(fragments.get(0)))
                return false;
        } else {
            if (!validateLengthFragment(fragments.get(1)))
                return false;
        }
        fIndexBinding = indexBinding;
        return true;
    }
    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) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 18 with VariableDeclarationExpression

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

the class ConvertIterableLoopOperation method checkIteratorCondition.

private IStatus checkIteratorCondition() {
    List<Expression> initializers = getForStatement().initializers();
    if (initializers.size() != 1)
        return SEMANTIC_CHANGE_WARNING_STATUS;
    Expression expression = initializers.get(0);
    if (!(expression instanceof VariableDeclarationExpression))
        return SEMANTIC_CHANGE_WARNING_STATUS;
    VariableDeclarationExpression declaration = (VariableDeclarationExpression) expression;
    List<VariableDeclarationFragment> variableDeclarationFragments = declaration.fragments();
    if (variableDeclarationFragments.size() != 1)
        return SEMANTIC_CHANGE_WARNING_STATUS;
    VariableDeclarationFragment declarationFragment = variableDeclarationFragments.get(0);
    Expression initializer = declarationFragment.getInitializer();
    if (!(initializer instanceof MethodInvocation))
        return SEMANTIC_CHANGE_WARNING_STATUS;
    MethodInvocation methodInvocation = (MethodInvocation) initializer;
    String methodName = methodInvocation.getName().getIdentifier();
    if (//$NON-NLS-1$
    !"iterator".equals(methodName) || methodInvocation.arguments().size() != 0)
        return SEMANTIC_CHANGE_WARNING_STATUS;
    return StatusInfo.OK_STATUS;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 19 with VariableDeclarationExpression

use of org.eclipse.jdt.core.dom.VariableDeclarationExpression 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 20 with VariableDeclarationExpression

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

the class VariableDeclarationFix method createAddFinalOperation.

private static ModifierChangeOperation createAddFinalOperation(SimpleName name, ASTNode decl) {
    if (decl == null)
        return null;
    IBinding binding = name.resolveBinding();
    if (!canAddFinal(binding, decl))
        return null;
    if (decl instanceof SingleVariableDeclaration) {
        return new ModifierChangeOperation(decl, new ArrayList<VariableDeclarationFragment>(), Modifier.FINAL, Modifier.NONE);
    } else if (decl instanceof VariableDeclarationExpression) {
        return new ModifierChangeOperation(decl, new ArrayList<VariableDeclarationFragment>(), Modifier.FINAL, Modifier.NONE);
    } else if (decl instanceof VariableDeclarationFragment) {
        VariableDeclarationFragment frag = (VariableDeclarationFragment) decl;
        decl = decl.getParent();
        if (decl instanceof FieldDeclaration || decl instanceof VariableDeclarationStatement) {
            List<VariableDeclarationFragment> list = new ArrayList<VariableDeclarationFragment>();
            list.add(frag);
            return new ModifierChangeOperation(decl, list, Modifier.FINAL, Modifier.NONE);
        } else if (decl instanceof VariableDeclarationExpression) {
            return new ModifierChangeOperation(decl, new ArrayList<VariableDeclarationFragment>(), Modifier.FINAL, Modifier.NONE);
        }
    }
    return null;
}
Also used : SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) IBinding(org.eclipse.jdt.core.dom.IBinding) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ArrayList(java.util.ArrayList) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Aggregations

VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)22 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)18 Expression (org.eclipse.jdt.core.dom.Expression)12 AST (org.eclipse.jdt.core.dom.AST)11 ASTNode (org.eclipse.jdt.core.dom.ASTNode)10 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)10 Assignment (org.eclipse.jdt.core.dom.Assignment)8 Block (org.eclipse.jdt.core.dom.Block)7 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)7 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)7 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)7 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)6 SimpleName (org.eclipse.jdt.core.dom.SimpleName)6 Type (org.eclipse.jdt.core.dom.Type)6 ContextSensitiveImportRewriteContext (org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)6 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)5 ForStatement (org.eclipse.jdt.core.dom.ForStatement)5 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)5 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)5 Statement (org.eclipse.jdt.core.dom.Statement)5