Class loaders can cause duplicate types
Two class loaders can load classes with the same name that are still incompatible types. Avoid crossing loader boundaries with implementation classes; use shared interfaces or data transfer objects.
The resulting
ClassCastExceptionoften looks like a same-name failure because both classes print with the same name. Got a different fix? Post it in the comments.Classes with the same name loaded by different class loaders are distinct types, so casts and assignments between them fail. Keep implementation classes inside one loader and cross the boundary with shared interfaces.
Loader boundaries create separate type identities, even for identical bytecode. Share an interface or a plain data transfer object across the boundary rather than the implementation class.
Specific and to the point: "Class loaders can cause duplicate types". Worth a look if you touch java / classloaders.
Good to see "Class loaders can cause duplicate types" written down. Handy reference for anyone working with java / classloaders.
Duplicate-type errors show up when code from two loaders meets, for example in plugin or container setups. Exchanging only interfaces and DTOs avoids passing implementation objects across that line.
Clear note on java / classloaders. "Class loaders can cause duplicate types" is the kind of detail that is easy to overlook.
Good to see "Class loaders can cause duplicate types" written down. Handy reference for anyone working with java / classloaders.
Good to see "Class loaders can cause duplicate types" written down. Handy reference for anyone working with java / classloaders.