KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > code > ExceptionAnalyzer


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.code;
12
13 import java.util.List JavaDoc;
14
15 import org.eclipse.jdt.core.dom.ASTNode;
16 import org.eclipse.jdt.core.dom.ClassInstanceCreation;
17 import org.eclipse.jdt.core.dom.IMethodBinding;
18 import org.eclipse.jdt.core.dom.ITypeBinding;
19 import org.eclipse.jdt.core.dom.MethodInvocation;
20 import org.eclipse.jdt.core.dom.SuperMethodInvocation;
21 import org.eclipse.jdt.core.dom.ThrowStatement;
22
23 import org.eclipse.jdt.internal.corext.refactoring.util.AbstractExceptionAnalyzer;
24
25 /* package */ class ExceptionAnalyzer extends AbstractExceptionAnalyzer {
26
27     public static ITypeBinding[] perform(ASTNode[] statements) {
28         ExceptionAnalyzer analyzer= new ExceptionAnalyzer();
29         for (int i= 0; i < statements.length; i++) {
30             statements[i].accept(analyzer);
31         }
32         List JavaDoc exceptions= analyzer.getCurrentExceptions();
33         return (ITypeBinding[]) exceptions.toArray(new ITypeBinding[exceptions.size()]);
34     }
35
36     public boolean visit(ThrowStatement node) {
37         ITypeBinding exception= node.getExpression().resolveTypeBinding();
38         if (exception == null) // Safety net for null bindings when compiling fails.
39
return true;
40         
41         addException(exception);
42         return true;
43     }
44     
45     public boolean visit(MethodInvocation node) {
46         return handleExceptions((IMethodBinding)node.getName().resolveBinding());
47     }
48     
49     public boolean visit(SuperMethodInvocation node) {
50         return handleExceptions((IMethodBinding)node.getName().resolveBinding());
51     }
52     
53     public boolean visit(ClassInstanceCreation node) {
54         return handleExceptions(node.resolveConstructorBinding());
55     }
56     
57     private boolean handleExceptions(IMethodBinding binding) {
58         if (binding == null)
59             return true;
60         addExceptions(binding.getExceptionTypes());
61         return true;
62     }
63 }
64
Popular Tags