1 11 package org.eclipse.jdt.internal.core.jdom; 12 13 import java.util.ArrayList ; 14 import java.util.Map ; 15 16 import org.eclipse.jdt.core.JavaCore; 17 import org.eclipse.jdt.core.compiler.CategorizedProblem; 18 import org.eclipse.jdt.core.compiler.CharOperation; 19 import org.eclipse.jdt.core.jdom.*; 20 import org.eclipse.jdt.internal.compiler.DocumentElementParser; 21 import org.eclipse.jdt.internal.compiler.IDocumentElementRequestor; 22 import org.eclipse.jdt.internal.compiler.env.ICompilationUnit; 23 import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; 24 import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory; 25 35 public class DOMBuilder extends AbstractDOMBuilder implements IDocumentElementRequestor { 36 37 41 protected boolean fBuildingSingleMember= false; 42 43 47 protected boolean fFinishedSingleMember = false; 48 49 52 protected ArrayList fFields; 53 54 Map options = JavaCore.getOptions(); 55 56 59 public DOMBuilder() { 60 } 62 65 public void acceptImport(int declarationStart, int declarationEnd, int[] javaDocPositions, char[] name, 66 int nameStart, boolean onDemand, int modifiers) { 67 int[] sourceRange = {declarationStart, declarationEnd}; 68 int[] nameRange = {nameStart, declarationEnd - 1}; 69 70 71 String importName = new String (fDocument, nameRange[0], nameRange[1] + 1 - nameRange[0]); 72 73 fNode= new DOMImport(fDocument, sourceRange, importName, nameRange, onDemand, modifiers); 74 addChild(fNode); 75 if (fBuildingSingleMember) { 76 fFinishedSingleMember= true; 77 } 78 } 79 82 public void acceptInitializer(int declarationStart, int declarationEnd, int[] javaDocPositions, int modifiers, 83 int modifiersStart, int bodyStart, int bodyEnd) { 84 int[] sourceRange = {declarationStart, declarationEnd}; 85 int[] commentRange = {-1, -1}; 86 if (javaDocPositions != null) { 87 int length = javaDocPositions.length; 88 commentRange[0] = javaDocPositions[length - 2]; 89 commentRange[1] = javaDocPositions[length - 1]; 90 } 91 92 int[] modifiersRange = {-1, -1}; 93 if (modifiersStart >= declarationStart) { 94 modifiersRange[0] = modifiersStart; 95 modifiersRange[1] = bodyStart - 1; 96 } 97 fNode = new DOMInitializer(fDocument, sourceRange, commentRange, modifiers, 98 modifiersRange, bodyStart); 99 addChild(fNode); 100 if (fBuildingSingleMember) { 101 fFinishedSingleMember= true; 102 } 103 } 104 107 public void acceptPackage(int declarationStart, int declarationEnd, int[] javaDocPositions, char[] name, 108 int nameStartPosition) { 109 int[] sourceRange = {declarationStart, declarationEnd}; 110 int[] nameRange = {nameStartPosition, declarationEnd - 1}; 111 fNode= new DOMPackage(fDocument, sourceRange, CharOperation.charToString(name), nameRange); 112 addChild(fNode); 113 if (fBuildingSingleMember) { 114 fFinishedSingleMember= true; 115 } 116 } 117 124 public void acceptProblem(CategorizedProblem problem){ 125 if (fBuildingSingleMember && fFinishedSingleMember) { 126 return; 127 } 128 fAbort= true; 129 } 130 138 protected void addChild(IDOMNode child) { 139 super.addChild(child); 140 if (fStack.isEmpty() && fFields != null) { 141 fFields.add(child); 142 } 143 } 144 147 public IDOMCompilationUnit createCompilationUnit() { 148 return new DOMCompilationUnit(); 149 } 150 153 public IDOMCompilationUnit createCompilationUnit(ICompilationUnit compilationUnit) { 154 initializeBuild(compilationUnit.getContents(), true, true, false); 155 getParser(options).parseCompilationUnit(compilationUnit); 156 return super.createCompilationUnit(compilationUnit); 157 } 158 161 public IDOMField createField(char[] sourceCode) { 162 initializeBuild(sourceCode, false, false, true); 163 getParser(options).parseField(sourceCode); 164 if (fAbort || fNode == null) { 165 return null; 166 } 167 168 if (fFieldCount > 1) { 170 return null; 171 } 172 173 fNode.normalize(this); 174 return (IDOMField)fNode; 175 } 176 179 public IDOMField[] createFields(char[] sourceCode) { 180 initializeBuild(sourceCode, false, false, false); 181 fFields= new ArrayList (); 182 getParser(options).parseField(sourceCode); 183 if (fAbort) { 184 return null; 185 } 186 IDOMField[] fields= new IDOMField[fFields.size()]; 187 fFields.toArray(fields); 188 for (int i= 0; i < fields.length; i++) { 189 DOMNode node= (DOMNode)fields[i]; 190 if (i < (fields.length - 1)) { 191 DOMNode next= (DOMNode)fields[i + 1]; 192 node.fNextNode= next; 193 next.fPreviousNode= node; 194 } 195 ((DOMNode)fields[i]).normalize(this); 196 } 197 return fields; 198 } 199 202 public IDOMImport createImport() { 203 return new DOMImport(); 204 } 205 208 public IDOMImport createImport(char[] sourceCode) { 209 initializeBuild(sourceCode, false, false, true); 210 getParser(options).parseImport(sourceCode); 211 if (fAbort || fNode == null) { 212 return null; 213 } 214 fNode.normalize(this); 215 return (IDOMImport)fNode; 216 } 217 222 public IDOMInitializer createInitializer(char[] sourceCode) { 223 initializeBuild(sourceCode, false, false, true); 224 getParser(options).parseInitializer(sourceCode); 225 if (fAbort || fNode == null || !(fNode instanceof IDOMInitializer)) { 226 return null; 227 } 228 fNode.normalize(this); 229 return (IDOMInitializer)fNode; 230 } 231 234 public IDOMMethod createMethod(char[] sourceCode) { 235 initializeBuild(sourceCode, false, false, true); 236 getParser(options).parseMethod(sourceCode); 237 if (fAbort || fNode == null) { 238 return null; 239 } 240 fNode.normalize(this); 241 return (IDOMMethod)fNode; 242 } 243 246 public IDOMPackage createPackage() { 247 return new DOMPackage(); 248 } 249 252 public IDOMPackage createPackage(char[] sourceCode) { 253 initializeBuild(sourceCode, false, false, true); 254 getParser(options).parsePackage(sourceCode); 255 if (fAbort || fNode == null) { 256 return null; 257 } 258 fNode.normalize(this); 259 return (IDOMPackage)fNode; 260 } 261 264 public IDOMType createType(char[] sourceCode) { 265 initializeBuild(sourceCode, false, true, false); 266 getParser(options).parseType(sourceCode); 267 if (fAbort) { 268 return null; 269 } 270 if (fNode != null) fNode.normalize(this); 271 if (fNode instanceof IDOMType) { 272 return (IDOMType) fNode; 273 } 274 return null; 275 } 276 314 protected void enterAbstractMethod(int declarationStart, int[] javaDocPositions, int modifiers, int modifiersStart, 315 char[] returnType, int returnTypeStart, int returnTypeEnd, int returnTypeDimensionCount, 316 char[] name, int nameStart, int nameEnd, char[][] parameterTypes, int[] parameterTypeStarts, 317 int[] parameterTypeEnds, char[][] parameterNames, int[] parameterNameStarts, 318 int[] parameterNameEnds, int parametersEnd, int extendedReturnTypeDimensionCount, 319 int extendedReturnTypeDimensionEnd, char[][] exceptionTypes, int[] exceptionTypeStarts, 320 int[] exceptionTypeEnds, int bodyStart, boolean isConstructor) { 321 int[] sourceRange = {declarationStart, -1}; int[] nameRange = {nameStart, nameEnd}; 323 int[] commentRange = {-1, -1}; 324 if (javaDocPositions != null) { 325 int length = javaDocPositions.length; 326 commentRange[0] = javaDocPositions[length - 2]; commentRange[1] = javaDocPositions[length - 1]; 328 } 329 int[] modifiersRange = {-1, -1}; 330 if (modifiersStart > -1) { 331 modifiersRange[0] = modifiersStart; 332 if (isConstructor) { 333 modifiersRange[1] = nameStart - 1; 334 } else { 335 modifiersRange[1] = returnTypeStart - 1; 336 } 337 } 338 int[] returnTypeRange = null; 339 340 if (extendedReturnTypeDimensionCount > 0) 341 returnTypeRange = new int[] {returnTypeStart, returnTypeEnd, 342 parametersEnd + 1, extendedReturnTypeDimensionEnd}; 343 else 344 returnTypeRange = new int[] {returnTypeStart, returnTypeEnd}; 345 int[] parameterRange = {nameEnd + 1, parametersEnd}; 346 int[] exceptionRange = {-1, -1}; 347 if (exceptionTypes != null && exceptionTypes.length > 0) { 348 int exceptionCount = exceptionTypes.length; 349 exceptionRange[0] = exceptionTypeStarts[0]; 350 exceptionRange[1] = exceptionTypeEnds[exceptionCount - 1]; 351 } 352 int[] bodyRange = null; 353 if (exceptionRange[1] > -1) { 354 bodyRange = new int[] {exceptionRange[1] + 1, -1}; } else { 356 bodyRange = new int[] {parametersEnd + 1, -1}; 357 } 358 fNode = new DOMMethod(fDocument, sourceRange, CharOperation.charToString(name), nameRange, commentRange, modifiers, 359 modifiersRange, isConstructor, CharOperation.charToString(returnType), returnTypeRange, 360 CharOperation.charArrayToStringArray(parameterTypes), 361 CharOperation.charArrayToStringArray(parameterNames), 362 parameterRange, CharOperation.charArrayToStringArray(exceptionTypes), exceptionRange, bodyRange); 363 addChild(fNode); 364 fStack.push(fNode); 365 } 366 384 public void enterClass(int declarationStart, int[] javaDocPositions, int modifiers, int modifiersStart, int keywordStart, 385 char[] name, int nameStart, int nameEnd, char[] superclass, int superclassStart, 386 int superclassEnd, char[][] superinterfaces, int[] superinterfaceStarts, 387 int[] superinterfaceEnds, int bodyStart) { 388 389 enterType(declarationStart, javaDocPositions, modifiers, modifiersStart, keywordStart, 390 name, nameStart, nameEnd, superclass, superclassStart, 391 superclassEnd, superinterfaces, superinterfaceStarts, 392 superinterfaceEnds, bodyStart, true); 393 } 394 415 public void enterConstructor(int declarationStart, int[] javaDocPositions, int modifiers, int modifiersStart, 416 char[] name, int nameStart, int nameEnd, char[][] parameterTypes, 417 int[] parameterTypeStarts, int[] parameterTypeEnds, char[][] parameterNames, 418 int[] parameterNameStarts, int[] parameterNameEnds, int parametersEnd, 419 char[][] exceptionTypes, int[] exceptionTypeStarts, int[] exceptionTypeEnds, 420 int bodyStart) { 421 422 423 String nameString = new String (fDocument, nameStart, nameEnd - nameStart); 424 int openParenPosition = nameString.indexOf('('); 425 if (openParenPosition > -1) 426 nameEnd = nameStart + openParenPosition - 1; 427 428 enterAbstractMethod(declarationStart, javaDocPositions, modifiers, modifiersStart, 429 null, -1, -1, 0, 430 name, nameStart, nameEnd, parameterTypes, parameterTypeStarts, 431 parameterTypeEnds, parameterNames, parameterNameStarts, 432 parameterNameEnds, parametersEnd, 0, 433 -1, exceptionTypes, exceptionTypeStarts, 434 exceptionTypeEnds, bodyStart,true); 435 } 436 452 public void enterField(int declarationStart, int[] javaDocPositions, int modifiers, int modifiersStart, 453 char[] type, int typeStart, int typeEnd, int typeDimensionCount, char[] name, 454 int nameStart, int nameEnd, int extendedTypeDimensionCount, 455 int extendedTypeDimensionEnd) { 456 int[] sourceRange = {declarationStart, 457 (extendedTypeDimensionEnd > nameEnd) ? extendedTypeDimensionEnd : nameEnd}; 458 int[] nameRange = {nameStart, nameEnd}; 459 int[] commentRange = {-1, -1}; 460 if (javaDocPositions != null) { 461 int length = javaDocPositions.length; 462 commentRange[0] = javaDocPositions[length - 2]; commentRange[1] = javaDocPositions[length - 1]; 464 } 465 int[] modifiersRange = {-1, -1}; 466 if (modifiersStart > -1) { 467 modifiersRange[0] = modifiersStart; 468 modifiersRange[1] = typeStart - 1; 469 } 470 int[] typeRange = {typeStart, typeEnd}; 471 boolean hasInitializer = false; int[] initializerRange = {-1, -1}; boolean isVariableDeclarator = false; 474 if (fNode instanceof DOMField) { 475 DOMField field = (DOMField)fNode; 476 if (field.fTypeRange[0] == typeStart) 477 isVariableDeclarator = true; 478 } 479 fNode = new DOMField(fDocument, sourceRange, CharOperation.charToString(name), nameRange, commentRange, 480 modifiers, modifiersRange, typeRange, CharOperation.charToString(type), hasInitializer, 481 initializerRange, isVariableDeclarator); 482 addChild(fNode); 483 fStack.push(fNode); 484 } 485 500 public void enterInterface(int declarationStart, int[] javaDocPositions, int modifiers, int modifiersStart, int keywordStart, 501 char[] name, int nameStart, int nameEnd, char[][] superinterfaces, 502 int[] superinterfaceStarts, int[] superinterfaceEnds, int bodyStart) { 503 504 enterType(declarationStart, javaDocPositions, modifiers, modifiersStart, keywordStart, 505 name, nameStart, nameEnd, null, -1, -1, superinterfaces, 506 superinterfaceStarts, superinterfaceEnds, bodyStart, false); 507 } 508 535 public void enterMethod(int declarationStart, int[] javaDocPositions, int modifiers, int modifiersStart, 536 char[] returnType, int returnTypeStart, int returnTypeEnd, int returnTypeDimensionCount, 537 char[] name, int nameStart, int nameEnd, char[][] parameterTypes, int[] parameterTypeStarts, 538 int[] parameterTypeEnds, char[][] parameterNames, int[] parameterNameStarts, 539 int[] parameterNameEnds, int parametersEnd, int extendedReturnTypeDimensionCount, 540 int extendedReturnTypeDimensionEnd, char[][] exceptionTypes, int[] exceptionTypeStarts, 541 int[] exceptionTypeEnds, int bodyStart) { 542 enterAbstractMethod(declarationStart, javaDocPositions, modifiers, modifiersStart, 543 returnType, returnTypeStart, returnTypeEnd, returnTypeDimensionCount, 544 name, nameStart, nameEnd, parameterTypes, parameterTypeStarts, 545 parameterTypeEnds, parameterNames, parameterNameStarts, 546 parameterNameEnds, parametersEnd, extendedReturnTypeDimensionCount, 547 extendedReturnTypeDimensionEnd, exceptionTypes, exceptionTypeStarts, 548 exceptionTypeEnds, bodyStart,false); 549 } 550 551 protected void enterType(int declarationStart, int[] javaDocPositions, 552 int modifiers, int modifiersStart, int keywordStart, char[] name, 553 int nameStart, int nameEnd, char[] superclass, int superclassStart, 554 int superclassEnd, char[][] superinterfaces, int[] superinterfaceStarts, 555 int[] superinterfaceEnds, int bodyStart, boolean isClass) { 556 if (fBuildingType) { 557 int[] sourceRange = {declarationStart, -1}; int[] commentRange = {-1, -1}; 559 if (javaDocPositions != null) { 560 int length = javaDocPositions.length; 561 commentRange[0] = javaDocPositions[length - 2]; commentRange[1] = javaDocPositions[length - 1]; 563 } 564 int[] modifiersRange = {-1, -1}; 565 if (modifiersStart > -1) { 566 modifiersRange[0] = modifiersStart; 567 modifiersRange[1] = (modifiersStart > -1) ? keywordStart - 1 : -1; 568 } 569 int[] typeKeywordRange = {keywordStart, nameStart - 1}; 570 int[] nameRange = new int[] {nameStart, nameEnd}; 571 int[] extendsKeywordRange = {-1, -1}; 572 int[] superclassRange = {-1, -1}; 573 int[] implementsKeywordRange = {-1, -1}; 574 int[] interfacesRange = {-1, -1}; 575 if (isClass) { 576 if (superclass != null) { 577 extendsKeywordRange[0] = nameEnd + 1; 578 extendsKeywordRange[1] = superclassStart - 1; 579 superclassRange[0] = superclassStart; 580 superclassRange[1] = superclassEnd; 581 } 582 if (superinterfaces != null && superinterfaces.length > 0) { 583 superclassRange[1] = superclassEnd; 584 if (superclassEnd > -1) { 585 implementsKeywordRange[0] = superclassEnd + 1; 586 } else { 587 implementsKeywordRange[0] = nameEnd + 1; 588 } 589 implementsKeywordRange[1] = superinterfaceStarts[0] - 1; 590 interfacesRange[0] = superinterfaceStarts[0]; 591 interfacesRange[1] = superinterfaceEnds[superinterfaces.length - 1]; 592 } 593 } else { 594 if (superinterfaces != null && superinterfaces.length > 0) { 595 extendsKeywordRange[0] = nameEnd + 1; 596 extendsKeywordRange[1] = superinterfaceStarts[0] - 1; 597 interfacesRange[0] = superinterfaceStarts[0]; 598 interfacesRange[1] = superinterfaceEnds[superinterfaces.length - 1]; 599 } 600 } 601 int[] openBodyRange = {bodyStart, -1}; int[] closeBodyRange = {-1, -1}; fNode = new DOMType(fDocument, sourceRange, new String (name), nameRange, commentRange, 604 modifiers, modifiersRange, typeKeywordRange, superclassRange, extendsKeywordRange, 605 CharOperation.charArrayToStringArray(superinterfaces), interfacesRange, 606 implementsKeywordRange, openBodyRange, 607 closeBodyRange, isClass); 608 addChild(fNode); 609 fStack.push(fNode); 610 } 611 } 612 619 protected void exitAbstractMethod(int bodyEnd, int declarationEnd) { 620 DOMMethod method = (DOMMethod) fStack.pop(); 621 method.setSourceRangeEnd(declarationEnd); 622 method.setBodyRangeEnd(bodyEnd + 1); 623 fNode = method; 624 if (fBuildingSingleMember) { 625 fFinishedSingleMember= true; 626 } 627 } 628 634 public void exitClass(int bodyEnd, int declarationEnd) { 635 exitType(bodyEnd, declarationEnd); 636 } 637 643 public void exitConstructor(int bodyEnd, int declarationEnd) { 644 exitAbstractMethod(bodyEnd, declarationEnd); 645 } 646 652 public void exitField(int bodyEnd, int declarationEnd) { 653 DOMField field = (DOMField)fStack.pop(); 654 if (field.getEndPosition() < declarationEnd) { 655 field.setSourceRangeEnd(declarationEnd); 656 int nameEnd = field.fNameRange[1]; 657 if (nameEnd < bodyEnd) { 658 659 String initializer = new String (fDocument, nameEnd + 1, bodyEnd - nameEnd); 660 int index = initializer.indexOf('='); 661 if (index > -1) { 662 field.setHasInitializer(true); 663 field.setInitializerRange(nameEnd + index + 2, bodyEnd); 664 } 665 } 666 } 667 fFieldCount++; 668 fNode = field; 669 if (fBuildingSingleMember) { 670 fFinishedSingleMember= true; 671 } 672 } 673 679 public void exitInterface(int bodyEnd, int declarationEnd) { 680 exitType(bodyEnd, declarationEnd); 681 } 682 688 public void exitMethod(int bodyEnd, int declarationEnd) { 689 exitAbstractMethod(bodyEnd, declarationEnd); 690 } 691 694 protected DocumentElementParser getParser(Map settings) { 695 return new DocumentElementParser(this, new DefaultProblemFactory(), new CompilerOptions(settings)); 696 } 697 707 protected void initializeBuild(char[] sourceCode, boolean buildingCompilationUnit, boolean buildingType, boolean singleMember) { 708 super.initializeBuild(sourceCode, buildingCompilationUnit, buildingType); 709 fBuildingSingleMember= singleMember; 710 fFinishedSingleMember= false; 711 712 } 713 } 714 | Popular Tags |