KickJava   Java API By Example, From Geeks To Geeks.

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


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 field structure for parsing recovery
15  */

16 import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
17 import org.eclipse.jdt.internal.compiler.ast.ASTNode;
18 import org.eclipse.jdt.internal.compiler.ast.Block;
19 import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
20 import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
21 import org.eclipse.jdt.internal.compiler.ast.ImportReference;
22 import org.eclipse.jdt.internal.compiler.ast.Initializer;
23 import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
24
25 public class RecoveredUnit extends RecoveredElement {
26
27     public CompilationUnitDeclaration unitDeclaration;
28     
29     public RecoveredImport[] imports;
30     public int importCount;
31     public RecoveredType[] types;
32     public int typeCount;
33 public RecoveredUnit(CompilationUnitDeclaration unitDeclaration, int bracketBalance, Parser parser){
34     super(null, bracketBalance, parser);
35     this.unitDeclaration = unitDeclaration;
36 }
37 /*
38  * Record a method declaration: should be attached to last type
39  */

40 public RecoveredElement add(AbstractMethodDeclaration methodDeclaration, int bracketBalanceValue) {
41
42     /* attach it to last type - if any */
43     if (this.typeCount > 0){
44         RecoveredType type = this.types[this.typeCount -1];
45         int start = type.bodyEnd;
46         int end = type.typeDeclaration.bodyEnd;
47         type.bodyEnd = 0; // reset position
48
type.typeDeclaration.declarationSourceEnd = 0; // reset position
49
type.typeDeclaration.bodyEnd = 0;
50         
51         int kind = TypeDeclaration.kind(type.typeDeclaration.modifiers);
52         if(start > 0 &&
53                 start < end &&
54                 kind != TypeDeclaration.INTERFACE_DECL &&
55                 kind != TypeDeclaration.ANNOTATION_TYPE_DECL) {
56             // the } of the last type can be considered as the end of an initializer
57
Initializer initializer = new Initializer(new Block(0), 0);
58             initializer.bodyStart = end;
59             initializer.bodyEnd = end;
60             initializer.declarationSourceStart = end;
61             initializer.declarationSourceEnd = end;
62             initializer.sourceStart = end;
63             initializer.sourceEnd = end;
64             type.add(initializer, bracketBalanceValue);
65         }
66         
67         return type.add(methodDeclaration, bracketBalanceValue);
68     }
69     return this; // ignore
70
}
71 /*
72  * Record a field declaration: should be attached to last type
73  */

74 public RecoveredElement add(FieldDeclaration fieldDeclaration, int bracketBalanceValue) {
75
76     /* attach it to last type - if any */
77     if (this.typeCount > 0){
78         RecoveredType type = this.types[this.typeCount -1];
79         type.bodyEnd = 0; // reset position
80
type.typeDeclaration.declarationSourceEnd = 0; // reset position
81
type.typeDeclaration.bodyEnd = 0;
82         return type.add(fieldDeclaration, bracketBalanceValue);
83     }
84     return this; // ignore
85
}
86 public RecoveredElement add(ImportReference importReference, int bracketBalanceValue) {
87     if (this.imports == null) {
88         this.imports = new RecoveredImport[5];
89         this.importCount = 0;
90     } else {
91         if (this.importCount == this.imports.length) {
92             System.arraycopy(
93                 this.imports,
94                 0,
95                 (this.imports = new RecoveredImport[2 * this.importCount]),
96                 0,
97                 this.importCount);
98         }
99     }
100     RecoveredImport element = new RecoveredImport(importReference, this, bracketBalanceValue);
101     this.imports[this.importCount++] = element;
102
103     /* if import not finished, then import becomes current */
104     if (importReference.declarationSourceEnd == 0) return element;
105     return this;
106 }
107 public RecoveredElement add(TypeDeclaration typeDeclaration, int bracketBalanceValue) {
108     
109     if ((typeDeclaration.bits & ASTNode.IsAnonymousType) != 0){
110         if (this.typeCount > 0) {
111             // add it to the last type
112
RecoveredType lastType = this.types[this.typeCount-1];
113             lastType.bodyEnd = 0; // reopen type
114
lastType.typeDeclaration.bodyEnd = 0; // reopen type
115
lastType.typeDeclaration.declarationSourceEnd = 0; // reopen type
116
lastType.bracketBalance++; // expect one closing brace
117
return lastType.add(typeDeclaration, bracketBalanceValue);
118         }
119     }
120     if (this.types == null) {
121         this.types = new RecoveredType[5];
122         this.typeCount = 0;
123     } else {
124         if (this.typeCount == this.types.length) {
125             System.arraycopy(
126                 this.types,
127                 0,
128                 (this.types = new RecoveredType[2 * this.typeCount]),
129                 0,
130                 this.typeCount);
131         }
132     }
133     RecoveredType element = new RecoveredType(typeDeclaration, this, bracketBalanceValue);
134     this.types[this.typeCount++] = element;
135
136     /* if type not finished, then type becomes current */
137     if (typeDeclaration.declarationSourceEnd == 0) return element;
138     return this;
139 }
140 /*
141  * Answer the associated parsed structure
142  */

143 public ASTNode parseTree(){
144     return this.unitDeclaration;
145 }
146 /*
147  * Answer the very source end of the corresponding parse node
148  */

149 public int sourceEnd(){
150     return this.unitDeclaration.sourceEnd;
151 }
152 public String JavaDoc toString(int tab) {
153     StringBuffer JavaDoc result = new StringBuffer JavaDoc(tabString(tab));
154     result.append("Recovered unit: [\n"); //$NON-NLS-1$
155
this.unitDeclaration.print(tab + 1, result);
156     result.append(tabString(tab + 1));
157     result.append("]"); //$NON-NLS-1$
158
if (this.imports != null) {
159         for (int i = 0; i < this.importCount; i++) {
160             result.append("\n"); //$NON-NLS-1$
161
result.append(this.imports[i].toString(tab + 1));
162         }
163     }
164     if (this.types != null) {
165         for (int i = 0; i < this.typeCount; i++) {
166             result.append("\n"); //$NON-NLS-1$
167
result.append(this.types[i].toString(tab + 1));
168         }
169     }
170     return result.toString();
171 }
172 public CompilationUnitDeclaration updatedCompilationUnitDeclaration(){
173
174     /* update imports */
175     if (this.importCount > 0){
176         ImportReference[] importRefences = new ImportReference[this.importCount];
177         for (int i = 0; i < this.importCount; i++){
178             importRefences[i] = this.imports[i].updatedImportReference();
179         }
180         this.unitDeclaration.imports = importRefences;
181     }
182     /* update types */
183     if (this.typeCount > 0){
184         int existingCount = this.unitDeclaration.types == null ? 0 : this.unitDeclaration.types.length;
185         TypeDeclaration[] typeDeclarations = new TypeDeclaration[existingCount + this.typeCount];
186         if (existingCount > 0){
187             System.arraycopy(this.unitDeclaration.types, 0, typeDeclarations, 0, existingCount);
188         }
189         // may need to update the declarationSourceEnd of the last type
190
if (this.types[this.typeCount - 1].typeDeclaration.declarationSourceEnd == 0){
191             this.types[this.typeCount - 1].typeDeclaration.declarationSourceEnd = this.unitDeclaration.sourceEnd;
192             this.types[this.typeCount - 1].typeDeclaration.bodyEnd = this.unitDeclaration.sourceEnd;
193         }
194         int actualCount = existingCount;
195         for (int i = 0; i < this.typeCount; i++){
196             TypeDeclaration typeDecl = this.types[i].updatedTypeDeclaration();
197             // filter out local types (12454)
198
if ((typeDecl.bits & ASTNode.IsLocalType) == 0){
199                 typeDeclarations[actualCount++] = typeDecl;
200             }
201         }
202         if (actualCount != this.typeCount){
203             System.arraycopy(
204                 typeDeclarations,
205                 0,
206                 typeDeclarations = new TypeDeclaration[existingCount+actualCount],
207                 0,
208                 existingCount+actualCount);
209         }
210         this.unitDeclaration.types = typeDeclarations;
211     }
212     return this.unitDeclaration;
213 }
214 public void updateParseTree(){
215     this.updatedCompilationUnitDeclaration();
216 }
217 /*
218  * Update the sourceEnd of the corresponding parse node
219  */

220 public void updateSourceEndIfNecessary(int bodyStart, int bodyEnd){
221     if (this.unitDeclaration.sourceEnd == 0)
222         this.unitDeclaration.sourceEnd = bodyEnd;
223 }
224 }
225
Popular Tags