Search in sources :

Example 36 with IVariableBinding

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

the class ImportRemover method applyRemoves.

public void applyRemoves(ImportRewrite importRewrite) {
    IBinding[] bindings = getImportsToRemove();
    for (int i = 0; i < bindings.length; i++) {
        if (bindings[i] instanceof ITypeBinding) {
            ITypeBinding typeBinding = (ITypeBinding) bindings[i];
            importRewrite.removeImport(typeBinding.getTypeDeclaration().getQualifiedName());
        } else if (bindings[i] instanceof IMethodBinding) {
            IMethodBinding binding = (IMethodBinding) bindings[i];
            importRewrite.removeStaticImport(binding.getDeclaringClass().getQualifiedName() + '.' + binding.getName());
        } else if (bindings[i] instanceof IVariableBinding) {
            IVariableBinding binding = (IVariableBinding) bindings[i];
            importRewrite.removeStaticImport(binding.getDeclaringClass().getQualifiedName() + '.' + binding.getName());
        }
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) IBinding(org.eclipse.jdt.core.dom.IBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 37 with IVariableBinding

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

the class RemoveDeclarationCorrectionProposal method getName.

@Override
public String getName() {
    IBinding binding = fName.resolveBinding();
    String name = BasicElementLabels.getJavaElementName(fName.getIdentifier());
    switch(binding.getKind()) {
        case IBinding.TYPE:
            return Messages.format(CorrectionMessages.RemoveDeclarationCorrectionProposal_removeunusedtype_description, name);
        case IBinding.METHOD:
            if (((IMethodBinding) binding).isConstructor()) {
                return Messages.format(CorrectionMessages.RemoveDeclarationCorrectionProposal_removeunusedconstructor_description, name);
            } else {
                return Messages.format(CorrectionMessages.RemoveDeclarationCorrectionProposal_removeunusedmethod_description, name);
            }
        case IBinding.VARIABLE:
            if (((IVariableBinding) binding).isField()) {
                return Messages.format(CorrectionMessages.RemoveDeclarationCorrectionProposal_removeunusedfield_description, name);
            } else {
                return Messages.format(CorrectionMessages.RemoveDeclarationCorrectionProposal_removeunusedvar_description, name);
            }
        default:
            return super.getDisplayString();
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) IBinding(org.eclipse.jdt.core.dom.IBinding) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 38 with IVariableBinding

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

the class BindingLabelProvider method getAdornmentFlags.

/*extends LabelProvider */
private static int getAdornmentFlags(IBinding binding) {
    int adornments = 0;
    final int modifiers = binding.getModifiers();
    if (Modifier.isAbstract(modifiers))
        adornments |= JavaElementImageDescriptor.ABSTRACT;
    if (Modifier.isFinal(modifiers))
        adornments |= JavaElementImageDescriptor.FINAL;
    if (Modifier.isStatic(modifiers))
        adornments |= JavaElementImageDescriptor.STATIC;
    if (binding.isDeprecated())
        adornments |= JavaElementImageDescriptor.DEPRECATED;
    if (binding instanceof IMethodBinding) {
        if (((IMethodBinding) binding).isConstructor())
            adornments |= JavaElementImageDescriptor.CONSTRUCTOR;
        if (Modifier.isSynchronized(modifiers))
            adornments |= JavaElementImageDescriptor.SYNCHRONIZED;
        if (Modifier.isNative(modifiers))
            adornments |= JavaElementImageDescriptor.NATIVE;
        ITypeBinding type = ((IMethodBinding) binding).getDeclaringClass();
        if (type.isInterface() && !Modifier.isAbstract(modifiers) && !Modifier.isStatic(modifiers))
            adornments |= JavaElementImageDescriptor.DEFAULT_METHOD;
        if (((IMethodBinding) binding).getDefaultValue() != null)
            adornments |= JavaElementImageDescriptor.ANNOTATION_DEFAULT;
    }
    if (binding instanceof IVariableBinding && ((IVariableBinding) binding).isField()) {
        if (Modifier.isTransient(modifiers))
            adornments |= JavaElementImageDescriptor.TRANSIENT;
        if (Modifier.isVolatile(modifiers))
            adornments |= JavaElementImageDescriptor.VOLATILE;
    }
    return adornments;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) Point(org.eclipse.swt.graphics.Point)

Example 39 with IVariableBinding

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

the class ExtractMethodRefactoring method replaceBranches.

private void replaceBranches(final CompilationUnitChange result) {
    ASTNode[] selectedNodes = fAnalyzer.getSelectedNodes();
    for (int i = 0; i < selectedNodes.length; i++) {
        ASTNode astNode = selectedNodes[i];
        astNode.accept(new ASTVisitor() {

            private LinkedList<String> fOpenLoopLabels = new LinkedList<String>();

            private void registerLoopLabel(Statement node) {
                String identifier;
                if (node.getParent() instanceof LabeledStatement) {
                    LabeledStatement labeledStatement = (LabeledStatement) node.getParent();
                    identifier = labeledStatement.getLabel().getIdentifier();
                } else {
                    identifier = null;
                }
                fOpenLoopLabels.add(identifier);
            }

            @Override
            public boolean visit(ForStatement node) {
                registerLoopLabel(node);
                return super.visit(node);
            }

            @Override
            public void endVisit(ForStatement node) {
                fOpenLoopLabels.removeLast();
            }

            @Override
            public boolean visit(WhileStatement node) {
                registerLoopLabel(node);
                return super.visit(node);
            }

            @Override
            public void endVisit(WhileStatement node) {
                fOpenLoopLabels.removeLast();
            }

            @Override
            public boolean visit(EnhancedForStatement node) {
                registerLoopLabel(node);
                return super.visit(node);
            }

            @Override
            public void endVisit(EnhancedForStatement node) {
                fOpenLoopLabels.removeLast();
            }

            @Override
            public boolean visit(DoStatement node) {
                registerLoopLabel(node);
                return super.visit(node);
            }

            @Override
            public void endVisit(DoStatement node) {
                fOpenLoopLabels.removeLast();
            }

            @Override
            public void endVisit(ContinueStatement node) {
                final SimpleName label = node.getLabel();
                if (fOpenLoopLabels.isEmpty() || (label != null && !fOpenLoopLabels.contains(label.getIdentifier()))) {
                    TextEditGroup description = new TextEditGroup(RefactoringCoreMessages.ExtractMethodRefactoring_replace_continue);
                    result.addTextEditGroup(description);
                    ReturnStatement rs = fAST.newReturnStatement();
                    IVariableBinding returnValue = fAnalyzer.getReturnValue();
                    if (returnValue != null) {
                        rs.setExpression(fAST.newSimpleName(getName(returnValue)));
                    }
                    fRewriter.replace(node, rs, description);
                }
            }
        });
    }
}
Also used : DoStatement(org.eclipse.jdt.core.dom.DoStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) SimpleName(org.eclipse.jdt.core.dom.SimpleName) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) LinkedList(java.util.LinkedList) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) TextEditGroup(org.eclipse.text.edits.TextEditGroup) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement)

