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.parser;
14 * Internal local variable structure for parsing recovery
16 import org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference;
17 import org.eclipse.jdt.internal.compiler.ast.ASTNode;
18 import org.eclipse.jdt.internal.compiler.ast.Expression;
19 import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration;
20 import org.eclipse.jdt.internal.compiler.ast.Statement;
22 public class RecoveredLocalVariable extends RecoveredStatement {
24 public LocalDeclaration localDeclaration;
25 boolean alreadyCompletedLocalInitialization;
26 public RecoveredLocalVariable(LocalDeclaration localDeclaration, RecoveredElement parent, int bracketBalance){
27 super(localDeclaration, parent, bracketBalance);
28 this.localDeclaration = localDeclaration;
29 this.alreadyCompletedLocalInitialization = localDeclaration.initialization != null;
32 * Record an expression statement if local variable is expecting an initialization expression.
34 public RecoveredElement add(Statement stmt, int bracketBalanceValue) {
36 if (this.alreadyCompletedLocalInitialization || !(stmt instanceof Expression)) {
37 return super.add(stmt, bracketBalanceValue);
39 this.alreadyCompletedLocalInitialization = true;
40 this.localDeclaration.initialization = (Expression)stmt;
41 this.localDeclaration.declarationSourceEnd = stmt.sourceEnd;
42 this.localDeclaration.declarationEnd = stmt.sourceEnd;
47 * Answer the associated parsed structure
49 public ASTNode parseTree(){
50 return localDeclaration;
53 * Answer the very source end of the corresponding parse node
55 public int sourceEnd(){
56 return this.localDeclaration.declarationSourceEnd;
58 public String toString(int tab) {
59 return tabString(tab) + "Recovered local variable:\n" + localDeclaration.print(tab + 1, new StringBuffer(10)); //$NON-NLS-1$
61 public Statement updatedStatement(){
62 return localDeclaration;
65 * A closing brace got consumed, might have closed the current element,
66 * in which case both the currentElement is exited.
68 * Fields have no associated braces, thus if matches, then update parent.
70 public RecoveredElement updateOnClosingBrace(int braceStart, int braceEnd){
71 if (bracketBalance > 0){ // was an array initializer
73 if (bracketBalance == 0) alreadyCompletedLocalInitialization = true;
77 return parent.updateOnClosingBrace(braceStart, braceEnd);
82 * An opening brace got consumed, might be the expected opening one of the current element,
83 * in which case the bodyStart is updated.
85 public RecoveredElement updateOnOpeningBrace(int braceStart, int braceEnd){
86 if (localDeclaration.declarationSourceEnd == 0
87 && localDeclaration.type instanceof ArrayTypeReference
88 && !alreadyCompletedLocalInitialization){
90 return null; // no update is necessary (array initializer)
92 // might be an array initializer
93 this.updateSourceEndIfNecessary(braceStart - 1, braceEnd - 1);
94 return this.parent.updateOnOpeningBrace(braceStart, braceEnd);
96 public void updateParseTree(){
97 this.updatedStatement();
100 * Update the declarationSourceEnd of the corresponding parse node
102 public void updateSourceEndIfNecessary(int bodyStart, int bodyEnd){
103 if (this.localDeclaration.declarationSourceEnd == 0) {
104 this.localDeclaration.declarationSourceEnd = bodyEnd;
105 this.localDeclaration.declarationEnd = bodyEnd;