Skip to content

Java: Detect Objects.equals calls on arrays - #23169

Merged
aschackmull merged 1 commit into
github:mainfrom
zhangxinyao88:codex/java-objects-equals-arrays
Nov 19, 2026
Merged

aschackmull merged 1 commit into
github:mainfrom
zhangxinyao88:codex/java-objects-equals-arrays

Conversation

@zhangxinyao88

Copy link
Copy Markdown
Contributor

Extend java/equals-on-arrays to report java.util.Objects.equals calls with compatible array arguments. These calls compare object identity, so arrays with equal contents can compare unequal.

Addresses the Objects.equals case in #21547.

Validation with CodeQL CLI 2.27.4 passed the EqualsArray query tests, including five new positive cases and nine negative cases. QL formatting, query-help generation, query-ID and repository consistency checks also passed.

@zhangxinyao88
zhangxinyao88 requested a review from a team as a code owner November 19, 2026 06:16
Copilot AI adaptive review requested due to automatic review settings November 19, 2026 06:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The PR description lacks DCA results for the final query change, including an assessment of changed results and any unexpected regressions.

1 open issue
What changed in this PR

Extends java/equals-on-arrays to detect identity comparisons through java.util.Objects.equals when both arguments have compatible array types. This addresses the helper-API case in #21547; arrays widened to Object remain outside this change.

Changes:

  • Adds detection for compatible array arguments to Objects.equals.
  • Adds five positive and nine negative test cases with updated expected results.
  • Updates query help and adds a release note.
File Description
java/​ql/​test/​query-tests/​EqualsArray/​ObjectsEquals.java Adds positive and negative helper-API test cases.
java/​ql/​test/​query-tests/​EqualsArray/​EqualsArray.expected Records five new expected alerts.
java/​ql/​src/​Likely Bugs/​Comparison/​EqualsArray.ql Detects Objects.equals calls with compatible arrays.
java/​ql/​src/​Likely Bugs/​Comparison/​EqualsArray.qhelp Documents array identity behavior in Objects.equals.
java/​ql/​src/​change-notes/​2026-11-19-objects-equals-arrays.md Announces the expanded query coverage.

Review effort: Balanced (adaptive)

Comment on lines +28 to +30
m.hasQualifiedName("java.util", "Objects", "equals") and
recvtype = ma.getArgument(0).getType() and
haveIntersection(recvtype, ma.getArgument(1).getType().(Array))
@github-actions

Copy link
Copy Markdown
Contributor

QHelp previews:

java/ql/src/Likely Bugs/Comparison/EqualsArray.qhelp

Equals or hashCode on arrays

The equals and hashCode methods on arrays only consider object identity, not array contents, which is unlikely to be what is intended. The same applies to Objects.equals when both arguments are arrays.

Recommendation

To compare the lengths of the arrays and the corresponding pairs of elements in the arrays, use one of the comparison methods from java.util.Arrays:

  • The method Arrays.equals performs a shallow comparison. That is, array elements are compared using equals.
  • The method Arrays.deepEquals performs a deep comparison, which is appropriate for comparisons of nested arrays.
    Similarly, Arrays.hashCode and Arrays.deepHashCode can be used to compute shallow and deep hash codes based on the hash codes of individual array elements.

Example

In the following example, the two arrays are first compared using the Object.equals method. Because this checks only reference equality and the two arrays are different objects, Object.equals returns false. The two arrays are then compared using the Arrays.equals method. Because this compares the length and contents of the arrays, Arrays.equals returns true.

public void arrayExample(){
    String[] array1 = new String[]{"a", "b", "c"};
    String[] array2 = new String[]{"a", "b", "c"};

    // Reference equality tested: prints 'false'
    System.out.println(array1.equals(array2));
    
    // Equality of array elements tested: prints 'true'
    System.out.println(Arrays.equals(array1, array2));
}

References

@aschackmull aschackmull left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the contribution. I'll run it through our internal test suite to check for any mis-optimisations, just in case. But otherwise this LGTM.

@aschackmull
aschackmull merged commit c9d9a32 into github:main Nov 19, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants