KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > parser > RecoveredLocalVariable


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.parser;
12
13 /**
14  * Internal local variable structure for parsing recovery
15  */

16 import org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference;
17 import org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference;
18 import org.eclipse.jdt.internal.compiler.ast.ASTNode;
19 import org.eclipse.jdt.internal.compiler.ast.Expression;
20 import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration;
21 import org.eclipse.jdt.internal.compiler.ast.Statement;
22
23 public class RecoveredLocalVariable extends RecoveredStatement {
24
25     public LocalDeclaration localDeclaration;
26     boolean alreadyCompletedLocalInitialization;
27 public RecoveredLocalVariable(LocalDeclaration localDeclaration, RecoveredElement parent, int bracketBalance){
28     super(localDeclaration, parent, bracketBalance);
29     this.localDeclaration = localDeclaration;
30     this.alreadyCompletedLocalInitialization = localDeclaration.initialization != null;
31 }
32 /*
33  * Record an expression statement if local variable is expecting an initialization expression.
34  */

35 public RecoveredElement add(Statement stmt, int bracketBalanceValue) {
36
37     if (this.alreadyCompletedLocalInitialization || !(stmt instanceof Expression)) {
38         return super.add(stmt, bracketBalanceValue);
39     } else {
40         this.alreadyCompletedLocalInitialization = true;
41         this.localDeclaration.initialization = (Expression)stmt;
42         this.localDeclaration.declarationSourceEnd = stmt.sourceEnd;
43         this.localDeclaration.declarationEnd = stmt.sourceEnd;
44         return this;
45     }
46 }
47 /*
48  * Answer the associated parsed structure
49  */

50 public ASTNode parseTree(){
51     return localDeclaration;
52 }
53 /*
54  * Answer the very source end of the corresponding parse node
55  */

56 public int sourceEnd(){
57     return this.localDeclaration.declarationSourceEnd;
58 }
59 public String JavaDoc toString(int tab) {
60     return tabString(tab) + "Recovered local variable:\n" + localDeclaration.print(tab + 1, new StringBuffer JavaDoc(10)); //$NON-NLS-1$
61
}
62 public Statement updatedStatement(){
63     return localDeclaration;
64 }
65 /*
66  * A closing brace got consumed, might have closed the current element,
67  * in which case both the currentElement is exited.
68  *
69  * Fields have no associated braces, thus if matches, then update parent.
70  */

71 public RecoveredElement updateOnClosingBrace(int braceStart, int braceEnd){
72     if (bracketBalance > 0){ // was an array initializer
73
bracketBalance--;
74         if (bracketBalance == 0) alreadyCompletedLocalInitialization = true;
75         return this;
76     }
77     if (parent != null){
78         return parent.updateOnClosingBrace(braceStart, braceEnd);
79     }
80     return this;
81 }
82 /*
83  * An opening brace got consumed, might be the expected opening one of the current element,
84  * in which case the bodyStart is updated.
85  */

86 public RecoveredElement updateOnOpeningBrace(int braceStart, int braceEnd){
87     if (localDeclaration.declarationSourceEnd == 0
88         && (localDeclaration.type instanceof ArrayTypeReference || localDeclaration.type instanceof ArrayQualifiedTypeReference)
89         && !alreadyCompletedLocalInitialization){
90         bracketBalance++;
91         return null; // no update is necessary (array initializer)
92
}
93     // might be an array initializer
94
this.updateSourceEndIfNecessary(braceStart - 1, braceEnd - 1);
95     return this.parent.updateOnOpeningBrace(braceStart, braceEnd);
96 }
97 public void updateParseTree(){
98     this.updatedStatement();
99 }
100 /*
101  * Update the declarationSourceEnd of the corresponding parse node
102  */

103 public void updateSourceEndIfNecessary(int bodyStart, int bodyEnd){
104     if (this.localDeclaration.declarationSourceEnd == 0) {
105         this.localDeclaration.declarationSourceEnd = bodyEnd;
106         this.localDeclaration.declarationEnd = bodyEnd;
107     }
108 }
109 }
110
Popular Tags