1 11 package org.eclipse.jdt.internal.corext.refactoring.util; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 import java.util.Stack ; 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 fCurrentExceptions; private Stack fTryStack; 34 35 protected AbstractExceptionAnalyzer() { 36 fTryStack= new Stack (); 37 fCurrentExceptions= new ArrayList (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 if (node.isLocalTypeDeclaration()) 50 return false; 51 return true; 52 } 53 54 public boolean visit(EnumDeclaration node) { 55 if (node.isLocalTypeDeclaration()) 57 return false; 58 return true; 59 } 60 61 public boolean visit(AnnotationTypeDeclaration node) { 62 if (node.isLocalTypeDeclaration()) 64 return false; 65 return true; 66 } 67 68 public boolean visit(AnonymousClassDeclaration node) { 69 return false; 71 } 72 73 public boolean visit(TryStatement node) { 74 fCurrentExceptions= new ArrayList (1); 75 fTryStack.push(fCurrentExceptions); 76 77 node.getBody().accept(this); 79 80 List catchClauses= node.catchClauses(); 82 if (!catchClauses.isEmpty()) 83 handleCatchArguments(catchClauses); 84 List current= (List )fTryStack.pop(); 85 fCurrentExceptions= (List )fTryStack.peek(); 86 for (Iterator iter= current.iterator(); iter.hasNext();) { 87 addException((ITypeBinding)iter.next()); 88 } 89 90 for (Iterator 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; 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 getCurrentExceptions() { 115 return fCurrentExceptions; 116 } 117 118 private void handleCatchArguments(List catchClauses) { 119 for (Iterator iter= catchClauses.iterator(); iter.hasNext(); ) { 120 CatchClause clause= (CatchClause)iter.next(); 121 ITypeBinding catchTypeBinding= clause.getException().getType().resolveBinding(); 122 if (catchTypeBinding == null) continue; 124 for (Iterator exceptions= new ArrayList (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 |