Throw on incompatible visitor type

This commit is contained in:
Clément Fournier
2020-07-01 11:14:55 +02:00
parent feeb1f0a87
commit 25beb215fc
5 changed files with 12 additions and 5 deletions

View File

@ -51,7 +51,7 @@
value="((${visitor-name}<? super P, ? extends R>) visitor)" /> value="((${visitor-name}<? super P, ? extends R>) visitor)" />
<replacefilter token="return visitor.visitChildren(this);" <replacefilter token="return visitor.visitChildren(this);"
value="return visitor.visitNode(this, data);" /> value="return super.acceptVisitor(visitor, data);" />
</replace> </replace>

View File

@ -296,11 +296,18 @@ public interface Node {
* @param data Parameter to the visit * @param data Parameter to the visit
* *
* @return What the visitor returned * @return What the visitor returned
*
* @throws IllegalArgumentException If the visitor is incompatible with this node
*
* @implSpec A typical implementation will check the type of the visitor to
* be that of the language specific visitor, then call the most specific
* visit method of this Node. This is typically implemented by having
* a different override per concrete node class (no shortcuts).
*/ */
// TODO remove the default implementation, convert all visitors to be generic // TODO remove the default implementation, convert all visitors to be generic
default <P, R> R acceptVisitor(AstVisitor<? super P, ? extends R> visitor, P data) { default <P, R> R acceptVisitor(AstVisitor<? super P, ? extends R> visitor, P data) {
// override me // override me
return visitor.visitNode(this, data); throw new IllegalArgumentException("Unsupported visitor" + visitor + " for node " + this);
} }

View File

@ -38,7 +38,7 @@ public abstract class AbstractJavaNode extends AbstractJjtreeNode<AbstractJavaNo
if (visitor instanceof JavaVisitor) { if (visitor instanceof JavaVisitor) {
return this.acceptVisitor((JavaVisitor<? super P, ? extends R>) visitor, data); return this.acceptVisitor((JavaVisitor<? super P, ? extends R>) visitor, data);
} }
return visitor.visitNode(this, data); return super.acceptVisitor(visitor, data);
} }
protected abstract <P, R> R acceptVisitor(JavaVisitor<? super P, ? extends R> visitor, P data); protected abstract <P, R> R acceptVisitor(JavaVisitor<? super P, ? extends R> visitor, P data);

View File

@ -18,7 +18,7 @@ abstract class AbstractJspNode extends AbstractJjtreeNode<AbstractJspNode, JspNo
if (visitor instanceof JspVisitor) { if (visitor instanceof JspVisitor) {
return this.acceptVisitor((JspVisitor<? super P, ? extends R>) visitor, data); return this.acceptVisitor((JspVisitor<? super P, ? extends R>) visitor, data);
} }
return visitor.visitNode(this, data); return super.acceptVisitor(visitor, data);
} }
protected abstract <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data); protected abstract <P, R> R acceptVisitor(JspVisitor<? super P, ? extends R> visitor, P data);

View File

@ -38,7 +38,7 @@ abstract class AbstractScalaNode<T extends Tree> extends AbstractNode<AbstractSc
if (visitor instanceof ScalaParserVisitor) { if (visitor instanceof ScalaParserVisitor) {
return this.acceptVisitor((ScalaParserVisitor<P, R>) visitor, data); return this.acceptVisitor((ScalaParserVisitor<P, R>) visitor, data);
} }
return visitor.visitNode(this, data); return super.acceptVisitor(visitor, data);
} }
protected abstract <P, R> R acceptVisitor(ScalaParserVisitor<? super P, ? extends R> visitor, P data); protected abstract <P, R> R acceptVisitor(ScalaParserVisitor<? super P, ? extends R> visitor, P data);