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] New rule: Prevent use of interface -> abstract class with equals/hashCode as key in Map · Issue #6492 · pmd/pmd

[apex] New rule: Prevent use of interface -> abstract class with equals/hashCode as key in Map #6492

New issue
Copy link
New issue
Copy link
Closed
#6493
Closed
[apex] New rule: Prevent use of interface -> abstract class with equals/hashCode as key in Map#6492
#6493
Copy link
Labels
a:new-ruleProposal to add a new built-in rule
Milestone
7.24.0

Description

@JonnyPower
JonnyPower
opened on Mar 8, 2026
Issue body actions

Proposed Rule Name: AvoidInterfaceAsMapKey

Proposed Category: Error Prone

Description: In apex, when a Map uses an interface as key, where equals / hashCode are implemented by an abstract class implementation of the interface, methods on Map such as containsKey do not dispatch to the correct equals/hashCode implementation.

Involves many classes which might make a more accurate rule implementation difficult, easiest would be to prevent usage of interfaces in key, but that would likely be too broad. An ideal implementation would probably be difficult - where an interface is used as key & there is an implementation of that interface that defines equals/abstract.

Code Sample: A minimal reproduction of the apex bug can be seen here: https://github.com/Traction-Rec/map-eq-dispatch-bug/blob/main/force-app/main/default/classes/MapEqualsDispatchBugTest.cls

public interface IKey {
    Boolean equals(Object obj);
    Integer hashCode();
    String toString();
}

public abstract class AbstractKey implements IKey {
    protected abstract String getKeyValue();

    public Boolean equals(Object obj) {
        System.debug('AbstractKey.equals() called - dispatch worked!');
        if (obj instanceof AbstractKey) {
            return ((AbstractKey) obj).getKeyValue() == this.getKeyValue();
        }
        return false;
    }

    public Integer hashCode() {
        System.debug('AbstractKey.hashCode() called - dispatch worked!');
        String v = getKeyValue();
        return v != null ? System.hashCode(v) : 0;
    }

    public override String toString() {
        return getKeyValue();
    }
}

public class StringKey extends AbstractKey {
    private String value;

    public StringKey(String value) {
        this.value = value;
    }

    protected override String getKeyValue() {
        return value;
    }
}

@IsTest
private class MapEqualsDispatchBugTest {
    @IsTest
    static void containsKeyWithAbstractMiddle() {
        Map<IKey, String> m = new Map<IKey, String>(); // Rule would throw, violation is use of this interface
        IKey k1 = new StringKey('hello');
        m.put(k1, 'value');
        IKey k2 = new StringKey('hello');
        System.assertEquals(k1.hashCode(), k2.hashCode());
        for (IKey existing : m.keySet()) {
            System.assert(k2.equals(existing), 'k2.equals(existing)');
            System.assert(existing.equals(k2), 'existing.equals(k2)');
        }
        System.assert(m.containsKey(k2), 'Hypothesis: containsKey with interface→abstract→concrete (StringKey)'); // This incorrectly returns false
    }
}

Possible Properties:

I don't think properties are necessary / useful

