KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > surround > SurroundWithAnalyzer


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.refactoring.surround;
12
13 import java.util.List JavaDoc;
14
15 import org.eclipse.jdt.core.ICompilationUnit;
16 import org.eclipse.jdt.core.JavaModelException;
17 import org.eclipse.jdt.core.dom.ASTNode;
18 import org.eclipse.jdt.core.dom.Block;
19 import org.eclipse.jdt.core.dom.BodyDeclaration;
20 import org.eclipse.jdt.core.dom.CompilationUnit;
21 import org.eclipse.jdt.core.dom.ConstructorInvocation;
22 import org.eclipse.jdt.core.dom.Expression;
23 import org.eclipse.jdt.core.dom.ExpressionStatement;
24 import org.eclipse.jdt.core.dom.Initializer;
25 import org.eclipse.jdt.core.dom.Message;
26 import org.eclipse.jdt.core.dom.MethodDeclaration;
27 import org.eclipse.jdt.core.dom.Statement;
28 import org.eclipse.jdt.core.dom.SuperConstructorInvocation;
29 import org.eclipse.jdt.core.dom.VariableDeclaration;
30
31 import org.eclipse.jdt.internal.corext.dom.ASTNodes;
32 import org.eclipse.jdt.internal.corext.dom.Selection;
33 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
34 import org.eclipse.jdt.internal.corext.refactoring.base.JavaStatusContext;
35 import org.eclipse.jdt.internal.corext.refactoring.util.CodeAnalyzer;
36
37 public class SurroundWithAnalyzer extends CodeAnalyzer {
38     
39     private VariableDeclaration[] fLocals;
40
41     public SurroundWithAnalyzer(ICompilationUnit cunit, Selection selection) throws JavaModelException {
42         super(cunit, selection, false);
43     }
44     
45     public Statement[] getSelectedStatements() {
46         if (hasSelectedNodes()) {
47             return (Statement[])internalGetSelectedNodes().toArray(new Statement[internalGetSelectedNodes().size()]);
48         } else {
49             return new Statement[0];
50         }
51     }
52     
53     public VariableDeclaration[] getAffectedLocals() {
54         return fLocals;
55     }
56     
57     public BodyDeclaration getEnclosingBodyDeclaration() {
58         ASTNode node= getFirstSelectedNode();
59         if (node == null)
60             return null;
61         return (BodyDeclaration)ASTNodes.getParent(node, BodyDeclaration.class);
62     }
63     
64     protected boolean handleSelectionEndsIn(ASTNode node) {
65         return true;
66     }
67     
68     public void endVisit(CompilationUnit node) {
69         postProcessSelectedNodes(internalGetSelectedNodes());
70         BodyDeclaration enclosingNode= null;
71         superCall: {
72             if (getStatus().hasFatalError())
73                 break superCall;
74             if (!hasSelectedNodes()) {
75                 ASTNode coveringNode= getLastCoveringNode();
76                 if (coveringNode instanceof Block) {
77                     Block block= (Block)coveringNode;
78                     Message[] messages= ASTNodes.getMessages(block, ASTNodes.NODE_ONLY);
79                     if (messages.length > 0) {
80                         invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_compile_errors,
81                             JavaStatusContext.create(getCompilationUnit(), block));
82                         break superCall;
83                     }
84                 }
85                 invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_doesNotCover);
86                 break superCall;
87             }
88             enclosingNode= (BodyDeclaration)ASTNodes.getParent(getFirstSelectedNode(), BodyDeclaration.class);
89             if (!(enclosingNode instanceof MethodDeclaration) && !(enclosingNode instanceof Initializer)) {
90                 invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_doesNotContain);
91                 break superCall;
92             }
93             if (!onlyStatements()) {
94                 invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_onlyStatements);
95             }
96             fLocals= LocalDeclarationAnalyzer.perform(enclosingNode, getSelection());
97         }
98         super.endVisit(node);
99     }
100     
101     public void endVisit(SuperConstructorInvocation node) {
102         if (getSelection().getEndVisitSelectionMode(node) == Selection.SELECTED) {
103             invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_cannotHandleSuper, JavaStatusContext.create(fCUnit, node));
104         }
105         super.endVisit(node);
106     }
107     
108     public void endVisit(ConstructorInvocation node) {
109         if (getSelection().getEndVisitSelectionMode(node) == Selection.SELECTED) {
110             invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_cannotHandleThis, JavaStatusContext.create(fCUnit, node));
111         }
112         super.endVisit(node);
113     }
114     
115     protected void postProcessSelectedNodes(List JavaDoc selectedNodes) {
116         if (selectedNodes == null || selectedNodes.size() == 0)
117             return;
118         if (selectedNodes.size() == 1) {
119             ASTNode node= (ASTNode)selectedNodes.get(0);
120             if (node instanceof Expression && node.getParent() instanceof ExpressionStatement) {
121                 selectedNodes.clear();
122                 selectedNodes.add(node.getParent());
123             }
124         }
125     }
126     
127     private boolean onlyStatements() {
128         ASTNode[] nodes= getSelectedNodes();
129         for (int i= 0; i < nodes.length; i++) {
130             if (!(nodes[i] instanceof Statement))
131                 return false;
132         }
133         return true;
134     }
135
136 }
137
Popular Tags