Example 40 with IVariableBinding

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

the class ExtractMethodRefactoring method createCallNodes.

//---- Code generation -----------------------------------------------------------------------
private ASTNode[] createCallNodes(SnippetFinder.Match duplicate, int modifiers) {
    List<ASTNode> result = new ArrayList<ASTNode>(2);
    IVariableBinding[] locals = fAnalyzer.getCallerLocals();
    for (int i = 0; i < locals.length; i++) {
        result.add(createDeclaration(locals[i], null));
    }
    MethodInvocation invocation = fAST.newMethodInvocation();
    invocation.setName(fAST.newSimpleName(fMethodName));
    ASTNode typeNode = ASTResolving.findParentType(fAnalyzer.getEnclosingBodyDeclaration());
    RefactoringStatus status = new RefactoringStatus();
    while (fDestination != typeNode) {
        fAnalyzer.checkInput(status, fMethodName, typeNode);
        if (!status.isOK()) {
            SimpleName destinationTypeName = fAST.newSimpleName(ASTNodes.getEnclosingType(fDestination).getName());
            if ((modifiers & Modifier.STATIC) == 0) {
                ThisExpression thisExpression = fAST.newThisExpression();
                thisExpression.setQualifier(destinationTypeName);
                invocation.setExpression(thisExpression);
            } else {
                invocation.setExpression(destinationTypeName);
            }
            break;
        }
        typeNode = typeNode.getParent();
    }
    List<Expression> arguments = invocation.arguments();
    for (int i = 0; i < fParameterInfos.size(); i++) {
        ParameterInfo parameter = fParameterInfos.get(i);
        arguments.add(ASTNodeFactory.newName(fAST, getMappedName(duplicate, parameter)));
    }
    if (fLinkedProposalModel != null) {
        LinkedProposalPositionGroup nameGroup = fLinkedProposalModel.getPositionGroup(KEY_NAME, true);
        nameGroup.addPosition(fRewriter.track(invocation.getName()), false);
    }
    ASTNode call;
    int returnKind = fAnalyzer.getReturnKind();
    switch(returnKind) {
        case ExtractMethodAnalyzer.ACCESS_TO_LOCAL:
            IVariableBinding binding = fAnalyzer.getReturnLocal();
            if (binding != null) {
                VariableDeclarationStatement decl = createDeclaration(getMappedBinding(duplicate, binding), invocation);
                call = decl;
            } else {
                Assignment assignment = fAST.newAssignment();
                assignment.setLeftHandSide(ASTNodeFactory.newName(fAST, getMappedBinding(duplicate, fAnalyzer.getReturnValue()).getName()));
                assignment.setRightHandSide(invocation);
                call = assignment;
            }
            break;
        case ExtractMethodAnalyzer.RETURN_STATEMENT_VALUE:
            ReturnStatement rs = fAST.newReturnStatement();
            rs.setExpression(invocation);
            call = rs;
            break;
        default:
            call = invocation;
    }
    if (call instanceof Expression && !fAnalyzer.isExpressionSelected()) {
        call = fAST.newExpressionStatement((Expression) call);
    }
    result.add(call);
    // return;
    if (returnKind == ExtractMethodAnalyzer.RETURN_STATEMENT_VOID && !fAnalyzer.isLastStatementSelected()) {
        result.add(fAST.newReturnStatement());
    }
    return result.toArray(new ASTNode[result.size()]);
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) Assignment(org.eclipse.jdt.core.dom.Assignment) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) LinkedProposalPositionGroup(org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup)

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