Search in sources :

Example 6 with Selection

use of org.eclipse.jdt.internal.corext.dom.Selection in project che by eclipse.

the class JavaTextSelection method resolveSelectedNodes.

public ASTNode[] resolveSelectedNodes() {
    if (fNodesRequested)
        return fSelectedNodes;
    fNodesRequested = true;
    CompilationUnit root = resolvePartialAstAtOffset();
    if (root == null)
        return null;
    Selection ds = Selection.createFromStartLength(getOffset(), getLength());
    SelectionAnalyzer analyzer = new SelectionAnalyzer(ds, false);
    root.accept(analyzer);
    fSelectedNodes = analyzer.getSelectedNodes();
    fCoveringNode = analyzer.getLastCoveringNode();
    return fSelectedNodes;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) SelectionAnalyzer(org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer) Selection(org.eclipse.jdt.internal.corext.dom.Selection) TextSelection(org.eclipse.che.jface.text.TextSelection)

Example 7 with Selection

use of org.eclipse.jdt.internal.corext.dom.Selection in project che by eclipse.

the class TypeContextChecker method parseType.

private static Type parseType(String typeString, IJavaProject javaProject, List<String> problemsCollector) {
    if (//speed up for a common case //$NON-NLS-1$
    "".equals(typeString.trim()))
        return null;
    if (!typeString.trim().equals(typeString))
        return null;
    StringBuffer cuBuff = new StringBuffer();
    //$NON-NLS-1$
    cuBuff.append("interface A{");
    int offset = cuBuff.length();
    //$NON-NLS-1$
    cuBuff.append(typeString).append(" m();}");
    ASTParser p = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
    p.setSource(cuBuff.toString().toCharArray());
    p.setProject(javaProject);
    CompilationUnit cu = (CompilationUnit) p.createAST(null);
    Selection selection = Selection.createFromStartLength(offset, typeString.length());
    SelectionAnalyzer analyzer = new SelectionAnalyzer(selection, false);
    cu.accept(analyzer);
    ASTNode selected = analyzer.getFirstSelectedNode();
    if (!(selected instanceof Type))
        return null;
    Type type = (Type) selected;
    if (MethodTypesSyntaxChecker.isVoidArrayType(type))
        return null;
    IProblem[] problems = ASTNodes.getProblems(type, ASTNodes.NODE_ONLY, ASTNodes.PROBLEMS);
    if (problems.length > 0) {
        for (int i = 0; i < problems.length; i++) problemsCollector.add(problems[i].getMessage());
    }
    String typeNodeRange = cuBuff.substring(type.getStartPosition(), ASTNodes.getExclusiveEnd(type));
    if (typeString.equals(typeNodeRange))
        return type;
    else
        return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) SelectionAnalyzer(org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) IType(org.eclipse.jdt.core.IType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) Type(org.eclipse.jdt.core.dom.Type) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) Selection(org.eclipse.jdt.internal.corext.dom.Selection) ASTNode(org.eclipse.jdt.core.dom.ASTNode) RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser) ASTParser(org.eclipse.jdt.core.dom.ASTParser) IProblem(org.eclipse.jdt.core.compiler.IProblem)

Example 8 with Selection

use of org.eclipse.jdt.internal.corext.dom.Selection in project che by eclipse.

the class ExtractMethodAnalyzer method visit.

@Override
public boolean visit(MethodDeclaration node) {
    Block body = node.getBody();
    if (body == null)
        return false;
    Selection selection = getSelection();
    int nodeStart = body.getStartPosition();
    int nodeExclusiveEnd = nodeStart + body.getLength();
    // if selection node inside of the method body ignore method
    if (!(nodeStart < selection.getOffset() && selection.getExclusiveEnd() < nodeExclusiveEnd))
        return false;
    return super.visit(node);
}
Also used : Selection(org.eclipse.jdt.internal.corext.dom.Selection) Block(org.eclipse.jdt.core.dom.Block)

Example 9 with Selection

use of org.eclipse.jdt.internal.corext.dom.Selection in project che by eclipse.

the class ExtractMethodAnalyzer method removeSelectedDeclarations.

private IVariableBinding[] removeSelectedDeclarations(IVariableBinding[] bindings) {
    List<IVariableBinding> result = new ArrayList<IVariableBinding>(bindings.length);
    Selection selection = getSelection();
    for (int i = 0; i < bindings.length; i++) {
        ASTNode decl = ((CompilationUnit) fEnclosingBodyDeclaration.getRoot()).findDeclaringNode(bindings[i]);
        if (!selection.covers(decl))
            result.add(bindings[i]);
    }
    return result.toArray(new IVariableBinding[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) ArrayList(java.util.ArrayList) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 10 with Selection

use of org.eclipse.jdt.internal.corext.dom.Selection 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)

Aggregations

Selection (org.eclipse.jdt.internal.corext.dom.Selection)11 ASTNode (org.eclipse.jdt.core.dom.ASTNode)7 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)6 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)5 SelectionAnalyzer (org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer)4 ASTParser (org.eclipse.jdt.core.dom.ASTParser)3 Expression (org.eclipse.jdt.core.dom.Expression)3 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)3 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)3 RefactoringASTParser (org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser)3 ArrayList (java.util.ArrayList)2 Block (org.eclipse.jdt.core.dom.Block)2 FlowContext (org.eclipse.jdt.internal.corext.refactoring.code.flow.FlowContext)2 InputFlowAnalyzer (org.eclipse.jdt.internal.corext.refactoring.code.flow.InputFlowAnalyzer)2 HashSet (java.util.HashSet)1 TextSelection (org.eclipse.che.jface.text.TextSelection)1 CoreException (org.eclipse.core.runtime.CoreException)1 IType (org.eclipse.jdt.core.IType)1 IProblem (org.eclipse.jdt.core.compiler.IProblem)1 ArrayType (org.eclipse.jdt.core.dom.ArrayType)1