1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler.ast;
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;
21 public class ThrowStatement extends Statement {
22 public Expression exception;
23 public TypeBinding exceptionType;
25 public ThrowStatement(Expression exception, int startPosition) {
26 this.exception = exception;
27 this.sourceStart = startPosition;
28 this.sourceEnd = exception.sourceEnd;
31 public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {
33 exception.analyseCode(currentScope, flowContext, flowInfo);
34 // need to check that exception thrown is actually caught somewhere
35 flowContext.checkExceptionHandlers(exceptionType, this, flowInfo, currentScope);
36 return FlowInfo.DEAD_END;
40 * Throw code generation
42 * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
43 * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
45 public void generateCode(BlockScope currentScope, CodeStream codeStream) {
47 if ((bits & IsReachableMASK) == 0)
49 int pc = codeStream.position;
50 exception.generateCode(currentScope, codeStream, true);
52 codeStream.recordPositionsFrom(pc, this.sourceStart);
55 public StringBuffer printStatement(int indent, StringBuffer output) {
57 printIndent(indent, output).append("throw "); //$NON-NLS-1$
58 exception.printExpression(0, output);
59 return output.append(';');
62 public void resolve(BlockScope scope) {
64 exceptionType = exception.resolveTypeExpecting(scope, scope.getJavaLangThrowable());
66 if (exceptionType == NullBinding
67 && scope.environment().options.complianceLevel <= ClassFileConstants.JDK1_3){
68 // if compliant with 1.4, this problem will not be reported
69 scope.problemReporter().cannotThrowNull(this);
71 exception.implicitWidening(exceptionType, exceptionType);
74 public void traverse(ASTVisitor visitor, BlockScope blockScope) {
75 if (visitor.visit(this, blockScope))
76 exception.traverse(visitor, blockScope);
77 visitor.endVisit(this, blockScope);