Skip to content

Record documentation-comment reference packages in TypesInUse#8078

Merged
jkschneider merged 1 commit into
mainfrom
fancy-bison
Jun 19, 2026
Merged

Record documentation-comment reference packages in TypesInUse#8078
jkschneider merged 1 commit into
mainfrom
fancy-bison

Conversation

@knutwannheden

@knutwannheden knutwannheden commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Motivation

TypesInUse deliberately excludes fully qualified documentation-comment references (Javadoc {@link com.foo.Bar}, and the equivalent constructs in the other languages that share the JavaType attribution — C# <see cref="..."/>, etc.) from its import-retention set, because such a reference needs no import and must not count toward import retention (#5738). The consequence is that ChangePackage could not rely on TypesInUse to discover those references, so it grew its own dedicated full-compilation-unit walk to rediscover them for its precondition. That duplicated the "is this a fully qualified doc reference" predicate in two places with opposite intent — FindTypesInUse to exclude, ChangePackage to recover — which is precisely the kind of drift that produced the regression fixed in #7284. It also meant an extra per-file, per-recipe-instance traversal alongside the one TypesInUse already performs and caches.

This records the packages of those excluded references once, as a byproduct of the single FindTypesInUse walk, and lets ChangePackage consume them — removing the duplicated predicate and the redundant traversal. Only the package is retained, since that is all a package-renaming recipe asks of a doc reference; this also keeps the in-memory shape aligned with the package-keyed form a future serialized precondition would use.

Examples

ChangePackage's precondition doc-reference branch collapses from its own dedicated full-tree visitor to a single lookup against the already-cached TypesInUse:

if (cu.getTypesInUse().hasDocReferenceInPackage(oldPackageName, recursive)) {
    return SearchResult.found(cu);
}

Summary

  • Add a separate docReferencePackages bucket to TypesInUse, populated during FindTypesInUse with the packages of fully qualified documentation-comment references — the references that are (still) excluded from the import-retention typesInUse set.
  • The import-retention set is byte-for-byte unchanged, so RemoveUnusedImports, UsesType, and the FQN trie behave exactly as before. The new bucket is consulted only by callers that explicitly want it.
  • Add TypesInUse.hasDocReferenceInPackage(packageName, recursive), mirroring the per-type package check package-renaming recipes apply to typesInUse. The raw set is not exposed — the query method is the only entry point.
  • Rewire ChangePackage's precondition to call it and delete its bespoke documentation-comment walk, so the fully-qualified-doc-reference predicate now lives in exactly one place.
  • The bucket is transient: it is populated only by TypesInUse.build(...) and left empty by TypesInUse.of(...), since a serialized type index carries no doc references. No serialization-format change.

Test plan

  • New TypesInUseTest.recordsFullyQualifiedJavadocReferencesSeparately — uses a Java {@link} fixture to verify the reference is excluded from getTypesInUse() yet its package is discoverable via hasDocReferenceInPackage, covering exact / recursive / non-matching package queries.
  • ChangePackageTest (including the #2537 fully-qualified-Javadoc cases) stays green — behavior preserved.
  • RemoveUnusedImportsTest stays green — the import-retention set is unchanged (Remove fully qualified JavaDoc references from TypesInUse for RemoveUnusedImports #5738 behavior intact).
  • Kotlin ChangePackageTest stays green — the shared recipe behaves the same on the extended J model.

@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Jun 19, 2026
@knutwannheden knutwannheden changed the title Record fully qualified Javadoc references in TypesInUse Record Javadoc reference packages in TypesInUse Jun 19, 2026
`TypesInUse` deliberately excludes fully qualified documentation-comment
references (Javadoc `{@link ...}`, and the equivalent in other languages
that share the `JavaType` attribution) from its import-retention set
(#5738), since such references need no import. `ChangePackage` therefore
carried its own LST walk to rediscover exactly those references for its
precondition — duplicating the "is this a fully qualified doc reference"
predicate, which is how the regression fixed in #7284 originally crept in.

Capture the packages of those references in a separate transient
`docReferencePackages` bucket during the single `FindTypesInUse` walk, and
have `ChangePackage` query it via `hasDocReferenceInPackage`. Only the
package is retained, since that is all package-renaming recipes ask. The
import-retention set is unchanged, so `RemoveUnusedImports`/`UsesType`/the
FQN trie behave exactly as before. The bucket is not serialized
(`TypesInUse.of` leaves it empty) and is queried only through
`hasDocReferenceInPackage`; the raw set is not exposed.
@knutwannheden knutwannheden changed the title Record Javadoc reference packages in TypesInUse Record documentation-comment reference packages in TypesInUse Jun 19, 2026
@jkschneider
jkschneider merged commit 2b30059 into main Jun 19, 2026
1 check passed
@jkschneider
jkschneider deleted the fancy-bison branch June 19, 2026 11:33
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Jun 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants