KickJava   Java API By Example, From Geeks To Geeks.

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


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.flow.*;
15 import org.eclipse.jdt.internal.compiler.lookup.*;
16
17 public class BreakStatement extends BranchStatement {
18     
19 public BreakStatement(char[] label, int sourceStart, int e) {
20     super(label, sourceStart, e);
21 }
22
23 public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {
24
25     // here requires to generate a sequence of finally blocks invocations depending corresponding
26
// to each of the traversed try statements, so that execution will terminate properly.
27

28     // lookup the label, this should answer the returnContext
29
FlowContext targetContext = (this.label == null)
30         ? flowContext.getTargetContextForDefaultBreak()
31         : flowContext.getTargetContextForBreakLabel(this.label);
32
33     if (targetContext == null) {
34         if (this.label == null) {
35             currentScope.problemReporter().invalidBreak(this);
36         } else {
37             currentScope.problemReporter().undefinedLabel(this);
38         }
39         return flowInfo; // pretend it did not break since no actual target
40
}
41     
42     this.initStateIndex =
43         currentScope.methodScope().recordInitializationStates(flowInfo);
44     
45     this.targetLabel = targetContext.breakLabel();
46     FlowContext traversedContext = flowContext;
47     int subCount = 0;
48     this.subroutines = new SubRoutineStatement[5];
49     
50     do {
51         SubRoutineStatement sub;
52         if ((sub = traversedContext.subroutine()) != null) {
53             if (subCount == this.subroutines.length) {
54                 System.arraycopy(this.subroutines, 0, (this.subroutines = new SubRoutineStatement[subCount*2]), 0, subCount); // grow
55
}
56             this.subroutines[subCount++] = sub;
57             if (sub.isSubRoutineEscaping()) {
58                 break;
59             }
60         }
61         traversedContext.recordReturnFrom(flowInfo.unconditionalInits());
62         traversedContext.recordBreakTo(targetContext);
63         
64         if (traversedContext instanceof InsideSubRoutineFlowContext) {
65             ASTNode node = traversedContext.associatedNode;
66             if (node instanceof TryStatement) {
67                 TryStatement tryStatement = (TryStatement) node;
68                 flowInfo.addInitializationsFrom(tryStatement.subRoutineInits); // collect inits
69
}
70         } else if (traversedContext == targetContext) {
71             // only record break info once accumulated through subroutines, and only against target context
72
targetContext.recordBreakFrom(flowInfo);
73             break;
74         }
75     } while ((traversedContext = traversedContext.parent) != null);
76     
77     // resize subroutines
78
if (subCount != this.subroutines.length) {
79         System.arraycopy(this.subroutines, 0, (this.subroutines = new SubRoutineStatement[subCount]), 0, subCount);
80     }
81     return FlowInfo.DEAD_END;
82 }
83
84 public StringBuffer JavaDoc printStatement(int tab, StringBuffer JavaDoc output) {
85     printIndent(tab, output).append("break "); //$NON-NLS-1$
86
if (this.label != null) output.append(this.label);
87     return output.append(';');
88 }
89
90 public void traverse(ASTVisitor visitor, BlockScope blockscope) {
91     visitor.visit(this, blockscope);
92     visitor.endVisit(this, blockscope);
93 }
94 }
95
Popular Tags