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.
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 #100829). 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 tree.
Checking in src/org/netbeans/api/java/source/TreeMaker.java; /cvs/java/source/src/org/netbeans/api/java/source/TreeMaker.java,v <-- TreeMaker.java new revision: 1.31; previous revision: 1.30 done RCS file: /cvs/java/source/src/org/netbeans/api/java/source/support/TranslateIdentifier.java,v done Checking in src/org/netbeans/api/java/source/support/TranslateIdentifier.java; /cvs/java/source/src/org/netbeans/api/java/source/support/TranslateIdentifier.java,v <-- TranslateIdentifier.java initial revision: 1.1 done Checking in test/unit/src/org/netbeans/api/java/source/gen/CompilationUnitTest.java; /cvs/java/source/test/unit/src/org/netbeans/api/java/source/gen/CompilationUnitTest.java,v <-- CompilationUnitTest.java new revision: 1.5; previous revision: 1.4 done
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 = ...; make.Method( origin.getModifiers(), "new name", make.copyTree(origin.getReturnType(), cut), make.copyTree(origin.getTypeParameters(), cut), // <== !!! make.copyTree(origin.getParameters, cut), // <== !!! make.copyTree(origin.getThrows, cut), // <== !!! (BlockTree) null, null); Or is it possible to pass a result of TreeMaker.Method to copyTree?
Created attachment 50619 [details] NPEs
Added test. Checking in unit/src/org/netbeans/api/java/source/gen/CompilationUnitTest.java; /cvs/java/source/test/unit/src/org/netbeans/api/java/source/gen/CompilationUnitTest.java,v <-- CompilationUnitTest.java new revision: 1.6; previous revision: 1.5 done
Checking in src/org/netbeans/api/java/source/support/TranslateIdentifier.java; /cvs/java/source/src/org/netbeans/api/java/source/support/TranslateIdentifier.java,v <-- TranslateIdentifier.java new revision: 1.2; previous revision: 1.1 done
small improvement /cvs/java/source/src/org/netbeans/api/java/source/support/TranslateIdentifier.java,v <-- TranslateIdentifier.java 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.