A DESCRIPTION OF THE REQUEST :
It should be possible to create an instance of an object without calling constructors or field initialisation.
Within Sun's classes this is already possible by using:
ReflectionFactory#newConstructorForSerialization()
This method or a similar should also be added to the J2SE standard so it's reliable to be used for all JDKs of all vendors.
JUSTIFICATION :
Persistence systems need a way to bypass calling constructors and field initialisation so they can *always* create an instance of any object without risking side effects from constructor code.
We would need this for db4o:
http://www.db4o.com
I have talked to maintainers of other persistence projects (for example: Klaus Wuestefeld / Prevayler) and apparently every persistence system would require this functionality.
JDO could also benefit.
CUSTOMER SUBMITTED WORKAROUND :
Below is code that works on Sun's 1.4 and 1.5 JDK.
import java.lang.reflect.*;
import sun.reflect.*;
public class NoConst {
private NoConst(){
throw new RuntimeException("No we never want this object.");
}
public static void main(String[] args) throws Exception{
Constructor oc = Object.class.getDeclaredConstructor(new Class[0]);
ReflectionFactory factory = ReflectionFactory.getReflectionFactory();
Class clazz = NoConst.class;
Constructor c = factory.newConstructorForSerialization(clazz,oc);
NoConst ncc = (NoConst) c.newInstance(null);
System.out.println(ncc);
}
public String toString(){
return "This is a NoConstructorCalled object";
}
}
###@###.### 2005-1-24 12:24:35 GMT