KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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 org.eclipse.jdt.core.ICompilationUnit;
14 import org.eclipse.jdt.core.JavaModelException;
15 import org.eclipse.jdt.core.dom.BodyDeclaration;
16 import org.eclipse.jdt.core.dom.CompilationUnit;
17 import org.eclipse.jdt.core.dom.ITypeBinding;
18
19 import org.eclipse.jdt.internal.corext.dom.ASTNodes;
20 import org.eclipse.jdt.internal.corext.dom.Selection;
21 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
22
23 public class SurroundWithTryCatchAnalyzer extends SurroundWithAnalyzer {
24
25
26     private ISurroundWithTryCatchQuery fQuery;
27     private ITypeBinding[] fExceptions;
28
29     public SurroundWithTryCatchAnalyzer(ICompilationUnit unit, Selection selection, ISurroundWithTryCatchQuery query) throws JavaModelException {
30         super(unit, selection);
31         fQuery= query;
32     }
33     
34     public ITypeBinding[] getExceptions() {
35         return fExceptions;
36     }
37
38     public void endVisit(CompilationUnit node) {
39         BodyDeclaration enclosingNode= null;
40         if (!getStatus().hasFatalError() && hasSelectedNodes())
41             enclosingNode= (BodyDeclaration)ASTNodes.getParent(getFirstSelectedNode(), BodyDeclaration.class);
42
43         super.endVisit(node);
44         if (enclosingNode != null && !getStatus().hasFatalError()) {
45             fExceptions= ExceptionAnalyzer.perform(enclosingNode, getSelection());
46             if (fExceptions == null || fExceptions.length == 0) {
47                 if (fQuery == null) {
48                     invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_noUncaughtExceptions);
49                 } else if (fQuery.catchRuntimeException()) {
50                     fExceptions= new ITypeBinding[] {node.getAST().resolveWellKnownType("java.lang.RuntimeException")}; //$NON-NLS-1$
51
}
52             }
53         }
54     }
55 }
56
Popular Tags