1 11 package org.eclipse.jdt.internal.compiler.parser; 12 13 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 40 public RecoveredElement add(AbstractMethodDeclaration methodDeclaration, int bracketBalanceValue) { 41 42 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; type.typeDeclaration.declarationSourceEnd = 0; 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 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; } 71 74 public RecoveredElement add(FieldDeclaration fieldDeclaration, int bracketBalanceValue) { 75 76 77 if (this.typeCount > 0){ 78 RecoveredType type = this.types[this.typeCount -1]; 79 type.bodyEnd = 0; type.typeDeclaration.declarationSourceEnd = 0; type.typeDeclaration.bodyEnd = 0; 82 return type.add(fieldDeclaration, bracketBalanceValue); 83 } 84 return this; } 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 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 RecoveredType lastType = this.types[this.typeCount-1]; 113 lastType.bodyEnd = 0; lastType.typeDeclaration.bodyEnd = 0; lastType.typeDeclaration.declarationSourceEnd = 0; lastType.bracketBalance++; 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 137 if (typeDeclaration.declarationSourceEnd == 0) return element; 138 return this; 139 } 140 143 public ASTNode parseTree(){ 144 return this.unitDeclaration; 145 } 146 149 public int sourceEnd(){ 150 return this.unitDeclaration.sourceEnd; 151 } 152 public String toString(int tab) { 153 StringBuffer result = new StringBuffer (tabString(tab)); 154 result.append("Recovered unit: [\n"); this.unitDeclaration.print(tab + 1, result); 156 result.append(tabString(tab + 1)); 157 result.append("]"); if (this.imports != null) { 159 for (int i = 0; i < this.importCount; i++) { 160 result.append("\n"); 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"); result.append(this.types[i].toString(tab + 1)); 168 } 169 } 170 return result.toString(); 171 } 172 public CompilationUnitDeclaration updatedCompilationUnitDeclaration(){ 173 174 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 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 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 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 220 public void updateSourceEndIfNecessary(int bodyStart, int bodyEnd){ 221 if (this.unitDeclaration.sourceEnd == 0) 222 this.unitDeclaration.sourceEnd = bodyEnd; 223 } 224 } 225 | Popular Tags |