admin管理员组文章数量:1336357
I am currently using Java 17
along with ModelMapper
to convert objects from one type to another.
I came across a confusing scenario where I assign the result of a method that returns a List<Test>
to a new ArrayList<Boolean>
.
Later, I attempt to add a boolean
value to this list, and it surprisingly compiles and gives an output, which I'm struggling to understand.
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import .modelmapper.ModelMapper;
import com.google.gson.reflect.TypeToken;
public final class ModelMapperAdapter {
private static ModelMapper MODEL_MAPPER;
public static class Test{
Integer var;
//getter
//setter
}
public static void main(String[] args) {
MODEL_MAPPER = new ModelMapper();
Test test = new Test();
List<Test> list = new ArrayList<>();
List<Boolean> ids = new ArrayList<>();
list.add(test);
ids.addAll(MODEL_MAPPER.map(list, new TypeToken<List<Test>>(){}.getType()));
System.out.println(ids);//[ModelMapperAdapter.Test(var=null)]
ids.add(true);
System.out.println(ids);//[ModelMapperAdapter.Test(var=null), true]
}
}
I am currently using Java 17
along with ModelMapper
to convert objects from one type to another.
I came across a confusing scenario where I assign the result of a method that returns a List<Test>
to a new ArrayList<Boolean>
.
Later, I attempt to add a boolean
value to this list, and it surprisingly compiles and gives an output, which I'm struggling to understand.
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import .modelmapper.ModelMapper;
import com.google.gson.reflect.TypeToken;
public final class ModelMapperAdapter {
private static ModelMapper MODEL_MAPPER;
public static class Test{
Integer var;
//getter
//setter
}
public static void main(String[] args) {
MODEL_MAPPER = new ModelMapper();
Test test = new Test();
List<Test> list = new ArrayList<>();
List<Boolean> ids = new ArrayList<>();
list.add(test);
ids.addAll(MODEL_MAPPER.map(list, new TypeToken<List<Test>>(){}.getType()));
System.out.println(ids);//[ModelMapperAdapter.Test(var=null)]
ids.add(true);
System.out.println(ids);//[ModelMapperAdapter.Test(var=null), true]
}
}
Share
edited Nov 28, 2024 at 20:54
Marce Puente
6383 silver badges15 bronze badges
asked Nov 28, 2024 at 17:48
Piyush MaheshwariPiyush Maheshwari
441 silver badge7 bronze badges
1 Answer
Reset to default 0The problem is that the ModelMapper.map(Object source, Type destinationType)
method makes promises that it cannot fulfill.
If you look at the complete method signature:
public <D> D map(Object source, Type destinationType)
it pretends that for any source
object and type descriptor destinationType
it will return an instance of D
.
The problem is that D
and the destinationType
don't need to be related.
Due to the way the method signature is written:
- the Java Compiler cannot verify that those two types are related (
Type destinationType
is not generic). If the Java Compiler could verify it the code would not compile. - the
ModelMapper.map()
method has no way of knowing whatD
is and therefore cannot check that you are using the method correctly. If it could verify whatD
is it could throw an exception at runtime.
That means that (when using ModelMapper
) it is your responsability to make sure that D
refers the same type as destinationType
.
Note that this problem (together with performance problems) is the reason I stopped using ModelMapper years ago and replaced it with MapStruct
本文标签: genericsConfusion with Type Mismatch in ModelMapper Code Using Java 17Stack Overflow
版权声明:本文标题:generics - Confusion with Type Mismatch in ModelMapper Code Using Java 17 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742264129a2443067.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论