Activity

  1. JonnyPower
    added
    a:new-ruleProposal to add a new built-in rule
    on Mar 8, 2026
  2. JonnyPower
    added 2 commits that reference this issue on Mar 8, 2026

    feat: add new rule that avoids incorrect Map behavior

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

    feat: add new rule that avoids incorrect Map behavior

    This issue will close once commit 58bf4fd is merged into the 'main' branch.
    58bf4fd
  3. JonnyPower commented on Mar 9, 2026

    @JonnyPower
    JonnyPower
    on Mar 9, 2026
    ContributorAuthor
    More actions

    While testing the rule implementation in #6493, discovered that even implementing the equals/hashCode in the concrete class rather than the abstract also breaks Map behaviour - so it's more broad than originally believed

  4. adangel
    added this to the 7.24.0 milestone on Mar 24, 2026
  5. adangel commented on Mar 27, 2026

    @adangel
    adangel
    on Mar 27, 2026
    Member
    More actions

    Thanks for reporting this and working on a rule.
    I was investigating in this area now and I have a few follow-up questions:

    • Is this a known apex platform bug? Is Salesforce support aware of this? I've seen a couple of stackoverflow questions regarding hashCode not calculated correctly. And especially this one https://salesforce.stackexchange.com/questions/122623/methods-equals-and-hashcode-misbehaving-in-map-containskey
    • Can you extend your test case on https://github.com/Traction-Rec/map-eq-dispatch-bug/blob/main/force-app/main/default/classes/MapEqualsDispatchBugTest.cls to verify
      • m.put(k2, 'value2'); System.assertEquals(1, m.size()); - e.g. put works correctly
      • System.assertEquals(m.get(k1), m.get(k2)); - e.g. get works correctly
      • m.remove(k2); System.assertEquals(0, m.size()); - e.g. remove works correctly
    • I would be interested in knowing, whether this bug only is relevant for containsKey...

    @sfdcsteve @jbartolotta-sfdc Are you aware of this Map#containsKey issue?

  6. JonnyPower commented on Mar 29, 2026

    @JonnyPower
    JonnyPower
    on Mar 29, 2026
    ContributorAuthor
    More actions

    Hi @adangel - thanks for looking into this with me! I'm currently on vacation until April 10th but would love to pick this back up when I return.

    I have not created a case with Salesforce support to report this issue, I find that a sisyphean exercise ;)

  7. JonnyPower commented on Apr 16, 2026

    @JonnyPower
    JonnyPower
    on Apr 16, 2026
    ContributorAuthor
    More actions

    @adangel added those tests to further confirm behaviour;

    in aer (a local alternative runtime for apex with a working Map implementation) all tests pass

    % aer test force-app
    PASS MapEqualsDispatchBugTest.containsKeyWithAbstractMiddle
    PASS MapEqualsDispatchBugTest.containsKeyWithTransitiveHierarchy
    PASS MapEqualsDispatchBugTest.containsKeyWithVirtualConcreteImplementor
    PASS MapEqualsDispatchBugTest.putWithEqualKeyKeepsSizeOneAbstractMiddle
    PASS MapEqualsDispatchBugTest.getWithEqualKeysReturnsSameValueAbstractMiddle
    PASS MapEqualsDispatchBugTest.removeWithEqualKeyEmptiesMapAbstractMiddle
    PASS MapEqualsDispatchBugTest.putWithEqualKeyKeepsSizeOneTransitiveHierarchy
    PASS MapEqualsDispatchBugTest.getWithEqualKeysReturnsSameValueTransitiveHierarchy
    PASS MapEqualsDispatchBugTest.removeWithEqualKeyEmptiesMapTransitiveHierarchy
    PASS MapEqualsDispatchBugTest.putWithEqualKeyKeepsSizeOneVirtualConcreteImplementor
    PASS MapEqualsDispatchBugTest.getWithEqualKeysReturnsSameValueVirtualConcreteImplementor
    PASS MapEqualsDispatchBugTest.removeWithEqualKeyEmptiesMapVirtualConcreteImplementor
    
    Ran 12 tests: 12 passed, 0 failed
    

    in Salesforce's runtime, all 12 tests fail - so it's more than just containsKey - valuable tests, thanks for the suggestion;

    14:29:18.6 (26066448)|CODE_UNIT_FINISHED|MapEqualsDispatchBugTest.containsKeyWithAbstractMiddle()
    
    System.AssertException: Assertion Failed: Hypothesis: containsKey with interface�abstract�concrete (StringKey)
    Class.MapEqualsDispatchBugTest.containsKeyWithAbstractMiddle: line 24, column 1
    
    14:29:18.38 (48186778)|CODE_UNIT_FINISHED|MapEqualsDispatchBugTest.containsKeyWithTransitiveHierarchy()
    
    System.AssertException: Assertion Failed: Hypothesis: containsKey with transitive IBaseKey<-ConcreteBase<-MidKey<-DeepAbstractKey<-ConcreteDeepKey
    Class.MapEqualsDispatchBugTest.containsKeyWithTransitiveHierarchy: line 43, column 1
    
    14:29:18.58 (63744838)|CODE_UNIT_FINISHED|MapEqualsDispatchBugTest.containsKeyWithVirtualConcreteImplementor()
    
    System.AssertException: Assertion Failed: Hypothesis: containsKey with virtual concrete implementor (IKey<-BaseKeyVirtual<-MidKeyVirtual)
    Class.MapEqualsDispatchBugTest.containsKeyWithVirtualConcreteImplementor: line 62, column 1
    
    14:29:18.74 (75383407)|CODE_UNIT_FINISHED|MapEqualsDispatchBugTest.getWithEqualKeysReturnsSameValueAbstractMiddle()
    
    System.AssertException: Assertion Failed: get should resolve k2 to the same entry as k1:
    Expected: value
    Actual:   null
    Class.MapEqualsDispatchBugTest.getWithEqualKeysReturnsSameValueAbstractMiddle: line 93, column 1
    
    14:29:18.85 (86877105)|CODE_UNIT_FINISHED|MapEqualsDispatchBugTest.getWithEqualKeysReturnsSameValueTransitiveHierarchy()
    
    System.AssertException: Assertion Failed: get should resolve k2 to the same entry as k1:
    Expected: value
    Actual:   null
    Class.MapEqualsDispatchBugTest.getWithEqualKeysReturnsSameValueTransitiveHierarchy: line 137, column 1
    
    14:29:18.99 (100221753)|CODE_UNIT_FINISHED|MapEqualsDispatchBugTest.getWithEqualKeysReturnsSameValueVirtualConcreteImplementor()
    
    System.AssertException: Assertion Failed: get should resolve k2 to the same entry as k1:
    Expected: value
    Actual:   null
    Class.MapEqualsDispatchBugTest.getWithEqualKeysReturnsSameValueVirtualConcreteImplementor: line 181, column 1
    
    14:29:18.110 (111837869)|CODE_UNIT_FINISHED|MapEqualsDispatchBugTest.putWithEqualKeyKeepsSizeOneAbstractMiddle()
    
    System.AssertException: Assertion Failed: put(k2) should replace same logical key, not add a second bucket:
    Expected: 1
    Actual:   2
    Class.MapEqualsDispatchBugTest.putWithEqualKeyKeepsSizeOneAbstractMiddle: line 79, column 1
    
    14:29:18.121 (122573944)|CODE_UNIT_FINISHED|MapEqualsDispatchBugTest.putWithEqualKeyKeepsSizeOneTransitiveHierarchy()
    
    System.AssertException: Assertion Failed: put(k2) should replace same logical key, not add a second bucket:
    Expected: 1
    Actual:   2
    Class.MapEqualsDispatchBugTest.putWithEqualKeyKeepsSizeOneTransitiveHierarchy: line 123, column 1
    
    14:29:18.131 (132598690)|CODE_UNIT_FINISHED|MapEqualsDispatchBugTest.putWithEqualKeyKeepsSizeOneVirtualConcreteImplementor()
    
    System.AssertException: Assertion Failed: put(k2) should replace same logical key, not add a second bucket:
    Expected: 1
    Actual:   2
    Class.MapEqualsDispatchBugTest.putWithEqualKeyKeepsSizeOneVirtualConcreteImplementor: line 167, column 1
    
    14:29:18.143 (143884420)|CODE_UNIT_FINISHED|MapEqualsDispatchBugTest.removeWithEqualKeyEmptiesMapAbstractMiddle()
    
    System.AssertException: Assertion Failed: remove(k2) should remove the lone entry keyed equivalently to k1:
    Expected: 0
    Actual:   1
    Class.MapEqualsDispatchBugTest.removeWithEqualKeyEmptiesMapAbstractMiddle: line 108, column 1
    
    14:29:18.153 (154734841)|CODE_UNIT_FINISHED|MapEqualsDispatchBugTest.removeWithEqualKeyEmptiesMapTransitiveHierarchy()
    
    System.AssertException: Assertion Failed: remove(k2) should remove the lone entry keyed equivalently to k1:
    Expected: 0
    Actual:   1
    Class.MapEqualsDispatchBugTest.removeWithEqualKeyEmptiesMapTransitiveHierarchy: line 152, column 1
    
    14:29:18.164 (164831867)|CODE_UNIT_FINISHED|MapEqualsDispatchBugTest.removeWithEqualKeyEmptiesMapVirtualConcreteImplementor()
    
    System.AssertException: Assertion Failed: remove(k2) should remove the lone entry keyed equivalently to k1:
    Expected: 0
    Actual:   1
    Class.MapEqualsDispatchBugTest.removeWithEqualKeyEmptiesMapVirtualConcreteImplementor: line 196, column 1
    

    However, the map size test cases might be a different issue more general to use of an interface as a key. @cwarden (developer building aer) reported similar troubling behaviour for apex's map size regardless of key's type; https://gist.github.com/cwarden/9b7740710a61cbf482c7b29fb8723b33

  8. adangel commented on Apr 17, 2026

    @adangel
    adangel
    on Apr 17, 2026
    Member
    More actions

    Thanks for doing the tests (I don't have access to salesforce).
    It looks like, this is an important/useful rule to add. I hope, I find some time next week to look at your PR (#6493).

  9. adangel
    changed the title [-][apex] Prevent use of interface -> abstract class with equals/hashcode as key in map[/-] [+][apex] New rule: Prevent use of interface -> abstract class with equals/hashCode as key in Map[/+] on Apr 24, 2026
  10. adangel
    closed this as completedin 58bf4fdon Apr 24, 2026
  11. adangel
    added a commit that references this issue on Apr 24, 2026

    [doc] Update release notes (#6492, #6493)

    4325ba7
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

    a:new-ruleProposal to add a new built-in rule

    Type

    No type

    Projects

    No projects

      Milestone

      • 7.24.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.