Skip to content

Deprecate getImage/@Image #4787

@adangel

Description

@adangel

Description

An abstract syntax tree should be abstract, but in the same time, should not be too abstract. One of the base interfaces for PMD's AST for all languages is net.sourceforge.pmd.lang.ast.Node, which provides the methods Node#getImage() and Node#hasImageEqualTo(java.lang.String).

However, these methods don't necessarily make sense for all nodes in all contexts. That's why getImage() often returns just null. Also, the name is not very describing. AST nodes should try to use more specific names, such as getValue() or getName().

For PMD 7, most languages have been adapted (#4782). And when writing XPath rules, you need to replace @Image with whatever is appropriate now (e.g. @Name).

There are 4 languages, which still need to be refactored:

  • Apex
  • Java
  • PLSQL
  • Visualforce

The getImage() method should be annotated with @deprecated and @DeprecatedAttribute and a replacement method with a better name should be provided.

There are also other base class implementations, that need to be deprecated:

  • net.sourceforge.pmd.lang.ast.impl.javacc.AbstractJjtreeNode#getImage

Special considerations:

  • XPathRule requires something to get the message for reporting violations. Currently it is getImage(). Maybe we need to extend Reportable, to provide something like a main attribute for nodes. Usually, this feature is only used for variable/field names to be present in the rule violation message.

    • Maybe add an annotation "@MainXPathAttribute" to mark one getter as the main one
    • Then we could figure this out at runtime (refs Node#getXPathAttributesIterator())
  • Maybe add a custom PMD rule to dogfooding ruleset - but probably not needed as we are going to remove getImage anyway.

Current status (2024-01-06)

  • in PMD 6.55.0, the methods Node#getImage() and Node#hasImageEqualTo() are not deprecated
  • in PMD 7.0.0-rc4, these methods are deprecated, but no alternative (where needed) has been provided. Refactoring for Java has been started in [java] Remove usages of getImage in java module #4352.
  • The methods can't be deleted without more work. Leaving these methods deprecated creates many deprecation warnings without the possibility to resolve them.
  • in [core] Avoid using getImage/@Image #4782 most languages have been refactored

Plan

Related issues, PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    in:astAbout the AST structure or API, the parsing stepis:deprecationThe main focus is deprecating public APIs or rules, eg to make them internal, or removing them

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions