1 19 24 25 package org.netbeans.jmi.javamodel.regenerator; 26 27 import java.util.ArrayList ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import org.netbeans.jmi.javamodel.*; 31 import org.netbeans.modules.javacore.internalapi.JavaMetamodel; 32 import org.openide.filesystems.FileSystem; 33 34 35 39 public class Cloner { 40 41 private JavaModelPackage target; 42 private FileSystem targetFS; 43 private boolean useJavadoc = Boolean.getBoolean("org.netbeans.javacore.useJavadoc"); 44 private boolean useBody = Boolean.getBoolean("org.netbeans.javacore.useBody"); 45 private boolean useInitialValue = Boolean.getBoolean("org.netbeans.javacore.useInitialValue"); 46 47 48 public Cloner(FileSystem targetFS) { 49 System.out.println(useBody); 50 this.targetFS = targetFS; 51 target = JavaMetamodel.getManager().resolveJavaExtent(targetFS.getRoot()); 52 } 53 54 public Resource duplicate(Resource resource) { 55 Resource result = target.getResource().createResource( 56 resource.getName(), 57 resource.getTimestamp(), 58 duplicate(resource.getClassifiers()), 59 duplicate(resource.getImports()), 60 resource.getPackageName(), 61 null, 62 duplicate(resource.getPackageAnnotations()) 63 ); 64 return result; 65 } 66 67 public List duplicate(List list) { 68 if (list == null) 69 return null; 70 71 if (list.isEmpty()) 72 return new ArrayList (0); 73 74 Iterator it = list.iterator(); 75 List result = new ArrayList (); 76 77 while (it.hasNext()) { 78 result.add(duplicate(it.next())); 79 } 80 81 return result; 82 } 83 84 85 public Parameter duplicate(Parameter param) { 86 Parameter result; 87 result = target.getParameter().createParameter( 88 param.getName(), 89 duplicate(param.getAnnotations()), 90 param.isFinal(), 91 (TypeReference) duplicate(param.getTypeName()), 92 param.getDimCount(), 93 param.isVarArg() 94 ); 95 97 return result; 98 } 99 100 public Initializer duplicate(Initializer init) { 101 102 Initializer newinit; 103 104 newinit = target.getInitializer().createInitializer( 105 init.getName(), 106 duplicate(init.getAnnotations()), 107 init.getModifiers(), 108 useJavadoc?null:init.getJavadocText(), 109 useJavadoc?duplicate(init.getJavadoc()):null, 110 useBody?duplicate(init.getBody()):null, 111 useBody?null:init.getBodyText() 112 ); 113 114 return newinit; 115 } 116 117 118 public Import duplicate(Import imp) { 119 return target.getImport().createImport(imp.getName(), null, imp.isStatic(), imp.isOnDemand()); 120 } 121 122 public JavaClass duplicate(JavaClass clazz) { 123 if (clazz == null) 124 return null; 125 126 JavaClass newclazz; 127 128 newclazz = target.getJavaClass().createJavaClass( 129 clazz.getName(), 130 duplicate(clazz.getAnnotations()), 131 clazz.getModifiers(), 132 useJavadoc?null:clazz.getJavadocText(), 133 useJavadoc?duplicate(clazz.getJavadoc()):null, 134 duplicate(clazz.getFeatures()), 135 duplicate(clazz.getSuperClassName()), 136 duplicate(clazz.getInterfaceNames()), 137 duplicate(clazz.getTypeParameters()) 138 ); 139 140 143 return newclazz; 144 } 145 146 public Constructor duplicate(Constructor constructor) { 147 148 Constructor newcon; 149 150 newcon = target.getConstructor().createConstructor( 151 constructor.getName(), 152 duplicate(constructor.getAnnotations()), 153 constructor.getModifiers(), 154 useJavadoc?null:constructor.getJavadocText(), 155 useJavadoc?duplicate(constructor.getJavadoc()):null, 156 useBody?duplicate(constructor.getBody()):null, 157 useBody?null:constructor.getBodyText(), 158 duplicate(constructor.getTypeParameters()), 159 duplicate(constructor.getParameters()), 160 duplicate(constructor.getExceptionNames()) 161 ); 162 163 165 return newcon; 166 167 189 } 190 191 public Method duplicate(Method method) { 192 Method newmet; 193 194 newmet = target.getMethod().createMethod( 195 method.getName(), 196 duplicate(method.getAnnotations()), 197 method.getModifiers(), 198 useJavadoc?null:method.getJavadocText(), 199 useJavadoc?duplicate(method.getJavadoc()):null, 200 useBody?duplicate(method.getBody()):null, 201 useBody?null:method.getBodyText(), 202 duplicate(method.getTypeParameters()), 203 duplicate(method.getParameters()), 204 duplicate(method.getExceptionNames()), 205 (TypeReference) duplicate(method.getTypeName()), 206 method.getDimCount() 207 ); 208 209 212 return newmet; 213 214 215 238 } 239 240 public Field duplicate(Field field) { 241 Field newfield; 242 243 newfield = target.getField().createField( 244 field.getName(), 245 duplicate(field.getAnnotations()), 246 field.getModifiers(), 247 useJavadoc?null:field.getJavadocText(), 248 useJavadoc?duplicate(field.getJavadoc()):null, 249 field.isFinal(), 250 (TypeReference) duplicate(field.getTypeName()), 251 field.getDimCount(), 252 useInitialValue?(InitialValue)duplicate(field.getInitialValue()):null, 253 useInitialValue?null:field.getInitialValueText() 254 ); 255 256 newfield.setType(field.getType()); 257 258 return newfield; 259 260 281 } 282 283 public JavaDoc duplicate(JavaDoc javadoc) { 284 if (javadoc == null) 285 return null; 286 return target.getJavaDoc().createJavaDoc(javadoc.getText(), duplicate(javadoc.getTags())); 287 } 288 289 public TagValue duplicate(TagValue elem){ 290 TagValue val = target.getTagValue().createTagValue(elem.getValue()); 291 val.setDefinition(duplicate(elem.getDefinition())); 292 return val; 293 } 294 295 public TagDefinition duplicate(TagDefinition elem){ 296 if (elem == null) 297 return null; 298 return target.getTagDefinition().createTagDefinition(elem.getName()); 299 } 300 301 303 public StatementBlock duplicate (StatementBlock statement) { 304 return target.getStatementBlock().createStatementBlock(duplicate(statement.getStatements())); 305 } 306 307 public LabeledStatement duplicate(LabeledStatement elem) { 308 return target.getLabeledStatement().createLabeledStatement( 309 elem.getLabel(), 310 (Statement) duplicate(elem.getBody()) 311 ); 312 } 313 314 public WhileStatement duplicate(WhileStatement elem) { 315 return target.getWhileStatement().createWhileStatement( 316 (Expression)duplicate(elem.getExpression()), 317 (Statement) duplicate(elem.getBody()) 318 ); 319 } 320 321 public DoStatement duplicate(DoStatement elem) { 322 return target.getDoStatement().createDoStatement( 323 (Expression) duplicate(elem.getExpression()), 324 (Statement) duplicate(elem.getBody()) 325 ); 326 } 327 328 public IfStatement duplicate(IfStatement elem) { 329 return target.getIfStatement().createIfStatement( 330 (Expression) duplicate(elem.getExpression()), 331 (Statement) duplicate(elem.getThenPart()), 332 (Statement) duplicate(elem.getElsePart()) 333 ); 334 } 335 336 public AssertStatement duplicate(AssertStatement elem) { 337 return target.getAssertStatement().createAssertStatement( 338 (Expression) duplicate(elem.getExpression()), 339 (Expression) duplicate(elem.getDetail()) 340 ); 341 } 342 343 public TryStatement duplicate(TryStatement elem) { 344 return target.getTryStatement().createTryStatement( 345 duplicate(elem.getBody()), 346 duplicate(elem.getCatches()), 347 duplicate(elem.getFinalizer()) 348 ); 349 } 350 351 public ConstructorInvocation duplicate(ConstructorInvocation elem) { 352 return target.getConstructorInvocation().createConstructorInvocation( 353 null, 354 duplicate(elem.getParameters()), 355 elem.isHasSuper(), 356 (PrimaryExpression) duplicate(elem.getParentClass()) 357 ); 358 } 359 360 public BreakStatement duplicate(BreakStatement elem) { 361 return target.getBreakStatement().createBreakStatement(elem.getLabel()); 362 } 363 364 365 public ForStatement duplicate(ForStatement elem) { 366 return target.getForStatement().createForStatement( 367 (Expression) duplicate(elem.getExpression()), 368 duplicate(elem.getSteps()), 369 (Statement) duplicate(elem.getBody()), 370 duplicate(elem.getInit()) 371 ); 372 } 373 374 375 public LocalVariable duplicate(LocalVariable elem) { 376 LocalVariable var = target.getLocalVariable().createLocalVariable( 377 elem.getName(), 378 null, 380 elem.isFinal(), 381 null, 382 elem.getDimCount(), 383 useInitialValue?(InitialValue)duplicate(elem.getInitialValue()):null, 384 useInitialValue?null:elem.getInitialValueText() 385 ); 386 var.setType(elem.getType()); 387 return var; 388 } 389 390 public ContinueStatement duplicate(ContinueStatement elem) { 391 return target.getContinueStatement().createContinueStatement(elem.getLabel()); 392 } 393 394 395 public LocalVarDeclaration duplicate(LocalVarDeclaration elem) { 396 LocalVarDeclaration result = target.getLocalVarDeclaration().createLocalVarDeclaration( 397 elem.isFinal(), 398 (TypeReference) duplicate(elem.getTypeName()), 399 duplicate(elem.getVariables())); 400 return result; 402 } 403 404 405 public EmptyStatement duplicate(EmptyStatement elem) { 406 return target.getEmptyStatement().createEmptyStatement(); 407 } 408 409 410 public ReturnStatement duplicate(ReturnStatement elem) { 411 return target.getReturnStatement().createReturnStatement((Expression) duplicate(elem.getExpression())); 412 } 413 414 415 public ForEachStatement duplicate(ForEachStatement elem) { 416 return target.getForEachStatement().createForEachStatement( 417 (Statement) duplicate(elem.getBody()), 418 duplicate(elem.getParameter()), 419 (Expression) duplicate(elem.getIterable()) 420 ); 421 } 422 423 424 public SwitchStatement duplicate(SwitchStatement elem) { 425 return target.getSwitchStatement().createSwitchStatement( 426 (Expression) duplicate(elem.getExpression()), 427 duplicate(elem.getCases())); 428 } 429 430 431 public SynchronizedStatement duplicate(SynchronizedStatement elem) { 432 return target.getSynchronizedStatement().createSynchronizedStatement( 433 (Expression) duplicate(elem.getLock()), 434 duplicate(elem.getBody()) 435 ); 436 } 437 438 439 public ThrowStatement duplicate(ThrowStatement elem) { 440 return target.getThrowStatement().createThrowStatement((Expression) duplicate(elem.getExpression())); 441 } 442 443 444 445 447 public Annotation duplicate(Annotation elem) { 448 Annotation result = target.getAnnotation().createAnnotation(duplicate(elem.getTypeName()), duplicate(elem.getAttributeValues())); 449 return result; 450 } 451 452 453 public ArrayInitialization duplicate(ArrayInitialization elem) { 454 if (elem == null) 455 return null; 456 ArrayInitialization result = target.getArrayInitialization().createArrayInitialization(duplicate(elem.getElementValues())); 457 return result; 458 } 459 460 461 public Assignment duplicate(Assignment elem) { 462 return target.getAssignment().createAssignment( 463 (PrimaryExpression) duplicate(elem.getLeftSide()), 464 elem.getOperator(), 465 (Expression) duplicate(elem.getRightSide()) 466 ); 467 } 468 469 470 public InfixExpression duplicate(InfixExpression elem) { 471 return target.getInfixExpression().createInfixExpression( 472 (Expression) duplicate(elem.getLeftSide()), 473 elem.getOperator(), 474 (Expression) duplicate(elem.getRightSide()) 475 ); 476 } 477 478 479 public ConditionalExpression duplicate(ConditionalExpression elem) { 480 return target.getConditionalExpression().createConditionalExpression( 481 (Expression)duplicate(elem.getCondition()), 482 (Expression)duplicate(elem.getTruePart()), 483 (Expression)duplicate(elem.getFalsePart()) 484 ); 485 } 486 487 488 public TypeCast duplicate(TypeCast elem) { 489 return target.getTypeCast().createTypeCast((TypeReference) duplicate(elem.getTypeName()), (Expression) duplicate(elem.getExpression())); 490 } 491 492 493 public PrefixExpression duplicate(PrefixExpression elem) { 494 return target.getPrefixExpression().createPrefixExpression( 495 elem.getOperator(), 496 (UnaryExpression) duplicate(elem.getExpression()) 497 ); 498 } 499 500 501 public Literal duplicate(Literal elem) { 502 if (elem instanceof IntLiteral) 503 return target.getIntLiteral().createIntLiteral(((IntLiteral) elem).getValue()); 504 505 if (elem instanceof StringLiteral) 506 return target.getStringLiteral().createStringLiteral(((StringLiteral) elem).getValue()); 507 508 if (elem instanceof BooleanLiteral) 509 return target.getBooleanLiteral().createBooleanLiteral(((BooleanLiteral) elem).isValue()); 510 511 if (elem instanceof CharLiteral) 512 return target.getCharLiteral().createCharLiteral(((CharLiteral) elem).getValue()); 513 514 if (elem instanceof NullLiteral) 515 return target.getNullLiteral().createNullLiteral(); 516 517 throw new IllegalArgumentException ("unknow type to duplicate: " + elem.getClass().getName()); 518 } 519 520 521 public ThisExpression duplicate(ThisExpression elem) { 522 return target.getThisExpression().createThisExpression(); 523 } 524 525 526 public ComplexExpression duplicate(ComplexExpression elem) { 527 return target.getComplexExpression().createComplexExpression((Expression) duplicate(elem.getExpression())); 528 } 529 530 531 public ClassExpression duplicate(ClassExpression elem) { 532 return target.getClassExpression().createClassExpression(); 533 } 534 535 536 public PostfixExpression duplicate(PostfixExpression elem) { 537 return target.getPostfixExpression().createPostfixExpression( 538 elem.getOperator(), 539 (UnaryExpression) duplicate(elem.getExpression()) 540 ); 541 } 542 543 544 public ArrayAccess duplicate(ArrayAccess elem) { 545 return target.getArrayAccess().createArrayAccess( 546 (PrimaryExpression) duplicate(elem.getArray()), 547 (Expression) duplicate(elem.getIndex()) 548 ); 549 } 550 551 552 public NewArrayExpression duplicate(NewArrayExpression elem) { 553 return target.getNewArrayExpression().createNewArrayExpression( 554 duplicate(elem.getTypeName()), 555 duplicate(elem.getDimensions()), 556 elem.getDimCount(), 557 duplicate(elem.getInitializer()) 558 ); 559 } 560 561 562 public VariableAccess duplicate(VariableAccess elem) { 563 return target.getVariableAccess().createVariableAccess( 564 elem.getName(), 565 (PrimaryExpression) duplicate(elem.getParentClass()), 566 elem.isHasSuper() 567 ); 568 } 569 570 571 public MultipartId duplicate(MultipartId elem) { 572 if (elem == null) return null; 573 return target.getMultipartId().createMultipartId(elem.getName(), duplicate(elem.getParent()), null); 574 } 575 576 public ArrayReference duplicate(ArrayReference elem) { 577 return target.getArrayReference().createArrayReference(elem.getName(), duplicate(elem.getParent()), elem.getDimCount()); 578 } 579 580 public MethodInvocation duplicate(MethodInvocation elem) { 581 return target.getMethodInvocation().createMethodInvocation( 582 elem.getName(), 583 duplicate(elem.getParameters()), 584 (PrimaryExpression) duplicate(elem.getParentClass()), 585 elem.isHasSuper() 586 ); 587 } 588 589 590 public NewClassExpression duplicate(NewClassExpression elem) { 591 return target.getNewClassExpression().createNewClassExpression( 592 elem.getName(), 593 duplicate(elem.getParameters()), 594 (PrimaryExpression) duplicate(elem.getEnclosingClass()), 595 duplicate(elem.getClassName()), 596 (ClassDefinition) duplicate(elem.getClassDefinition()) 597 ); 598 } 599 600 public Object duplicate(Object elem) { 601 System.out.println("duplicating " + elem); 602 if (elem == null) 603 return null; 604 605 if (elem instanceof Import) 606 return duplicate((Import) elem); 607 608 if (elem instanceof JavaClass) 609 return duplicate((JavaClass) elem); 610 611 if (elem instanceof Method) 612 return duplicate((Method) elem); 613 614 if (elem instanceof Constructor) 615 return duplicate((Constructor) elem); 616 617 if (elem instanceof Field) 618 return duplicate((Field) elem); 619 620 if (elem instanceof Initializer) 621 return duplicate((Initializer) elem); 622 623 if (elem instanceof Parameter) 624 return duplicate((Parameter) elem); 625 626 if (elem instanceof Annotation) 627 return duplicate((Annotation) elem); 628 629 if (elem instanceof ArrayInitialization) 630 return duplicate((ArrayInitialization) elem); 631 632 if (elem instanceof Assignment) 633 return duplicate((Assignment) elem); 634 635 if (elem instanceof InfixExpression) 636 return duplicate((InfixExpression) elem); 637 638 if (elem instanceof ConditionalExpression) 639 return duplicate((ConditionalExpression) elem); 640 641 if (elem instanceof TypeCast) 642 return duplicate((TypeCast) elem); 643 644 if (elem instanceof PrefixExpression) 645 return duplicate((PrefixExpression) elem); 646 647 if (elem instanceof Literal) 648 return duplicate((Literal) elem); 649 650 if (elem instanceof ThisExpression) 651 return duplicate((ThisExpression) elem); 652 653 if (elem instanceof ComplexExpression) 654 return duplicate((ComplexExpression) elem); 655 656 if (elem instanceof ClassExpression) 657 return duplicate((ClassExpression) elem); 658 659 if (elem instanceof PostfixExpression) 660 return duplicate((PostfixExpression) elem); 661 662 if (elem instanceof ArrayAccess ) 663 return duplicate((ArrayAccess) elem); 664 665 if (elem instanceof NewArrayExpression) 666 return duplicate((NewArrayExpression) elem); 667 668 if (elem instanceof VariableAccess) 669 return duplicate((VariableAccess) elem); 670 671 if (elem instanceof MultipartId) 672 return duplicate((MultipartId) elem); 673 674 if (elem instanceof MethodInvocation ) 675 return duplicate((MethodInvocation) elem); 676 677 if (elem instanceof NewClassExpression) 678 return duplicate((NewClassExpression) elem); 679 680 if (elem instanceof LabeledStatement) 681 return duplicate((LabeledStatement) elem); 682 683 if (elem instanceof WhileStatement) 684 return duplicate((WhileStatement) elem); 685 686 if (elem instanceof DoStatement) 687 return duplicate((DoStatement) elem); 688 689 if (elem instanceof IfStatement) 690 return duplicate((IfStatement) elem); 691 692 if (elem instanceof AssertStatement) 693 return duplicate((AssertStatement) elem); 694 695 if (elem instanceof TryStatement) 696 return duplicate((TryStatement) elem); 697 698 if (elem instanceof ConstructorInvocation) 699 return duplicate((ConstructorInvocation) elem); 700 701 if (elem instanceof BreakStatement) 702 return duplicate((BreakStatement) elem); 703 704 if (elem instanceof ForStatement) 705 return duplicate((ForStatement) elem); 706 707 if (elem instanceof ContinueStatement) 708 return duplicate((ContinueStatement) elem); 709 710 if (elem instanceof LocalVarDeclaration) 711 return duplicate((LocalVarDeclaration) elem); 712 713 if (elem instanceof EmptyStatement) 714 return duplicate((EmptyStatement) elem); 715 716 if (elem instanceof ReturnStatement) 717 return duplicate((ReturnStatement) elem); 718 719 if (elem instanceof ForEachStatement) 720 return duplicate((ForEachStatement) elem); 721 722 if (elem instanceof SwitchStatement) 723 return duplicate((SwitchStatement) elem); 724 725 if (elem instanceof SynchronizedStatement) 726 return duplicate((SynchronizedStatement) elem); 727 728 if (elem instanceof ThrowStatement) 729 return duplicate((ThrowStatement) elem); 730 731 if (elem instanceof StatementBlock) 732 return duplicate((StatementBlock) elem); 733 734 if (elem instanceof SynchronizedStatement) 735 return duplicate((SynchronizedStatement) elem); 736 737 if (elem instanceof SynchronizedStatement) 738 return duplicate((SynchronizedStatement) elem); 739 740 if (elem instanceof TagValue) 741 return duplicate((TagValue) elem); 742 743 if (elem instanceof LocalVariable) 744 return duplicate((LocalVariable) elem); 745 746 if (elem instanceof ArrayReference) 747 return duplicate((ArrayReference) elem); 748 749 throw new IllegalArgumentException ("unknow type to duplicate: " + elem.getClass().getName()); 750 } 751 } 752 | Popular Tags |