1 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 41 public RecoveredElement add(Block nestedBlockDeclaration, int bracketBalanceValue) { 42 43 47 if (fieldDeclaration.declarationSourceEnd > 0 48 && nestedBlockDeclaration.sourceStart > fieldDeclaration.declarationSourceEnd){ 49 if (this.parent == null) return this; return this.parent.add(nestedBlockDeclaration, bracketBalanceValue); 51 } 52 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 64 public RecoveredElement add(FieldDeclaration newFieldDeclaration, int bracketBalanceValue) { 65 66 67 char[][] fieldTypeName; 68 if ((newFieldDeclaration.modifiers & ~ClassFileConstants.AccFinal) != 0 69 || (newFieldDeclaration.type == null) || ((fieldTypeName = newFieldDeclaration.type.getTypeName()).length == 1 && CharOperation.equals(fieldTypeName[0], TypeBinding.VOID.sourceName()))){ 72 if (this.parent == null) return this; this.updateSourceEndIfNecessary(this.previousAvailableLineEnd(newFieldDeclaration.declarationSourceStart - 1)); 74 return this.parent.add(newFieldDeclaration, bracketBalanceValue); 75 } 76 77 81 if (this.fieldDeclaration.declarationSourceEnd > 0 82 && newFieldDeclaration.declarationSourceStart > this.fieldDeclaration.declarationSourceEnd){ 83 if (this.parent == null) return this; return this.parent.add(newFieldDeclaration, bracketBalanceValue); 85 } 86 return this; } 89 92 public RecoveredElement add(LocalDeclaration localDeclaration, int bracketBalanceValue) { 93 94 96 if (fieldDeclaration.declarationSourceEnd != 0 97 && localDeclaration.declarationSourceStart > fieldDeclaration.declarationSourceEnd){ 98 if (parent == null) return this; return this.parent.add(localDeclaration, bracketBalanceValue); 100 } 101 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 110 public RecoveredElement add(Statement statement, int bracketBalanceValue) { 111 112 114 if (fieldDeclaration.declarationSourceEnd != 0 115 && statement.sourceStart > fieldDeclaration.declarationSourceEnd){ 116 if (parent == null) return this; return this.parent.add(statement, bracketBalanceValue); 118 } 119 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 129 if (fieldDeclaration.declarationSourceEnd != 0 130 && typeDeclaration.declarationSourceStart > fieldDeclaration.declarationSourceEnd){ 131 if (parent == null) return this; return this.parent.add(typeDeclaration, bracketBalanceValue); 133 } 134 if ((typeDeclaration.bits & ASTNode.IsLocalType) != 0){ 135 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 158 if (!foundOpeningBrace){ 159 foundOpeningBrace = true; 160 this.bracketBalance++; 161 } 162 return element; 163 } 164 public String toString(int tab) { 165 StringBuffer result = new StringBuffer (tabString(tab)); 166 result.append("Recovered initializer:\n"); this.fieldDeclaration.print(tab + 1, result); 168 if (this.initializerBody != null) { 169 result.append("\n"); 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 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 204 public RecoveredElement updateOnOpeningBrace(int braceStart, int braceEnd){ 205 bracketBalance++; 206 return this; } 208 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 |