Example usage for org.eclipse.jdt.core.dom TypeDeclaration getBodyDeclarationsProperty

List of usage examples for org.eclipse.jdt.core.dom TypeDeclaration getBodyDeclarationsProperty

Introduction

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

Prototype

public final ChildListPropertyDescriptor getBodyDeclarationsProperty() 

Source Link

Document

Returns structural property descriptor for the "bodyDeclarations" property of this node (element type: BodyDeclaration ).

Usage

From source file:org.autorefactor.refactoring.rules.AndroidWakeLockRefactoring.java

License:Open Source License

@Override
public boolean visit(MethodInvocation node) {
    if (isMethod(node, "android.os.PowerManager.WakeLock", "release")) {
        // check whether it is being called in onDestroy()
        MethodDeclaration enclosingMethod = getAncestor(node, MethodDeclaration.class);
        if (isMethod(enclosingMethod, "android.app.Activity", "onDestroy")) {
            final Refactorings r = ctx.getRefactorings();
            TypeDeclaration typeDeclaration = getAncestor(enclosingMethod, TypeDeclaration.class);
            MethodDeclaration onPauseMethod = findMethod(typeDeclaration, "onPause");
            if (onPauseMethod != null && node.getParent().getNodeType() == ASTNode.EXPRESSION_STATEMENT) {
                r.remove(node.getParent());
                r.insertLast(onPauseMethod.getBody(), Block.STATEMENTS_PROPERTY,
                        createWakelockReleaseStmt(node));
            } else {
                // Add the missing onPause() method to the class.
                r.insertAfter(createOnPauseMethodDeclaration(), enclosingMethod);
            }/*from   w w w . j  ava  2 s .c o m*/
            return DO_NOT_VISIT_SUBTREE;
        }
    } else if (isMethod(node, "android.os.PowerManager.WakeLock", "acquire")) {
        final Refactorings r = ctx.getRefactorings();
        TypeDeclaration typeDeclaration = getAncestor(node, TypeDeclaration.class);
        ReleasePresenceChecker releasePresenceChecker = new ReleasePresenceChecker();
        if (!releasePresenceChecker.findOrDefault(typeDeclaration, false)) {
            MethodDeclaration onPauseMethod = findMethod(typeDeclaration, "onPause");
            if (onPauseMethod != null && node.getParent().getNodeType() == ASTNode.EXPRESSION_STATEMENT) {
                r.insertLast(onPauseMethod.getBody(), Block.STATEMENTS_PROPERTY,
                        createWakelockReleaseStmt(node));
            } else {
                r.insertLast(typeDeclaration, typeDeclaration.getBodyDeclarationsProperty(),
                        createOnPauseMethodDeclaration());
            }
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    return VISIT_SUBTREE;
}

From source file:org.autorefactor.refactoring.rules.WakeLockRefactoring.java

License:Open Source License

@Override
public boolean visit(MethodInvocation node) {
    if (isMethod(node, "android.os.PowerManager.WakeLock", "release")) {
        // check whether it is being called in onDestroy
        final Refactorings r = this.ctx.getRefactorings();
        final ASTBuilder b = this.ctx.getASTBuilder();
        MethodDeclaration enclosingMethod = (MethodDeclaration) ASTNodes.getParent(node,
                ASTNode.METHOD_DECLARATION);
        if (isMethod(enclosingMethod.resolveBinding(), "android.app.Activity", "onDestroy")) {
            TypeDeclaration typeDeclaration = (TypeDeclaration) ASTNodes.getParent(enclosingMethod,
                    TypeDeclaration.class);
            MethodDeclaration onPauseMethod = findMethodOfType("onPause", typeDeclaration);
            if (onPauseMethod != null && node.getParent().getNodeType() == ASTNode.EXPRESSION_STATEMENT) {
                Statement releaseNode = createWakelockReleaseNode(node);
                r.remove(node.getParent());
                r.insertAt(releaseNode, onPauseMethod.getBody().statements().size(), Block.STATEMENTS_PROPERTY,
                        onPauseMethod.getBody());
                return DO_NOT_VISIT_SUBTREE;
            }/*from   w  ww  . ja  v  a2 s. c  o  m*/
            /* If it reaches this part of the code, it
             * means it did not find onPause method in the class.
             */
            MethodDeclaration onPauseDeclaration = createOnPauseDeclaration();

            // add onPause declaration to the Activity
            r.insertAfter(onPauseDeclaration, enclosingMethod);
            return DO_NOT_VISIT_SUBTREE;

        }
    } else if (isMethod(node, "android.os.PowerManager.WakeLock", "acquire")) {
        final Refactorings r = this.ctx.getRefactorings();
        final ASTBuilder b = this.ctx.getASTBuilder();
        TypeDeclaration typeDeclaration = (TypeDeclaration) ASTNodes.getParent(node, ASTNode.TYPE_DECLARATION);
        ReleasePresenceChecker releasePresenceChecker = new ReleasePresenceChecker();
        typeDeclaration.accept(releasePresenceChecker);
        if (!releasePresenceChecker.releasePresent) {
            Statement releaseNode = createWakelockReleaseNode(node);
            MethodDeclaration onPauseMethod = findMethodOfType("onPause", typeDeclaration);
            if (onPauseMethod != null && node.getParent().getNodeType() == ASTNode.EXPRESSION_STATEMENT) {
                r.insertAt(releaseNode, onPauseMethod.getBody().statements().size(), Block.STATEMENTS_PROPERTY,
                        onPauseMethod.getBody());
                return DO_NOT_VISIT_SUBTREE;
            } else {
                MethodDeclaration onPauseDeclaration = createOnPauseDeclaration();
                /* TODO @JnRouvignac, instead of using insertAt, calling 
                 * typeDeclaration.bodyDeclarations().add(onPauseDeclaration);
                 * would be more intuitive.
                 */
                r.insertAt(onPauseDeclaration, typeDeclaration.bodyDeclarations().size(),
                        typeDeclaration.getBodyDeclarationsProperty(), typeDeclaration);
                return DO_NOT_VISIT_SUBTREE;
            }
        }
    }
    return VISIT_SUBTREE;
}

From source file:org.eclipse.objectteams.otdt.internal.refactoring.otrefactorings.rolefile.MoveToRoleFileRefactoring.java

License:Open Source License

@Override
public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {
    pm.beginTask(OTRefactoringMessages.MoveToRoleFileRefactoring_creatingChange_progress, 5);
    CompositeChange change = new CompositeChange(OTRefactoringMessages.MoveToRoleFileRefactoring_change_name);
    TextEditGroup editGroup = new TextEditGroup(OTRefactoringMessages.MoveToRoleFileRefactoring_change_name);
    ICompilationUnit newCuWC = null;/*from  w  ww.  j a  v a 2  s.  c o  m*/
    try {
        // packages
        IPackageFragment enclosingPackage = fRoleType.getPackageFragment();
        IPackageFragmentRoot root = (IPackageFragmentRoot) enclosingPackage
                .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
        String enclPackName = enclosingPackage.getElementName();
        IPackageFragment teamPackage = root
                .getPackageFragment(enclPackName.length() == 0 ? fTeamType.getElementName()
                        : enclPackName + '.' + fTeamType.getElementName());

        // fetch AST for team and role:
        CompilationUnitRewrite cuRewrite = new CompilationUnitRewrite(fTeamCUnit);
        CompilationUnit teamCU = cuRewrite.getRoot();
        TypeDeclaration teamNode = (TypeDeclaration) findDeclaration(teamCU, fTeamType);
        ASTNode roleNode = findDeclaration(teamCU, fRoleType);

        // new CU:
        if (!teamPackage.getResource().exists())
            change.add(new CreatePackageChange(teamPackage));
        newCuWC = teamPackage.getCompilationUnit(fRoleType.getElementName() + JavaModelUtil.DEFAULT_CU_SUFFIX)
                .getWorkingCopy(new SubProgressMonitor(pm, 2));

        // (1) create role:
        // extract role source (as text):
        String oldSource = fTeamCUnit.getSource();
        ISourceRange sourceRange = fRoleType.getSourceRange();
        StringBuilder newRoleSource = new StringBuilder();
        IJavaProject javaProject = fRoleType.getJavaProject();
        if (StubUtility.doAddComments(javaProject))
            newRoleSource.append(
                    CodeGeneration.getFileComment(newCuWC, StubUtility.getLineDelimiterUsed(javaProject)));
        newRoleSource.append("\nteam package " + teamPackage.getElementName() + ";\n\n"); //$NON-NLS-1$ //$NON-NLS-2$
        newRoleSource.append(getAlignedSourceBlock(javaProject, oldSource.substring(sourceRange.getOffset(),
                sourceRange.getOffset() + sourceRange.getLength())));

        // done change #1:
        change.add(new CreateCompilationUnitChange(newCuWC, newRoleSource.toString(), null));

        // (2) modify team:
        // remove role from team:
        ASTRewrite rewrite = cuRewrite.getASTRewrite();
        ListRewrite teamMembersRewrite = rewrite.getListRewrite(teamNode,
                teamNode.getBodyDeclarationsProperty());
        teamMembersRewrite.remove(roleNode, editGroup);

        // add javadoc tag '@role roleName'
        Javadoc teamDoc = teamNode.getJavadoc();
        AST ast = teamCU.getAST();
        TextElement roleName = ast.newTextElement();
        roleName.setText(fRoleType.getElementName());
        TagElement roleTag = ast.newTagElement();
        roleTag.setTagName("@role"); //$NON-NLS-1$
        roleTag.fragments().add(roleName);
        if (teamDoc == null) { // need to add a fresh Javadoc
            teamDoc = ast.newJavadoc();
            teamDoc.tags().add(roleTag);
            rewrite.set(teamNode, teamNode.getJavadocProperty(), teamDoc, editGroup);
        } else { // need to insert tag into existing Javadoc
            ListRewrite tags = rewrite.getListRewrite(teamDoc, Javadoc.TAGS_PROPERTY);
            tags.insertLast(roleTag, editGroup);
        }

        // done change #2:
        change.add(cuRewrite.createChange(true, new SubProgressMonitor(pm, 2)));
    } finally {
        if (newCuWC != null)
            newCuWC.discardWorkingCopy();
    }

    pm.done();
    return change;
}