KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.eclipse.jdt.core.compiler.*;
14 import org.eclipse.jdt.internal.compiler.ast.ASTNode;
15 import org.eclipse.jdt.internal.compiler.ast.Block;
16 import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
17 import org.eclipse.jdt.internal.compiler.ast.Initializer;
18 import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration;
19 import org.eclipse.jdt.internal.compiler.ast.Statement;
20 import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
21 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
22 import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
23
24 public class RecoveredInitializer extends RecoveredField implements TerminalTokens {
25
26     public RecoveredType[] localTypes;
27     public int localTypeCount;
28
29     public RecoveredBlock initializerBody;
30
31 public RecoveredInitializer(FieldDeclaration fieldDeclaration, RecoveredElement parent, int bracketBalance){
32     this(fieldDeclaration, parent, bracketBalance, null);
33 }
34 public RecoveredInitializer(FieldDeclaration fieldDeclaration, RecoveredElement parent, int bracketBalance, Parser parser){
35     super(fieldDeclaration, parent, bracketBalance, parser);
36     this.foundOpeningBrace = true;
37 }
38 /*
39  * Record a nested block declaration
40  */

41 public RecoveredElement add(Block nestedBlockDeclaration, int bracketBalanceValue) {
42
43     /* default behavior is to delegate recording to parent if any,
44     do not consider elements passed the known end (if set)
45     it must be belonging to an enclosing element
46     */

47     if (fieldDeclaration.declarationSourceEnd > 0
48             && nestedBlockDeclaration.sourceStart > fieldDeclaration.declarationSourceEnd){
49         if (this.parent == null) return this; // ignore
50
return this.parent.add(nestedBlockDeclaration, bracketBalanceValue);
51     }
52     /* consider that if the opening brace was not found, it is there */
53     if (!foundOpeningBrace){
54         foundOpeningBrace = true;
55         this.bracketBalance++;
56     }
57     initializerBody = new RecoveredBlock(nestedBlockDeclaration, this, bracketBalanceValue);
58     if (nestedBlockDeclaration.sourceEnd == 0) return initializerBody;
59     return this;
60 }
61 /*
62  * Record a field declaration (act like inside method body)
63  */

64 public RecoveredElement add(FieldDeclaration newFieldDeclaration, int bracketBalanceValue) {
65
66     /* local variables inside initializer can only be final and non void */
67     char[][] fieldTypeName;
68     if ((newFieldDeclaration.modifiers & ~ClassFileConstants.AccFinal) != 0 /* local var can only be final */
69             || (newFieldDeclaration.type == null) // initializer
70
|| ((fieldTypeName = newFieldDeclaration.type.getTypeName()).length == 1 // non void
71
&& CharOperation.equals(fieldTypeName[0], TypeBinding.VOID.sourceName()))){
72         if (this.parent == null) return this; // ignore
73
this.updateSourceEndIfNecessary(this.previousAvailableLineEnd(newFieldDeclaration.declarationSourceStart - 1));
74         return this.parent.add(newFieldDeclaration, bracketBalanceValue);
75     }
76
77     /* default behavior is to delegate recording to parent if any,
78     do not consider elements passed the known end (if set)
79     it must be belonging to an enclosing element
80     */

81     if (this.fieldDeclaration.declarationSourceEnd > 0
82             && newFieldDeclaration.declarationSourceStart > this.fieldDeclaration.declarationSourceEnd){
83         if (this.parent == null) return this; // ignore
84
return this.parent.add(newFieldDeclaration, bracketBalanceValue);
85     }
86     // still inside initializer, treat as local variable
87
return this; // ignore
88
}
89 /*
90  * Record a local declaration - regular method should have been created a block body
91  */

92 public RecoveredElement add(LocalDeclaration localDeclaration, int bracketBalanceValue) {
93
94     /* do not consider a type starting passed the type end (if set)
95         it must be belonging to an enclosing type */

96     if (fieldDeclaration.declarationSourceEnd != 0
97             && localDeclaration.declarationSourceStart > fieldDeclaration.declarationSourceEnd){
98         if (parent == null) return this; // ignore
99
return this.parent.add(localDeclaration, bracketBalanceValue);
100     }
101     /* method body should have been created */
102     Block block = new Block(0);
103     block.sourceStart = ((Initializer)fieldDeclaration).sourceStart;
104     RecoveredElement element = this.add(block, 1);
105     return element.add(localDeclaration, bracketBalanceValue);
106 }
107 /*
108  * Record a statement - regular method should have been created a block body
109  */

110 public RecoveredElement add(Statement statement, int bracketBalanceValue) {
111
112     /* do not consider a statement starting passed the initializer end (if set)
113         it must be belonging to an enclosing type */

114     if (fieldDeclaration.declarationSourceEnd != 0
115             && statement.sourceStart > fieldDeclaration.declarationSourceEnd){
116         if (parent == null) return this; // ignore
117
return this.parent.add(statement, bracketBalanceValue);
118     }
119     /* initializer body should have been created */
120     Block block = new Block(0);
121     block.sourceStart = ((Initializer)fieldDeclaration).sourceStart;
122     RecoveredElement element = this.add(block, 1);
123     return element.add(statement, bracketBalanceValue);
124 }
125 public RecoveredElement add(TypeDeclaration typeDeclaration, int bracketBalanceValue) {
126
127     /* do not consider a type starting passed the type end (if set)
128         it must be belonging to an enclosing type */

129     if (fieldDeclaration.declarationSourceEnd != 0
130             && typeDeclaration.declarationSourceStart > fieldDeclaration.declarationSourceEnd){
131         if (parent == null) return this; // ignore
132
return this.parent.add(typeDeclaration, bracketBalanceValue);
133     }
134     if ((typeDeclaration.bits & ASTNode.IsLocalType) != 0){
135         /* method body should have been created */
136         Block block = new Block(0);
137         block.sourceStart = ((Initializer)fieldDeclaration).sourceStart;
138         RecoveredElement element = this.add(block, 1);
139         return element.add(typeDeclaration, bracketBalanceValue);
140     }
141     if (localTypes == null) {
142         localTypes = new RecoveredType[5];
143         localTypeCount = 0;
144     } else {
145         if (localTypeCount == localTypes.length) {
146             System.arraycopy(
147                 localTypes,
148                 0,
149                 (localTypes = new RecoveredType[2 * localTypeCount]),
150                 0,
151                 localTypeCount);
152         }
153     }
154     RecoveredType element = new RecoveredType(typeDeclaration, this, bracketBalanceValue);
155     localTypes[localTypeCount++] = element;
156     
157     /* consider that if the opening brace was not found, it is there */
158     if (!foundOpeningBrace){
159         foundOpeningBrace = true;
160         this.bracketBalance++;
161     }
162     return element;
163 }
164 public String JavaDoc toString(int tab) {
165     StringBuffer JavaDoc result = new StringBuffer JavaDoc(tabString(tab));
166     result.append("Recovered initializer:\n"); //$NON-NLS-1$
167
this.fieldDeclaration.print(tab + 1, result);
168     if (this.initializerBody != null) {
169         result.append("\n"); //$NON-NLS-1$
170
result.append(this.initializerBody.toString(tab + 1));
171     }
172     return result.toString();
173 }
174 public FieldDeclaration updatedFieldDeclaration(){
175
176     if (initializerBody != null){
177         Block block = initializerBody.updatedBlock();
178         if (block != null){
179             ((Initializer)fieldDeclaration).block = block;
180         }
181         if (this.localTypeCount > 0) fieldDeclaration.bits |= ASTNode.HasLocalType;
182
183     }
184     if (fieldDeclaration.sourceEnd == 0){
185         fieldDeclaration.sourceEnd = fieldDeclaration.declarationSourceEnd;
186     }
187     return fieldDeclaration;
188 }
189 /*
190  * A closing brace got consumed, might have closed the current element,
191  * in which case both the currentElement is exited
192  */

193 public RecoveredElement updateOnClosingBrace(int braceStart, int braceEnd){
194     if ((--bracketBalance <= 0) && (parent != null)){
195         this.updateSourceEndIfNecessary(braceStart, braceEnd);
196         return parent;
197     }
198     return this;
199 }
200 /*
201  * An opening brace got consumed, might be the expected opening one of the current element,
202  * in which case the bodyStart is updated.
203  */

204 public RecoveredElement updateOnOpeningBrace(int braceStart, int braceEnd){
205     bracketBalance++;
206     return this; // request to restart
207
}
208 /*
209  * Update the declarationSourceEnd of the corresponding parse node
210  */

211 public void updateSourceEndIfNecessary(int braceStart, int braceEnd){
212     if (this.fieldDeclaration.declarationSourceEnd == 0) {
213         Initializer initializer = (Initializer)fieldDeclaration;
214         if(parser().rBraceSuccessorStart >= braceEnd) {
215             if (initializer.bodyStart < parser().rBraceEnd) {
216                 initializer.declarationSourceEnd = parser().rBraceEnd;
217             } else {
218                 initializer.declarationSourceEnd = initializer.bodyStart;
219             }
220             if (initializer.bodyStart < parser().rBraceStart) {
221                 initializer.bodyEnd = parser().rBraceStart;
222             } else {
223                 initializer.bodyEnd = initializer.bodyStart;
224             }
225         } else {
226             initializer.declarationSourceEnd = braceEnd;
227             initializer.bodyEnd = braceStart - 1;
228         }
229         if(initializer.block != null) {
230             initializer.block.sourceEnd = initializer.declarationSourceEnd;
231         }
232     }
233 }
234 }
235
Popular Tags