This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 117607 - TreeMaker should provide support to safely copy a tree
Summary: TreeMaker should provide support to safely copy a tree
Alias: None
Product: java
Classification: Unclassified
Component: Source (show other bugs)
Version: 6.x
Hardware: All All
: P3 blocker (vote)
Assignee: Pavel Flaska
Depends on:
Blocks: 75076 110039 114396
  Show dependency tree
Reported: 2007-10-03 12:45 UTC by Jan Pokorsky
Modified: 2007-10-11 11:28 UTC (History)
1 user (show)

See Also:
Issue Type: DEFECT
Exception Reporter:

NPEs (3.48 KB, application/octet-stream)
2007-10-10 19:13 UTC, Jan Pokorsky

Note You need to log in before you can comment on or make changes to this bug.
Description Jan Pokorsky 2007-10-03 12:45:03 UTC
Please add a support allowing to safely copy a tree. Something like

Methods should return new tree and replace IdentifierTrees with QualIdentTrees and possibly copy comments (see issue

public Tree TreeMaker.copyTree(Tree t, CompilationUnitTree cu, boolean useFQN, boolean preserveComments);
public List<Tree> TreeMaker.copyTree(List<Tree> t, CompilationUnitTree cu, boolean useFQN, boolean preserveComments);

Use case:
When I implement refactorings I need to copy a tree (e.g. method) to another compilation unit. The tree may contain
identifiers that are not FQN (e.g. return type, params, body, ...). Today I get a broken source in case I use the origin
Comment 1 Pavel Flaska 2007-10-10 17:22:23 UTC
Checking in src/org/netbeans/api/java/source/;
/cvs/java/source/src/org/netbeans/api/java/source/,v  <--
new revision: 1.31; previous revision: 1.30
RCS file: /cvs/java/source/src/org/netbeans/api/java/source/support/,v
Checking in src/org/netbeans/api/java/source/support/;
/cvs/java/source/src/org/netbeans/api/java/source/support/,v  <--
initial revision: 1.1
Checking in test/unit/src/org/netbeans/api/java/source/gen/;
/cvs/java/source/test/unit/src/org/netbeans/api/java/source/gen/,v  <--
new revision: 1.5; previous revision: 1.4
Comment 2 Jan Pokorsky 2007-10-10 19:10:33 UTC
Unfortunately, it does not work. I tried 2 use cases according to issue #110039. The first copied a return type to a new
method and the second copied the whole method. Both ended up with NPE.

The copied method was really simple:

    File m() {
       return null;

I will attach stack traces.

I would also plead for CopyTree with List<? extends Tree> as argument since it helps me write code in following way:
TreeMethod origin = ...;
    "new name",
    make.copyTree(origin.getReturnType(), cut),
    make.copyTree(origin.getTypeParameters(), cut),  // <== !!!
    make.copyTree(origin.getParameters, cut),  // <== !!!
    make.copyTree(origin.getThrows, cut),  // <== !!!
    (BlockTree) null,

Or is it possible to pass a result of TreeMaker.Method to copyTree?
Comment 3 Jan Pokorsky 2007-10-10 19:13:12 UTC
Created attachment 50619 [details]
Comment 4 Pavel Flaska 2007-10-11 09:23:25 UTC
Added test.

Checking in unit/src/org/netbeans/api/java/source/gen/;
/cvs/java/source/test/unit/src/org/netbeans/api/java/source/gen/,v  <--
new revision: 1.6; previous revision: 1.5
Comment 5 Pavel Flaska 2007-10-11 09:30:33 UTC
Checking in src/org/netbeans/api/java/source/support/;
/cvs/java/source/src/org/netbeans/api/java/source/support/,v  <--
new revision: 1.2; previous revision: 1.1
Comment 6 Jan Pokorsky 2007-10-11 11:28:48 UTC
small improvement

/cvs/java/source/src/org/netbeans/api/java/source/support/,v  <--
new revision: 1.3; previous revision: 1.2

I tried to pass the result of TreeMaker.Method to TM.copyTree and it works. So TM.copyTree(List<? extends Tree>) is not
necessary now.