Skip to content

Conversation

@amishra-u
Copy link
Contributor

@amishra-u amishra-u commented May 19, 2025

What's changed?

Introduced the ImportManager to handle import management. It provides APIs to:

  • Detect missing or ambiguous imports
  • Check whether a type or member can be referenced via an import safely

This utility can be leveraged by existing visitors AddImport and RemoveImport, and we can also create a new visitor (e.g., FixImport, see #5435) to automatically add or remove imports based on usage. We can also build recipes like RemoveUnusedImport on top of this.

Additionally, ImportManager can support APIs to determine whether a list of imports can be safely folded or to unfold wildcard imports. Currently not implemented but could be easily built on top of the existing structure.

What's your motivation?

While working on #5439, I noticed that AddImport misses some edge cases. I attempted to address import ambiguity, but the solution still doesn't cover all scenarios. This utility class provides a centralized and robust solution to handle all such cases.

Additional context

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@timtebeek timtebeek moved this from In Progress to Ready to Review in OpenRewrite May 19, 2025
@MBoegers MBoegers self-requested a review June 4, 2025 10:28
@MBoegers MBoegers mentioned this pull request Jun 4, 2025
3 tasks
Copy link
Contributor

@MBoegers MBoegers left a comment

Choose a reason for hiding this comment

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

Sorry for the long run.. Overall looks good, just some ideas around the namings and annotations

import static org.openrewrite.Tree.randomId;
import static org.openrewrite.java.ImportManager.ImportStatus.*;

public class ImportManager {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we find a better name than Manager? I think of a manager like something that not only knows about something but also can alter them.

We ususally use the suffix Util for helper, but I get that it is not a classic Util as it maintains state.

Maybe ImportReport as it can answer questions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree with you. What about ImportAnalyzer?

import static org.openrewrite.Tree.randomId;
import static org.openrewrite.java.ImportManager.ImportStatus.*;

public class ImportManager {
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be final reasonable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IMO final could be optional considering it can be used for others JVM based languages with few modifications.

IMPORT_AMBIGUITY,
}

@Getter
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe a @Value ?

@Override
public J visitFieldAccess(J.FieldAccess fieldAccess, Set<JavaType.FullyQualified> types) {
if (fieldAccess.getType() instanceof JavaType.FullyQualified) {
if (fieldAccess.isFullyQualifiedClassReference(((JavaType.FullyQualified) fieldAccess.getType()).getFullyQualifiedName())) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We haveTypeUtils.asFullyQualified defined which does the same.

return identifier;
}
JavaType.Variable variable = identifier.getFieldType();
if (variable != null && variable.getOwner() instanceof JavaType.Class && variable.hasFlags(Flag.Static)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

You have ImportManager.isStaticClassVariable() which does the same.

@github-project-automation github-project-automation bot moved this from Ready to Review to In Progress in OpenRewrite Jun 10, 2025
@amishra-u
Copy link
Contributor Author

@MBoegers incorporated feedback. PTAL

@github-actions
Copy link
Contributor

This PR is stale because it has been open for 90 days with no activity. Remove stale label or comment or this will be closed in two weeks. PRs may be reopened when there is renewed interest.

@github-actions github-actions bot added the Stale label Sep 15, 2025
@amishra-u
Copy link
Contributor Author

@timtebeek @MBoegers Got a stale notification for this pr. I believe I have already incorporated all feedbacks. PTAL

@github-actions github-actions bot removed the Stale label Sep 22, 2025
@timtebeek timtebeek self-requested a review October 25, 2025 11:16
@timtebeek timtebeek added the enhancement New feature or request label Oct 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

3 participants