KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > ast > ThrowStatement


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.compiler.ast;
12
13 import org.eclipse.jdt.internal.compiler.ASTVisitor;
14 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
15 import org.eclipse.jdt.internal.compiler.codegen.CodeStream;
16 import org.eclipse.jdt.internal.compiler.flow.FlowContext;
17 import org.eclipse.jdt.internal.compiler.flow.FlowInfo;
18 import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
19 import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
20 import org.eclipse.jdt.internal.compiler.lookup.TypeIds;
21
22 public class ThrowStatement extends Statement {
23     
24     public Expression exception;
25     public TypeBinding exceptionType;
26
27 public ThrowStatement(Expression exception, int sourceStart, int sourceEnd) {
28     this.exception = exception;
29     this.sourceStart = sourceStart;
30     this.sourceEnd = sourceEnd;
31 }
32
33 public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {
34     this.exception.analyseCode(currentScope, flowContext, flowInfo);
35     // need to check that exception thrown is actually caught somewhere
36
flowContext.checkExceptionHandlers(this.exceptionType, this, flowInfo, currentScope);
37     return FlowInfo.DEAD_END;
38 }
39
40 /**
41  * Throw code generation
42  *
43  * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
44  * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
45  */

46 public void generateCode(BlockScope currentScope, CodeStream codeStream) {
47     if ((this.bits & ASTNode.IsReachable) == 0)
48         return;
49     int pc = codeStream.position;
50     this.exception.generateCode(currentScope, codeStream, true);
51     codeStream.athrow();
52     codeStream.recordPositionsFrom(pc, this.sourceStart);
53 }
54
55 public StringBuffer JavaDoc printStatement(int indent, StringBuffer JavaDoc output) {
56     printIndent(indent, output).append("throw "); //$NON-NLS-1$
57
this.exception.printExpression(0, output);
58     return output.append(';');
59 }
60
61 public void resolve(BlockScope scope) {
62     this.exceptionType = this.exception.resolveType(scope);
63     if (this.exceptionType != null && this.exceptionType.isValidBinding()) {
64         if (this.exceptionType == TypeBinding.NULL) {
65             if (scope.compilerOptions().complianceLevel <= ClassFileConstants.JDK1_3){
66                 // if compliant with 1.4, this problem will not be reported
67
scope.problemReporter().cannotThrowNull(this.exception);
68             }
69         } else if (exceptionType.findSuperTypeErasingTo(TypeIds.T_JavaLangThrowable, true) == null) {
70             scope.problemReporter().cannotThrowType(this.exception, this.exceptionType);
71         }
72         this.exception.computeConversion(scope, this.exceptionType, this.exceptionType);
73     }
74 }
75
76 public void traverse(ASTVisitor visitor, BlockScope blockScope) {
77     if (visitor.visit(this, blockScope))
78         this.exception.traverse(visitor, blockScope);
79     visitor.endVisit(this, blockScope);
80 }
81 }
82
Popular Tags