Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6220682

(reflect) Object creation without calling constructors or field initialisation

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Won't Fix
    • Icon: P4 P4
    • None
    • 5.0
    • core-libs

      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

            darcy Joe Darcy
            jssunw Jitender S (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: