Java language support module for the PMD static code analyzer with AST processing, symbol resolution, type system, metrics, and 400+ built-in rules
npx @tessl/cli install tessl/maven-net-sourceforge-pmd--pmd-java@7.13.00
# PMD Java
1
2
PMD Java is a comprehensive Java language support module for the PMD static code analyzer. It provides sophisticated Abstract Syntax Tree (AST) parsing, symbol resolution, type checking, code metrics calculation, and over 400 built-in rules for detecting code quality issues. This module enables advanced static analysis of Java code for quality assurance, security analysis, performance optimization, and code style enforcement.
3
4
## Package Information
5
6
- **Package Name**: net.sourceforge.pmd:pmd-java
7
- **Package Type**: Maven
8
- **Language**: Java
9
- **Version**: 7.13.0
10
- **Installation**:
11
```xml
12
<dependency>
13
<groupId>net.sourceforge.pmd</groupId>
14
<artifactId>pmd-java</artifactId>
15
<version>7.13.0</version>
16
</dependency>
17
```
18
- **Java Version Support**: Java 1.3 through Java 24 (including preview features)
19
20
## Core Imports
21
22
```java { .api }
23
import net.sourceforge.pmd.lang.java.JavaLanguageModule;
24
import net.sourceforge.pmd.lang.java.ast.*;
25
import net.sourceforge.pmd.lang.java.symbols.*;
26
import net.sourceforge.pmd.lang.java.types.*;
27
import net.sourceforge.pmd.lang.java.metrics.JavaMetrics;
28
```
29
30
For AST processing:
31
```java { .api }
32
import net.sourceforge.pmd.lang.java.ast.JavaNode;
33
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
34
import net.sourceforge.pmd.lang.java.ast.JavaVisitorBase;
35
```
36
37
For symbol resolution and type system:
38
```java { .api }
39
import net.sourceforge.pmd.lang.java.symbols.JClassSymbol;
40
import net.sourceforge.pmd.lang.java.symbols.table.JSymbolTable;
41
import net.sourceforge.pmd.lang.java.types.JTypeMirror;
42
import net.sourceforge.pmd.lang.java.types.TypeSystem;
43
```
44
45
## Basic Usage
46
47
```java { .api }
48
import net.sourceforge.pmd.lang.java.JavaLanguageModule;
49
import net.sourceforge.pmd.lang.java.ast.*;
50
import net.sourceforge.pmd.lang.java.symbols.*;
51
import net.sourceforge.pmd.lang.java.types.*;
52
53
// Get the Java language module instance
54
JavaLanguageModule javaModule = JavaLanguageModule.getInstance();
55
56
// Create a visitor to traverse AST
57
public class MyJavaVisitor extends JavaVisitorBase<Void, Void> {
58
@Override
59
public Void visit(ASTMethodCall node, Void data) {
60
// Access method information
61
String methodName = node.getMethodName();
62
JTypeMirror returnType = node.getTypeMirror();
63
64
// Access symbol information
65
JSymbolTable symbolTable = node.getSymbolTable();
66
67
return super.visit(node, data);
68
}
69
70
@Override
71
public Void visit(ASTClassDeclaration node, Void data) {
72
// Access class information
73
String className = node.getSimpleName();
74
JClassSymbol classSymbol = node.getSymbol();
75
76
return super.visit(node, data);
77
}
78
}
79
80
// Example: Calculate metrics for a compilation unit
81
ASTCompilationUnit compilationUnit = /* ... parsed AST ... */;
82
double cyclomaticComplexity = JavaMetrics.CYCLO.computeFor(compilationUnit, null);
83
```
84
85
## Architecture
86
87
PMD Java is built around several key components that work together to provide comprehensive Java language analysis:
88
89
- **Language Module**: Core entry point (`JavaLanguageModule`) that integrates with the PMD framework and provides language identification, parser creation, and version support
90
- **AST System**: 187+ node types representing all Java language constructs from basic expressions to advanced features like records and pattern matching
91
- **Symbol Resolution**: Sophisticated symbol table system that resolves names to their declarations with full support for scoping rules and overloading
92
- **Type System**: Complete type representation with support for generics, wildcards, intersection types, and type inference
93
- **Visitor Pattern**: Hierarchical visitor implementation for traversing and analyzing AST structures
94
- **Metrics Engine**: 12 built-in metrics with configurable options for measuring code quality and complexity
95
- **Rule Framework**: Infrastructure for implementing over 400 built-in rules plus support for custom rule development
96
97
## Capabilities
98
99
### AST Processing
100
101
Complete Abstract Syntax Tree processing with 187+ node types covering all Java language constructs. Provides visitor pattern support for tree traversal and analysis.
102
103
```java { .api }
104
public interface JavaNode extends JjtreeNode<JavaNode> {
105
ASTTypeDeclaration getEnclosingType();
106
@NonNull ASTCompilationUnit getRoot();
107
@NonNull JSymbolTable getSymbolTable();
108
TypeSystem getTypeSystem();
109
}
110
111
public class JavaVisitorBase<P, R> extends AstVisitorBase<P, R> implements JavaVisitor<P, R> {
112
// Visit methods for all 187+ AST node types
113
public R visit(ASTCompilationUnit node, P data);
114
public R visit(ASTClassDeclaration node, P data);
115
public R visit(ASTMethodDeclaration node, P data);
116
// ... and 180+ more visit methods
117
}
118
```
119
120
[AST Processing](./ast-processing.md)
121
122
### Symbol Resolution and Type System
123
124
Advanced symbol resolution with comprehensive type system supporting generics, wildcards, and type inference. Enables semantic analysis and cross-reference resolution.
125
126
```java { .api }
127
public interface JElementSymbol {
128
String getSimpleName();
129
boolean nameEquals(@NonNull String name);
130
TypeSystem getTypeSystem();
131
boolean isUnresolved();
132
}
133
134
public interface JTypeMirror {
135
boolean isSubtypeOf(@NonNull JTypeMirror other);
136
JTypeMirror getErasure();
137
boolean isPrimitive();
138
boolean isArray();
139
boolean isClassOrInterface();
140
}
141
```
142
143
[Symbols and Types](./symbols-types.md)
144
145
### Code Metrics
146
147
Built-in metrics system with 12 configurable metrics for measuring code quality, complexity, and maintainability.
148
149
```java { .api }
150
public final class JavaMetrics {
151
public static final Metric<ASTAnyTypeDeclaration, Double> CYCLO;
152
public static final Metric<ASTAnyTypeDeclaration, Integer> LINES_OF_CODE;
153
public static final Metric<ASTAnyTypeDeclaration, Integer> NCSS;
154
public static final Metric<ASTAnyTypeDeclaration, Integer> NPATH;
155
// ... 8 more metrics
156
}
157
```
158
159
[Metrics](./metrics.md)
160
161
### Rule Framework
162
163
Infrastructure for rule definition and execution with support for visitor-based rules, XPath rules, and custom rule development.
164
165
```java { .api }
166
public class JavaLanguageModule extends LanguageModuleBase
167
implements PmdCapableLanguage, CpdCapableLanguage {
168
169
public static JavaLanguageModule getInstance();
170
public LanguageProcessor createProcessor(LanguagePropertyBundle bundle);
171
public CpdLexer createCpdLexer(LanguagePropertyBundle bundle);
172
}
173
```
174
175
[Rule Framework](./rule-framework.md)
176
177
### Language Support
178
179
Core language module capabilities including parsing, copy-paste detection, and multi-version Java support from 1.3 to 24.
180
181
```java { .api }
182
public class JavaLanguageModule extends LanguageModuleBase {
183
public static JavaLanguageModule getInstance();
184
public LanguagePropertyBundle newPropertyBundle();
185
public LanguageProcessor createProcessor(LanguagePropertyBundle bundle);
186
}
187
```
188
189
[Language Support](./language-support.md)
190
191
## Common Types
192
193
```java { .api }
194
// Core AST interfaces
195
public interface JavaNode extends JjtreeNode<JavaNode> {
196
ASTTypeDeclaration getEnclosingType();
197
@NonNull ASTCompilationUnit getRoot();
198
@NonNull JSymbolTable getSymbolTable();
199
TypeSystem getTypeSystem();
200
}
201
202
// Modifier enumeration
203
public enum JModifier {
204
PUBLIC, PROTECTED, PRIVATE, SEALED, NON_SEALED, ABSTRACT, STATIC,
205
FINAL, SYNCHRONIZED, NATIVE, DEFAULT, STRICTFP, TRANSIENT, VOLATILE;
206
207
String getToken();
208
int getReflectMod();
209
}
210
211
// Visibility enumeration
212
public enum Visibility {
213
PRIVATE(1), PACKAGE(2), PROTECTED(3), PUBLIC(4);
214
215
boolean isAtLeast(Visibility other);
216
boolean isAtMost(Visibility other);
217
}
218
```