Source:
<code>public boolean visit(MethodInvocation node) { if (isMethod(node, "android.os.PowerManager.WakeLock", "release")) { final Refactorings r = this.ctx.getRefactorings(); final ASTBuilder b = this.ctx.getASTBuilder(); MethodDeclaration enclosingMethod = getAncestor(node, MethodDeclaration.class); if (isMethod(enclosingMethod.resolveBinding(), "android.app.Activity", "onDestroy")) { TypeDeclaration typeDeclaration; <START> typeDeclaration = getAncestor(enclosingMethod, TypeDeclaration.class); <END> MethodDeclaration onPauseMethod = findMethod(typeDeclaration, "onPause"); if (onPauseMethod != null && node.getParent().getNodeType() == ASTNode.EXPRESSION_STATEMENT) { r.remove(node.getParent()); r.insertAt(createWakelockReleaseNode(node), onPauseMethod.getBody().statements().size(), Block.STATEMENTS_PROPERTY, onPauseMethod.getBody()); return DO_NOT_VISIT_SUBTREE; } r.insertAfter(createOnPauseMethodDeclaration(), 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 = getAncestor(node, TypeDeclaration.class); ReleasePresenceChecker releasePresenceChecker = new ReleasePresenceChecker(); if (!releasePresenceChecker.findOrDefault(typeDeclaration, false)) { Statement releaseNode = createWakelockReleaseNode(node); MethodDeclaration onPauseMethod = findMethod(typeDeclaration, "onPause"); 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 = createOnPauseMethodDeclaration(); r.insertAt( onPauseDeclaration, typeDeclaration.bodyDeclarations().size(), typeDeclaration.getBodyDeclarationsProperty(), typeDeclaration); return DO_NOT_VISIT_SUBTREE; } } } return VISIT_SUBTREE; }</code><technical_language>Please join declaration: java TypeDeclaration typeDeclaration = getAncestor(enclosingMethod, TypeDeclaration.class);</technical_language>

Target:
public boolean visit(MethodInvocation node) { if (isMethod(node, "android.os.PowerManager.WakeLock", "release")) { MethodDeclaration enclosingMethod = getAncestor(node, MethodDeclaration.class); if (isMethod(enclosingMethod.resolveBinding(), "android.app.Activity", "onDestroy")) { final Refactorings r = this.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, createWakelockReleaseNode(node)); return DO_NOT_VISIT_SUBTREE; } r.insertAfter(createOnPauseMethodDeclaration(), enclosingMethod); return DO_NOT_VISIT_SUBTREE; } } else if (isMethod(node, "android.os.PowerManager.WakeLock", "acquire")) { final Refactorings r = this.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, createWakelockReleaseNode(node)); return DO_NOT_VISIT_SUBTREE; } else { r.insertLast( typeDeclaration, typeDeclaration.getBodyDeclarationsProperty(), createOnPauseMethodDeclaration()); return DO_NOT_VISIT_SUBTREE; } } } return VISIT_SUBTREE; }