Search in sources :

Example 16 with Initializer

use of org.eclipse.jdt.core.dom.Initializer in project flux by eclipse.

the class ScopeAnalyzer method addLocalDeclarations.

private boolean addLocalDeclarations(ASTNode node, int offset, int flags, IBindingRequestor requestor) {
    if (hasFlag(VARIABLES, flags) || hasFlag(TYPES, flags)) {
        BodyDeclaration declaration = ASTResolving.findParentBodyDeclaration(node);
        if (declaration instanceof MethodDeclaration || declaration instanceof Initializer || declaration instanceof FieldDeclaration) {
            ScopeAnalyzerVisitor visitor = new ScopeAnalyzerVisitor(offset, flags, requestor);
            declaration.accept(visitor);
            return visitor.fBreak;
        }
    }
    return false;
}
Also used : Initializer(org.eclipse.jdt.core.dom.Initializer) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 17 with Initializer

use of org.eclipse.jdt.core.dom.Initializer in project eclipse.jdt.ls by eclipse.

the class NavigateToDefinitionHandler method computeBreakContinue.

private Location computeBreakContinue(ITypeRoot typeRoot, int line, int column) throws CoreException {
    int offset = JsonRpcHelpers.toOffset(typeRoot.getBuffer(), line, column);
    if (offset >= 0) {
        CompilationUnit unit = SharedASTProviderCore.getAST(typeRoot, SharedASTProviderCore.WAIT_YES, null);
        if (unit == null) {
            return null;
        }
        ASTNode selectedNode = NodeFinder.perform(unit, offset, 0);
        ASTNode node = null;
        SimpleName label = null;
        if (selectedNode instanceof BreakStatement) {
            node = selectedNode;
            label = ((BreakStatement) node).getLabel();
        } else if (selectedNode instanceof ContinueStatement) {
            node = selectedNode;
            label = ((ContinueStatement) node).getLabel();
        } else if (selectedNode instanceof SimpleName && selectedNode.getParent() instanceof BreakStatement) {
            node = selectedNode.getParent();
            label = ((BreakStatement) node).getLabel();
        } else if (selectedNode instanceof SimpleName && selectedNode.getParent() instanceof ContinueStatement) {
            node = selectedNode.getParent();
            label = ((ContinueStatement) node).getLabel();
        }
        if (node != null) {
            ASTNode parent = node.getParent();
            ASTNode target = null;
            while (parent != null) {
                if (parent instanceof MethodDeclaration || parent instanceof Initializer) {
                    break;
                }
                if (label == null) {
                    if (parent instanceof ForStatement || parent instanceof EnhancedForStatement || parent instanceof WhileStatement || parent instanceof DoStatement) {
                        target = parent;
                        break;
                    }
                    if (node instanceof BreakStatement) {
                        if (parent instanceof SwitchStatement || parent instanceof SwitchExpression) {
                            target = parent;
                            break;
                        }
                    }
                    if (node instanceof LabeledStatement) {
                        target = parent;
                        break;
                    }
                } else if (LabeledStatement.class.isInstance(parent)) {
                    LabeledStatement ls = (LabeledStatement) parent;
                    if (ls.getLabel().getIdentifier().equals(label.getIdentifier())) {
                        target = ls;
                        break;
                    }
                }
                parent = parent.getParent();
            }
            if (target != null) {
                int start = target.getStartPosition();
                int end = new TokenScanner(unit.getTypeRoot()).getNextEndOffset(node.getStartPosition(), true) - start;
                if (start >= 0 && end >= 0) {
                    return JDTUtils.toLocation((ICompilationUnit) typeRoot, start, end);
                }
            }
        }
    }
    return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) TokenScanner(org.eclipse.jdt.internal.corext.dom.TokenScanner) DoStatement(org.eclipse.jdt.core.dom.DoStatement) SwitchExpression(org.eclipse.jdt.core.dom.SwitchExpression) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) Initializer(org.eclipse.jdt.core.dom.Initializer) ASTNode(org.eclipse.jdt.core.dom.ASTNode) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement)

Example 18 with Initializer

use of org.eclipse.jdt.core.dom.Initializer in project eclipse.jdt.ls by eclipse.

the class LocalCorrectionsSubProcessor method addConstructorFromSuperclassProposal.

