question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

In case of multiple constructor candidates give preference to the public one

See original GitHub issue

Speaking of immutable dto classes, there are many cases where there are several non-default constructors in it. Current implementation of PreferredConstructorDiscoverer.Discoverers.DEFAULT can’t resolve these cases without @PersistenceConstructor. Usually this is not a big problem, however when using libraries such as Lombok or Immutables this adds additional inconvenience. These libraries can generate additional private constructors for builders contructor(builder) or copy methods copyOf(self)/with*

The idea is not to return null when several non-default constructors are found, but try to find one public among them (if it is the only public one) and return it.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
AbstractCoderXcommented, Apr 28, 2021

Yes, this can solve the problem. In immutables also that should be available with:

@Value.Immutable
@Value.Style(of = "new", allParameters = true)
class Test {

  @PersistenceConstructor
  Test() {
  }

}

Аlthough this is solved with two additional lines of code, it becomes very annoying when you need to turn this in almost every dto. One of the cool features of immutables is that you can make custom annotations with style settings and reuse them, but you can’t put a constructor that is declared explicitly (and not in the style annotation) there. Here are my considerations.

2reactions
mp911decommented, Apr 27, 2021

Thanks for bringing up this topic. Let me take it to the team so we can discuss your proposal.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why can I create another bean if a @Primary bean exists?
@Primary just tells Spring which bean to give precedence to if there are two or more possible candidates. You can always be explicit....
Read more >
Choosing from multiple beans in the context - Manning
In this case, Spring selects the primary bean for injection. You can explicitly select a specific bean using the @Qualifier annotation. If none ......
Read more >
Spring @Qualifier Annotation | Baeldung
This annotation defines a preference when multiple beans of the same type are present. The bean associated with the @Primary annotation will be ......
Read more >
Are Multiple Constructors possible in Java? - Tutorialspoint
There can be multiple constructors in a class. However, the parameter list of the constructors should not be same.
Read more >
Removing repetitive code (overloading methods and ...
Creating multiple methods with the same name in a class. ... is in fact a special case of the second constructor - the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found