GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [apex] Expose the referenced type name on ClassRefExpression for XPath rules · Issue #7182 · pmd/pmd

[apex] Expose the referenced type name on ClassRefExpression for XPath rules #7182

New issue
Copy link
New issue
Copy link
Closed
#7201
Closed
[apex] Expose the referenced type name on ClassRefExpression for XPath rules#7182
#7201
Copy link
Labels
an:enhancementAn improvement on existing features / rules
Milestone
7.30.0

Description

@geoffswift
geoffswift
opened on Oct 14, 2026
Issue body actions

Is your feature request related to a problem? Please describe.

In Apex, XPath rules can identify a ClassRefExpression representing a class literal, but cannot inspect the referenced type name. This prevents a rule from precisely matching a particular class literal passed as a method argument.

Reproduced with PMD 7.28.0. For TargetClass.class, the XPath-visible node has an empty Image, no child nodes, and no attribute containing TargetClass. Its DefiningType names the enclosing class (ClassLiteralExample), not the referenced class.

Describe the solution you'd like

Expose the referenced type name on ASTClassRefExpression through an XPath-visible attribute, for example ReferencedType or another name consistent with PMD's AST conventions.

For the reproduction below, a query such as this should be possible (the attribute name is illustrative):

//MethodCallExpression[@MethodName = 'consume'][
    ClassRefExpression[@ReferencedType = 'TargetClass']
]

The requested value is the type name written in the class literal; full symbol/type resolution is not required. It should remain available when the expression is used without a cast or typed variable declaration. Qualified/nested type names should also be retained.

Describe alternatives you've considered

  • Inspecting the surrounding cast or variable/field declaration. That identifies the result/assignment type, which can differ from the class literal passed into the method, and does not cover standalone calls.
  • Using DefiningType. That identifies the enclosing class, so it cannot distinguish different class literals within the same class.
  • Writing a Java rule that reads the source text for the node. This adds custom rule/build infrastructure for information already present in the parsed expression; exposing the type name would allow a straightforward XPath rule.

Additional context

Fresh, self-contained minimal reproduction (ClassLiteralExample.cls):

public class ClassLiteralExample {
    public class TargetClass {}

    public static void example() {
        consume(TargetClass.class);
    }

    private static void consume(Type referencedType) {}
}

Command:

pmd ast-dump --language apex --file ClassLiteralExample.cls --format xml

Actual MethodCallExpression subtree from a newly generated PMD 7.28.0 dump:

<MethodCallExpression DefiningType="ClassLiteralExample" FullMethodName="consume" Image="" InputParametersSize="1" MethodName="consume" RealLoc="true">
  <EmptyReferenceExpression DefiningType="" Image="" RealLoc="false" />
  <ClassRefExpression DefiningType="ClassLiteralExample" Image="" RealLoc="true" />
</MethodCallExpression>

The practical use case is enforcing a shared test helper instead of manually mocking a particular implementation class. Matching the method call itself is possible; matching which class literal was passed is the missing piece.

Current implementation for reference: https://github.com/pmd/pmd/blob/main/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ASTClassRefExpression.java

Activity

  1. geoffswift
    added
    an:enhancementAn improvement on existing features / rules
    on Oct 14, 2026
  2. Lyin-OTTF
    added 2 commits that reference this issue on Oct 30, 2026

    [apex] Expose the referenced type name on ClassRefExpression

    This issue will close once commit f420710 is merged into the 'main' branch.
    f420710

    [apex] Expose the referenced type name on ClassRefExpression

    This issue will close once commit 4c3661c is merged into the 'main' branch.
    4c3661c
  3. adangel commented on Nov 6, 2026

    @adangel
    adangel
    on Nov 6, 2026
    Member
    More actions

    Note: The attribute will be named @Type to be consistent with ASTCastExpression / ASTNewObjectExpression.

  4. adangel
    added this to the 7.30.0 milestone on Nov 6, 2026
  5. adangel
    closed this as completedin #7201on Nov 6, 2026
  6. adangel
    added a commit that references this issue on Nov 6, 2026

    [apex] Fix pmd#7182: Expose the referenced type name on ClassRefExpre…

    This issue will close once commit 1970f64 is merged into the 'main' branch.
    1970f64
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    an:enhancementAn improvement on existing features / rules

    Type

    No type

    Projects

    No projects

      Milestone

      • 7.30.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.