1 /*******************************************************************************
2 * Copyright (c) 2005, 2011 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.corext.refactoring.surround;
13 import java.util.List;
15 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.jdt.core.ICompilationUnit;
18 import org.eclipse.jdt.core.dom.ASTNode;
19 import org.eclipse.jdt.core.dom.Block;
20 import org.eclipse.jdt.core.dom.BodyDeclaration;
21 import org.eclipse.jdt.core.dom.CompilationUnit;
22 import org.eclipse.jdt.core.dom.ConstructorInvocation;
23 import org.eclipse.jdt.core.dom.Expression;
24 import org.eclipse.jdt.core.dom.ExpressionStatement;
25 import org.eclipse.jdt.core.dom.Initializer;
26 import org.eclipse.jdt.core.dom.Message;
27 import org.eclipse.jdt.core.dom.MethodDeclaration;
28 import org.eclipse.jdt.core.dom.Statement;
29 import org.eclipse.jdt.core.dom.SuperConstructorInvocation;
30 import org.eclipse.jdt.core.dom.VariableDeclaration;
32 import org.eclipse.jdt.internal.corext.dom.ASTNodes;
33 import org.eclipse.jdt.internal.corext.dom.Selection;
34 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
35 import org.eclipse.jdt.internal.corext.refactoring.base.JavaStatusContext;
36 import org.eclipse.jdt.internal.corext.refactoring.util.CodeAnalyzer;
38 public class SurroundWithAnalyzer extends CodeAnalyzer {
40 private VariableDeclaration[] fLocals;
42 public SurroundWithAnalyzer(ICompilationUnit cunit, Selection selection) throws CoreException {
43 super(cunit, selection, false);
46 public Statement[] getSelectedStatements() {
47 if (hasSelectedNodes()) {
48 return internalGetSelectedNodes().toArray(new Statement[internalGetSelectedNodes().size()]);
50 return new Statement[0];
54 public VariableDeclaration[] getAffectedLocals() {
58 public BodyDeclaration getEnclosingBodyDeclaration() {
59 ASTNode node= getFirstSelectedNode();
62 return (BodyDeclaration)ASTNodes.getParent(node, BodyDeclaration.class);
66 protected boolean handleSelectionEndsIn(ASTNode node) {
71 public void endVisit(CompilationUnit node) {
72 postProcessSelectedNodes(internalGetSelectedNodes());
73 BodyDeclaration enclosingNode= null;
75 if (getStatus().hasFatalError())
77 if (!hasSelectedNodes()) {
78 ASTNode coveringNode= getLastCoveringNode();
79 if (coveringNode instanceof Block) {
80 Block block= (Block)coveringNode;
81 Message[] messages= ASTNodes.getMessages(block, ASTNodes.NODE_ONLY);
82 if (messages.length > 0) {
83 invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_compile_errors,
84 JavaStatusContext.create(getCompilationUnit(), block));
88 invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_doesNotCover);
91 enclosingNode= (BodyDeclaration)ASTNodes.getParent(getFirstSelectedNode(), BodyDeclaration.class);
92 if (!(enclosingNode instanceof MethodDeclaration) && !(enclosingNode instanceof Initializer)) {
93 invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_doesNotContain);
96 if (!onlyStatements()) {
97 invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_onlyStatements);
99 fLocals= LocalDeclarationAnalyzer.perform(enclosingNode, getSelection());
101 super.endVisit(node);
105 public void endVisit(SuperConstructorInvocation node) {
106 if (getSelection().getEndVisitSelectionMode(node) == Selection.SELECTED) {
107 invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_cannotHandleSuper, JavaStatusContext.create(fCUnit, node));
109 super.endVisit(node);
113 public void endVisit(ConstructorInvocation node) {
114 if (getSelection().getEndVisitSelectionMode(node) == Selection.SELECTED) {
115 invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_cannotHandleThis, JavaStatusContext.create(fCUnit, node));
117 super.endVisit(node);
120 protected void postProcessSelectedNodes(List<ASTNode> selectedNodes) {
121 if (selectedNodes == null || selectedNodes.size() == 0)
123 if (selectedNodes.size() == 1) {
124 ASTNode node= selectedNodes.get(0);
125 if (node instanceof Expression && node.getParent() instanceof ExpressionStatement) {
126 selectedNodes.clear();
127 selectedNodes.add(node.getParent());
132 private boolean onlyStatements() {
133 ASTNode[] nodes= getSelectedNodes();
134 for (int i= 0; i < nodes.length; i++) {
135 if (!(nodes[i] instanceof Statement))