Pitfalls of Java Property Copy Utilities and Safer Alternatives
This article examines the drawbacks of using Java property copy utilities such as Spring BeanUtils, CGLIB BeanCopier, and Apache Commons BeanUtils, demonstrates type‑conversion errors through code examples, compares their performance, and recommends defining explicit conversion classes or using MapStruct for safer, compile‑time‑checked mappings.
Background : The author previously warned against using generic property‑copy tools and recommended defining explicit conversion classes, possibly assisted by IDE plugins, to avoid hidden runtime issues.
Example 1 – Spring BeanUtils : Using org.springframework.beans.BeanUtils.copyProperties to copy an instance of class A (with List<Integer> ) to class B (with List<String> ) causes a ClassCastException at runtime because the generic types are erased.
import lombok.Data;
import java.util.List;
@Data
public class A {
private String name;
private List
ids;
}
@Data
public class B {
private String name;
private List
ids;
}
public class BeanUtilDemo {
public static void main(String[] args) {
A first = new A();
first.setName("demo");
first.setIds(Arrays.asList(1, 2, 3));
B second = new B();
BeanUtils.copyProperties(first, second);
for (String each : second.getIds()) { // throws ClassCastException
System.out.println(each);
}
}
}Example 2 – CGLIB BeanCopier : The same type‑mismatch problem appears when using org.easymock.cglib.beans.BeanCopier without a custom converter.
import org.easymock.cglib.beans.BeanCopier;
public class BeanUtilDemo {
public static void main(String[] args) {
A first = new A();
first.setName("demo");
first.setIds(Arrays.asList(1, 2, 3));
B second = new B();
BeanCopier beanCopier = BeanCopier.create(A.class, B.class, false);
beanCopier.copy(first, second, null);
for (String each : second.getIds()) { // throws ClassCastException
System.out.println(each);
}
}
}Example 3 – MapStruct : Using MapStruct generates a mapper implementation that automatically converts List<Integer> to List<String> , avoiding the runtime exception.
@Mapper
public interface Converter {
Converter INSTANCE = Mappers.getMapper(Converter.class);
B aToB(A source);
}
public class BeanUtilDemo {
public static void main(String[] args) {
A first = new A();
first.setName("demo");
first.setIds(Arrays.asList(1, 2, 3));
B second = Converter.INSTANCE.aToB(first);
for (String each : second.getIds()) { // works correctly
System.out.println(each);
}
}
}The generated ConverterImpl shows how MapStruct reads generic types at compile time and inserts conversion code, which can be helpful but also hides type mismatches.
Conclusion : Because Java generics are erased at runtime, many property‑copy tools silently copy incompatible collections, leading to subtle bugs. MapStruct can catch some issues at compile time but may still perform unwanted conversions. The safest practice is to write explicit conversion logic or use IDE‑generated getters/setters, which provide compile‑time type safety and high performance.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.