Search in sources :

Example 61 with IVariableBinding

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

the class ExtractMethodAnalyzer method computeOutput.

private void computeOutput(RefactoringStatus status) {
    // First find all writes inside the selection.
    FlowContext flowContext = new FlowContext(0, fMaxVariableId + 1);
    flowContext.setConsiderAccessMode(true);
    flowContext.setComputeMode(FlowContext.RETURN_VALUES);
    FlowInfo returnInfo = new InOutFlowAnalyzer(flowContext).perform(getSelectedNodes());
    IVariableBinding[] returnValues = returnInfo.get(flowContext, FlowInfo.WRITE | FlowInfo.WRITE_POTENTIAL | FlowInfo.UNKNOWN);
    // Compute a selection that exactly covers the selected nodes
    IRegion region = getSelectedNodeRange();
    Selection selection = Selection.createFromStartLength(region.getOffset(), region.getLength());
    List<IVariableBinding> localReads = new ArrayList<IVariableBinding>();
    flowContext.setComputeMode(FlowContext.ARGUMENTS);
    FlowInfo argInfo = new InputFlowAnalyzer(flowContext, selection, true).perform(fEnclosingBodyDeclaration);
    IVariableBinding[] reads = argInfo.get(flowContext, FlowInfo.READ | FlowInfo.READ_POTENTIAL | FlowInfo.UNKNOWN);
    outer: for (int i = 0; i < returnValues.length && localReads.size() < returnValues.length; i++) {
        IVariableBinding binding = returnValues[i];
        for (int x = 0; x < reads.length; x++) {
            if (reads[x] == binding) {
                localReads.add(binding);
                fReturnValue = binding;
                continue outer;
            }
        }
    }
    switch(localReads.size()) {
        case 0:
            fReturnValue = null;
            break;
        case 1:
            break;
        default:
            fReturnValue = null;
            StringBuffer affectedLocals = new StringBuffer();
            for (int i = 0; i < localReads.size(); i++) {
                IVariableBinding binding = localReads.get(i);
                String bindingName = BindingLabelProvider.getBindingLabel(binding, BindingLabelProvider.DEFAULT_TEXTFLAGS | JavaElementLabels.F_PRE_TYPE_SIGNATURE);
                affectedLocals.append(bindingName);
                if (i != localReads.size() - 1) {
                    affectedLocals.append('\n');
                }
            }
            String message = MessageFormat.format(RefactoringCoreMessages.ExtractMethodAnalyzer_assignments_to_local, new Object[] { affectedLocals.toString() });
            status.addFatalError(message, JavaStatusContext.create(fCUnit, getSelection()));
            return;
    }
    List<IVariableBinding> callerLocals = new ArrayList<IVariableBinding>(5);
    FlowInfo localInfo = new InputFlowAnalyzer(flowContext, selection, false).perform(fEnclosingBodyDeclaration);
    IVariableBinding[] writes = localInfo.get(flowContext, FlowInfo.WRITE | FlowInfo.WRITE_POTENTIAL | FlowInfo.UNKNOWN);
    for (int i = 0; i < writes.length; i++) {
        IVariableBinding write = writes[i];
        if (getSelection().covers(ASTNodes.findDeclaration(write, fEnclosingBodyDeclaration)))
            callerLocals.add(write);
    }
    fCallerLocals = callerLocals.toArray(new IVariableBinding[callerLocals.size()]);
    if (fReturnValue != null && getSelection().covers(ASTNodes.findDeclaration(fReturnValue, fEnclosingBodyDeclaration)))
        fReturnLocal = fReturnValue;
}
Also used : Selection(org.eclipse.jdt.internal.corext.dom.Selection) InputFlowAnalyzer(org.eclipse.jdt.internal.corext.refactoring.code.flow.InputFlowAnalyzer) ArrayList(java.util.ArrayList) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) FlowContext(org.eclipse.jdt.internal.corext.refactoring.code.flow.FlowContext) InOutFlowAnalyzer(org.eclipse.jdt.internal.corext.refactoring.code.flow.InOutFlowAnalyzer) IRegion(org.eclipse.jface.text.IRegion) FlowInfo(org.eclipse.jdt.internal.corext.refactoring.code.flow.FlowInfo)

Example 62 with IVariableBinding

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

the class ExtractMethodAnalyzer method computeTypeVariables.

private ITypeBinding[] computeTypeVariables(ITypeBinding[] bindings) {
    Selection selection = getSelection();
    Set<ITypeBinding> result = new HashSet<ITypeBinding>();
    // first remove all type variables that come from outside of the method
    // or are covered by the selection
    CompilationUnit compilationUnit = (CompilationUnit) fEnclosingBodyDeclaration.getRoot();
    for (int i = 0; i < bindings.length; i++) {
        ASTNode decl = compilationUnit.findDeclaringNode(bindings[i]);
        if (decl == null || (!selection.covers(decl) && decl.getParent() instanceof MethodDeclaration))
            result.add(bindings[i]);
    }
    // all all type variables which are needed since a local variable uses it
    for (int i = 0; i < fArguments.length; i++) {
        IVariableBinding arg = fArguments[i];
        ITypeBinding type = arg.getType();
        if (type != null && type.isTypeVariable()) {
            ASTNode decl = compilationUnit.findDeclaringNode(type);
            if (decl == null || (!selection.covers(decl) && decl.getParent() instanceof MethodDeclaration))
                result.add(type);
        }
    }
    return result.toArray(new ITypeBinding[result.size()]);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) Selection(org.eclipse.jdt.internal.corext.dom.Selection) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) HashSet(java.util.HashSet)

Example 63 with IVariableBinding

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

the class ConvertAnonymousToNestedRefactoring method getUsedLocalVariables.

private IVariableBinding[] getUsedLocalVariables() {
    final Set<IBinding> result = new HashSet<IBinding>(0);
    collectRefrencedVariables(fAnonymousInnerClassNode, result);
    ArrayList<IVariableBinding> usedLocals = new ArrayList<IVariableBinding>();
    for (Iterator<IBinding> iterator = result.iterator(); iterator.hasNext(); ) {
        IVariableBinding next = (IVariableBinding) iterator.next();
        if (isBindingToTemp(next)) {
            usedLocals.add(next);
        }
    }
    return usedLocals.toArray(new IVariableBinding[usedLocals.size()]);
}
Also used : IBinding(org.eclipse.jdt.core.dom.IBinding) ArrayList(java.util.ArrayList) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) HashSet(java.util.HashSet)

Example 64 with IVariableBinding

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

the class ConvertAnonymousToNestedRefactoring method getAllEnclosingAnonymousTypesField.

private List<IVariableBinding> getAllEnclosingAnonymousTypesField() {
    final List<IVariableBinding> ans = new ArrayList<IVariableBinding>();
    final AbstractTypeDeclaration declaration = (AbstractTypeDeclaration) ASTNodes.getParent(fAnonymousInnerClassNode, AbstractTypeDeclaration.class);
    AnonymousClassDeclaration anonymous = (AnonymousClassDeclaration) ASTNodes.getParent(fAnonymousInnerClassNode, ASTNode.ANONYMOUS_CLASS_DECLARATION);
    while (anonymous != null) {
        if (ASTNodes.isParent(anonymous, declaration)) {
            ITypeBinding binding = anonymous.resolveBinding();
            if (binding != null) {
                ans.addAll(Arrays.asList(binding.getDeclaredFields()));
            }
        } else {
            break;
        }
        anonymous = (AnonymousClassDeclaration) ASTNodes.getParent(anonymous, ASTNode.ANONYMOUS_CLASS_DECLARATION);
    }
    return ans;
}
Also used : ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ArrayList(java.util.ArrayList) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 65 with IVariableBinding

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

the class RemoveDeclarationCorrectionProposal method getRewrite.

/*(non-Javadoc)
	 * @see org.eclipse.jdt.internal.ui.text.correction.ASTRewriteCorrectionProposal#getRewrite()
	 */
@Override
protected ASTRewrite getRewrite() {
    IBinding binding = fName.resolveBinding();
    CompilationUnit root = (CompilationUnit) fName.getRoot();
    ASTRewrite rewrite;
    if (binding.getKind() == IBinding.METHOD) {
        IMethodBinding decl = ((IMethodBinding) binding).getMethodDeclaration();
        ASTNode declaration = root.findDeclaringNode(decl);
        rewrite = ASTRewrite.create(root.getAST());
        rewrite.remove(declaration, null);
    } else if (binding.getKind() == IBinding.TYPE) {
        ITypeBinding decl = ((ITypeBinding) binding).getTypeDeclaration();
        ASTNode declaration = root.findDeclaringNode(decl);
        rewrite = ASTRewrite.create(root.getAST());
        rewrite.remove(declaration, null);
    } else if (binding.getKind() == IBinding.VARIABLE) {
        // needs full AST
        CompilationUnit completeRoot = SharedASTProvider.getAST(getCompilationUnit(), SharedASTProvider.WAIT_YES, null);
        SimpleName nameNode = (SimpleName) NodeFinder.perform(completeRoot, fName.getStartPosition(), fName.getLength());
        rewrite = ASTRewrite.create(completeRoot.getAST());
        SimpleName[] references = LinkedNodeFinder.findByBinding(completeRoot, nameNode.resolveBinding());
        for (int i = 0; i < references.length; i++) {
            removeVariableReferences(rewrite, references[i]);
        }
        IVariableBinding bindingDecl = ((IVariableBinding) nameNode.resolveBinding()).getVariableDeclaration();
        ASTNode declaringNode = completeRoot.findDeclaringNode(bindingDecl);
        if (declaringNode instanceof SingleVariableDeclaration) {
            removeParamTag(rewrite, (SingleVariableDeclaration) declaringNode);
        }
    } else {
        //$NON-NLS-1$
        throw new IllegalArgumentException("Unexpected binding");
    }
    return rewrite;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) IBinding(org.eclipse.jdt.core.dom.IBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) 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