Skip to content

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

Closed

Description

@martinlippert

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.

Activity

  1. added this to the 4.29.0.RELEASE milestone on Feb 28, 2025
  2. martinlippert commented on Feb 28, 2025

    @martinlippert
    MemberAuthor

    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...

  3. BoykoAlex commented on Feb 28, 2025

    @BoykoAlex
    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...

  4. martinlippert commented on Feb 28, 2025

    @martinlippert
    MemberAuthor

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

  5. BoykoAlex commented on Feb 28, 2025

    @BoykoAlex
    Contributor

    Generated unique name for the field: b0d42d1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

for: vscodetheme: code completiontype: enhancement

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions