0
# PMD Scala 2.12
1
2
PMD Scala 2.12 provides comprehensive Scala language support for the PMD static code analysis framework. It enables parsing of Scala source code into abstract syntax trees (AST) and supports copy-paste detection (CPD) for Scala codebases. The package integrates Scalameta parsing technology with PMD's analysis infrastructure to support Scala 2.12 projects.
3
4
## Package Information
5
6
- **Package Name**: pmd-scala_2.12
7
- **Package Type**: maven
8
- **Language**: Java
9
- **Group ID**: net.sourceforge.pmd
10
- **Artifact ID**: pmd-scala_2.12
11
- **Installation**: Add as Maven dependency
12
13
## Core Imports
14
15
Main language registration and parser:
16
17
```java
18
import net.sourceforge.pmd.lang.scala.ScalaLanguageModule;
19
import net.sourceforge.pmd.lang.scala.ScalaParser;
20
import net.sourceforge.pmd.lang.scala.ScalaLanguageHandler;
21
```
22
23
AST nodes and visitor pattern:
24
25
```java
26
import net.sourceforge.pmd.lang.scala.ast.*;
27
import net.sourceforge.pmd.lang.scala.ast.ScalaParserVisitor;
28
```
29
30
Copy-paste detection:
31
32
```java
33
import net.sourceforge.pmd.cpd.ScalaLanguage;
34
import net.sourceforge.pmd.cpd.ScalaTokenizer;
35
import net.sourceforge.pmd.cpd.SourceCode;
36
import net.sourceforge.pmd.cpd.Tokens;
37
```
38
39
Rule development:
40
41
```java
42
import net.sourceforge.pmd.lang.scala.rule.ScalaRule;
43
```
44
45
## Basic Usage
46
47
```java
48
import java.io.StringReader;
49
import java.io.Reader;
50
import net.sourceforge.pmd.lang.ParserOptions;
51
import net.sourceforge.pmd.lang.LanguageRegistry;
52
import net.sourceforge.pmd.lang.LanguageVersion;
53
import net.sourceforge.pmd.lang.scala.ScalaLanguageModule;
54
import net.sourceforge.pmd.lang.scala.ScalaLanguageHandler;
55
import net.sourceforge.pmd.lang.scala.ScalaParser;
56
import net.sourceforge.pmd.lang.scala.ast.ASTSource;
57
import net.sourceforge.pmd.lang.scala.ast.ScalaParserVisitorAdapter;
58
import net.sourceforge.pmd.lang.scala.ast.ASTDefnObject;
59
import net.sourceforge.pmd.cpd.ScalaTokenizer;
60
import net.sourceforge.pmd.cpd.Tokens;
61
import net.sourceforge.pmd.cpd.SourceCode;
62
import scala.meta.Dialect;
63
64
// Get Scala language and version (registered via SPI)
65
LanguageVersion scalaLang = LanguageRegistry.getLanguage(ScalaLanguageModule.NAME).getVersion("2.12");
66
ScalaLanguageHandler handler = (ScalaLanguageHandler) scalaLang.getLanguageVersionHandler();
67
Dialect dialect = handler.getDialect();
68
69
// Create parser
70
ScalaParser parser = handler.getParser(new ParserOptions());
71
72
// Parse Scala source code
73
String scalaCode = "object HelloWorld { def main(args: Array[String]): Unit = println(\"Hello\") }";
74
Reader sourceReader = new StringReader(scalaCode);
75
ASTSource ast = parser.parse("HelloWorld.scala", sourceReader);
76
77
// Traverse AST using visitor pattern
78
ScalaParserVisitorAdapter visitor = new ScalaParserVisitorAdapter() {
79
@Override
80
public Object visit(ASTDefnObject node, Object data) {
81
System.out.println("Found object: " + node.getClass().getSimpleName());
82
return super.visit(node, data);
83
}
84
};
85
ast.accept(visitor, null);
86
87
// Copy-paste detection
88
ScalaTokenizer tokenizer = new ScalaTokenizer();
89
Tokens tokens = new Tokens();
90
SourceCode sourceCode = new SourceCode("HelloWorld.scala", scalaCode);
91
tokenizer.tokenize(sourceCode, tokens);
92
```
93
94
## Architecture
95
96
PMD Scala 2.12 is built around several key components:
97
98
- **Language Module**: Registers Scala language support with PMD and manages version-specific dialects
99
- **Parser Infrastructure**: Integrates Scalameta parser with PMD's parsing framework
100
- **AST Hierarchy**: Comprehensive Java wrapper classes for all Scala AST node types
101
- **Visitor Pattern**: Type-safe traversal of Scala ASTs for rule implementation
102
- **Tree Builder**: Converts Scalameta AST to PMD-compatible node hierarchy
103
- **Copy-Paste Detection**: Scala tokenization for duplicate code detection
104
- **Rule Framework**: Base classes and utilities for developing Scala-specific analysis rules
105
106
## Capabilities
107
108
### Language Module and Parsing
109
110
Core language registration and parsing infrastructure that integrates Scala with PMD's analysis framework. Supports multiple Scala versions through dialect configuration.
111
112
```java { .api }
113
public class ScalaLanguageModule extends BaseLanguageModule {
114
public static final String NAME = "Scala";
115
public static final String TERSE_NAME = "scala";
116
public ScalaLanguageModule();
117
}
118
119
public class ScalaLanguageHandler extends AbstractLanguageVersionHandler {
120
public ScalaLanguageHandler(Dialect scalaDialect);
121
public Dialect getDialect();
122
public RuleViolationFactory getRuleViolationFactory();
123
public ScalaParser getParser(ParserOptions parserOptions);
124
}
125
126
public class ScalaParser extends AbstractParser {
127
public ScalaParser(Dialect scalaDialect, ParserOptions parserOptions);
128
public boolean canParse();
129
public ASTSource parse(String fileName, Reader source) throws ParseException;
130
public Map<Integer, String> getSuppressMap();
131
}
132
133
class ScalaTreeBuilder {
134
<T extends Tree> ScalaNode<T> build(T astNode);
135
}
136
```
137
138
[Language and Parsing](./language-parsing.md)
139
140
### AST Node Hierarchy
141
142
Complete set of wrapper classes representing all Scala AST node types, providing PMD-compatible interfaces for Scala language constructs including declarations, expressions, types, patterns, and modifiers.
143
144
```java { .api }
145
public interface ScalaNode<T extends Tree> extends Node {
146
<D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
147
T getNode(); // @Deprecated
148
boolean isImplicit();
149
ScalaNode<?> getChild(int idx);
150
ScalaNode<?> getParent();
151
Iterable<? extends ScalaNode<?>> children();
152
}
153
154
public class ASTSource extends AbstractScalaNode<Source> implements RootNode {
155
public ASTSource(Source scalaNode);
156
public <D, R> R accept(ScalaParserVisitor<D, R> visitor, D data);
157
}
158
```
159
160
[AST Nodes](./ast-nodes.md)
161
162
### Visitor Pattern
163
164
Type-safe visitor pattern implementation for traversing Scala ASTs, enabling rule development and custom analysis through comprehensive visit methods for all node types.
165
166
```java { .api }
167
public interface ScalaParserVisitor<D, R> {
168
R visit(ScalaNode<?> node, D data);
169
R visit(ASTSource node, D data);
170
// 100+ specific visit methods for all AST node types
171
// R visit(ASTDefnClass node, D data);
172
// R visit(ASTTermApply node, D data);
173
// ... etc
174
}
175
176
public class ScalaParserVisitorAdapter implements ScalaParserVisitor<Object, Object> {
177
// Default implementations for all visit methods
178
}
179
```
180
181
[Visitor Pattern](./visitor-pattern.md)
182
183
### Copy-Paste Detection
184
185
Scala tokenization support for PMD's copy-paste detection (CPD) system, providing language-specific tokenization and filtering for duplicate code analysis.
186
187
```java { .api }
188
public class ScalaLanguage extends AbstractLanguage {
189
public ScalaLanguage();
190
}
191
192
public class ScalaTokenizer implements Tokenizer {
193
public static final String SCALA_VERSION_PROPERTY = "net.sourceforge.pmd.scala.version";
194
public ScalaTokenizer();
195
public ScalaTokenizer(Properties properties);
196
public void tokenize(SourceCode sourceCode, Tokens tokenEntries) throws IOException;
197
}
198
```
199
200
[Copy-Paste Detection](./cpd.md)
201
202
### Rule Development Framework
203
204
Base classes and infrastructure for developing custom PMD rules for Scala code analysis, including rule violation factories and chain visitors for efficient processing.
205
206
```java { .api }
207
public class ScalaRule extends AbstractRule implements ScalaParserVisitor<RuleContext, RuleContext> {
208
public ScalaRule();
209
public void apply(List<? extends Node> nodes, RuleContext ctx);
210
public RuleContext visit(ScalaNode<?> node, RuleContext data);
211
// Implements all ScalaParserVisitor visit methods
212
}
213
214
@Deprecated
215
public class ScalaRuleViolationFactory extends AbstractRuleViolationFactory {
216
public static final RuleViolationFactory INSTANCE;
217
}
218
```
219
220
[Rule Development](./rule-development.md)
221
222
## Types
223
224
### Core Types
225
226
```java { .api }
227
// Scala Dialect from Scalameta
228
import scala.meta.Dialect;
229
import scala.meta.Tree;
230
231
// PMD Core Types
232
import net.sourceforge.pmd.lang.ParserOptions;
233
import net.sourceforge.pmd.lang.ast.ParseException;
234
import net.sourceforge.pmd.RuleContext;
235
236
// Common Interfaces
237
public interface Node {
238
Node getChild(int index);
239
Node getParent();
240
int getNumChildren();
241
// ... other Node interface methods
242
}
243
244
public interface RootNode extends Node {
245
// Marker interface for root AST nodes
246
}
247
248
// Base implementation class (internal)
249
public abstract class AbstractScalaNode<T extends Tree> implements ScalaNode<T> {
250
// Base implementation for all Scala AST nodes
251
// Provides common functionality like parent-child relationships
252
}
253
```