Example usage for org.eclipse.jdt.core.dom Block STATEMENTS_PROPERTY

List of usage examples for org.eclipse.jdt.core.dom Block STATEMENTS_PROPERTY

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom Block STATEMENTS_PROPERTY.

Prototype

ChildListPropertyDescriptor STATEMENTS_PROPERTY

To view the source code for org.eclipse.jdt.core.dom Block STATEMENTS_PROPERTY.

Click Source Link

Document

The "statements" structural property of this node type (element type: Statement ).

Usage

From source file:org.eclipse.andmore.android.model.java.BroadcastReceiverClass.java

License:Apache License

@Override
protected void addComments() throws AndroidException {
    ASTNode todoComment;//from w  w  w .j  a  v a 2 s . c o m

    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(document.get().toCharArray());

    compUnit = (CompilationUnit) parser.createAST(null);
    ast = compUnit.getAST();
    rewrite = ASTRewrite.create(ast);

    todoComment = rewrite.createStringPlaceholder(CodeUtilsNLS.MODEL_Common_ToDoPutYourCodeHere,
            ASTNode.EMPTY_STATEMENT);

    TypeDeclaration receiverClass = (TypeDeclaration) compUnit.types().get(0);
    MethodDeclaration method;
    Block block;

    // Adds the Override annotation and ToDo comment to all overridden
    // methods
    for (int i = 0; i < receiverClass.bodyDeclarations().size(); i++) {
        method = (MethodDeclaration) receiverClass.bodyDeclarations().get(i);

        // Adds the Override annotation
        rewrite.getListRewrite(method, method.getModifiersProperty()).insertFirst(OVERRIDE_ANNOTATION, null);

        // Adds the ToDo comment
        block = method.getBody();
        rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY).insertFirst(todoComment, null);
    }

    try {
        // Writes the modifications
        TextEdit modifications = rewrite.rewriteAST(document, null);
        modifications.apply(document);
    } catch (IllegalArgumentException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        AndmoreLogger.error(BroadcastReceiverClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    } catch (MalformedTreeException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        AndmoreLogger.error(BroadcastReceiverClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    } catch (BadLocationException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        AndmoreLogger.error(BroadcastReceiverClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    }
}

From source file:org.eclipse.andmore.android.model.java.ContentProviderClass.java

License:Apache License

@Override
protected void addComments() throws AndroidException {
    ASTNode todoComment;// ww w  . j  ava 2s . c  o m

    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(document.get().toCharArray());

    compUnit = (CompilationUnit) parser.createAST(null);
    ast = compUnit.getAST();
    rewrite = ASTRewrite.create(ast);

    todoComment = rewrite.createStringPlaceholder(CodeUtilsNLS.MODEL_Common_ToDoPutYourCodeHere,
            ASTNode.EMPTY_STATEMENT);

    TypeDeclaration cpClass = (TypeDeclaration) compUnit.types().get(0);
    MethodDeclaration method;
    Block block;

    // Adds the Override annotation and ToDo comment to all abstract methods
    for (int i = 0; i < cpClass.bodyDeclarations().size(); i++) {
        BodyDeclaration bodyDecl = (BodyDeclaration) cpClass.bodyDeclarations().get(i);

        if (bodyDecl instanceof MethodDeclaration) {
            method = (MethodDeclaration) bodyDecl;

            // Adds the Override annotation
            rewrite.getListRewrite(method, method.getModifiersProperty()).insertFirst(OVERRIDE_ANNOTATION,
                    null);

            // Adds the ToDo comment
            block = method.getBody();
            rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY).insertFirst(todoComment, null);
        }
    }

    try {
        // Writes the modifications
        TextEdit modifications = rewrite.rewriteAST(document, null);
        modifications.apply(document);
    } catch (IllegalArgumentException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        AndmoreLogger.error(ContentProviderClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    } catch (MalformedTreeException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        AndmoreLogger.error(ContentProviderClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    } catch (BadLocationException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        AndmoreLogger.error(ContentProviderClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    }
}

From source file:org.eclipse.andmore.android.model.java.ServiceClass.java

License:Apache License

@Override
protected void addComments() throws AndroidException {
    ASTNode todoComment;//from ww w  . j av  a  2 s  . co m

    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(document.get().toCharArray());

    compUnit = (CompilationUnit) parser.createAST(null);
    ast = compUnit.getAST();
    rewrite = ASTRewrite.create(ast);

    todoComment = rewrite.createStringPlaceholder(CodeUtilsNLS.MODEL_Common_ToDoPutYourCodeHere,
            ASTNode.EMPTY_STATEMENT);

    TypeDeclaration serviceClass = (TypeDeclaration) compUnit.types().get(0);
    MethodDeclaration method;
    Block block;

    // Adds the Override annotation and ToDo comment to all abstract methods
    for (int i = 0; i < serviceClass.bodyDeclarations().size(); i++) {
        method = (MethodDeclaration) serviceClass.bodyDeclarations().get(i);

        // Adds the Override annotation
        rewrite.getListRewrite(method, method.getModifiersProperty()).insertFirst(OVERRIDE_ANNOTATION, null);

        // Adds the ToDo comment
        block = method.getBody();
        rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY).insertFirst(todoComment, null);
    }

    try {
        // Writes the modifications
        TextEdit modifications = rewrite.rewriteAST(document, null);
        modifications.apply(document);
    } catch (IllegalArgumentException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        AndmoreLogger.error(ServiceClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    } catch (MalformedTreeException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        AndmoreLogger.error(ServiceClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    } catch (BadLocationException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        AndmoreLogger.error(ServiceClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    }
}

From source file:org.eclipse.andmore.android.model.java.WidgetProviderClass.java

License:Apache License

@Override
protected void addComments() throws AndroidException {
    ASTNode todoComment;//from  w w w.  j  a  va  2s  .  com

    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(document.get().toCharArray());

    compUnit = (CompilationUnit) parser.createAST(null);
    ast = compUnit.getAST();
    rewrite = ASTRewrite.create(ast);

    todoComment = rewrite.createStringPlaceholder(CodeUtilsNLS.MODEL_Common_ToDoPutYourCodeHere,
            ASTNode.EMPTY_STATEMENT);

    TypeDeclaration widgetProviderClass = (TypeDeclaration) compUnit.types().get(0);
    MethodDeclaration method;
    Block block;

    // Adds the Override annotation and ToDo comment to all overridden
    // methods
    for (int i = 0; i < widgetProviderClass.bodyDeclarations().size(); i++) {
        method = (MethodDeclaration) widgetProviderClass.bodyDeclarations().get(i);

        // Adds the Override annotation
        rewrite.getListRewrite(method, method.getModifiersProperty()).insertFirst(OVERRIDE_ANNOTATION, null);

        // Adds the ToDo comment
        block = method.getBody();
        rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY).insertFirst(todoComment, null);
    }

    try {
        // Writes the modifications
        TextEdit modifications = rewrite.rewriteAST(document, null);
        modifications.apply(document);
    } catch (IllegalArgumentException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        AndmoreLogger.error(BroadcastReceiverClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    } catch (MalformedTreeException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        AndmoreLogger.error(BroadcastReceiverClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    } catch (BadLocationException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        AndmoreLogger.error(BroadcastReceiverClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    }
}

From source file:org.evosuite.eclipse.quickfixes.ResolutionMarkerTryBlock.java

License:Open Source License

@Override
public void run(IMarker marker) {
    // TODO Auto-generated method stub
    IResource res = marker.getResource();

    try {//from  w w  w.  ja  va2  s. c  o  m
        String markerMessage = (String) marker.getAttribute(IMarker.MESSAGE);
        String exception = markerMessage.split(" ")[0];
        String[] exceptionPackageArray = exception.split("\\.");
        String exceptionPackage = "";
        for (int i = 0; i < exceptionPackageArray.length - 1; i++) {
            exceptionPackage += exceptionPackageArray[i] + ".";
        }
        exception = exception.substring(exceptionPackage.length());

        System.out.println("Package: " + exceptionPackage + ", Exception: " + exception);

        ICompilationUnit icomp = CompilationUnitManager.getICompilationUnit(res);

        CompilationUnit compunit = CompilationUnitManager.getCompilationUnit(res);

        int position = marker.getAttribute(IMarker.CHAR_START, 0) + 1;
        int line = marker.getAttribute(IMarker.LINE_NUMBER, 0);
        if (position == 1) {
            position = compunit.getPosition(line, 0);
        }

        AST ast = compunit.getAST();
        ASTRewrite rewriter = ASTRewrite.create(ast);

        IJavaElement element = icomp.getElementAt(position);
        IJavaElement method = getMethod(element);

        // TypeDeclaration td = (TypeDeclaration) compunit.types().get(0);
        TypeDeclaration td = (TypeDeclaration) compunit.types().get(0);

        MethodDeclaration md = td.getMethods()[0];

        int counter = 1;
        while (!md.getName().getFullyQualifiedName().equals(method.getElementName())
                && counter < td.getMethods().length) {
            md = td.getMethods()[counter];
            System.out.println(md.getName().getFullyQualifiedName() + " " + method.getElementName());
            counter++;
        }

        Block block = md.getBody();

        ListRewrite lr = rewriter.getListRewrite(block, Block.STATEMENTS_PROPERTY);
        ImportDeclaration id = ast.newImportDeclaration();
        id.setName(ast.newName(exceptionPackage + exception));
        ListRewrite lrClass = rewriter.getListRewrite(compunit, CompilationUnit.TYPES_PROPERTY);
        lrClass.insertAt(id, 0, null);
        ASTNode currentNode = null;
        List<ASTNode> list = new ArrayList<ASTNode>();
        for (Object o : lr.getOriginalList()) {
            if (o instanceof ASTNode) {
                list.add((ASTNode) o);
            }
        }
        for (int i = 0; i < list.size(); i++) {
            ASTNode node = list.get(i);
            int nodeLine = compunit.getLineNumber(node.getStartPosition());
            System.out.println(line + " " + nodeLine);
            if (line == nodeLine) {
                currentNode = node;
                break;
            }
            List childrenList = node.structuralPropertiesForType();
            for (int j = 0; j < childrenList.size(); j++) {
                StructuralPropertyDescriptor curr = (StructuralPropertyDescriptor) childrenList.get(j);
                Object child = node.getStructuralProperty(curr);
                if (child instanceof List) {
                    for (Object ob : (List) child) {
                        if (ob instanceof ASTNode) {
                            list.add((ASTNode) ob);
                        }
                    }
                } else if (child instanceof ASTNode) {
                    list.add((ASTNode) child);
                }
            }
        }

        TryStatement ts = ast.newTryStatement();
        Statement emptyStatement = (Statement) rewriter.createStringPlaceholder("\n", ASTNode.EMPTY_STATEMENT);
        Statement emptyStatement2 = (Statement) rewriter.createStringPlaceholder("\n\t",
                ASTNode.EMPTY_STATEMENT);
        Block b2 = ast.newBlock();
        b2.statements().add(0, ASTNode.copySubtree(b2.getAST(), currentNode));
        b2.statements().add(1, emptyStatement);
        b2.statements().add(0, emptyStatement2);
        ts.setBody(b2);
        CatchClause cc = ast.newCatchClause();
        SingleVariableDeclaration svd = ast.newSingleVariableDeclaration();
        svd.setName(ast.newSimpleName("e"));
        Type type = ast.newSimpleType(ast.newName(exception));
        svd.setType(type);
        cc.setException(svd);
        Block b3 = ast.newBlock();
        Statement printStatement = (Statement) rewriter.createStringPlaceholder("e.printStackTrace();",
                ASTNode.EMPTY_STATEMENT);
        b3.statements().add(0, printStatement);
        cc.setBody(b3);
        ListRewrite parentList = rewriter.getListRewrite(currentNode.getParent(), Block.STATEMENTS_PROPERTY);
        parentList.replace(currentNode, ts, null);
        parentList.insertAfter(cc, ts, null);

        ITextFileBufferManager bm = FileBuffers.getTextFileBufferManager();
        IPath path = compunit.getJavaElement().getPath();
        try {
            bm.connect(path, null, null);
            ITextFileBuffer textFileBuffer = bm.getTextFileBuffer(path, null);
            IDocument document = textFileBuffer.getDocument();
            TextEdit edits = rewriter.rewriteAST(document, null);
            edits.apply(document);
            textFileBuffer.commit(null, false);

        } catch (CoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MalformedTreeException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (BadLocationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                bm.disconnect(path, null, null);
            } catch (CoreException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } // (4)
        }
        marker.delete();
    } catch (JavaModelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (CoreException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

}

From source file:org.jboss.forge.parser.java.impl.MethodImpl.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public String getBody() {
    String result = "";

    List<Statement> statements = (List<Statement>) method.getBody()
            .getStructuralProperty(Block.STATEMENTS_PROPERTY);
    for (Statement statement : statements) {
        result += statement + " ";
    }// ww w .  ja  va 2  s .  c  o  m

    return result;
}

From source file:org.jboss.tools.arquillian.ui.internal.refactoring.AddMissingTypeRefactoring.java

License:Open Source License

private void insertAt(ASTNode target, Statement declaration) {
    //ASTRewrite rewrite= fCURewrite.getASTRewrite();
    //TextEditGroup groupDescription= fCURewrite.createGroupDescription(RefactoringCoreMessages.ExtractTempRefactoring_declare_local_variable);

    ASTNode parent = target.getParent();
    StructuralPropertyDescriptor locationInParent = target.getLocationInParent();
    while (locationInParent != Block.STATEMENTS_PROPERTY
            && locationInParent != SwitchStatement.STATEMENTS_PROPERTY) {
        if (locationInParent == IfStatement.THEN_STATEMENT_PROPERTY
                || locationInParent == IfStatement.ELSE_STATEMENT_PROPERTY
                || locationInParent == ForStatement.BODY_PROPERTY
                || locationInParent == EnhancedForStatement.BODY_PROPERTY
                || locationInParent == DoStatement.BODY_PROPERTY
                || locationInParent == WhileStatement.BODY_PROPERTY) {
            // create intermediate block if target was the body property of a control statement:
            Block replacement = rewrite.getAST().newBlock();
            ListRewrite replacementRewrite = rewrite.getListRewrite(replacement, Block.STATEMENTS_PROPERTY);
            replacementRewrite.insertFirst(declaration, null);
            replacementRewrite.insertLast(rewrite.createMoveTarget(target), null);
            rewrite.replace(target, replacement, null);
            return;
        }/*w w w  .ja v  a2  s. c  o m*/
        target = parent;
        parent = parent.getParent();
        locationInParent = target.getLocationInParent();
    }
    ListRewrite lw = rewrite.getListRewrite(parent, (ChildListPropertyDescriptor) locationInParent);
    lw.insertBefore(declaration, target, null);
}

From source file:org.jboss.tools.arquillian.ui.internal.refactoring.AddMissingTypeRefactoring.java

License:Open Source License

private void createStatement(String variableName, Map<String, Set<String>> classNamesMap,
        MethodDeclaration deploymentMethod, ReturnStatement returnStatement, MultiTextEdit rootEdit,
        IProgressMonitor pm) throws JavaModelException, CoreException {
    for (Map.Entry<String, Set<String>> entry : classNamesMap.entrySet()) {
        Set<String> values = entry.getValue();
        if (values == null || values.size() == 0) {
            continue;
        }/*from  ww w .  j ava 2  s. c om*/
        for (String value : values) {
            importRewrite.addImport(value);
        }
        MethodInvocation mi = ast.newMethodInvocation();
        mi.setExpression(ast.newSimpleName(variableName));
        if (values.size() == 1) {
            mi.setName(ast.newSimpleName("addClass")); //$NON-NLS-1$
        } else {
            mi.setName(ast.newSimpleName("addClasses")); //$NON-NLS-1$
        }
        for (String value : values) {
            int index = value.lastIndexOf("."); //$NON-NLS-1$
            String simpleClassName;
            if (index >= 0) {
                simpleClassName = value.substring(index + 1);
            } else {
                simpleClassName = value;
            }
            Name typeName = ast.newName(simpleClassName);
            org.eclipse.jdt.core.dom.Type type = ast.newSimpleType(typeName);
            TypeLiteral typeLiteral = ast.newTypeLiteral();
            typeLiteral.setType(type);
            mi.arguments().add(typeLiteral);
        }
        Statement newStatement = ast.newExpressionStatement(mi);
        if (abstractMethodBindings != null) {
            deploymentMethod.getBody().statements().add(newStatement);
        } else {
            ListRewrite lw = rewrite.getListRewrite(deploymentMethod.getBody(), Block.STATEMENTS_PROPERTY);
            lw.insertBefore(newStatement, returnStatement, null);
        }
    }
}

From source file:org.moe.natjgen.ConstructorEditor.java

License:Apache License

public void setBodyAsSuperInitWithClassAsArg() {
    Block block = getAST().newBlock();/*from w w w.  j  a  v a2  s.c o m*/
    getRewrite().set(methodDecl, MethodDeclaration.BODY_PROPERTY, block, getEditGroup());
    ListRewrite block_stmts = getRewrite().getListRewrite(block, Block.STATEMENTS_PROPERTY);
    SuperConstructorInvocation invoke = getAST().newSuperConstructorInvocation();
    block_stmts.insertLast(invoke, getEditGroup());
    ListRewrite invoke_args = getRewrite().getListRewrite(invoke,
            SuperConstructorInvocation.ARGUMENTS_PROPERTY);
    TypeLiteral literal = getAST().newTypeLiteral();
    invoke_args.insertLast(literal, getEditGroup());
    getRewrite().set(literal, TypeLiteral.TYPE_PROPERTY,
            getAST().newSimpleType(getAST().newName(getManager().getUnitName())), getEditGroup());
}

From source file:org.moe.natjgen.ConstructorEditor.java

License:Apache License

public void setBodyAsSuperInitWithPointerAsArg(String varname) {
    Block block = getAST().newBlock();//from   ww  w. j a  v a 2 s  .  c  o m
    getRewrite().set(methodDecl, MethodDeclaration.BODY_PROPERTY, block, getEditGroup());
    ListRewrite block_stmts = getRewrite().getListRewrite(block, Block.STATEMENTS_PROPERTY);
    SuperConstructorInvocation invoke = getAST().newSuperConstructorInvocation();
    block_stmts.insertLast(invoke, getEditGroup());
    ListRewrite invoke_args = getRewrite().getListRewrite(invoke,
            SuperConstructorInvocation.ARGUMENTS_PROPERTY);
    invoke_args.insertLast(getAST().newSimpleName(varname), getEditGroup());
}