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.codegen.*;
14 import org.eclipse.jdt.internal.compiler.flow.*;
15 import org.eclipse.jdt.internal.compiler.impl.Constant;
16 import org.eclipse.jdt.internal.compiler.lookup.*;
17 import org.eclipse.jdt.internal.compiler.ASTVisitor;
19 public class AssertStatement extends Statement {
21 public Expression assertExpression, exceptionArgument;
23 // for local variable attribute
24 int preAssertInitStateIndex = -1;
25 private FieldBinding assertionSyntheticFieldBinding;
27 public AssertStatement(
28 Expression exceptionArgument,
29 Expression assertExpression,
32 this.assertExpression = assertExpression;
33 this.exceptionArgument = exceptionArgument;
34 sourceStart = startPosition;
35 sourceEnd = exceptionArgument.sourceEnd;
38 public AssertStatement(Expression assertExpression, int startPosition) {
40 this.assertExpression = assertExpression;
41 sourceStart = startPosition;
42 sourceEnd = assertExpression.sourceEnd;
45 public FlowInfo analyseCode(
46 BlockScope currentScope,
47 FlowContext flowContext,
50 preAssertInitStateIndex = currentScope.methodScope().recordInitializationStates(flowInfo);
52 Constant cst = this.assertExpression.optimizedBooleanConstant();
53 boolean isOptimizedTrueAssertion = cst != NotAConstant && cst.booleanValue() == true;
54 boolean isOptimizedFalseAssertion = cst != NotAConstant && cst.booleanValue() == false;
56 FlowInfo assertInfo = flowInfo.copy();
57 if (isOptimizedTrueAssertion) {
58 assertInfo.setReachMode(FlowInfo.UNREACHABLE);
60 assertInfo = assertExpression.analyseCode(currentScope, flowContext, assertInfo).unconditionalInits();
62 if (exceptionArgument != null) {
63 // only gets evaluated when escaping - results are not taken into account
64 FlowInfo exceptionInfo = exceptionArgument.analyseCode(currentScope, flowContext, assertInfo.copy());
66 if (!isOptimizedTrueAssertion){
67 flowContext.checkExceptionHandlers(
68 currentScope.getJavaLangAssertionError(),
75 if (!isOptimizedTrueAssertion){
76 // add the assert support in the clinit
77 manageSyntheticAccessIfNecessary(currentScope, flowInfo);
79 if (isOptimizedFalseAssertion) {
80 return flowInfo; // if assertions are enabled, the following code will be unreachable
82 return flowInfo.mergedWith(assertInfo.unconditionalInits());
86 public void generateCode(BlockScope currentScope, CodeStream codeStream) {
88 if ((bits & IsReachableMASK) == 0) {
91 int pc = codeStream.position;
93 if (this.assertionSyntheticFieldBinding != null) {
94 Label assertionActivationLabel = new Label(codeStream);
95 codeStream.getstatic(this.assertionSyntheticFieldBinding);
96 codeStream.ifne(assertionActivationLabel);
98 Label falseLabel = new Label(codeStream);
99 this.assertExpression.generateOptimizedBoolean(currentScope, codeStream, (falseLabel = new Label(codeStream)), null , true);
100 codeStream.newJavaLangAssertionError();
102 if (exceptionArgument != null) {
103 exceptionArgument.generateCode(currentScope, codeStream, true);
104 codeStream.invokeJavaLangAssertionErrorConstructor(exceptionArgument.implicitConversion & 0xF);
106 codeStream.invokeJavaLangAssertionErrorDefaultConstructor();
110 assertionActivationLabel.place();
113 // May loose some local variable initializations : affecting the local variable attributes
114 if (preAssertInitStateIndex != -1) {
115 codeStream.removeNotDefinitelyAssignedVariables(currentScope, preAssertInitStateIndex);
117 codeStream.recordPositionsFrom(pc, this.sourceStart);
120 public void resolve(BlockScope scope) {
122 assertExpression.resolveTypeExpecting(scope, BooleanBinding);
123 if (exceptionArgument != null) {
124 TypeBinding exceptionArgumentType = exceptionArgument.resolveType(scope);
125 if (exceptionArgumentType != null){
126 int id = exceptionArgumentType.id;
129 scope.problemReporter().illegalVoidExpression(exceptionArgument);
141 exceptionArgument.implicitConversion = (id << 4) + id;
147 public void traverse(ASTVisitor visitor, BlockScope scope) {
149 if (visitor.visit(this, scope)) {
150 assertExpression.traverse(visitor, scope);
151 if (exceptionArgument != null) {
152 exceptionArgument.traverse(visitor, scope);
155 visitor.endVisit(this, scope);
158 public void manageSyntheticAccessIfNecessary(BlockScope currentScope, FlowInfo flowInfo) {
160 if (!flowInfo.isReachable()) return;
162 // need assertion flag: $assertionsDisabled on outer most source clas
163 // (in case of static member of interface, will use the outermost static member - bug 22334)
164 SourceTypeBinding outerMostClass = currentScope.enclosingSourceType();
165 while (outerMostClass.isLocalType()){
166 ReferenceBinding enclosing = outerMostClass.enclosingType();
167 if (enclosing == null || enclosing.isInterface()) break;
168 outerMostClass = (SourceTypeBinding) enclosing;
171 this.assertionSyntheticFieldBinding = outerMostClass.addSyntheticField(this, currentScope);
173 // find <clinit> and enable assertion support
174 TypeDeclaration typeDeclaration = outerMostClass.scope.referenceType();
175 AbstractMethodDeclaration[] methods = typeDeclaration.methods;
176 for (int i = 0, max = methods.length; i < max; i++) {
177 AbstractMethodDeclaration method = methods[i];
178 if (method.isClinit()) {
179 ((Clinit) method).setAssertionSupport(assertionSyntheticFieldBinding);
185 public StringBuffer printStatement(int tab, StringBuffer output) {
187 printIndent(tab, output);
188 output.append("assert "); //$NON-NLS-1$
189 this.assertExpression.printExpression(0, output);
190 if (this.exceptionArgument != null) {
191 output.append(": "); //$NON-NLS-1$
192 this.exceptionArgument.printExpression(0, output);
194 return output.append(';');