Skip to content

Compare types with TypeUtils.isOfType instead of JavaType.equals - #1252

Merged
knutwannheden merged 1 commit into
mainfrom
replace-javatype-equals-with-isoftype
Oct 27, 2026
Merged

knutwannheden merged 1 commit into
mainfrom
replace-javatype-equals-with-isoftype

Conversation

@knutwannheden

Copy link
Copy Markdown
Contributor

Six JavaType classes override equals without hashCode, so a HashSet or HashMap keyed on types matches by identity while equals matches by name. openrewrite/rewrite is removing those equals overrides. Fifteen classes here compare types by value in 29 places, and each of those comparisons would silently become an identity check.

This switches them to TypeUtils.isOfType, which already exists, so it can merge ahead of the rewrite change:

-                if (field.equals(identifier.getFieldType())) {
+                if (TypeUtils.isOfType(field, identifier.getFieldType())) {

Six of the sites are not plain equals calls. Four Objects.equals calls become isOfType as well. The List.equals over parameter types in AssertTrueNegationToAssertFalse becomes an element-wise isOfType, and the Map.containsValue in JMockitMockUpToMockito becomes a noneMatch over the map's values.

isOfType is not a drop-in copy of equals:

  • Most sites compare JavaType.Variables, and isOfType also compares the variable's type. Two separate instances of a field typed JavaType.Unknown no longer match, though the parser normally shares one instance per field. RemoveInitMocksIfRunnersSpecified admits such fields as possible handle receivers.
  • A Method also compares flags and thrown exceptions, which matters only for JMockitMockUpToMockito.

Sets of variables in ExpectedSystemExitToCatchSystemExit and RemoveDoNothingForDefaultMocks are left alone. They hash by identity today and keep doing so.

The existing tests of the touched classes, 251 in 25 classes, pass against the current rewrite snapshot and against a local rewrite-java build without the equals overrides. Five SystemRulesMigrationTest cases could not run, because Maven Central answered their downloads with HTTP 429. Unchanged main passes against that build too, so no test exercises a case where value and identity differ.

openrewrite/rewrite is removing the equals overrides from JavaType, which would silently turn these comparisons into identity checks. This covers direct equals calls as well as Objects.equals, List.equals and Map.containsValue on types.
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Oct 27, 2026
@knutwannheden
knutwannheden merged commit 1945693 into main Oct 27, 2026
1 check passed
@knutwannheden
knutwannheden deleted the replace-javatype-equals-with-isoftype branch October 27, 2026 12:10
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Oct 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

1 participant