When writing a new compilation unit through WorkingCopy it is not possible to change comments supplied by the template, as the comments are not imported.
To fix this, two changes are necessary:
- a new API is needed to create a CompilationUnitTree from a template
- the current implementation of WorkingCopy needs to import comments
See related bug 208438.
Created attachment 116936 [details]
[JG01] The test does not seem to check creation of a package-info.java, which is a special case that deserves explicit testing.
[JG02] Since java.source APIs now rely even more on the existence of standard templates with particular names under Classes/, perhaps these core templates should simply be moved into java.source? Currently they are registered on NewJavaFileWizardIterator in java.project, and java.source just seems to assume that they are there.
Would however mean a java.source > java.project dep, since JavaTemplates.createJavaTemplateIterator must be used for interactive purposes. (Might be possible to define the contents of the template in java.source and just the wizard attribute in java.project, though this would be a bit too clever for my taste.)
Could simplify testNewCompilationUnitFromTemplate a bit - no need to create emptyJava or classJava.
[JG03] apisupport.project.ui.wizard.common.CreatedModifiedFiles.PackageInfo.run currently uses the APIs from bug #119887 and bug #157760. Would this new API be more appropriate than #119887? There needs to be a clearly documented "right way" to create new source files.
Created attachment 117323 [details]
I updated the patch with;
[JG01] added to the test.
[JG02] I do not think it is good to move this to java.source. The logic is currently clearly in one place, and moving it will not guarantee they will exist (the user can still delete them). The added method in GeneratorUtilities is aware the template could not be there, but I added a missing null-check to FileObjects.
[JG03] using workingcopy.rewrite(null, CompilationUnitTree) is still correct. This api gives more control if the new compilation unit needs to be created from the templates. I will add documentation to the JavaDevelopersFAQ.
(In reply to comment #3)
> moving it will not guarantee they will exist (the user can still delete them)
Hmm, probably TemplatesPanel should disable deleteButton when !node.lookup.lookup(FileObject).canRevert()?
> I will add documentation to the JavaDevelopersFAQ.
Javadoc is more discoverable.
Created attachment 117804 [details]
Update patch, added javadoc
[JG03] added/changed the javadoc.
If everything is ok, I will integrate tomorrow.
Author: Ralph Benjamin Ruijs <email@example.com>
Date: 2012-04-05 14:04
Message: Issue #209798 - New API to create CompilationUnitTree from a template
Integrated into 'main-golden', will be available in build *201204080400* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)
User: Ralph Benjamin Ruijs <firstname.lastname@example.org>
Log: Issue #209798 - New API to create CompilationUnitTree from a template