Details
-
Type: Improvement
-
Status: Closed
-
Resolution: Fixed
-
Affects Version/s: None
-
Fix Version/s: 1.1
-
Component/s: None
-
Labels:None
Description
I like to keep the access level of all methods/constructors down to the minimum possible, but XStream forced to put public constructors on all the objects I wanted to serialize.
Is there any reason why getDeclaredConstructors and setAccessible isn't used instead of type.newInstance().
The only problem that I can see is a slight speed impact.
XStream stream = new XStream(new PureJavaReflectionProvider() {
public Object newInstance(Class type) {
try {
Constructor[] constructors = type.getDeclaredConstructors();
for(int i=0; i < constructors.length; i++) {
if(constructors[i].getParameterTypes().length == 0) {
if(!Modifier.isPublic(constructors[i].getModifiers()))
}
throw new ObjectAccessException("Cannot construct " + type.getName());
} catch (InstantiationException e)
catch (InvocationTargetException e)
{ throw new ObjectAccessException("Cannot construct " + type.getName(), e); } }
});