PMD Scala language support for Scala 2.12 - provides parsing and static analysis capabilities for Scala code as part of the PMD extensible multilanguage static code analyzer
npx @tessl/cli install tessl/maven-net-sourceforge-pmd--pmd-scala-2-12@7.13.0PMD Scala 2.12 provides Scala language support for PMD, the extensible multilanguage static code analyzer. It enables parsing of Scala 2.12 source code into abstract syntax trees (AST) for static analysis, code quality checks, and copy-paste detection. The module integrates Scalameta parsers with PMD's rule engine to provide comprehensive Scala code analysis capabilities.
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-scala_2.12</artifactId>
<version>7.13.0</version>
</dependency>import net.sourceforge.pmd.lang.scala.ScalaLanguageModule;
import net.sourceforge.pmd.lang.scala.ScalaLanguageHandler;
import net.sourceforge.pmd.lang.scala.ast.ScalaParser;
import net.sourceforge.pmd.lang.scala.ast.ASTSource;
import net.sourceforge.pmd.lang.scala.ast.AbstractScalaNode;
import net.sourceforge.pmd.lang.scala.ast.ScalaNode;
import net.sourceforge.pmd.lang.scala.ast.ScalaVisitor;
import net.sourceforge.pmd.lang.scala.ast.ScalaTreeBuilder;
import net.sourceforge.pmd.lang.scala.rule.ScalaRule;
import net.sourceforge.pmd.lang.scala.cpd.ScalaCpdLexer;
import net.sourceforge.pmd.lang.scala.cpd.ScalaTokenAdapter;// Get the Scala language module
ScalaLanguageModule module = ScalaLanguageModule.getInstance();
// Parse Scala source code
ScalaParser parser = new ScalaParser();
ASTSource ast = parser.parse(parserTask);
// Create a custom Scala rule
public class MyScalaRule extends ScalaRule {
@Override
public RuleContext visit(ASTDefnClass node, RuleContext data) {
// Rule logic for class definitions
return super.visit(node, data);
}
}The PMD Scala module follows a layered architecture:
ScalaLanguageModule and ScalaLanguageHandlerScalaParser and ScalaTreeBuilderCore PMD integration and language module registration for Scala versions 2.10-2.13.
public class ScalaLanguageModule extends SimpleLanguageModuleBase {
public static ScalaLanguageModule getInstance();
public CpdLexer createCpdLexer(LanguagePropertyBundle bundle);
}Complete Scala source code parsing into PMD-compatible abstract syntax trees using Scalameta.
public final class ScalaParser implements Parser {
public ASTSource parse(ParserTask task) throws ParseException;
}
public final class ASTSource extends AbstractScalaNode<Source> implements RootNode {
public AstInfo<ASTSource> getAstInfo();
}Comprehensive visitor pattern implementation for traversing and analyzing Scala ASTs.
public interface ScalaVisitor<D, R> extends AstVisitor<D, R> {
default R visit(ScalaNode<?> node, D data);
default R visit(ASTSource node, D data);
// 140+ visit methods for all AST node types
}
public interface ScalaNode<T extends Tree> extends GenericNode<ScalaNode<?>> {
boolean isImplicit();
}Framework for creating custom static analysis rules for Scala code.
public class ScalaRule extends AbstractRule implements ScalaVisitor<RuleContext, RuleContext> {
public void apply(Node target, RuleContext ctx);
public RuleContext visitNode(Node node, RuleContext param);
}Scalameta-based tokenization for detecting code duplication in Scala projects.
public class ScalaCpdLexer implements CpdLexer {
public ScalaCpdLexer(LanguagePropertyBundle bundle);
public void tokenize(TextDocument document, TokenFactory tokenEntries);
}// Language integration
class ScalaLanguageModule extends SimpleLanguageModuleBase
class ScalaLanguageHandler extends AbstractPmdLanguageVersionHandler
// Parsing
class ScalaParser implements Parser
class ScalaTreeBuilder // Internal tree building
class ASTSource extends AbstractScalaNode<Source> implements RootNode
// AST base types
abstract class AbstractScalaNode<T extends Tree> extends AbstractNode<AbstractScalaNode<?>, ScalaNode<?>> implements ScalaNode<T>
interface ScalaNode<T extends Tree> extends GenericNode<ScalaNode<?>>
interface ScalaVisitor<D, R> extends AstVisitor<D, R>
class ScalaVisitorBase implements ScalaVisitor<Object, Object>
// Rule development
class ScalaRule extends AbstractRule implements ScalaVisitor<RuleContext, RuleContext>
// Copy-paste detection
class ScalaCpdLexer implements CpdLexer
class ScalaTokenAdapter // Token adapter for PMD integration
// Internal utilities
class ScalaDialect // Maps language versions to Scalameta dialectsnet.sourceforge.pmd:pmd-core - Core PMD frameworkorg.scalameta:parsers_2.12 and org.scalameta:trees_2.12 - Scala parsingorg.scala-lang:scala-library:2.12.20 - Scala standard library