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.0PMD 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.
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-java</artifactId>
<version>7.13.0</version>
</dependency>import net.sourceforge.pmd.lang.java.JavaLanguageModule;
import net.sourceforge.pmd.lang.java.ast.*;
import net.sourceforge.pmd.lang.java.symbols.*;
import net.sourceforge.pmd.lang.java.types.*;
import net.sourceforge.pmd.lang.java.metrics.JavaMetrics;For AST processing:
import net.sourceforge.pmd.lang.java.ast.JavaNode;
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
import net.sourceforge.pmd.lang.java.ast.JavaVisitorBase;For symbol resolution and type system:
import net.sourceforge.pmd.lang.java.symbols.JClassSymbol;
import net.sourceforge.pmd.lang.java.symbols.table.JSymbolTable;
import net.sourceforge.pmd.lang.java.types.JTypeMirror;
import net.sourceforge.pmd.lang.java.types.TypeSystem;import net.sourceforge.pmd.lang.java.JavaLanguageModule;
import net.sourceforge.pmd.lang.java.ast.*;
import net.sourceforge.pmd.lang.java.symbols.*;
import net.sourceforge.pmd.lang.java.types.*;
// Get the Java language module instance
JavaLanguageModule javaModule = JavaLanguageModule.getInstance();
// Create a visitor to traverse AST
public class MyJavaVisitor extends JavaVisitorBase<Void, Void> {
@Override
public Void visit(ASTMethodCall node, Void data) {
// Access method information
String methodName = node.getMethodName();
JTypeMirror returnType = node.getTypeMirror();
// Access symbol information
JSymbolTable symbolTable = node.getSymbolTable();
return super.visit(node, data);
}
@Override
public Void visit(ASTClassDeclaration node, Void data) {
// Access class information
String className = node.getSimpleName();
JClassSymbol classSymbol = node.getSymbol();
return super.visit(node, data);
}
}
// Example: Calculate metrics for a compilation unit
ASTCompilationUnit compilationUnit = /* ... parsed AST ... */;
double cyclomaticComplexity = JavaMetrics.CYCLO.computeFor(compilationUnit, null);PMD Java is built around several key components that work together to provide comprehensive Java language analysis:
JavaLanguageModule) that integrates with the PMD framework and provides language identification, parser creation, and version supportComplete Abstract Syntax Tree processing with 187+ node types covering all Java language constructs. Provides visitor pattern support for tree traversal and analysis.
public interface JavaNode extends JjtreeNode<JavaNode> {
ASTTypeDeclaration getEnclosingType();
@NonNull ASTCompilationUnit getRoot();
@NonNull JSymbolTable getSymbolTable();
TypeSystem getTypeSystem();
}
public class JavaVisitorBase<P, R> extends AstVisitorBase<P, R> implements JavaVisitor<P, R> {
// Visit methods for all 187+ AST node types
public R visit(ASTCompilationUnit node, P data);
public R visit(ASTClassDeclaration node, P data);
public R visit(ASTMethodDeclaration node, P data);
// ... and 180+ more visit methods
}Advanced symbol resolution with comprehensive type system supporting generics, wildcards, and type inference. Enables semantic analysis and cross-reference resolution.
public interface JElementSymbol {
String getSimpleName();
boolean nameEquals(@NonNull String name);
TypeSystem getTypeSystem();
boolean isUnresolved();
}
public interface JTypeMirror {
boolean isSubtypeOf(@NonNull JTypeMirror other);
JTypeMirror getErasure();
boolean isPrimitive();
boolean isArray();
boolean isClassOrInterface();
}Built-in metrics system with 12 configurable metrics for measuring code quality, complexity, and maintainability.
public final class JavaMetrics {
public static final Metric<ASTAnyTypeDeclaration, Double> CYCLO;
public static final Metric<ASTAnyTypeDeclaration, Integer> LINES_OF_CODE;
public static final Metric<ASTAnyTypeDeclaration, Integer> NCSS;
public static final Metric<ASTAnyTypeDeclaration, Integer> NPATH;
// ... 8 more metrics
}Infrastructure for rule definition and execution with support for visitor-based rules, XPath rules, and custom rule development.
public class JavaLanguageModule extends LanguageModuleBase
implements PmdCapableLanguage, CpdCapableLanguage {
public static JavaLanguageModule getInstance();
public LanguageProcessor createProcessor(LanguagePropertyBundle bundle);
public CpdLexer createCpdLexer(LanguagePropertyBundle bundle);
}Core language module capabilities including parsing, copy-paste detection, and multi-version Java support from 1.3 to 24.
public class JavaLanguageModule extends LanguageModuleBase {
public static JavaLanguageModule getInstance();
public LanguagePropertyBundle newPropertyBundle();
public LanguageProcessor createProcessor(LanguagePropertyBundle bundle);
}// Core AST interfaces
public interface JavaNode extends JjtreeNode<JavaNode> {
ASTTypeDeclaration getEnclosingType();
@NonNull ASTCompilationUnit getRoot();
@NonNull JSymbolTable getSymbolTable();
TypeSystem getTypeSystem();
}
// Modifier enumeration
public enum JModifier {
PUBLIC, PROTECTED, PRIVATE, SEALED, NON_SEALED, ABSTRACT, STATIC,
FINAL, SYNCHRONIZED, NATIVE, DEFAULT, STRICTFP, TRANSIENT, VOLATILE;
String getToken();
int getReflectMod();
}
// Visibility enumeration
public enum Visibility {
PRIVATE(1), PACKAGE(2), PROTECTED(3), PUBLIC(4);
boolean isAtLeast(Visibility other);
boolean isAtMost(Visibility other);
}