KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > retouche > editor > semantic > MethodExitDetector


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.retouche.editor.semantic;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.EnumSet JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.Stack JavaDoc;
31 import javax.swing.text.Document JavaDoc;
32 import org.netbeans.api.retouche.source.CompilationInfo;
33 import org.netbeans.modules.editor.highlights.spi.Highlight;
34
35 /**
36  * This file is originally from Retouche, the Java Support
37  * infrastructure in NetBeans. I have modified the file as little
38  * as possible to make merging Retouche fixes back as simple as
39  * possible.
40  *
41  *
42  * @author Jan Lahoda
43  */

44 public class MethodExitDetector { // extends CancellableTreePathScanner<Boolean, Stack<Tree>> {
45

46     /** Creates a new instance of MethodExitDetector */
47     public MethodExitDetector() {
48     }
49     
50     private CompilationInfo info;
51 // private Document doc;
52
// private Set<Highlight> highlights;
53
// private Collection<TypeMirror> exceptions;
54
// private Stack<Map<TypeMirror, List<Highlight>>> exceptions2HighlightsStack;
55

56     public Set JavaDoc<Highlight> process(CompilationInfo info, Document JavaDoc document/*, MethodTree methoddecl, Collection<Tree> excs*/) {
57         this.info = info;
58         System.err.println("MethodExitDetecter is Lobotomized");
59 // this.doc = document;
60
// this.highlights = new HashSet<Highlight>();
61
// this.exceptions2HighlightsStack = new Stack<Map<TypeMirror, List<Highlight>>>();
62
// this.exceptions2HighlightsStack.push(null);
63
//
64
// try {
65
Set JavaDoc<Highlight> result = new HashSet JavaDoc<Highlight>();
66 //
67
// CompilationUnitTree cu = info.getCompilationUnit();
68
//
69
// Boolean wasReturn = scan(TreePath.getPath(cu, methoddecl), null);
70
//
71
// if (isCanceled())
72
// return Collections.emptySet();
73
//
74
// if (excs == null) {
75
// //"return" exit point only if not searching for exceptions:
76
// result.addAll(highlights);
77
//
78
// if (wasReturn != Boolean.TRUE) {
79
// int lastBracket = Utilities.findLastBracket(methoddecl, cu, info.getTrees().getSourcePositions(), document);
80
//
81
// if (lastBracket != (-1)) {
82
// //highlight the "fall over" exitpoint:
83
// result.add(Utilities.createHighlight(cu, info.getTrees().getSourcePositions(), document, lastBracket, lastBracket + 1, EnumSet.of(ColoringAttributes.MARK_OCCURRENCES), MarkOccurencesHighlighter.ES_COLOR));
84
// }
85
// }
86
// }
87
//
88
// List<TypeMirror> exceptions = null;
89
//
90
// if (excs != null) {
91
// exceptions = new ArrayList<TypeMirror>();
92
//
93
// for (Tree t : excs) {
94
// if (isCanceled())
95
// return Collections.emptySet();
96
//
97
// TypeMirror m = info.getTrees().getTypeMirror(TreePath.getPath(cu, t));
98
//
99
// if (m != null) {
100
// exceptions.add(m);
101
// }
102
// }
103
// }
104
//
105
// Types t = info.getTypes();
106
//
107
// assert exceptions2HighlightsStack.size() == 1 : exceptions2HighlightsStack.size();
108
//
109
// Map<TypeMirror, List<Highlight>> exceptions2Highlights = exceptions2HighlightsStack.peek();
110
//
111
// //exceptions2Highlights may be null if the method is empty (or not finished, like "public void")
112
// //see ExitPointsEmptyMethod and ExitPointsStartedMethod tests:
113
// if (exceptions2Highlights != null) {
114
// for (TypeMirror type1 : exceptions2Highlights.keySet()) {
115
// if (isCanceled())
116
// return Collections.emptySet();
117
//
118
// boolean add = true;
119
//
120
// if (exceptions != null) {
121
// add = false;
122
//
123
// for (TypeMirror type2 : exceptions) {
124
// add |= t.isAssignable(type1, type2);
125
// }
126
// }
127
//
128
// if (add) {
129
// result.addAll(exceptions2Highlights.get(type1));
130
// }
131
// }
132
// }
133
//
134
return result;
135 // } finally {
136
// //clean-up:
137
// this.info = null;
138
// this.doc = null;
139
// this.highlights = null;
140
// this.exceptions2HighlightsStack = null;
141
// }
142
}
143     
144 // private void addToExceptionsMap(TypeMirror key, Highlight value) {
145
// if (value == null)
146
// return ;
147
//
148
// Map<TypeMirror, List<Highlight>> map = exceptions2HighlightsStack.peek();
149
//
150
// if (map == null) {
151
// map = new HashMap<TypeMirror, List<Highlight>>();
152
// exceptions2HighlightsStack.pop();
153
// exceptions2HighlightsStack.push(map);
154
// }
155
//
156
// List<Highlight> l = map.get(key);
157
//
158
// if (l == null) {
159
// map.put(key, l = new ArrayList<Highlight>());
160
// }
161
//
162
// l.add(value);
163
// }
164
//
165
// private void doPopup() {
166
// Map<TypeMirror, List<Highlight>> top = exceptions2HighlightsStack.pop();
167
//
168
// if (top == null)
169
// return ;
170
//
171
// Map<TypeMirror, List<Highlight>> result = exceptions2HighlightsStack.pop();
172
//
173
// if (result == null) {
174
// exceptions2HighlightsStack.push(top);
175
// return ;
176
// }
177
//
178
// for (TypeMirror key : top.keySet()) {
179
// List<Highlight> topKey = top.get(key);
180
// List<Highlight> resultKey = result.get(key);
181
//
182
// if (topKey == null)
183
// continue;
184
//
185
// if (resultKey == null) {
186
// result.put(key, topKey);
187
// continue;
188
// }
189
//
190
// resultKey.addAll(topKey);
191
// }
192
//
193
// exceptions2HighlightsStack.push(result);
194
// }
195
//
196
// private Highlight createHighlight(TreePath tree) {
197
// return Utilities.createHighlight(info.getCompilationUnit(), info.getTrees().getSourcePositions(), doc, tree, EnumSet.of(ColoringAttributes.MARK_OCCURRENCES), MarkOccurencesHighlighter.ES_COLOR);
198
// }
199
//
200
// @Override
201
// public Boolean visitTry(TryTree tree, Stack<Tree> d) {
202
// exceptions2HighlightsStack.push(null);
203
//
204
// Boolean returnInTryBlock = scan(tree.getBlock(), d);
205
//
206
// boolean returnInCatchBlock = true;
207
//
208
// for (Tree t : tree.getCatches()) {
209
// Boolean b = scan(t, d);
210
//
211
// returnInCatchBlock &= b == Boolean.TRUE;
212
// }
213
//
214
// Boolean returnInFinallyBlock = scan(tree.getFinallyBlock(), d);
215
//
216
// doPopup();
217
//
218
// if (returnInTryBlock == Boolean.TRUE && returnInCatchBlock)
219
// return Boolean.TRUE;
220
//
221
// return returnInFinallyBlock;
222
// }
223
//
224
// @Override
225
// public Boolean visitReturn(ReturnTree tree, Stack<Tree> d) {
226
// if (exceptions == null) {
227
// Highlight h = createHighlight(getCurrentPath());
228
//
229
// if (h != null) {
230
// highlights.add(h);
231
// }
232
// }
233
//
234
// super.visitReturn(tree, d);
235
// return Boolean.TRUE;
236
// }
237
//
238
// @Override
239
// public Boolean visitCatch(CatchTree tree, Stack<Tree> d) {
240
// TypeMirror type1 = info.getTrees().getTypeMirror(new TreePath(new TreePath(getCurrentPath(), tree.getParameter()), tree.getParameter().getType()));
241
// Types t = info.getTypes();
242
//
243
// if (type1 != null) {
244
// Set<TypeMirror> toRemove = new HashSet<TypeMirror>();
245
// Map<TypeMirror, List<Highlight>> exceptions2Highlights = exceptions2HighlightsStack.peek();
246
//
247
// if (exceptions2Highlights != null) {
248
// for (TypeMirror type2 : exceptions2Highlights.keySet()) {
249
// if (t.isAssignable(type2, type1)) {
250
// toRemove.add(type2);
251
// }
252
// }
253
//
254
// for (TypeMirror type : toRemove) {
255
// exceptions2Highlights.remove(type);
256
// }
257
// }
258
//
259
// }
260
//
261
// scan(tree.getParameter(), d);
262
// return scan(tree.getBlock(), d);
263
// }
264
//
265
// @Override
266
// public Boolean visitMethodInvocation(MethodInvocationTree tree, Stack<Tree> d) {
267
// Element el = info.getTrees().getElement(new TreePath(getCurrentPath(), tree.getMethodSelect()));
268
//
269
// if (el == null) {
270
// System.err.println("Warning: decl == null");
271
// System.err.println("tree=" + tree);
272
// }
273
//
274
// if (el != null && el.getKind() == ElementKind.METHOD) {
275
// for (TypeMirror m : ((ExecutableElement) el).getThrownTypes()) {
276
// addToExceptionsMap(m, createHighlight(getCurrentPath()));
277
// }
278
// }
279
//
280
// super.visitMethodInvocation(tree, d);
281
// return null;
282
// }
283
//
284
// @Override
285
// public Boolean visitThrow(ThrowTree tree, Stack<Tree> d) {
286
// addToExceptionsMap(info.getTrees().getTypeMirror(new TreePath(getCurrentPath(), tree.getExpression())), createHighlight(getCurrentPath()));
287
//
288
// super.visitThrow(tree, d);
289
//
290
// return Boolean.TRUE;
291
// }
292
//
293
// @Override
294
// public Boolean visitNewClass(NewClassTree tree, Stack<Tree> d) {
295
// Element el = info.getTrees().getElement(getCurrentPath());
296
//
297
// if (el != null && el.getKind() == ElementKind.CONSTRUCTOR) {
298
// for (TypeMirror m : ((ExecutableElement) el).getThrownTypes()) {
299
// addToExceptionsMap(m, createHighlight(getCurrentPath()));
300
// }
301
// }
302
//
303
// super.visitNewClass(tree, d);
304
// return null;
305
// }
306
//
307
// @Override
308
// public Boolean visitMethod(MethodTree node, Stack<Tree> p) {
309
// scan(node.getModifiers(), p);
310
// scan(node.getReturnType(), p);
311
// scan(node.getTypeParameters(), p);
312
// scan(node.getParameters(), p);
313
// scan(node.getThrows(), p);
314
// return scan(node.getBody(), p);
315
// }
316
//
317
// @Override
318
// public Boolean visitIf(IfTree node, Stack<Tree> p) {
319
// scan(node.getCondition(), p);
320
// Boolean thenResult = scan(node.getThenStatement(), p);
321
// Boolean elseResult = scan(node.getElseStatement(), p);
322
//
323
// if (thenResult == Boolean.TRUE && elseResult == Boolean.TRUE)
324
// return Boolean.TRUE;
325
//
326
// return null;
327
// }
328

329 }
330
Popular Tags