]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/core refactoring/org/eclipse/jdt/internal/corext/refactoring/surround/SurroundWithAnalyzer.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / core refactoring / org / eclipse / jdt / internal / corext / refactoring / surround / SurroundWithAnalyzer.java
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
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;
14
15 import org.eclipse.core.runtime.CoreException;
16
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;
31
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;
37
38 public class SurroundWithAnalyzer extends CodeAnalyzer {
39
40         private VariableDeclaration[] fLocals;
41
42         public SurroundWithAnalyzer(ICompilationUnit cunit, Selection selection) throws CoreException {
43                 super(cunit, selection, false);
44         }
45
46         public Statement[] getSelectedStatements() {
47                 if (hasSelectedNodes()) {
48                         return internalGetSelectedNodes().toArray(new Statement[internalGetSelectedNodes().size()]);
49                 } else {
50                         return new Statement[0];
51                 }
52         }
53
54         public VariableDeclaration[] getAffectedLocals() {
55                 return fLocals;
56         }
57
58         public BodyDeclaration getEnclosingBodyDeclaration() {
59                 ASTNode node= getFirstSelectedNode();
60                 if (node == null)
61                         return null;
62                 return (BodyDeclaration)ASTNodes.getParent(node, BodyDeclaration.class);
63         }
64
65         @Override
66         protected boolean handleSelectionEndsIn(ASTNode node) {
67                 return true;
68         }
69
70         @Override
71         public void endVisit(CompilationUnit node) {
72                 postProcessSelectedNodes(internalGetSelectedNodes());
73                 BodyDeclaration enclosingNode= null;
74                 superCall: {
75                         if (getStatus().hasFatalError())
76                                 break superCall;
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));
85                                                 break superCall;
86                                         }
87                                 }
88                                 invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_doesNotCover);
89                                 break superCall;
90                         }
91                         enclosingNode= (BodyDeclaration)ASTNodes.getParent(getFirstSelectedNode(), BodyDeclaration.class);
92                         if (!(enclosingNode instanceof MethodDeclaration) && !(enclosingNode instanceof Initializer)) {
93                                 invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_doesNotContain);
94                                 break superCall;
95                         }
96                         if (!onlyStatements()) {
97                                 invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_onlyStatements);
98                         }
99                         fLocals= LocalDeclarationAnalyzer.perform(enclosingNode, getSelection());
100                 }
101                 super.endVisit(node);
102         }
103
104         @Override
105         public void endVisit(SuperConstructorInvocation node) {
106                 if (getSelection().getEndVisitSelectionMode(node) == Selection.SELECTED) {
107                         invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_cannotHandleSuper, JavaStatusContext.create(fCUnit, node));
108                 }
109                 super.endVisit(node);
110         }
111
112         @Override
113         public void endVisit(ConstructorInvocation node) {
114                 if (getSelection().getEndVisitSelectionMode(node) == Selection.SELECTED) {
115                         invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_cannotHandleThis, JavaStatusContext.create(fCUnit, node));
116                 }
117                 super.endVisit(node);
118         }
119
120         protected void postProcessSelectedNodes(List<ASTNode> selectedNodes) {
121                 if (selectedNodes == null || selectedNodes.size() == 0)
122                         return;
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());
128                         }
129                 }
130         }
131
132         private boolean onlyStatements() {
133                 ASTNode[] nodes= getSelectedNodes();
134                 for (int i= 0; i < nodes.length; i++) {
135                         if (!(nodes[i] instanceof Statement))
136                                 return false;
137                 }
138                 return true;
139         }
140
141 }