1 24 25 package org.aspectj.compiler.base; 26 27 import org.aspectj.compiler.base.ast.*; 28 29 import org.aspectj.compiler.crosscuts.ast.ProceedExpr; 30 31 import java.util.*; 32 33 34 public class ExceptionFinder extends Walker { 35 42 43 public static Set getPossibleExceptions(ASTObject ast, boolean deFacto) { 44 if (ast == null) { 45 return new HashSet(); 46 } 47 48 ExceptionFinder finder = new ExceptionFinder(ast.getCompiler()); 49 finder.deFacto = deFacto; 50 finder.process(ast); 51 return finder.exceptions; 52 } 53 54 55 boolean deFacto; 56 Set exceptions = new HashSet(); 57 58 Set findingInMethods = new HashSet(); 59 60 private ExceptionFinder(JavaCompiler compiler) { 61 super(compiler); 62 } 63 64 void addAll(Set set, TypeDs typeDs) { 65 if (typeDs == null) return; 66 67 final int N = typeDs.size(); 68 for(int i=0; i<N; i++) { 69 set.add(typeDs.get(i).getType()); 70 } 71 } 72 73 void removeSubtypes(Set types, Type baseType) { 74 for(Iterator iter = types.iterator(); iter.hasNext(); ) { 75 Type checkType = (Type)iter.next(); 76 if (checkType.isSubtypeOf(baseType)) { 77 iter.remove(); 78 } 79 } 80 } 81 82 void removeCaughtExceptions(Set exceptions, CatchClauses catchClauses) { 83 if (catchClauses == null) return; 84 85 final int N = catchClauses.size(); 86 for(int i=0; i<N; i++) { 87 Type exceptionType = catchClauses.get(i).getFormal().getType(); 88 removeSubtypes(exceptions, exceptionType); 89 } 90 } 91 92 public ASTObject process(ASTObject node) { 93 if (node instanceof TryCatchStmt) { 94 TryCatchStmt tryStmt = (TryCatchStmt) node; 95 Set bodyExceptions = getPossibleExceptions(tryStmt.getBody(), deFacto); 96 removeCaughtExceptions(bodyExceptions, tryStmt.getCatches()); 97 exceptions.addAll(bodyExceptions); 98 super.process(tryStmt.getCatches()); 99 return node; 100 } 101 102 if (node instanceof ThrowStmt) { 103 ThrowStmt throwStmt = (ThrowStmt)node; 104 exceptions.add(throwStmt.getExpr().getType()); 105 } else if (node instanceof CallExpr) { 106 CallExpr callExpr = (CallExpr)node; 107 if (callExpr.getId().equals("aspectOf")) { 108 return super.process(node); 109 } 110 111 Method method = callExpr.getMethod(); 112 113 if (method == null) { 114 return super.process(node); 116 } 117 addAll(exceptions, method.getThrows()); 118 } else if (node instanceof NewInstanceExpr) { 119 NewInstanceExpr newExpr = (NewInstanceExpr)node; 120 Constructor init = newExpr.getConstructor(); 121 addAll(exceptions, init.getThrows()); 122 } else if (node instanceof ProceedExpr) { 123 exceptions.addAll(((ProceedExpr)node).getPossibleExceptions(deFacto)); 124 } 125 126 if (node instanceof TypeDec) return node; 127 return super.process(node); 128 } 129 } 130 131 | Popular Tags |