Search in sources :

Example 41 with IVariableBinding

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

the class ExtractTempRefactoring method isReferringToLocalVariableFromFor.

private static boolean isReferringToLocalVariableFromFor(Expression expression) {
    ASTNode current = expression;
    ASTNode parent = current.getParent();
    while (parent != null && !(parent instanceof BodyDeclaration)) {
        if (parent instanceof ForStatement) {
            ForStatement forStmt = (ForStatement) parent;
            if (forStmt.initializers().contains(current) || forStmt.updaters().contains(current) || forStmt.getExpression() == current) {
                List<Expression> initializers = forStmt.initializers();
                if (initializers.size() == 1 && initializers.get(0) instanceof VariableDeclarationExpression) {
                    List<IVariableBinding> forInitializerVariables = getForInitializedVariables((VariableDeclarationExpression) initializers.get(0));
                    ForStatementChecker checker = new ForStatementChecker(forInitializerVariables);
                    expression.accept(checker);
                    if (checker.isReferringToForVariable())
                        return true;
                }
            }
        }
        current = parent;
        parent = current.getParent();
    }
    return false;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 42 with IVariableBinding

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

the class ExtractTempRefactoring method getForInitializedVariables.

// return List<IVariableBinding>
private static List<IVariableBinding> getForInitializedVariables(VariableDeclarationExpression variableDeclarations) {
    List<IVariableBinding> forInitializerVariables = new ArrayList<IVariableBinding>(1);
    for (Iterator<VariableDeclarationFragment> iter = variableDeclarations.fragments().iterator(); iter.hasNext(); ) {
        VariableDeclarationFragment fragment = iter.next();
        IVariableBinding binding = fragment.resolveBinding();
        if (binding != null)
            forInitializerVariables.add(binding);
    }
    return forInitializerVariables;
}
Also used : VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ArrayList(java.util.ArrayList) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 43 with IVariableBinding

use of org.eclipse.jdt.core.dom.IVariableBinding in project AutoRefactor by JnRouvignac.

the class HotSpotIntrinsicedAPIsRefactoring method visit.

@Override
public boolean visit(ForStatement node) {
    final SystemArrayCopyParams params = new SystemArrayCopyParams();
    collectUniqueIndex(node, params);
    final IVariableBinding incrementedIdx = getUniqueIncrementedVariable(node);
    final List<Statement> stmts = asList(node.getBody());
    if (equalNotNull(params.indexVarBinding, incrementedIdx) && stmts.size() == 1) {
        collectLength(node.getExpression(), incrementedIdx, params);
        final Assignment as = asExpression(stmts.get(0), Assignment.class);
        if (hasOperator(as, ASSIGN)) {
            final Expression lhs = as.getLeftHandSide();
            final Expression rhs = as.getRightHandSide();
            if (lhs instanceof ArrayAccess && rhs instanceof ArrayAccess) {
                final ArrayAccess aaLHS = (ArrayAccess) lhs;
                final ArrayAccess aaRHS = (ArrayAccess) rhs;
                params.destArrayExpr = aaLHS.getArray();
                params.srcArrayExpr = aaRHS.getArray();
                if (haveSameType(params.srcArrayExpr, params.destArrayExpr)) {
                    params.destPos = calcIndex(aaLHS.getIndex(), params);
                    params.srcPos = calcIndex(aaRHS.getIndex(), params);
                    return replaceWithSystemArrayCopyCloneAll(node, params);
                }
            }
        }
    }
    return VISIT_SUBTREE;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ForStatement(org.eclipse.jdt.core.dom.ForStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) Statement(org.eclipse.jdt.core.dom.Statement) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 44 with IVariableBinding

use of org.eclipse.jdt.core.dom.IVariableBinding in project AutoRefactor by JnRouvignac.

the class AllInOneMethodRatherThanLoopRefactoring method visit.

@Override
public boolean visit(EnhancedForStatement node) {
    final Expression iterable = node.getExpression();
    final List<Statement> stmts = asList(node.getBody());
    if (stmts.size() != 1) {
        return VISIT_SUBTREE;
    }
    final MethodInvocation mi = asExpression(stmts.get(0), MethodInvocation.class);
    final IVariableBinding foreachVariable = node.getParameter().resolveBinding();
    // As we replace only one, there should be no more than one occurrence
    if (getVariableUseCount(foreachVariable, node.getBody()) == 1) {
        if (instanceOf(iterable, "java.util.Collection")) {
            if (isMethod(mi, "java.util.Collection", "add", "java.lang.Object")) {
                return maybeReplaceWithCollectionMethod(node, iterable, "addAll", mi);
            } else if (isMethod(mi, "java.util.Collection", "remove", "java.lang.Object")) {
                return maybeReplaceWithCollectionMethod(node, iterable, "removeAll", mi);
            }
        } else if (isArray(iterable) && isMethod(mi, "java.util.Collection", "add", "java.lang.Object") && areTypeCompatible(mi.getExpression(), iterable) && isSameLocalVariable(foreachVariable, arg0(mi))) {
            replaceWithCollectionsAddAll(node, iterable, mi);
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    return VISIT_SUBTREE;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) ASTHelper.asExpression(org.autorefactor.refactoring.ASTHelper.asExpression) Statement(org.eclipse.jdt.core.dom.Statement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 45 with IVariableBinding

use of org.eclipse.jdt.core.dom.IVariableBinding in project j2objc by google.

the class JdtTypeElement method getEnclosedElements.

@Override
public List<? extends Element> getEnclosedElements() {
    ITypeBinding decl = (ITypeBinding) binding;
    List<Element> toReturn = new ArrayList<>();
    for (IVariableBinding i : decl.getDeclaredFields()) {
        toReturn.add(BindingConverter.getElement(i));
    }
    for (IMethodBinding i : decl.getDeclaredMethods()) {
        toReturn.add(BindingConverter.getElement(i));
    }
    for (ITypeBinding i : decl.getDeclaredTypes()) {
        toReturn.add(BindingConverter.getElement(i));
    }
    return toReturn;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) TypeParameterElement(javax.lang.model.element.TypeParameterElement) ArrayList(java.util.ArrayList) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Aggregations

IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)106 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)49 IBinding (org.eclipse.jdt.core.dom.IBinding)48 SimpleName (org.eclipse.jdt.core.dom.SimpleName)43 ASTNode (org.eclipse.jdt.core.dom.ASTNode)30 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)29 Expression (org.eclipse.jdt.core.dom.Expression)24 ArrayList (java.util.ArrayList)22 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)16 Name (org.eclipse.jdt.core.dom.Name)13 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)13 Assignment (org.eclipse.jdt.core.dom.Assignment)12 CastExpression (org.eclipse.jdt.core.dom.CastExpression)12 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)12 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)12 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)12 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)12 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)11 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)11 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)11