1 11 package org.eclipse.jdt.internal.core.jdom; 12 13 import java.util.Enumeration ; 14 15 import org.eclipse.jdt.core.IJavaElement; 16 import org.eclipse.jdt.core.IType; 17 import org.eclipse.jdt.core.compiler.CharOperation; 18 import org.eclipse.jdt.core.jdom.*; 19 import org.eclipse.jdt.internal.core.util.Messages; 20 import org.eclipse.jdt.internal.core.util.CharArrayBuffer; 21 import org.eclipse.jdt.internal.core.util.Util; 22 31 class DOMField extends DOMMember implements IDOMField { 32 33 38 protected String fType; 39 40 44 protected int[] fTypeRange; 45 46 52 protected String fInitializer; 53 54 58 protected int[] fInitializerRange; 59 60 63 DOMField() { 64 } 66 110 DOMField(char[] document, int[] sourceRange, String name, int[] nameRange, int[] commentRange, int flags, int[] modifierRange, int[] typeRange, String type, boolean hasInitializer, int[] initRange, boolean isVariableDeclarator) { 111 super(document, sourceRange, name, nameRange, commentRange, flags, modifierRange); 112 113 fType= type; 114 fTypeRange= typeRange; 115 setHasInitializer(hasInitializer); 116 fInitializerRange= initRange; 117 setIsVariableDeclarator(isVariableDeclarator); 118 setMask(MASK_DETAILED_SOURCE_INDEXES, true); 119 120 } 121 143 DOMField(char[] document, int[] sourceRange, String name, int[] nameRange, int flags, String type, boolean isVariableDeclarator) { 144 this(document, sourceRange, name, nameRange, new int[] {-1, -1}, flags, new int[] {-1, -1}, new int[] {-1, -1}, type, false, new int[] {-1, -1}, isVariableDeclarator); 145 setMask(MASK_DETAILED_SOURCE_INDEXES, false); 146 } 147 155 protected void appendMemberBodyContents(CharArrayBuffer buffer) { 156 } 158 161 protected void appendMemberDeclarationContents(CharArrayBuffer buffer) { 162 163 if (isVariableDeclarator()) { 164 buffer.append(fDocument, fSourceRange[0], fNameRange[0] - fSourceRange[0]); 165 } else { 166 buffer 167 .append(getTypeContents()) 168 .append(fDocument, fTypeRange[1] + 1, fNameRange[0] - fTypeRange[1] - 1); 169 } 170 171 buffer.append(getNameContents()); 172 if (hasInitializer()) { 173 if (fInitializerRange[0] < 0) { 174 buffer 175 .append('=') 176 .append(fInitializer) 177 .append(fDocument, fNameRange[1] + 1, fSourceRange[1] - fNameRange[1]); 178 } else { 179 buffer 180 .append(fDocument, fNameRange[1] + 1, fInitializerRange[0] - fNameRange[1] - 1) 181 .append(getInitializer()) 182 .append(fDocument, fInitializerRange[1] + 1, fSourceRange[1] - fInitializerRange[1]); 183 } 184 } else { 185 if (fInitializerRange[0] < 0) { 186 buffer.append(fDocument, fNameRange[1] + 1, fSourceRange[1] - fNameRange[1]); 187 } else { 188 buffer.append(fDocument, fInitializerRange[1] + 1, fSourceRange[1] - fInitializerRange[1]); 189 } 190 } 191 192 } 193 201 protected void appendMemberHeaderFragment(CharArrayBuffer buffer) { 202 203 if (isVariableDeclarator()) { 204 return; 205 } else { 206 super.appendMemberHeaderFragment(buffer); 207 } 208 209 } 210 213 protected void appendSimpleContents(CharArrayBuffer buffer) { 214 buffer.append(fDocument, fSourceRange[0], fNameRange[0] - fSourceRange[0]); 216 buffer.append(fName); 218 buffer.append(fDocument, fNameRange[1] + 1, fSourceRange[1] - fNameRange[1]); 220 } 221 227 protected void becomeDetailed() throws DOMException { 228 if (!isDetailed()) { 229 if (isVariableDeclarator() || hasMultipleVariableDeclarators()) { 230 DOMNode first = getFirstFieldDeclaration(); 231 DOMNode last = getLastFieldDeclaration(); 232 DOMNode node= first; 233 String source= first.getContents(); 234 while (node != last) { 235 node= node.fNextNode; 236 source+=node.getContents(); 237 } 238 DOMBuilder builder = new DOMBuilder(); 239 IDOMField[] details= builder.createFields(source.toCharArray()); 240 if (details.length == 0) { 241 throw new DOMException(Messages.dom_cannotDetail); 242 } else { 243 node= this; 244 for (int i= 0; i < details.length; i++) { 245 node.shareContents((DOMNode)details[i]); 246 node= node.fNextNode; 247 } 248 } 249 } else { 250 super.becomeDetailed(); 251 } 252 253 } 254 } 255 258 public Object clone() { 259 if (isVariableDeclarator() || hasMultipleVariableDeclarators()) { 260 return getFactory().createField(new String (getSingleVariableDeclaratorContents())); 261 } else { 262 return super.clone(); 263 } 264 } 265 269 protected void expand() { 270 if (isVariableDeclarator() || hasMultipleVariableDeclarators()) { 271 Enumeration siblings= new SiblingEnumeration(getFirstFieldDeclaration()); 272 DOMField field= (DOMField)siblings.nextElement(); 273 DOMNode next= field.fNextNode; 274 while (siblings.hasMoreElements() && (next instanceof DOMField) && (((DOMField)next).isVariableDeclarator())) { 275 field.localizeContents(); 276 if (field.fParent != null) { 277 field.fParent.fragment(); 278 } 279 field= (DOMField)siblings.nextElement(); 280 next= field.fNextNode; 281 } 282 field.localizeContents(); 283 } 284 } 285 288 protected DOMNode getDetailedNode() { 289 if (isVariableDeclarator() || hasMultipleVariableDeclarators()) { 290 return (DOMNode)getFactory().createField(new String (getSingleVariableDeclaratorContents())); 291 } else { 292 return (DOMNode)getFactory().createField(getContents()); 293 } 294 } 295 299 protected DOMField getFirstFieldDeclaration() { 300 if (isVariableDeclarator()) { 301 return ((DOMField)fPreviousNode).getFirstFieldDeclaration(); 302 } else { 303 return this; 304 } 305 } 306 309 public String getInitializer() { 310 becomeDetailed(); 311 if (hasInitializer()) { 312 if (fInitializer != null) { 313 return fInitializer; 314 } else { 315 return new String (fDocument, fInitializerRange[0], fInitializerRange[1] + 1 - fInitializerRange[0]); 316 } 317 } else { 318 return null; 319 } 320 } 321 324 public IJavaElement getJavaElement(IJavaElement parent) throws IllegalArgumentException { 325 if (parent.getElementType() == IJavaElement.TYPE) { 326 return ((IType)parent).getField(getName()); 327 } else { 328 throw new IllegalArgumentException (Messages.element_illegalParent); 329 } 330 } 331 334 protected DOMField getLastFieldDeclaration() { 335 DOMField field = this; 336 while (field.isVariableDeclarator() || field.hasMultipleVariableDeclarators()) { 337 if (field.fNextNode instanceof DOMField && ((DOMField)field.fNextNode).isVariableDeclarator()) { 338 field= (DOMField)field.fNextNode; 339 } else { 340 break; 341 } 342 } 343 return field; 344 } 345 348 protected int getMemberDeclarationStartPosition() { 349 return fTypeRange[0]; 350 } 351 354 public int getNodeType() { 355 return IDOMNode.FIELD; 356 } 357 361 protected char[] getSingleVariableDeclaratorContents() { 362 363 364 CharArrayBuffer buffer= new CharArrayBuffer(); 365 DOMField first= getFirstFieldDeclaration(); 366 if (first.isDetailed()) { 367 first.appendMemberHeaderFragment(buffer); 368 buffer.append(getType()); 369 if (isVariableDeclarator()) { 370 buffer.append(' '); 371 } else { 372 buffer.append(fDocument, fTypeRange[1] + 1, fNameRange[0] - fTypeRange[1] - 1); 373 } 374 } else { 375 buffer.append(first.fDocument, first.fSourceRange[0], first.fNameRange[0] - first.fSourceRange[0]); 376 } 377 378 buffer.append(getName()); 379 if (hasInitializer()) { 380 if (fInitializerRange[0] < 0) { 381 buffer 382 .append('=') 383 .append(fInitializer) 384 .append(';') 385 .append(Util.getLineSeparator(buffer.toString(), null)); 386 } else { 387 buffer 388 .append(fDocument, fNameRange[1] + 1, fInitializerRange[0] - fNameRange[1] - 1) 389 .append(getInitializer()) 390 .append(';') 391 .append(Util.getLineSeparator(buffer.toString(), null)); 392 } 393 } else { 394 buffer.append(';').append(Util.getLineSeparator(buffer.toString(), null)); 395 } 396 return buffer.getContents(); 397 } 398 401 public String getType() { 402 return fType; 403 } 404 408 protected char[] getTypeContents() { 409 if (isTypeAltered()) { 410 return fType.toCharArray(); 411 } else { 412 return CharOperation.subarray(fDocument, fTypeRange[0], fTypeRange[1] + 1); 413 } 414 } 415 419 protected boolean hasInitializer() { 420 return getMask(MASK_FIELD_HAS_INITIALIZER); 421 } 422 426 protected boolean hasMultipleVariableDeclarators() { 427 return fNextNode != null && (fNextNode instanceof DOMField) && 428 ((DOMField)fNextNode).isVariableDeclarator(); 429 } 430 439 public void insertSibling(IDOMNode sibling) throws IllegalArgumentException , DOMException { 440 if (isVariableDeclarator()) { 441 expand(); 442 } 443 super.insertSibling(sibling); 444 } 445 449 protected boolean isTypeAltered() { 450 return getMask(MASK_FIELD_TYPE_ALTERED); 451 } 452 456 protected boolean isVariableDeclarator() { 457 return getMask(MASK_FIELD_IS_VARIABLE_DECLARATOR); 458 } 459 462 protected DOMNode newDOMNode() { 463 return new DOMField(); 464 } 465 468 void normalizeEndPosition(ILineStartFinder finder, DOMNode next) { 469 if (next == null) { 470 DOMNode parent = (DOMNode) getParent(); 473 if (parent == null || parent instanceof DOMCompilationUnit) { 474 setSourceRangeEnd(fDocument.length - 1); 475 } else { 476 int temp = ((DOMType)parent).getCloseBodyPosition() - 1; 478 setSourceRangeEnd(temp); 479 fInsertionPosition = Math.max(finder.getLineStart(temp + 1), getEndPosition()); 480 } 481 } else { 482 int temp = next.getStartPosition() - 1; 485 fInsertionPosition = Math.max(finder.getLineStart(temp + 1), getEndPosition()); 486 487 next.normalizeStartPosition(getEndPosition(), finder); 488 if (next instanceof DOMField) { 489 DOMField field = (DOMField) next; 490 if (field.isVariableDeclarator() && fTypeRange[0] == field.fTypeRange[0]) 491 return; 492 } 493 setSourceRangeEnd(next.getStartPosition() - 1); 494 } 495 } 496 499 void normalizeStartPosition(int endPosition, ILineStartFinder finder) { 500 if (isVariableDeclarator()) { 501 setStartPosition(fPreviousNode.getEndPosition() + 1); 503 } else { 504 super.normalizeStartPosition(endPosition, finder); 505 } 506 } 507 510 protected void offset(int offset) { 511 super.offset(offset); 512 offsetRange(fInitializerRange, offset); 513 offsetRange(fTypeRange, offset); 514 } 515 524 public void remove() { 525 expand(); 526 super.remove(); 527 } 528 531 public void setComment(String comment) { 532 expand(); 533 super.setComment(comment); 534 } 535 538 public void setFlags(int flags) { 539 expand(); 540 super.setFlags(flags); 541 } 542 546 protected void setHasInitializer(boolean hasInitializer) { 547 setMask(MASK_FIELD_HAS_INITIALIZER, hasInitializer); 548 } 549 552 public void setInitializer(String initializer) { 553 becomeDetailed(); 554 fragment(); 555 setHasInitializer(initializer != null); 556 fInitializer= initializer; 557 } 558 561 void setInitializerRange(int start, int end) { 562 fInitializerRange[0] = start; 563 fInitializerRange[1] = end; 564 } 565 570 protected void setIsVariableDeclarator(boolean isVariableDeclarator) { 571 setMask(MASK_FIELD_IS_VARIABLE_DECLARATOR, isVariableDeclarator); 572 } 573 576 public void setName(String name) throws IllegalArgumentException { 577 if (name == null) { 578 throw new IllegalArgumentException (Messages.element_nullName); 579 } else { 580 super.setName(name); 581 setTypeAltered(true); 582 } 583 } 584 587 public void setType(String typeName) throws IllegalArgumentException { 588 if (typeName == null) { 589 throw new IllegalArgumentException (Messages.element_nullType); 590 } 591 becomeDetailed(); 592 expand(); 593 fragment(); 594 setTypeAltered(true); 595 setNameAltered(true); 596 fType= typeName; 597 } 598 602 protected void setTypeAltered(boolean typeAltered) { 603 setMask(MASK_FIELD_TYPE_ALTERED, typeAltered); 604 } 605 608 protected void shareContents(DOMNode node) { 609 super.shareContents(node); 610 DOMField field= (DOMField)node; 611 fInitializer= field.fInitializer; 612 fInitializerRange= rangeCopy(field.fInitializerRange); 613 fType= field.fType; 614 fTypeRange= rangeCopy(field.fTypeRange); 615 } 616 619 public String toString() { 620 return "FIELD: " + getName(); } 622 } 623 | Popular Tags |