Skip to content

[bean completion proposals] already having a field with the name of the bean causes weird results #1488

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
martinlippert opened this issue Feb 28, 2025 · 4 comments
Assignees
Labels

Comments

@martinlippert
Copy link
Member

Having a class like:

@RestController
public class MyController {

    @Value("#{T(com.example.demo.TestBean1).foostatic()}")
    private String something;

    @GetMapping("/greeting")
    public String sayHello() {
        <*>
        return "Hello Greeting";
    }

}

When you select a bean with the name something from the list of bean proposals, the result looks like this:

@RestController
public class MyController {

    private final FeignClientExample something;

    @Value("#{T(com.example.demo.TestBean1).foostatic()}")
    private final String something;

    MyController(FeignClientExample something) {
        this.something = something;
    }

    MyController(FeignClientExample something) {
        this.something = something;
    }

    @GetMapping("/greeting")
    public String sayHello() {
        something
        return "Hello Greeting";
    }

}

From my observation, this causes:

  • two fields with the same name
  • two constructors with the exact same content

I think the best way to deal with this situation is to - of course - avoid the duplicated constructor - and to allow the user to easily switch to a different name for the injected bean. This could be done by popping up a dialog that asks the user for a different name or to activate a multi-part edit as part of the completion that pre-selects all the occurrences of the bean name (constructor param + field), so that the user can continue typing a different name and then continue.

@martinlippert
Copy link
Member Author

I think the multi-part edit can be triggered by marking the completion as a snippet completion and include placeholders inside. Although not sure how this works across multiple edits in the same proposal (like the main edit and the additional edits). To be investigated...

@BoykoAlex
Copy link
Contributor

I'd silently generate a different name for the field... Might be too much work for the edge case... IntelliJ doesn't treat this any better than us currently...

@martinlippert
Copy link
Member Author

Yeah, okay, let's not spend too much time on this edge case for now. Can polish this later, if necessary.

@BoykoAlex
Copy link
Contributor

Generated unique name for the field: b0d42d1

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants