Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2000, 2013 IBM Corporation and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: IBM Corporation - initial API and implementation Stephan Herrmann - Contributions for bug 319201 - [null] no warning when unboxing SingleNameReference causes NPE bug 349326 - [1.7] new warning for missing try-with-resources bug 345305 - [compiler][null] Compiler misidentifies a case of "variable can only be null" bug 383368 - [compiler][null] syntactic null analysis for field references bug 403147 - [compiler][null] FUP of bug 400761: consolidate interaction between unboxing, NPE, and deferred checking /
 
 package org.eclipse.jdt.internal.compiler.ast;
 
 
 public class IfStatement extends Statement {
 
 	//this class represents the case of only one statement in
 	//either else and/or then branches.
 
 	public Expression condition;
 
 	// for local variables table attributes
 	int thenInitStateIndex = -1;
 	int elseInitStateIndex = -1;
 	int mergedInitStateIndex = -1;
 
 public IfStatement(Expression conditionStatement thenStatementint sourceStartint sourceEnd) {
 	this. = condition;
 	this. = thenStatement;
 	// remember useful empty statement
 	if (thenStatement instanceof EmptyStatementthenStatement.bits |= ;
 	this. = sourceStart;
 	this. = sourceEnd;
 }
 
 public IfStatement(Expression conditionStatement thenStatementStatement elseStatementint sourceStartint sourceEnd) {
 	this. = condition;
 	this. = thenStatement;
 	// remember useful empty statement
 	if (thenStatement instanceof EmptyStatementthenStatement.bits |= ;
 	this. = elseStatement;
 	if (elseStatement instanceof IfStatementelseStatement.bits |= ;
 	if (elseStatement instanceof EmptyStatementelseStatement.bits |= ;
 	this. = sourceStart;
 	this. = sourceEnd;
 }
 
 public FlowInfo analyseCode(BlockScope currentScopeFlowContext flowContextFlowInfo flowInfo) {
 	// process the condition
 	FlowInfo conditionFlowInfo = this..analyseCode(currentScopeflowContextflowInfo);
 	int initialComplaintLevel = (flowInfo.reachMode() & .) != 0 ? . : .;
 
 	this..checkNPEbyUnboxing(currentScopeflowContextflowInfo);
 	boolean isConditionOptimizedTrue = cst != . && cst.booleanValue() == true;
 	boolean isConditionOptimizedFalse = cst != . && cst.booleanValue() == false;
 
 	flowContext.conditionalLevel++;
 
 	// process the THEN part
 	FlowInfo thenFlowInfo = conditionFlowInfo.safeInitsWhenTrue();
 	if (isConditionOptimizedFalse) {
 	}
 	FlowInfo elseFlowInfo = conditionFlowInfo.initsWhenFalse().copy();
 	if (isConditionOptimizedTrue) {
 	}
 	if (((flowInfo.tagBits & .) == 0) && 
 			((thenFlowInfo.tagBits & .) != 0)) {
 		// Mark then block as unreachable
 		// No need if the whole if-else construct itself lies in unreachable code
 	} else if (((flowInfo.tagBits & .) == 0) &&
 			((elseFlowInfo.tagBits & .) != 0)) {
 		// Mark else block as unreachable
 		// No need if the whole if-else construct itself lies in unreachable code
 	}
 	boolean reportDeadCodeForKnownPattern = !isKnowDeadCodePattern(this.) || currentScope.compilerOptions().;
 	if (this. != null) {
 		// Save info for code gen
 		this. = currentScope.methodScope().recordInitializationStates(thenFlowInfo);
 		if (isConditionOptimizedFalse || ((this. & .) != 0)) {
 			if (reportDeadCodeForKnownPattern) {
 				this..complainIfUnreachable(thenFlowInfocurrentScopeinitialComplaintLevelfalse);
 			} else {
				// its a known coding pattern which should be tolerated by dead code analysis
				// according to isKnowDeadCodePattern()
			}
		}
		thenFlowInfo = this..analyseCode(currentScopeflowContextthenFlowInfo);
	}
	// any null check from the condition is now expired
	// code gen: optimizing the jump around the ELSE part
	if ((thenFlowInfo.tagBits & .) != 0) {
	}
	// process the ELSE part
	if (this. != null) {
		// signal else clause unnecessarily nested, tolerate else-if code pattern
		if (thenFlowInfo == .
				&& (this. & ) == 0 	// else of an else-if
				&& !(this. instanceof IfStatement)) {
		}
		// Save info for code gen
		this. = currentScope.methodScope().recordInitializationStates(elseFlowInfo);
		if (isConditionOptimizedTrue || ((this. & .) != 0)) {
			if (reportDeadCodeForKnownPattern) {
				this..complainIfUnreachable(elseFlowInfocurrentScopeinitialComplaintLevelfalse);
else {
				// its a known coding pattern which should be tolerated by dead code analysis
				// according to isKnowDeadCodePattern()
			}
		}
		elseFlowInfo = this..analyseCode(currentScopeflowContextelseFlowInfo);
	}
	// process AutoCloseable resources closed in only one branch:
	currentScope.correlateTrackingVarsIfElse(thenFlowInfoelseFlowInfo);
	// merge THEN & ELSE initializations
	FlowInfo mergedInfo = FlowInfo.mergedOptimizedBranchesIfElse(
		thenFlowInfo,
		isConditionOptimizedTrue,
		elseFlowInfo,
		isConditionOptimizedFalse,
		true /*if(true){ return; }  fake-reachable(); */,
		flowInfo,
		this,
		reportDeadCodeForKnownPattern);
	this. = currentScope.methodScope().recordInitializationStates(mergedInfo);
	flowContext.conditionalLevel--;
	return mergedInfo;
If code generation

Parameters:
currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
public void generateCode(BlockScope currentScopeCodeStream codeStream) {
	if ((this. & ) == 0) {
		return;
	}
	int pc = codeStream.position;
	BranchLabel endifLabel = new BranchLabel(codeStream);
	// optimizing the then/else part code gen
	boolean hasThenPart =
				&& cst.booleanValue() == false)
			|| this. == null
	boolean hasElsePart =
		!((cst != . && cst.booleanValue() == true)
			|| this. == null
	if (hasThenPart) {
		BranchLabel falseLabel = null;
		// generate boolean condition only if needed
		if (cst != . && cst.booleanValue() == true) {
			this..generateCode(currentScopecodeStreamfalse);
else {
				currentScope,
				codeStream,
				null,
				hasElsePart ? (falseLabel = new BranchLabel(codeStream)) : endifLabel,
				true/*cst == Constant.NotAConstant*/);
		}
		// May loose some local variable initializations : affecting the local variable attributes
		if (this. != -1) {
			codeStream.addDefinitelyAssignedVariables(currentScopethis.);
		}
		// generate then statement
		this..generateCode(currentScopecodeStream);
		// jump around the else statement
		if (hasElsePart) {
			if ((this. & .) == 0) {
				this..branchChainTo(endifLabel);
				int position = codeStream.position;
				codeStream.goto_(endifLabel);
				//goto is pointing to the last line of the thenStatement
				codeStream.recordPositionsFrom(positionthis..);
				// generate else statement
			}
			// May loose some local variable initializations : affecting the local variable attributes
			if (this. != -1) {
					currentScope,
				codeStream.addDefinitelyAssignedVariables(currentScopethis.);
			}
			if (falseLabel != nullfalseLabel.place();
			this..generateCode(currentScopecodeStream);
		}
else if (hasElsePart) {
		// generate boolean condition only if needed
		if (cst != . && cst.booleanValue() == false) {
			this..generateCode(currentScopecodeStreamfalse);
else {
				currentScope,
				codeStream,
				endifLabel,
				null,
				true/*cst == Constant.NotAConstant*/);
		}
		// generate else statement
		// May loose some local variable initializations : affecting the local variable attributes
		if (this. != -1) {
				currentScope,
			codeStream.addDefinitelyAssignedVariables(currentScopethis.);
		}
		this..generateCode(currentScopecodeStream);
else {
		// generate condition side-effects
		this..generateCode(currentScopecodeStreamfalse);
		codeStream.recordPositionsFrom(pcthis.);
	}
	// May loose some local variable initializations : affecting the local variable attributes
	if (this. != -1) {
			currentScope,
	}
	endifLabel.place();
	codeStream.recordPositionsFrom(pcthis.);
public StringBuffer printStatement(int indentStringBuffer output) {
	printIndent(indentoutput).append("if ("); //$NON-NLS-1$
	this..printExpression(0, output).append(")\n");	//$NON-NLS-1$
	this..printStatement(indent + 2, output);
	if (this. != null) {
		output.append('\n');
		printIndent(indentoutput);
		output.append("else\n"); //$NON-NLS-1$
		this..printStatement(indent + 2, output);
	}
	return output;
public void resolve(BlockScope scope) {
	this..computeConversion(scopetypetype);
	if (this. != null)
	if (this. != null)
public void traverse(ASTVisitor visitorBlockScope blockScope) {
	if (visitor.visit(thisblockScope)) {
		this..traverse(visitorblockScope);
		if (this. != null)
			this..traverse(visitorblockScope);
		if (this. != null)
			this..traverse(visitorblockScope);
	}
	visitor.endVisit(thisblockScope);
New to GrepCode? Check out our FAQ X