public static void addConstructorFromSuperclassProposal(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) throws CoreException {
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (selectedNode == null) {
        return;
    }
    TypeDeclaration typeDeclaration = null;
    if (selectedNode.getLocationInParent() == TypeDeclaration.NAME_PROPERTY) {
        typeDeclaration = (TypeDeclaration) selectedNode.getParent();
    } else {
        BodyDeclaration declaration = ASTResolving.findParentBodyDeclaration(selectedNode);
        if (declaration instanceof Initializer && problem.getProblemId() == IProblem.UnhandledExceptionInDefaultConstructor) {
            addUncaughtExceptionProposals(context, problem, proposals);
        }
        return;
    }
    ITypeBinding binding = typeDeclaration.resolveBinding();
    if (binding == null || binding.getSuperclass() == null) {
        return;
    }
    ICompilationUnit cu = context.getCompilationUnit();
    IMethodBinding[] methods = binding.getSuperclass().getDeclaredMethods();
    for (int i = 0; i < methods.length; i++) {
        IMethodBinding curr = methods[i];
        if (curr.isConstructor() && !Modifier.isPrivate(curr.getModifiers())) {
            proposals.add(new ConstructorFromSuperclassProposal(cu, typeDeclaration, curr, IProposalRelevance.ADD_CONSTRUCTOR_FROM_SUPER_CLASS));
        }
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) Initializer(org.eclipse.jdt.core.dom.Initializer) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 19 with Initializer

use of org.eclipse.jdt.core.dom.Initializer in project eclipse.jdt.ls by eclipse.

the class ExtractMethodAnalyzer method computeLastStatementSelected.

private void computeLastStatementSelected() {
    ASTNode[] nodes = getSelectedNodes();
    if (nodes.length == 0) {
        fIsLastStatementSelected = false;
    } else {
        Block body = null;
        LambdaExpression enclosingLambdaExpr = ASTResolving.findEnclosingLambdaExpression(getFirstSelectedNode());
        if (enclosingLambdaExpr != null) {
            ASTNode lambdaBody = enclosingLambdaExpr.getBody();
            if (lambdaBody instanceof Block) {
                body = (Block) lambdaBody;
            } else {
                fIsLastStatementSelected = true;
                return;
            }
        } else {
            if (fEnclosingBodyDeclaration instanceof MethodDeclaration) {
                body = ((MethodDeclaration) fEnclosingBodyDeclaration).getBody();
            } else if (fEnclosingBodyDeclaration instanceof Initializer) {
                body = ((Initializer) fEnclosingBodyDeclaration).getBody();
            }
        }
        if (body != null) {
            List<Statement> statements = body.statements();
            if (statements.size() > 0) {
                fIsLastStatementSelected = nodes[nodes.length - 1] == statements.get(statements.size() - 1);
            } else {
                fIsLastStatementSelected = true;
            }
        }
    }
}
Also used : Initializer(org.eclipse.jdt.core.dom.Initializer) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) 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) TryStatement(org.eclipse.jdt.core.dom.TryStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression)

Example 20 with Initializer

use of org.eclipse.jdt.core.dom.Initializer in project eclipse.jdt.ls by eclipse.

the class ExtractConstantRefactoring method computeConstantDeclarationLocation.

private void computeConstantDeclarationLocation() throws JavaModelException {
    if (isDeclarationLocationComputed()) {
        return;
    }
    BodyDeclaration lastStaticDependency = null;
    Iterator<BodyDeclaration> decls = getContainingTypeDeclarationNode().bodyDeclarations().iterator();
    while (decls.hasNext()) {
        BodyDeclaration decl = decls.next();
        int modifiers;
        if (decl instanceof FieldDeclaration) {
            modifiers = ((FieldDeclaration) decl).getModifiers();
        } else if (decl instanceof Initializer) {
            modifiers = ((Initializer) decl).getModifiers();
        } else {
            continue;
        /* this declaration is not a field declaration
							    or initializer, so the placement of the constant
							    declaration relative to it does not matter */
        }
        if (Modifier.isStatic(modifiers) && depends(getSelectedExpression(), decl)) {
            lastStaticDependency = decl;
        }
    }
    if (lastStaticDependency == null) {
        fInsertFirst = true;
    } else {
        fToInsertAfter = lastStaticDependency;
    }
}
Also used : Initializer(org.eclipse.jdt.core.dom.Initializer) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Aggregations

Initializer (org.eclipse.jdt.core.dom.Initializer)23 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)18 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)18 ASTNode (org.eclipse.jdt.core.dom.ASTNode)17 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)11 AST (org.eclipse.jdt.core.dom.AST)8 Block (org.eclipse.jdt.core.dom.Block)8 SimpleName (org.eclipse.jdt.core.dom.SimpleName)8 Statement (org.eclipse.jdt.core.dom.Statement)7 Expression (org.eclipse.jdt.core.dom.Expression)6 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)6 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)6 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)6 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)6 Assignment (org.eclipse.jdt.core.dom.Assignment)5 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)5 ForStatement (org.eclipse.jdt.core.dom.ForStatement)5 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)5 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)5 TryStatement (org.eclipse.jdt.core.dom.TryStatement)4