Search in sources :

Example 91 with Block

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

the class ChangeSignatureProcessor method containsImplicitCallToSuperConstructor.

private static boolean containsImplicitCallToSuperConstructor(MethodDeclaration constructor) {
    Assert.isTrue(constructor.isConstructor());
    Block body = constructor.getBody();
    if (body == null)
        return false;
    if (body.statements().size() == 0)
        return true;
    if (body.statements().get(0) instanceof ConstructorInvocation)
        return false;
    if (body.statements().get(0) instanceof SuperConstructorInvocation)
        return false;
    return true;
}
Also used : SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) ConstructorInvocation(org.eclipse.jdt.core.dom.ConstructorInvocation) Block(org.eclipse.jdt.core.dom.Block) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation)

Example 92 with Block

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

the class ChangeSignatureProcessor method addNewConstructorToSubclass.

private void addNewConstructorToSubclass(AbstractTypeDeclaration subclass, CompilationUnitRewrite cuRewrite) {
    AST ast = subclass.getAST();
    MethodDeclaration newConstructor = ast.newMethodDeclaration();
    newConstructor.setName(ast.newSimpleName(subclass.getName().getIdentifier()));
    newConstructor.setConstructor(true);
    newConstructor.setJavadoc(null);
    newConstructor.modifiers().addAll(ASTNodeFactory.newModifiers(ast, getAccessModifier(subclass)));
    newConstructor.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID));
    Block body = ast.newBlock();
    newConstructor.setBody(body);
    SuperConstructorInvocation superCall = ast.newSuperConstructorInvocation();
    addArgumentsToNewSuperConstructorCall(superCall, cuRewrite);
    body.statements().add(superCall);
    String msg = RefactoringCoreMessages.ChangeSignatureRefactoring_add_constructor;
    TextEditGroup description = cuRewrite.createGroupDescription(msg);
    cuRewrite.getASTRewrite().getListRewrite(subclass, subclass.getBodyDeclarationsProperty()).insertFirst(newConstructor, description);
// TODO use AbstractTypeDeclaration
}
Also used : AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) Block(org.eclipse.jdt.core.dom.Block) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 93 with Block

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

the class SelfEncapsulateFieldRefactoring method createSetterMethod.

private MethodDeclaration createSetterMethod(AST ast, ASTRewrite rewriter, String lineDelimiter) throws CoreException {
    FieldDeclaration field = (FieldDeclaration) ASTNodes.getParent(fFieldDeclaration, FieldDeclaration.class);
    Type type = field.getType();
    MethodDeclaration result = ast.newMethodDeclaration();
    result.setName(ast.newSimpleName(fSetterName));
    result.modifiers().addAll(ASTNodeFactory.newModifiers(ast, createModifiers()));
    if (fSetterMustReturnValue) {
        result.setReturnType2((Type) rewriter.createCopyTarget(type));
    }
    SingleVariableDeclaration param = ast.newSingleVariableDeclaration();
    result.parameters().add(param);
    param.setName(ast.newSimpleName(fArgName));
    param.setType((Type) rewriter.createCopyTarget(type));
    List<Dimension> extraDimensions = DimensionRewrite.copyDimensions(fFieldDeclaration.extraDimensions(), rewriter);
    param.extraDimensions().addAll(extraDimensions);
    Block block = ast.newBlock();
    result.setBody(block);
    String fieldAccess = createFieldAccess();
    String body = CodeGeneration.getSetterMethodBodyContent(fField.getCompilationUnit(), getTypeName(field.getParent()), fSetterName, fieldAccess, fArgName, lineDelimiter);
    if (body != null) {
        ASTNode setterNode = rewriter.createStringPlaceholder(body, ASTNode.BLOCK);
        block.statements().add(setterNode);
    } else {
        Assignment ass = ast.newAssignment();
        ass.setLeftHandSide((Expression) rewriter.createStringPlaceholder(fieldAccess, ASTNode.QUALIFIED_NAME));
        ass.setRightHandSide(ast.newSimpleName(fArgName));
        block.statements().add(ass);
    }
    if (fSetterMustReturnValue) {
        ReturnStatement rs = ast.newReturnStatement();
        rs.setExpression(ast.newSimpleName(fArgName));
        block.statements().add(rs);
    }
    if (fGenerateJavadoc) {
        String string = CodeGeneration.getSetterComment(fField.getCompilationUnit(), getTypeName(field.getParent()), fSetterName, fField.getElementName(), ASTNodes.asString(type), fArgName, StubUtility.getBaseName(fField), lineDelimiter);
        if (string != null) {
            Javadoc javadoc = (Javadoc) fRewriter.createStringPlaceholder(string, ASTNode.JAVADOC);
            result.setJavadoc(javadoc);
        }
    }
    return result;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) IType(org.eclipse.jdt.core.IType) Type(org.eclipse.jdt.core.dom.Type) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block) Javadoc(org.eclipse.jdt.core.dom.Javadoc) Dimension(org.eclipse.jdt.core.dom.Dimension) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 94 with Block

use of org.eclipse.jdt.core.dom.Block in project processing by processing.

the class CompletionGenerator method findClosestNode.

protected static ASTNode findClosestNode(int lineNumber, ASTNode node) {
    log("findClosestNode to line " + lineNumber);
    ASTNode parent = findClosestParentNode(lineNumber, node);
    log("findClosestParentNode returned " + getNodeAsString(parent));
    if (parent == null)
        return null;
    if (getLineNumber(parent) == lineNumber) {
        log(parent + "|PNode " + getLineNumber(parent) + ", lfor " + lineNumber);
        return parent;
    }
    List<ASTNode> nodes;
    if (parent instanceof TypeDeclaration) {
        nodes = ((TypeDeclaration) parent).bodyDeclarations();
    } else if (parent instanceof Block) {
        nodes = ((Block) parent).statements();
    } else {
        log("findClosestNode() found " + getNodeAsString(parent));
        return null;
    }
    if (nodes.size() > 0) {
        ASTNode retNode = parent;
        for (ASTNode cNode : nodes) {
            log(cNode + "|cNode " + getLineNumber(cNode) + ", lfor " + lineNumber);
            if (getLineNumber(cNode) <= lineNumber)
                retNode = cNode;
        }
        return retNode;
    }
    return parent;
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 95 with Block

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

the class AddBracketsToControlStatementRefactoring method setBlock.

private boolean setBlock(Statement statement) {
    if (statement == null) {
        return VISIT_SUBTREE;
    }
    final ASTBuilder b = this.ctx.getASTBuilder();
    final Block block = b.block(b.copy(statement));
    block.accept(this);
    this.ctx.getRefactorings().replace(statement, block);
    return DO_NOT_VISIT_SUBTREE;
}
Also used : Block(org.eclipse.jdt.core.dom.Block) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Aggregations

Block (org.eclipse.jdt.core.dom.Block)105 ASTNode (org.eclipse.jdt.core.dom.ASTNode)61 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)45 AST (org.eclipse.jdt.core.dom.AST)44 Statement (org.eclipse.jdt.core.dom.Statement)42 Expression (org.eclipse.jdt.core.dom.Expression)39 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)38 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)37 ForStatement (org.eclipse.jdt.core.dom.ForStatement)35 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)35 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)34 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)30 IfStatement (org.eclipse.jdt.core.dom.IfStatement)30 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)28 DoStatement (org.eclipse.jdt.core.dom.DoStatement)26 Type (org.eclipse.jdt.core.dom.Type)26 SimpleName (org.eclipse.jdt.core.dom.SimpleName)22 SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)21 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)19 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)18