Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[apex] ASTAnnotation.getImage() does not return value as written in the class #4418

Closed
mitchspano opened this issue Mar 11, 2023 · 3 comments · Fixed by #4936
Closed

[apex] ASTAnnotation.getImage() does not return value as written in the class #4418

mitchspano opened this issue Mar 11, 2023 · 3 comments · Fixed by #4936
Labels
a:bug PMD crashes or fails to analyse a file.
Milestone

Comments

@mitchspano
Copy link

mitchspano commented Mar 11, 2023

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"
@mitchspano mitchspano added the a:bug PMD crashes or fails to analyse a file. label Mar 11, 2023
@rsoesemann
Copy link
Member

@mitchspano looks like getImage is going to be removed in PMD 7. Maybe you wanna rely on something else
#4318

@mitchspano
Copy link
Author

@rsoesemann When I take a look at the replacement implementation of getName in ASTAnnotation, it still has the same underlying method calls, so I doubt that would resolve the case sensitivity issue.

@adangel adangel changed the title [Apex] ASTAnnotation.getImage() does not return value as written in the class [apex] ASTAnnotation.getImage() does not return value as written in the class Mar 16, 2023
@jsotuyod jsotuyod added the needs:pmd7-revalidation The issue hasn't yet been retested vs PMD 7 and may be stale label Apr 2, 2024
@adangel adangel removed the needs:pmd7-revalidation The issue hasn't yet been retested vs PMD 7 and may be stale label Apr 6, 2024
adangel added a commit to adangel/pmd that referenced this issue Apr 6, 2024
@adangel adangel added this to the 7.1.0 milestone Apr 6, 2024
@adangel
Copy link
Member

adangel commented Apr 6, 2024

Yes, getImage() (which probably will be removed in some future version) and getName() both return the normalized annotation names.

For Java rules and PMD 7, the raw name is generally available via annotationNode.getTextDocument().sliceOriginalText(annotationNode.getTextRegion()), which would return in the above example @istest. PR #4936 adds a new getter getRawName() which returns the original annotation name without normalization. This can then also be used in XPath rules (e.g. //Annotation[@RawName != @Name]).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:bug PMD crashes or fails to analyse a file.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants