[core] Cleanup Node and AbstractNode#2447
Conversation
Replaced with a DataKey
No real reason to remove it, but it should be moved to pmd-core
Generated by 🚫 Danger |
adangel
left a comment
There was a problem hiding this comment.
Thanks for the huge cleanup!
Consider my comments as food for thought 😄
| * | ||
| * @param image The image to check | ||
| */ | ||
| default boolean hasImageEqualTo(String image) { |
There was a problem hiding this comment.
Once we removed the generic "getImage" method in favor of more specific methods likes "getName" etc., this method also doesn't make sense and should be removed
| * @param child The child to add | ||
| * @param index The index to which the child will be added | ||
| */ | ||
| protected void addChild(final B child, final int index) { |
There was a problem hiding this comment.
Do we need three different ways to add children? (addChild, insertChild, setChildren)
Can we reduce this maybe?
There was a problem hiding this comment.
setChildren in used only in tests AFAIK so could be pulled down to DummyNode.
I think addChild is a bit unsafe/hard to use:
- nothing prevents you from writing
addChild(child, 42), and leave 42nullslots in the array (which breaks the contract of the class)... - adding children with this method needs to be done right to left, otherwise adding
nchildren performsnarray resizes
OTOH I can't see another way to improve it right now apart from documentation
| * | ||
| * @param index the child index | ||
| */ | ||
| protected void setChildIndex(final int index) { |
There was a problem hiding this comment.
Is this needed as a protected method? It looks error prone, if this is ever forgotten to be called. And it probably shouldn't needed to be called -> addChild etc. should automatically make sure, the tree is consistent.
There was a problem hiding this comment.
Indeed, I'll look into whether we can remove it
|
|
||
| /** | ||
| * Interface that binds the return type of some node methods to a type | ||
| * parameter. This enforces that eg all children of such a node are from |
There was a problem hiding this comment.
I guess, we should demand that all language impls use this interface. Maybe that's just in the documentation then?
| * it must be set to some value that depends on the node type, not some | ||
| * arbitrary "1" or "2", and not necessarily a unique value. | ||
| */ | ||
| protected AbstractJjtreeNode(int id) { |
There was a problem hiding this comment.
Maybe we can name the "id" something like "productionId" or "jjtNodeNameIndex", to avoid the misinterpretation of "unique id"...
|
|
||
| import net.sourceforge.pmd.lang.ast.AbstractNode; | ||
| import net.sourceforge.pmd.lang.ast.Node; | ||
| import net.sourceforge.pmd.lang.ast.impl.AbstractNode; |
There was a problem hiding this comment.
In theory, AbstractNode should be irrelevant in this context - AbstractNode should only help to implement the Node API, but should not add to the API... (so no additional methods).
Maybe we don't need the NoAttrScope at all? If a intermediate (abstract) class adds a public method, it is public API... so why not expose it as XPath attribute?
| public void setImage(String image) { | ||
| super.setImage(image); | ||
| } | ||
|
|
There was a problem hiding this comment.
Hm... why do we have a "DummyJavaNode" in src/main? Looks like, this should have gone into src/test....
There was a problem hiding this comment.
Indeed, I recall it's still in main because some rules about imports create dummy nodes for ImportWrapper... Hopefully those rules may be rewritten with the newer symbol table and ImportWrapper can be nuked
| * Normally this is uppercase, unless the names is quoted ("name"). | ||
| * </p> | ||
| */ | ||
| default String getCanonicalImage() { |
There was a problem hiding this comment.
hm...yes, these methods are not ideal, too generic like "getImage". but this time on plsql...
Describe the PR
This removes deprecated methods of Node and AbstractNode, and tears down the functionality that is in AbstractNode but not used by all subclasses. It also cleans up some tests that were either using deprecated functionality, or were just building invalid nodes.
Changes in pmd-core:
java-grammarbranch and only available to AbstractJavaNode to AbstractNode (eg insertChild, this is now used by the apex module too)Node::ancestorsreturnsNodeStream<? extends Node>. This is to make it overridable by egNodeStream<JavaNode>. Duplication of the overrides is removed by an interface "GenericNode", because I found it was hard to add a type parameter to Node directly (very contagious), and it also wouldn't be useful in language-independent code, only in language modules. Some type parameters are added to AbstractNode, but this keeps complexity to a minimum while improving the type safety and ease of use of the API. For example, it's impossible to add an arbitrary Node as a child to a JavaNode now, the child must be an AbstractJavaNode. This in turn enables us to say for sure, that the ancestor stream contains only JavaNodes. Of course this is Java, so unchecked conversions can still break the type safety, but since mutation methods are now protected, we can be reasonably sure of it.Changes in language modules:
dump, because now we havegetTextanyway)The changes in test sources are mostly fixing the following problems:
Related issues
Ready?
./mvnw clean verifypasses (checked automatically by travis)