Affects PMD Version: 7.28.0
Description:
Java allows Unicode letters in identifiers (JLS §3.8), so class names that contain non-ASCII letters (e.g. Japanese) compile fine with javac. However, when a source file has a single-type import of such a class, PMD fails while building the symbol table with IllegalArgumentException: Not a Java binary name.
PMD reports this as a processing error and applies no rules to that file at all. As a result, every violation in that file is missed, not just ones related to the import. In the sample below, the file that imports Sample注文IF reports nothing, while the same code importing SampleOrderIF reports the expected UnusedLocalVariable violation.
With unmodified PMD 7.28.0, the error occurs both with and without an auxclasspath.
The cause is the patterns in net.sourceforge.pmd.util.AssertionUtil. They use \w, which only matches [a-zA-Z_0-9] in Java regex unless Pattern.UNICODE_CHARACTER_CLASS is set:
private static final Pattern BINARY_NAME_PATTERN = Pattern.compile("[\\w$]+(?:\\.[\\w$]+)*(?:\\[])*");
PACKAGE_PATTERN and BINARY_NAME_NO_ARRAY use the same character class.
Adding Pattern.UNICODE_CHARACTER_CLASS to these three patterns fixes the issue. I compiled AssertionUtil.java from the 7.28.0 sources with only this change, put it before pmd-core-7.28.0.jar on the classpath of the Ant task, and ran the sample below again. ImportsNonAsciiName.java then reports UnusedLocalVariable at line 8 and no processing error, the same as ImportsAsciiName.java. I checked this both without an auxclasspath and with the compiled sample classes on the auxclasspath. I did not run PMD's own test suite. (Checking characters with Character.isJavaIdentifierStart / Character.isJavaIdentifierPart would be another option.)
The change I tested (pmd-core/src/main/java/net/sourceforge/pmd/util/AssertionUtil.java):
- private static final Pattern PACKAGE_PATTERN = Pattern.compile("[\\w$]+(\\.[\\w$]+)*|");
- private static final Pattern BINARY_NAME_PATTERN = Pattern.compile("[\\w$]+(?:\\.[\\w$]+)*(?:\\[])*");
- private static final Pattern BINARY_NAME_NO_ARRAY = Pattern.compile("[\\w$]++(?:\\.[\\w$]++)*");
+ private static final Pattern PACKAGE_PATTERN = Pattern.compile("[\\w$]+(\\.[\\w$]+)*|", Pattern.UNICODE_CHARACTER_CLASS);
+ private static final Pattern BINARY_NAME_PATTERN = Pattern.compile("[\\w$]+(?:\\.[\\w$]+)*(?:\\[])*", Pattern.UNICODE_CHARACTER_CLASS);
+ private static final Pattern BINARY_NAME_NO_ARRAY = Pattern.compile("[\\w$]++(?:\\.[\\w$]++)*", Pattern.UNICODE_CHARACTER_CLASS);
Exception Stacktrace:
java.lang.IllegalArgumentException: Not a Java binary name 'demo.caller.Sample注文IF'
at net.sourceforge.pmd.util.AssertionUtil.assertValidJavaBinaryName(AssertionUtil.java:85)
at net.sourceforge.pmd.lang.java.symbols.internal.asm.AsmSymbolResolver.resolveClassFromBinaryName(AsmSymbolResolver.java:57)
at net.sourceforge.pmd.lang.java.symbols.SymbolResolver$1.resolveClassFromBinaryName(SymbolResolver.java:84)
at net.sourceforge.pmd.lang.java.symbols.SymbolResolver.resolveClassFromCanonicalName(SymbolResolver.java:48)
at net.sourceforge.pmd.lang.java.internal.JavaAstProcessor.findSymbolCannotFail(JavaAstProcessor.java:85)
at net.sourceforge.pmd.lang.java.symbols.table.internal.SymTableFactory.fillSingleImports(SymTableFactory.java:298)
at net.sourceforge.pmd.lang.java.symbols.table.internal.SymTableFactory.singleImportsSymbolTable(SymTableFactory.java:270)
at net.sourceforge.pmd.lang.java.symbols.table.internal.SymbolTableResolver$MyVisitor.visit(SymbolTableResolver.java:247)
at net.sourceforge.pmd.lang.java.symbols.table.internal.SymbolTableResolver$MyVisitor.visit(SymbolTableResolver.java:160)
at net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit.acceptVisitor(ASTCompilationUnit.java:108)
at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.acceptVisitor(AbstractJavaNode.java:38)
at net.sourceforge.pmd.lang.java.symbols.table.internal.SymbolTableResolver$MyVisitor.traverse(SymbolTableResolver.java:186)
at net.sourceforge.pmd.lang.java.symbols.table.internal.SymbolTableResolver.traverse(SymbolTableResolver.java:105)
at net.sourceforge.pmd.lang.java.internal.JavaAstProcessor.lambda$process$1(JavaAstProcessor.java:136)
at net.sourceforge.pmd.benchmark.TimeTracker.bench(TimeTracker.java:163)
at net.sourceforge.pmd.lang.java.internal.JavaAstProcessor.process(JavaAstProcessor.java:136)
at net.sourceforge.pmd.lang.java.internal.JavaAstProcessor.process(JavaAstProcessor.java:177)
at net.sourceforge.pmd.lang.java.internal.JavaAstProcessor.process(JavaAstProcessor.java:161)
at net.sourceforge.pmd.lang.java.ast.JavaParser.parseImpl(JavaParser.java:70)
at net.sourceforge.pmd.lang.java.ast.JavaParser.parseImpl(JavaParser.java:25)
at net.sourceforge.pmd.lang.ast.impl.javacc.JjtreeParserAdapter.parse(JjtreeParserAdapter.java:36)
at net.sourceforge.pmd.lang.impl.PmdRunnable.parse(PmdRunnable.java:112)
at net.sourceforge.pmd.lang.impl.PmdRunnable.processSource(PmdRunnable.java:132)
at net.sourceforge.pmd.lang.impl.PmdRunnable.run(PmdRunnable.java:80)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:572)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
at java.base/java.lang.Thread.run(Thread.java:1583)
Code Sample demonstrating the issue:
src/demo/caller/Sample注文IF.java
package demo.caller;
public class Sample注文IF {
}
src/demo/caller/SampleOrderIF.java
package demo.caller;
public class SampleOrderIF {
}
src/demo/ImportsNonAsciiName.java
package demo;
import demo.caller.Sample注文IF;
public class ImportsNonAsciiName {
public Sample注文IF create() {
int count = 1;
return new Sample注文IF();
}
}
src/demo/ImportsAsciiName.java
package demo;
import demo.caller.SampleOrderIF;
public class ImportsAsciiName {
public SampleOrderIF create() {
int count = 1;
return new SampleOrderIF();
}
}
All files are UTF-8 and compile with javac -encoding UTF-8.
Steps to reproduce:
- Save the four files above under
src/, and the following build.xml next to src/ (set pmd.lib to the lib directory of PMD 7.28.0).
<project name="pmd-demo" default="run">
<property name="pmd.lib" value="path/to/pmd-bin-7.28.0/lib"/>
<target name="run">
<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask">
<classpath><fileset dir="${pmd.lib}" includes="*.jar"/></classpath>
</taskdef>
<pmd rulesetfiles="category/java/bestpractices.xml/UnusedLocalVariable" encoding="UTF-8" failOnError="false" failOnRuleViolation="false">
<sourceLanguage name="java" version="21"/>
<formatter type="xml" toFile="result.xml"/>
<fileset dir="src" includes="demo/Imports*.java"/>
</pmd>
</target>
</project>
- Run
ant.
- Look at
result.xml.
Actual result:
ImportsAsciiName.java: UnusedLocalVariable at line 8.
ImportsNonAsciiName.java: no violations; only the processing error above.
Expected result: ImportsNonAsciiName.java should also report UnusedLocalVariable at line 8.
Running PMD through: Ant
Ant 1.10.14, JDK 21.0.2 (Eclipse Temurin), Windows 11.