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.

Resolve POJO constructor arguments by name rather than position

See original GitHub issue

The docs say:

In order to inject the results into the constructor, MyBatis needs to identify the constructor by the type of its parameters. Java has no way to introspect (or reflect) on parameter names. So when creating a constructor element, ensure that the arguments are in order, and that the data types are specified.

But actually there are few ways commonly used to get at the constructor’s parameter names. Spring can wire up immutable beans this way, for example. There’s compiling with debug info (very common nowadays), the older @ConstructorProperties annotation, explicit annotations on each constructor parameter (e.g. MyBatis’s own @Param), and in JDK8 the compilation option -parameters.

It’s a huge advantage, of course, when dealing with immutables to be able to match constructor parameters to fields not positionally, which is quite fragile, but by name.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
harawatacommented, Jan 26, 2017

I have committed the changes. You can specify the parameter name in name attribute of @Arg annotation or <idArg /> and <arg /> elements.

<constructor>
  <arg column="col2" name="secondArg" javaType="string" />
  <arg column="col1" name="firstArg" javaType="int" />
</constructor>
@ConstructorArgs({
  @Arg(column = "col2", name = "secondArg", javaType = String.class),
  @Arg(column = "col1", name = "firstArg", javaType = Integer.class)
})
  • To reference constructor parameters by name, you can either 1) annotate parameters with @Param annotation or 2) compile with -parameters option.
  • javaType can be omitted if there is a property with the same name and type.
  • When MyBatis could not find a matching constructor, it throws an exception during build-phase.

@usethe4ce , @reddyalready It would be great if you guys could try the latest 3.4.3-SNAPSHOT.

1reaction
reddyalreadycommented, Jul 13, 2017

@harawata Just wanted to thank you again for doing this, I finally got to play around with it - using Kotlin/immutability no less, and it works like a charm 😄

I believe it is not possible yet to create complex immutable objects (resulting from joins, for example) using constructor args, e.g.:

User {
  id: Int = 1,
  address: Address = {
    houseNum: Int = 1,
    line1: String = "Blah Blah"
  }
}

I believe this is related to #101

Can you please confirm?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Enforce Order of Arguments in Constructor [Java]
I'm writing code that reads values from props file and attempts to automatically create corresponding POJO instance.
Read more >
Object orientation - The Apache Groovy programming language
Constructors are special methods used to initialize an object with a specific state. As with normal methods, it is possible for a class...
Read more >
Constructor Dependency Injection in Spring - Baeldung
Quick and practical intro to Constructor based injection with Spring. ... to resolve ambiguity (for example if a constructor takes multiple arguments of...
Read more >
Named and Optional Arguments - C# Programming Guide
Named arguments enable you to specify an argument for a parameter by matching the argument with its name rather than with its position...
Read more >
Chapter 5. The Rule Language - JBoss.org
Mostly punctuation is not needed, even the double quotes for "name" are optional, ... Generate constructors with parameters for declared types.
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