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

(reflect) Object creation without calling constructors or field initialisation

    Details

    • Type: Enhancement
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: 5.0
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:

      Description

      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

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                • Created:
                  Updated:
                  Imported:
                  Indexed: