Skip to content

RemoveUnusedImports removes required static import for inherited nested type #7569

@protocol7

Description

@protocol7

What version of OpenRewrite are you using?

  • rewrite-core: 8.81.4
  • rewrite-java: 8.81.4

What is the smallest, simplest way to reproduce the problem?

This is an edge case involving a static import of a nested type through a class that inherits that nested type from an implemented interface.

package com.helloworld;

import static com.helloworld.FakeUserAccountClient.UserAccountDataAuth;

import java.util.List;

class Main {
  List<UserAccountDataAuth> getUsers() {
    return List.of(UserAccountDataAuth.create());
  }
}

class FakeUserAccountClient implements UserAccountClient {}

interface UserAccountClient {
  record UserAccountDataAuth() {
    static UserAccountDataAuth create() {
      return new UserAccountDataAuth();
    }
  }
}

Run org.openrewrite.java.RemoveUnusedImports.

What did you expect to see?

The static import should be preserved because UserAccountDataAuth is referenced as an unqualified type and static method receiver in Main.

The import is valid because FakeUserAccountClient inherits the nested type from UserAccountClient.

What did you see instead?

The recipe removes the static import:

package com.helloworld;

import java.util.List;

class Main {
  List<UserAccountDataAuth> getUsers() {
    return List.of(UserAccountDataAuth.create());
  }
}

class FakeUserAccountClient implements UserAccountClient {}

interface UserAccountClient {
  record UserAccountDataAuth() {
    static UserAccountDataAuth create() {
      return new UserAccountDataAuth();
    }
  }
}

The rewritten code no longer compiles because UserAccountDataAuth cannot be resolved.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions