Skip to content

Use Objects.equals instead of hand-rolled String == comparison (CodeQL java/reference-equality-on-strings)#230

Open
vharseko wants to merge 1 commit into
OpenIdentityPlatform:masterfrom
vharseko:features/codeql-string-ref-equality
Open

Use Objects.equals instead of hand-rolled String == comparison (CodeQL java/reference-equality-on-strings)#230
vharseko wants to merge 1 commit into
OpenIdentityPlatform:masterfrom
vharseko:features/codeql-string-ref-equality

Conversation

@vharseko

@vharseko vharseko commented Jul 8, 2026

Copy link
Copy Markdown
Member

Summary

Resolves the single java/reference-equality-on-strings CodeQL alert in
Responses.java.

ResourceResponseImpl.equals() delegated to a private isEqual(String, String)
helper whose first line was a s1 == s2 reference comparison. The reference
check was an intentional fast-path (and both-null shortcut) with the real
value comparison delegated to s1.equals(s2) — so the code was correct, but:

  • CodeQL flags the String == String, and
  • the helper is a hand-rolled reimplementation of java.util.Objects.equals.

Fix

Replace the two isEqual(...) calls with Objects.equals(...) (already
imported) and delete the helper:

return Objects.equals(id, that.id) && Objects.equals(revision, that.revision);

Behaviour is identical — Objects.equals(a, b) performs the same
a == b || (a != null && a.equals(b)) null-safe comparison, including the
same reference fast-path and both-null handling.

Testing

mvn -o -pl commons/rest/json-resource -am compile — BUILD SUCCESS.

…L java/reference-equality-on-strings)

ResourceResponseImpl.equals() delegated to a private isEqual(String, String)
helper that started with a "s1 == s2" reference check. The reference compare
was an intentional fast-path / both-null shortcut and the real value compare
was done by s1.equals(s2), so the code was correct, but CodeQL flags the
String == String and the helper simply reimplements java.util.Objects.equals.

Replace the two isEqual calls with Objects.equals (already imported) and drop
the helper. Behaviour is identical - Objects.equals performs the same
"a == b || (a != null && a.equals(b))" null-safe comparison.
@vharseko vharseko added java codeql CodeQL static-analysis findings refactoring Code cleanup / refactoring, no behavior change labels Jul 8, 2026
@vharseko vharseko requested a review from maximthomas July 8, 2026 12:42
@vharseko vharseko removed the java label Jul 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

codeql CodeQL static-analysis findings refactoring Code cleanup / refactoring, no behavior change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants