Is it generally preferred to use JavaTemplate instead of new'ing up elements "manually"? #275
-
(Questions at the bottom) Occasionally I'll find myself For example, some variation of this: if (!md.hasModifier(J.Modifier.Type.Private)) {
J.Modifier mod = new J.Modifier(Tree.randomId(), Space.EMPTY, Markers.EMPTY, J.Modifier.Type.Private);
md = md.withModifiers(
ListUtils.concat(md.getModifiers(), mod)
);
} Which has me thinking about this in the general case. Currently there are visitors which I wonder if there is specific circumstances where it's better to With all this said, here's what I'm wondering:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
While JavaTemplate is fully capable of doing these types of refactoring operations, I guess the answer is "it depends". We did not add any sort of coordinates for modifiers, because it just seemed like you could easily manipulate the modifiers via ListUtils. As for implements: In a majority of the cases you are simply adding a fully-qualified class name, the one exception to this might be where you need to implement an interface that has type parameters. While using JavaTemplate is easy (and fast), there is still some overhead because it is pruning an existing AST, inserting the template to create a synthetic compilation unit, compiling that code, and then extracting/merging the generated AST elements. |
Beta Was this translation helpful? Give feedback.
While JavaTemplate is fully capable of doing these types of refactoring operations, I guess the answer is "it depends". We did not add any sort of coordinates for modifiers, because it just seemed like you could easily manipulate the modifiers via ListUtils.
As for implements: In a majority of the cases you are simply adding a fully-qualified class name, the one exception to this might be where you need to implement an interface that has type parameters.
While using JavaTemplate is easy (and fast), there is still some overhead because it is pruning an existing AST, inserting the template to create a synthetic compilation unit, compiling that code, and then extracting/merging the generated…