Affects PMD Version:
6.55.0, 7.0.0
Description:
When calling getImage()
on an instance of ASTAnnotation
, the returned value will be in pascal case, instead of the string that is actually present in the .cls
file. This makes it impossible to enforce casing rules on annotations using PMD.
Note
Disclaimer: I am pretty new to writing custom PMD rules, so if there is a better way to get the raw annotation string, please let me know!
Code Sample demonstrating the issue:
PMD/pmd-rules/src/main/java/com/mycompany/custom_pmd/AnnotationExample.java
public class AnnotationExample extends AbstractApexRule { @Override public Object visit(ASTUserClass node, Object data) { checkAnnotation(node, data); return data; } private void checkAnnotation(ApexNode<?> node, Object data) { for (ASTAnnotation annotation : node.findDescendantsOfType(ASTAnnotation.class)) { System.out.println("annotation.getImage(): " + annotation.getImage()); } } }
SFDX_Project/force-app/main/default/classes/Example.cls
public with sharing class Example { @istest private static void fooShouldBar() { } }
We would expect this to print the value istest
because that is what's in the Example.cls
, but instead, here is the output produced:
annotation.getImage(): IsTest
Steps to reproduce:
Please provide detailed steps for how we can reproduce the bug.
dependency-reduced-pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.custom_pmd</groupId> <artifactId>pmd-rules</artifactId> <name>pmd-rules</name> <version>1.0.0</version> <build> <plugins> <plugin> <artifactId>maven-shade-plugin</artifactId> <version>3.4.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> </execution> </executions> <configuration> <mainClass>net.sourceforge.pmd.PMD</mainClass> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>net.sourceforge.pmd</groupId> <artifactId>pmd</artifactId> <version>6.55.0</version> <type>pom</type> <scope>compile</scope> </dependency> <dependency> <groupId>net.sourceforge.pmd</groupId> <artifactId>pmd-apex-jorje</artifactId> <version>6.55.0</version> <type>pom</type> <scope>compile</scope> </dependency> </dependencies> <properties> <maven.compiler.target>1.7</maven.compiler.target> <maven.compiler.source>1.7</maven.compiler.source> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> </project>
cd PMD/pmd-rules mvn package cd ../.. java -cp PMD/pmd-rules/target/pmd-rules-1.0.0.jar net.sourceforge.pmd.PMD \ -R SFDX_Project/pmd-ruleset.xml \ -d SFDX_Project/force-app \ -f json \ -r pmdFindings.json
Running PMD through:
Maven
MacBook-Pro:SampleProject mitchellspano$ java --version openjdk 11.0.7 2020-04-14 OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.7+10) OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.7+10, mixed mode) MacBook-Pro:SampleProject mitchellspano$ mvn -v Apache Maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d) Maven home: /usr/local/Cellar/maven/3.8.1/libexec Java version: 15.0.2, vendor: N/A, runtime: /usr/local/Cellar/openjdk/15.0.2/libexec/openjdk.jdk/Contents/Home Default locale: en_US, platform encoding: UTF-8 OS name: "mac os x", version: "13.0.1", arch: "x86_64", family: "mac"
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4