1 11 package org.eclipse.jdt.internal.codeassist; 12 13 import java.util.Stack ; 14 15 import org.eclipse.jdt.internal.compiler.ASTVisitor; 16 import org.eclipse.jdt.internal.compiler.ast.AllocationExpression; 17 import org.eclipse.jdt.internal.compiler.ast.Argument; 18 import org.eclipse.jdt.internal.compiler.ast.Block; 19 import org.eclipse.jdt.internal.compiler.ast.MessageSend; 20 import org.eclipse.jdt.internal.compiler.ast.ThrowStatement; 21 import org.eclipse.jdt.internal.compiler.ast.TryStatement; 22 import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; 23 import org.eclipse.jdt.internal.compiler.lookup.BlockScope; 24 import org.eclipse.jdt.internal.compiler.lookup.ClassScope; 25 import org.eclipse.jdt.internal.compiler.lookup.CompilationUnitScope; 26 import org.eclipse.jdt.internal.compiler.lookup.MethodBinding; 27 import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding; 28 import org.eclipse.jdt.internal.compiler.lookup.TypeBinding; 29 import org.eclipse.jdt.internal.compiler.util.SimpleSet; 30 31 public class ThrownExceptionFinder extends ASTVisitor { 32 33 private SimpleSet thrownExceptions; 34 private Stack exceptionsStack; 35 36 public ReferenceBinding[] find(TryStatement tryStatement, BlockScope scope) { 37 this.thrownExceptions = new SimpleSet(); 38 this.exceptionsStack = new Stack (); 39 tryStatement.traverse(this, scope); 40 removeCaughtExceptions(tryStatement); 41 42 ReferenceBinding[] result = new ReferenceBinding[this.thrownExceptions.elementSize]; 43 this.thrownExceptions.asArray(result); 44 return result; 45 } 46 47 private void acceptException(ReferenceBinding binding) { 48 if (binding != null && binding.isValidBinding()) { 49 this.thrownExceptions.add(binding); 50 } 51 } 52 53 public void endVisit(MessageSend messageSend, BlockScope scope) { 54 if (messageSend.binding != null) { 55 this.endVisitMethodInvocation(messageSend.binding); 56 } 57 super.endVisit(messageSend, scope); 58 } 59 60 public void endVisit(AllocationExpression allocationExpression, BlockScope scope) { 61 if (allocationExpression.binding != null) { 62 this.endVisitMethodInvocation(allocationExpression.binding); 63 } 64 super.endVisit(allocationExpression, scope); 65 } 66 67 public void endVisit(ThrowStatement throwStatement, BlockScope scope) { 68 this.acceptException((ReferenceBinding)throwStatement.exception.resolvedType); 69 super.endVisit(throwStatement, scope); 70 } 71 72 73 private void endVisitMethodInvocation(MethodBinding methodBinding) { 74 ReferenceBinding[] thrownExceptionBindings = methodBinding.thrownExceptions; 75 int length = thrownExceptionBindings == null ? 0 : thrownExceptionBindings.length; 76 for (int i = 0; i < length; i++) { 77 this.acceptException(thrownExceptionBindings[i]); 78 } 79 } 80 81 public boolean visit(TypeDeclaration typeDeclaration, CompilationUnitScope scope) { 82 return this.visitType(typeDeclaration); 83 } 84 85 public boolean visit(TypeDeclaration memberTypeDeclaration, ClassScope scope) { 86 return this.visitType(memberTypeDeclaration); 87 } 88 89 public boolean visit(TypeDeclaration localTypeDeclaration, BlockScope scope) { 90 return this.visitType(localTypeDeclaration); 91 } 92 93 private boolean visitType(TypeDeclaration typeDeclaration) { 94 return false; 95 } 96 97 public boolean visit(TryStatement tryStatement, BlockScope scope) { 98 this.exceptionsStack.push(this.thrownExceptions); 99 SimpleSet exceptionSet = new SimpleSet(); 100 this.thrownExceptions = exceptionSet; 101 tryStatement.tryBlock.traverse(this, scope); 102 103 this.removeCaughtExceptions(tryStatement); 104 105 this.thrownExceptions = (SimpleSet)this.exceptionsStack.pop(); 106 107 Object [] values = exceptionSet.values; 108 for (int i = 0; i < values.length; i++) { 109 if (values[i] != null) { 110 this.thrownExceptions.add(values[i]); 111 } 112 } 113 114 Block[] catchBlocks = tryStatement.catchBlocks; 115 int length = catchBlocks == null ? 0 : catchBlocks.length; 116 for (int i = 0; i < length; i++) { 117 catchBlocks[i].traverse(this, scope); 118 } 119 return false; 120 } 121 122 private void removeCaughtExceptions(TryStatement tryStatement) { 123 Argument[] catchArguments = tryStatement.catchArguments; 124 int length = catchArguments == null ? 0 : catchArguments.length; 125 for (int i = 0; i < length; i++) { 126 TypeBinding exception = catchArguments[i].type.resolvedType; 127 if (exception != null && exception.isValidBinding()) { 128 this.removeCaughtException((ReferenceBinding)exception); 129 130 } 131 } 132 } 133 134 private void removeCaughtException(ReferenceBinding caughtException) { 135 Object [] exceptions = this.thrownExceptions.values; 136 for (int i = 0; i < exceptions.length; i++) { 137 ReferenceBinding exception = (ReferenceBinding)exceptions[i]; 138 if (exception != null) { 139 if (exception == caughtException || caughtException.isSuperclassOf(exception)) { 140 this.thrownExceptions.remove(exception); 141 } 142 } 143 } 144 } 145 } 146 | Popular Tags |