-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[core] Make visitors generic #880
Comments
Good idea. JavaParserVisitor is indeed generated (I shortly mixed it up with the two implementations JavaParserVisitorAdapter and AbstractJavaRule that need to be updated manually whenever the grammar is changed to add a new node type). We are currently using javacc 5 - there is version 6 available. Maybe this one allows such code generation? |
As of yet I don't think it allows for a generic interface, no. See the visitor generation source: the visit method arguments and return type are customizable with options VISITOR_DATA_TYPE and VISITOR_RETURN_TYPE, but there's no option to make the interface generic yet. The implementation would also require the generated public <T> T jjtAccept(JavaParserVisitor<T> visitor, T data) I'm not certain but I think these methods may be generated here. Though we don't regenerate our node classes each time, and would need to change that by hand, right? Btw the latest version is apparently javacc 7.0 |
Sounds good. AFAIK, JavaCC 6 only added the support to compile grammars into C++ code. I've no idea of what is in JavaCC 7, last time I checked it was still in development. Also, remember we need to review dependencies, JavaCC is currently packaged and shipped and it shouldn't as per #710 |
I've opened a ticket at javacc/javacc#44 to start a dialog with javacc |
Without needing to patch javacc, I noticed that we could simply find/replace the generated visitor file with Ant. Adding the following lines to alljavacc.xml: <replace file="${target}/net/sourceforge/pmd/lang/java/ast/JavaParserVisitor.java"
token="JavaParserVisitor"
value="JavaParserVisitor<T>" />
<replace file="${target}/net/sourceforge/pmd/lang/java/ast/JavaParserVisitor.java"
token="Object"
value="T" /> The produced visitor is exactly what we want: public interface JavaParserVisitor<T>
{
public T visit(JavaNode node, T data);
public T visit(ASTCompilationUnit node, T data);
public T visit(ASTPackageDeclaration node, T data);
public T visit(ASTImportDeclaration node, T data); Can I open a PR? |
I was hoping, that we one day could get rid of the ant scripts, that manually search&replace the (javacc) generated files. |
I've fiddled a bit with generic visitors (see https://github.com/oowekyala/pmd/tree/generic-visitors), the biggest compatibility breaker is AbstractJavaRule. We're forced to make it implement the raw type if we want to preserve source compatibility. Binary compatibility is preserved anyway. In many other cases, such as when using a standalone visitor, genericity can be viewed as an opt-in feature, in that it doesn't break source compat if you implement the raw type |
@oowekyala sorry it took me this long to reply. I've been following this discussion and thinking thoroughly of the consequences. Changing this to generics is indeed a breaking API change (although a slight one). Even if erasure means using At runtime however, if using a plugin built against an older version, it will still work. |
TODO
Language modules to port:
Cleanup after this:
[ ] Remove default implementation in Node"I think it's useful to have a default as some languages do not support visitors (eg we have XML)"Use case
It's a somewhat common pattern to use a single data Object during the full visitation of a visitor, and pass it around to children to accumulate results. A Rule for instance accumulates violations in a RuleContext, whereas in the metrics framework, there are several examples (see eg ncss or cyclo) of using a visitor to eg count some nodes.
The point isn't that it's a single object that's passed around as data, but that the data object keeps the same type throughout the visitation. As you can see in the two examples with ncss and cyclo, we use
data
as a counter. The fact thatdata
is of typeObject
instead ofMutableInt
obscures the code with a lot of casts though.Generic visitor
These casts could be spared if JavaParserVisitor were generic, eg:
Compatibility
Type erasure would replace T with Object in concrete subclasses, so the generic and non generic versions would be identical after compilation. A class implementing the raw type would again be identical to the original version, so AFAIK this change is free (is it?)
Implementation
The JavaParserVisitor interface is generated by JavaCC, so we can't modify it directly. We could simply create another interface based on a copy paste and generify it, but we'd need to keep it up to date by hand. Another solution would be to modify javacc itself, which looks easy enough
The text was updated successfully, but these errors were encountered: