KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > util > AbstractExceptionAnalyzer


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.util;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Stack JavaDoc;
17
18 import org.eclipse.jdt.core.dom.ASTVisitor;
19 import org.eclipse.jdt.core.dom.AnnotationTypeDeclaration;
20 import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
21 import org.eclipse.jdt.core.dom.CatchClause;
22 import org.eclipse.jdt.core.dom.ClassInstanceCreation;
23 import org.eclipse.jdt.core.dom.EnumDeclaration;
24 import org.eclipse.jdt.core.dom.ITypeBinding;
25 import org.eclipse.jdt.core.dom.MethodInvocation;
26 import org.eclipse.jdt.core.dom.ThrowStatement;
27 import org.eclipse.jdt.core.dom.TryStatement;
28 import org.eclipse.jdt.core.dom.TypeDeclaration;
29
30 public abstract class AbstractExceptionAnalyzer extends ASTVisitor {
31     
32     private List JavaDoc fCurrentExceptions; // Elements in this list are of type TypeBinding
33
private Stack JavaDoc fTryStack;
34
35     protected AbstractExceptionAnalyzer() {
36         fTryStack= new Stack JavaDoc();
37         fCurrentExceptions= new ArrayList JavaDoc(1);
38         fTryStack.push(fCurrentExceptions);
39     }
40
41     public abstract boolean visit(ThrowStatement node);
42     
43     public abstract boolean visit(MethodInvocation node);
44     
45     public abstract boolean visit(ClassInstanceCreation node);
46     
47     public boolean visit(TypeDeclaration node) {
48         // Don't dive into a local type.
49
if (node.isLocalTypeDeclaration())
50             return false;
51         return true;
52     }
53
54     public boolean visit(EnumDeclaration node) {
55         // Don't dive into a local type.
56
if (node.isLocalTypeDeclaration())
57             return false;
58         return true;
59     }
60
61     public boolean visit(AnnotationTypeDeclaration node) {
62         // Don't dive into a local type.
63
if (node.isLocalTypeDeclaration())
64             return false;
65         return true;
66     }
67     
68     public boolean visit(AnonymousClassDeclaration node) {
69         // Don't dive into a local type.
70
return false;
71     }
72     
73     public boolean visit(TryStatement node) {
74         fCurrentExceptions= new ArrayList JavaDoc(1);
75         fTryStack.push(fCurrentExceptions);
76         
77         // visit try block
78
node.getBody().accept(this);
79         
80         // Remove those exceptions that get catch by following catch blocks
81
List JavaDoc catchClauses= node.catchClauses();
82         if (!catchClauses.isEmpty())
83             handleCatchArguments(catchClauses);
84         List JavaDoc current= (List JavaDoc)fTryStack.pop();
85         fCurrentExceptions= (List JavaDoc)fTryStack.peek();
86         for (Iterator JavaDoc iter= current.iterator(); iter.hasNext();) {
87             addException((ITypeBinding)iter.next());
88         }
89         
90         // visit catch and finally
91
for (Iterator JavaDoc iter= catchClauses.iterator(); iter.hasNext(); ) {
92             ((CatchClause)iter.next()).accept(this);
93         }
94         if (node.getFinally() != null)
95             node.getFinally().accept(this);
96             
97         // return false. We have visited the body by ourselves.
98
return false;
99     }
100     
101     protected void addExceptions(ITypeBinding[] exceptions) {
102         if(exceptions == null)
103             return;
104         for (int i= 0; i < exceptions.length;i++) {
105             addException(exceptions[i]);
106         }
107     }
108     
109     protected void addException(ITypeBinding exception) {
110         if (!fCurrentExceptions.contains(exception))
111             fCurrentExceptions.add(exception);
112     }
113     
114     protected List JavaDoc getCurrentExceptions() {
115         return fCurrentExceptions;
116     }
117     
118     private void handleCatchArguments(List JavaDoc catchClauses) {
119         for (Iterator JavaDoc iter= catchClauses.iterator(); iter.hasNext(); ) {
120             CatchClause clause= (CatchClause)iter.next();
121             ITypeBinding catchTypeBinding= clause.getException().getType().resolveBinding();
122             if (catchTypeBinding == null) // No correct type resolve.
123
continue;
124             for (Iterator JavaDoc exceptions= new ArrayList JavaDoc(fCurrentExceptions).iterator(); exceptions.hasNext(); ) {
125                 ITypeBinding throwTypeBinding= (ITypeBinding)exceptions.next();
126                 if (catches(catchTypeBinding, throwTypeBinding))
127                     fCurrentExceptions.remove(throwTypeBinding);
128             }
129         }
130     }
131     
132     private boolean catches(ITypeBinding catchTypeBinding, ITypeBinding throwTypeBinding) {
133         while(throwTypeBinding != null) {
134             if (throwTypeBinding == catchTypeBinding)
135                 return true;
136             throwTypeBinding= throwTypeBinding.getSuperclass();
137         }
138         return false;
139     }
140 }
141
Popular Tags