RetoucheDeveloperFAQ
Retouche Developer FAQ
This is the Developer FAQ for NetBeans. It provides answers to frequently asked questions relating to the use of the Retouche API.
Should I prefer Trees or Elements in cases when Elements would be enough, such as retrieving information about a method's parameters?
A: It does not matter, but elements could be a little faster, since the sigfiles can be reused instead of parsing the Java file.
How do I add an annotation to a declaration of a type or type member?
Here is a code sample how to do that:
...
if (Tree.Kind.CLASS == typeDecl.getKind()) {
ClassTree clazz = (ClassTree) typeDecl;
// create the new annotation
AnnotationTree newAnnotation = make.Annotation(
make.Identifier("Override"),
Collections.<ExpressionTree>emptyList()
);
// create new class modifiers, flags aren't changed,
// annotation is added to the end of annotation list.
ModifiersTree classModifiers = make.addModifiersAnnotation(clazz.getModifiers(), newAnnotation);
// rewrite the original modifiers with the new one:
workingCopy.rewrite(clazz.getModifiers(), classModifiers);
}
...
How do I add a Javadoc to type a or a type member? There is TreeMaker.addComment(), what then?
A:
Calling WorkingCOpy.toPhase() seems to remove any previous changes made using WorkingCopy.rewrite(), even though that phase has already been entered before making the changes. Is that a bug or a feature?
A: Issue 88909
Will it be true in the final version of the API that WorkingCopy extends CompilationController extends COmpilationInfo? That is CC and CI will not be final classes?
A: It will stay like that in the final version.
Why does JavaSource.getFileObjects() return Iterable instead of e.g. Set? The latter would allow e.g. calling contents() on the returned instance.
A: Easy to change, not sure if this is needed right now.
It seems possible to find an ExecutableElement for the default constructor for the following file:
class Foo {
}
Bug or feature?
A: Feature. The returned constructor is synthetic (ElementUtilities.isSyntetic().
Is it safe to type cast after getKind()? For example is the following safe?
if (element.getKind() == ElementKind.CONSTRUCTOR) {
ExecutableElement constructor = (ExecutableElement)element;
}
A: Yes, this is safe
What's the best way to get the type of a VariableElement?
A: Use
element.asType()
How do I get Elements from a class file?
A:
How do I traverse over an ancestors or referenced classes?
A:
[{InsertPage page='JavaHT_TreeMakerQA'
style='margin: 10px; padding: 5px;'
}]
