Cleanup some pmd-scala methods

* Remove ScalaNode#childrenAccept: this only makes stack traces longer.
Besides, the node doesn't know how to accumulate values, so it's useless.
* Specialize return type of some methods of the interface.
This commit is contained in:
Clément Fournier
2019-09-08 22:41:38 +02:00
parent be7d439134
commit 68cd5549b9
4 changed files with 36 additions and 26 deletions

View File

@ -39,18 +39,18 @@ abstract class AbstractScalaNode<T extends Tree> extends AbstractNode implements
public abstract <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
@Override
public <D, R> R childrenAccept(ScalaParserVisitor<D, R> visitor, D data) {
int numChildren = jjtGetNumChildren();
R returnValue = null;
for (int i = 0; i < numChildren; ++i) {
returnValue = ((ScalaNode<?>) jjtGetChild(i)).accept(visitor, data);
}
return returnValue;
public T getNode() {
return node;
}
@Override
public T getNode() {
return node;
public ScalaNode<?> jjtGetChild(int index) {
return (ScalaNode<?>) super.jjtGetChild(index);
}
@Override
public ScalaNode<?> jjtGetParent() {
return (ScalaNode<?>) super.jjtGetParent();
}
@Override

View File

@ -31,21 +31,6 @@ public interface ScalaNode<T extends Tree> extends Node {
*/
<D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
/**
* Accept the visitor against all children of this node if there are any and
* return.
*
* @param <D>
* The type of the data input
* @param <R>
* The type of the returned data
* @param visitor
* the visitor to visit this node's children with
* @param data
* context-specific data to pass along
* @return context-specific data for this Visitor pattern
*/
<D, R> R childrenAccept(ScalaParserVisitor<D, R> visitor, D data);
/**
* Get the underlying Scala Node.
@ -53,4 +38,12 @@ public interface ScalaNode<T extends Tree> extends Node {
* @return the Scala Node for this node
*/
T getNode();
@Override
ScalaNode<?> jjtGetChild(int idx);
@Override
ScalaNode<?> jjtGetParent();
}

View File

@ -35,9 +35,23 @@ import scala.meta.Type;
*/
public class ScalaParserVisitorAdapter<D, R> implements ScalaParserVisitor<D, R> {
/** Initial value when combining values returned by children. */
protected R zero() {
return null;
}
/** Merge two values of type R, used to combine values returned by children. */
protected R combine(R acc, R r) {
return r;
}
@Override
public R visit(ScalaNode<?> node, D data) {
return node.childrenAccept(this, data);
R returnValue = zero();
for (int i = 0; i < node.jjtGetNumChildren(); ++i) {
returnValue = combine(returnValue, node.jjtGetChild(i).accept(this, data));
}
return returnValue;
}
@Override

View File

@ -186,7 +186,10 @@ public class ScalaRule extends AbstractRule implements ScalaParserVisitor<RuleCo
@Override
public RuleContext visit(ScalaNode<?> node, RuleContext data) {
return node.childrenAccept(this, data);
for (int i = 0; i < node.jjtGetNumChildren(); ++i) {
node.jjtGetChild(i).accept(this, data);
}
return data;
}
@Override