MapStruct - How to ignore the unnecessary methods/non-getter-setter methods in the POJO
up vote
0
down vote
favorite
Below is the POJO:
public class TransferObjectListTO {
private List<A> transferList;
public List<A> getTransferList() {
return transferList;
}
public void setList(List<A> transferList) {
this.transferList= transferList;
}
public List<A> getList() {
return getTransferList();
}
public void incrementList(List<A> transferList) {
getTransferList().addAll(transferList);
}
}
It has a kind of adder method -
incrementList
along with duplicate to getter method -
getList
The Mapstruct generated code has below unnecessary stuff, which adds list, one more time of source type:
if ( targetTypeTransferObjectListTO.getList() != null ) {
List sourceTypeList = sourceTypeTransferObjectListTO.getList();
if ( sourceTypeList != null ) {
targetTypeTransferObjectListTO.getList().addAll( sourceTypeList );
}
}
We cannot remove these methods - incrementList() and getList()
in above POJO because it is used in many places. Now, how can we ignore these methods when mapstruct is generating implementation for mapping?
java list mapping pojo mapstruct
add a comment |
up vote
0
down vote
favorite
Below is the POJO:
public class TransferObjectListTO {
private List<A> transferList;
public List<A> getTransferList() {
return transferList;
}
public void setList(List<A> transferList) {
this.transferList= transferList;
}
public List<A> getList() {
return getTransferList();
}
public void incrementList(List<A> transferList) {
getTransferList().addAll(transferList);
}
}
It has a kind of adder method -
incrementList
along with duplicate to getter method -
getList
The Mapstruct generated code has below unnecessary stuff, which adds list, one more time of source type:
if ( targetTypeTransferObjectListTO.getList() != null ) {
List sourceTypeList = sourceTypeTransferObjectListTO.getList();
if ( sourceTypeList != null ) {
targetTypeTransferObjectListTO.getList().addAll( sourceTypeList );
}
}
We cannot remove these methods - incrementList() and getList()
in above POJO because it is used in many places. Now, how can we ignore these methods when mapstruct is generating implementation for mapping?
java list mapping pojo mapstruct
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Below is the POJO:
public class TransferObjectListTO {
private List<A> transferList;
public List<A> getTransferList() {
return transferList;
}
public void setList(List<A> transferList) {
this.transferList= transferList;
}
public List<A> getList() {
return getTransferList();
}
public void incrementList(List<A> transferList) {
getTransferList().addAll(transferList);
}
}
It has a kind of adder method -
incrementList
along with duplicate to getter method -
getList
The Mapstruct generated code has below unnecessary stuff, which adds list, one more time of source type:
if ( targetTypeTransferObjectListTO.getList() != null ) {
List sourceTypeList = sourceTypeTransferObjectListTO.getList();
if ( sourceTypeList != null ) {
targetTypeTransferObjectListTO.getList().addAll( sourceTypeList );
}
}
We cannot remove these methods - incrementList() and getList()
in above POJO because it is used in many places. Now, how can we ignore these methods when mapstruct is generating implementation for mapping?
java list mapping pojo mapstruct
Below is the POJO:
public class TransferObjectListTO {
private List<A> transferList;
public List<A> getTransferList() {
return transferList;
}
public void setList(List<A> transferList) {
this.transferList= transferList;
}
public List<A> getList() {
return getTransferList();
}
public void incrementList(List<A> transferList) {
getTransferList().addAll(transferList);
}
}
It has a kind of adder method -
incrementList
along with duplicate to getter method -
getList
The Mapstruct generated code has below unnecessary stuff, which adds list, one more time of source type:
if ( targetTypeTransferObjectListTO.getList() != null ) {
List sourceTypeList = sourceTypeTransferObjectListTO.getList();
if ( sourceTypeList != null ) {
targetTypeTransferObjectListTO.getList().addAll( sourceTypeList );
}
}
We cannot remove these methods - incrementList() and getList()
in above POJO because it is used in many places. Now, how can we ignore these methods when mapstruct is generating implementation for mapping?
java list mapping pojo mapstruct
java list mapping pojo mapstruct
asked Nov 8 at 13:20
Tushar Ahirrao
4,488155590
4,488155590
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
There are multiple ways that you can achieve what you are looking for. The problem is only with the getList()
method, the incrementList()
is already ignored by MapStruct.
Ignore the mapping
If you don't have a lot of POJOs that have this pattern and you have few mappers that use this you can just add
@Mapping(target = "list", ignore = true)
Write your own AccessorNamingStrategy
In case you have a lot of POJOs and a lot of mappers then I would suggest you to write your own custom AccessorNamingStrategy
that would mark the getList()
method as OTHER
method.
public class CustomAccessorNamingStrategy extends DefaultAccessorNamingStrategy {
@Override
public boolean isGetterMethod(ExecutableElement method) {
if (method.getSimpleName().toString().equals("getList")) {
return false;
} else {
return super.isGetterMethod(method);
}
}
}
Thank you Filip! It worked.
– Tushar Ahirrao
Nov 9 at 9:28
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
There are multiple ways that you can achieve what you are looking for. The problem is only with the getList()
method, the incrementList()
is already ignored by MapStruct.
Ignore the mapping
If you don't have a lot of POJOs that have this pattern and you have few mappers that use this you can just add
@Mapping(target = "list", ignore = true)
Write your own AccessorNamingStrategy
In case you have a lot of POJOs and a lot of mappers then I would suggest you to write your own custom AccessorNamingStrategy
that would mark the getList()
method as OTHER
method.
public class CustomAccessorNamingStrategy extends DefaultAccessorNamingStrategy {
@Override
public boolean isGetterMethod(ExecutableElement method) {
if (method.getSimpleName().toString().equals("getList")) {
return false;
} else {
return super.isGetterMethod(method);
}
}
}
Thank you Filip! It worked.
– Tushar Ahirrao
Nov 9 at 9:28
add a comment |
up vote
1
down vote
accepted
There are multiple ways that you can achieve what you are looking for. The problem is only with the getList()
method, the incrementList()
is already ignored by MapStruct.
Ignore the mapping
If you don't have a lot of POJOs that have this pattern and you have few mappers that use this you can just add
@Mapping(target = "list", ignore = true)
Write your own AccessorNamingStrategy
In case you have a lot of POJOs and a lot of mappers then I would suggest you to write your own custom AccessorNamingStrategy
that would mark the getList()
method as OTHER
method.
public class CustomAccessorNamingStrategy extends DefaultAccessorNamingStrategy {
@Override
public boolean isGetterMethod(ExecutableElement method) {
if (method.getSimpleName().toString().equals("getList")) {
return false;
} else {
return super.isGetterMethod(method);
}
}
}
Thank you Filip! It worked.
– Tushar Ahirrao
Nov 9 at 9:28
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
There are multiple ways that you can achieve what you are looking for. The problem is only with the getList()
method, the incrementList()
is already ignored by MapStruct.
Ignore the mapping
If you don't have a lot of POJOs that have this pattern and you have few mappers that use this you can just add
@Mapping(target = "list", ignore = true)
Write your own AccessorNamingStrategy
In case you have a lot of POJOs and a lot of mappers then I would suggest you to write your own custom AccessorNamingStrategy
that would mark the getList()
method as OTHER
method.
public class CustomAccessorNamingStrategy extends DefaultAccessorNamingStrategy {
@Override
public boolean isGetterMethod(ExecutableElement method) {
if (method.getSimpleName().toString().equals("getList")) {
return false;
} else {
return super.isGetterMethod(method);
}
}
}
There are multiple ways that you can achieve what you are looking for. The problem is only with the getList()
method, the incrementList()
is already ignored by MapStruct.
Ignore the mapping
If you don't have a lot of POJOs that have this pattern and you have few mappers that use this you can just add
@Mapping(target = "list", ignore = true)
Write your own AccessorNamingStrategy
In case you have a lot of POJOs and a lot of mappers then I would suggest you to write your own custom AccessorNamingStrategy
that would mark the getList()
method as OTHER
method.
public class CustomAccessorNamingStrategy extends DefaultAccessorNamingStrategy {
@Override
public boolean isGetterMethod(ExecutableElement method) {
if (method.getSimpleName().toString().equals("getList")) {
return false;
} else {
return super.isGetterMethod(method);
}
}
}
answered Nov 8 at 14:15
Filip
2,81921026
2,81921026
Thank you Filip! It worked.
– Tushar Ahirrao
Nov 9 at 9:28
add a comment |
Thank you Filip! It worked.
– Tushar Ahirrao
Nov 9 at 9:28
Thank you Filip! It worked.
– Tushar Ahirrao
Nov 9 at 9:28
Thank you Filip! It worked.
– Tushar Ahirrao
Nov 9 at 9:28
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53208604%2fmapstruct-how-to-ignore-the-unnecessary-methods-non-getter-setter-methods-in-t%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown