[core] Introduce an AbstractVisitorRule
Remove AntlrBaseRule
This commit is contained in:
@ -154,9 +154,12 @@ definitely don't come for free. It is much effort and requires perseverance to i
|
||||
## 10. Create an abstract rule class for the language
|
||||
* You need to create your own `AbstractRule` in order to interface your language with PMD's generic rule
|
||||
execution.
|
||||
* See [`AbstractSwiftRule`](https://github.com/pmd/pmd/blob/pmd/7.0.x/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/AbstractSwiftRule.java) as an example.
|
||||
* While the rule basically just extends
|
||||
[`AntlrBaseRule`](https://github.com/pmd/pmd/blob/pmd/7.0.x/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrBaseRule.java) without adding anything, every language should have its own base class for rule.
|
||||
* See [`AbstractSwiftRule`](https://github.com/pmd/pmd/blob/master/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/AbstractSwiftRule.java) as an example.
|
||||
* The rule basically just extends
|
||||
[`AbstractVisitorRule`](https://github.com/pmd/pmd/blob/master/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/AbstractVisitorRule.java)
|
||||
and only redefines the abstract `buildVisitor()` method to return our own type of visitor.
|
||||
In this case our `SwiftVisitor` is used.
|
||||
While there is no real functionality added, every language should have its own base class for rules.
|
||||
This helps to organize the code.
|
||||
* All other rules for your language should extend this class. The purpose of this class is to provide a visitor
|
||||
via the method `buildVisitor()` for analyzing the AST. The provided visitor only implements the visit methods
|
||||
|
@ -43,6 +43,7 @@ The remaining section describe the complete release notes for 7.0.0.
|
||||
* Moved the two classes {% jdoc core::cpd.impl.AntlrTokenizer %} and {% jdoc core::cpd.impl.JavaCCTokenizer %} from
|
||||
`internal` package into package {% jdoc_package core::cpd.impl %}. These two classes are part of the API and
|
||||
are base classes for CPD language implementations.
|
||||
* `AntlrBaseRule` is gone in favor of {% jdoc core::pmd.lang.rule.AbstractVisitorRule %}.
|
||||
|
||||
#### Fixed Issues:
|
||||
* java-codestyle
|
||||
|
@ -752,7 +752,9 @@ until the next major release, but it is recommended to stop using them.
|
||||
|
||||
* Moved the two classes {% jdoc core::cpd.impl.AntlrTokenizer %} and {% jdoc core::cpd.impl.JavaCCTokenizer %} from
|
||||
`internal` package into package {% jdoc_package core::cpd.impl %}. These two classes are part of the API and
|
||||
are base classes for CPD language implementations.
|
||||
are base classes for CPD language implementations. Since 7.0.0-rc2.
|
||||
* `AntlrBaseRule` is gone in favor of {% jdoc core::pmd.lang.rule.AbstractVisitorRule %}. Since 7.0.0-rc2.
|
||||
|
||||
|
||||
### XPath 3.1 support
|
||||
|
||||
|
@ -2,38 +2,31 @@
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
|
||||
package net.sourceforge.pmd.lang.ast.impl.antlr4;
|
||||
package net.sourceforge.pmd.lang.rule;
|
||||
|
||||
import net.sourceforge.pmd.RuleContext;
|
||||
import net.sourceforge.pmd.lang.ast.AstVisitor;
|
||||
import net.sourceforge.pmd.lang.ast.Node;
|
||||
import net.sourceforge.pmd.lang.rule.AbstractRule;
|
||||
|
||||
/**
|
||||
* Base implementation of an antlr rule.
|
||||
*/
|
||||
public abstract class AntlrBaseRule extends AbstractRule {
|
||||
|
||||
protected AntlrBaseRule() {
|
||||
// inheritance constructor
|
||||
}
|
||||
|
||||
public abstract class AbstractVisitorRule extends AbstractRule {
|
||||
@Override
|
||||
public void apply(Node target, RuleContext ctx) {
|
||||
AstVisitor<RuleContext, ?> visitor = buildVisitor();
|
||||
assert visitor != null : "Rule should provide a non-null visitor";
|
||||
assert target instanceof AntlrNode : "Incorrect node type " + target + " passed to " + this;
|
||||
|
||||
((AntlrNode<?>) target).acceptVisitor(visitor, ctx);
|
||||
target.acceptVisitor(visitor, ctx);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a rule visitor that can visit nodes for the given rule context.
|
||||
* This visitor should explore the nodes it's interested in and report
|
||||
* violations on the given rule context.
|
||||
* <p>
|
||||
* Language specific subclasses should redefine the return type to use
|
||||
* a language specific visitor interface.
|
||||
* </p>
|
||||
*
|
||||
* @return A visitor bound to the given rule context
|
||||
*/
|
||||
public abstract AstVisitor<RuleContext, ?> buildVisitor();
|
||||
|
||||
}
|
@ -5,10 +5,10 @@
|
||||
package net.sourceforge.pmd.lang.kotlin;
|
||||
|
||||
import net.sourceforge.pmd.RuleContext;
|
||||
import net.sourceforge.pmd.lang.ast.impl.antlr4.AntlrBaseRule;
|
||||
import net.sourceforge.pmd.lang.kotlin.ast.KotlinVisitor;
|
||||
import net.sourceforge.pmd.lang.rule.AbstractVisitorRule;
|
||||
|
||||
public abstract class AbstractKotlinRule extends AntlrBaseRule {
|
||||
public abstract class AbstractKotlinRule extends AbstractVisitorRule {
|
||||
|
||||
protected AbstractKotlinRule() {
|
||||
// inheritance constructor
|
||||
|
@ -5,15 +5,15 @@
|
||||
package net.sourceforge.pmd.lang.swift;
|
||||
|
||||
import net.sourceforge.pmd.RuleContext;
|
||||
import net.sourceforge.pmd.lang.ast.AstVisitor;
|
||||
import net.sourceforge.pmd.lang.ast.impl.antlr4.AntlrBaseRule;
|
||||
import net.sourceforge.pmd.lang.rule.AbstractVisitorRule;
|
||||
import net.sourceforge.pmd.lang.swift.ast.SwiftVisitor;
|
||||
|
||||
public abstract class AbstractSwiftRule extends AntlrBaseRule {
|
||||
public abstract class AbstractSwiftRule extends AbstractVisitorRule {
|
||||
|
||||
protected AbstractSwiftRule() {
|
||||
// inheritance constructor
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract AstVisitor<RuleContext, ?> buildVisitor();
|
||||
public abstract SwiftVisitor<RuleContext, ?> buildVisitor();
|
||||
}
|
||||
|
@ -7,7 +7,6 @@ package net.sourceforge.pmd.lang.swift.rule.bestpractices;
|
||||
import java.util.List;
|
||||
|
||||
import net.sourceforge.pmd.RuleContext;
|
||||
import net.sourceforge.pmd.lang.ast.AstVisitor;
|
||||
import net.sourceforge.pmd.lang.swift.AbstractSwiftRule;
|
||||
import net.sourceforge.pmd.lang.swift.ast.SwiftParser.SwAttribute;
|
||||
import net.sourceforge.pmd.lang.swift.ast.SwiftParser.SwAttributes;
|
||||
@ -15,6 +14,7 @@ import net.sourceforge.pmd.lang.swift.ast.SwiftParser.SwCodeBlock;
|
||||
import net.sourceforge.pmd.lang.swift.ast.SwiftParser.SwFunctionDeclaration;
|
||||
import net.sourceforge.pmd.lang.swift.ast.SwiftParser.SwInitializerDeclaration;
|
||||
import net.sourceforge.pmd.lang.swift.ast.SwiftParser.SwStatement;
|
||||
import net.sourceforge.pmd.lang.swift.ast.SwiftVisitor;
|
||||
import net.sourceforge.pmd.lang.swift.ast.SwiftVisitorBase;
|
||||
|
||||
public class UnavailableFunctionRule extends AbstractSwiftRule {
|
||||
@ -29,7 +29,7 @@ public class UnavailableFunctionRule extends AbstractSwiftRule {
|
||||
}
|
||||
|
||||
@Override
|
||||
public AstVisitor<RuleContext, ?> buildVisitor() {
|
||||
public SwiftVisitor<RuleContext, ?> buildVisitor() {
|
||||
return new SwiftVisitorBase<RuleContext, Void>() {
|
||||
|
||||
@Override
|
||||
|
Reference in New Issue
Block a user