Issue Details (XML | Word | Printable)

Key: NUCENHANCER-7
Type: Bug Bug
Status: Open Open
Priority: Major Major
Assignee: Unassigned
Reporter: Erik Bengtson
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
DataNucleus Enhancer

Persistent class with methods writeObject and writeReplace should be modified by Enhancer an call jdoPreSerialize + add detachedState

Created: 06/May/07 02:23 PM   Updated: 06/Jun/09 10:30 AM
Component/s: ASM Enhancer
Affects Version/s: None
Fix Version/s: None


 Description  « Hide
package org.jpox.samples.serialize;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.BitSet;
import java.util.Date;

import javax.jdo.spi.Detachable;
import javax.jdo.spi.PersistenceCapable;

public class SerializeSample implements Serializable
{
    private static final long serialVersionUID = 2765740961462495537L;

    private String name;
    
    private long age;
    
    private Date birthdate;

    private String zzzz;

    private String zzzzzz;
    
    private transient String password;

    private void writeObject(ObjectOutputStream stream) throws IOException {
        //do jdoPreSerialize() here
        try
        {
            stream.writeObject(getClass().getDeclaredField("jdoDetachedState").get(this));
        }
        catch (Exception e)
        {
        }
        stream.defaultWriteObject();
      }

      private void readObject(ObjectInputStream stream) throws IOException,
          ClassNotFoundException {
          //do jdoPreSerialize() here
            try
            {
                Object value = stream.readObject();
                getClass().getDeclaredField("jdoDetachedState").set(this,value);
            }
            catch (Exception e)
            {
            }
            stream.defaultReadObject();
      }
    /**
     * @param args
     */
    public static void main(String[] args) throws Exception
    {
        if( args.length < 1)
        {
            System.out.println("Must have one arg (out or in).");
        }
        else if( args[0].equalsIgnoreCase("out"))
        {
            testOut();
        }
        else if( args[0].equalsIgnoreCase("in"))
        {
            testIn();
        }
    }

    public static void testOut() throws Exception
    {
        if( !(Detachable.class.isAssignableFrom(SerializeSample.class)))
        {
            System.err.println("Expected a Detachable class");
        }
        SerializeSample s = new SerializeSample();
        BitSet set = new BitSet();
        set.set(0);
        set.set(1);
        set.set(2);
        set.set(3);
        s.getClass().getDeclaredField("jdoDetachedState").set(s,new Object[]{set,set,set,set});
        s.name ="Erik";
        s.age = 10;
        s.zzzz ="Sleeping";
        ByteArrayOutputStream buf = new ByteArrayOutputStream();
        File file =new File("test.out");
        file.createNewFile();
        ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(file));
        o.writeObject(s);
        o.close();
    }

    public static void testIn() throws Exception
    {
        if( Detachable.class.isAssignableFrom(SerializeSample.class))
        {
            System.out.println("Class is Detachable and detached state should be printed.");
        }
        else
        {
            System.out.println("Class is not Detachable.");
        }
        File file =new File("test.out");
        ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
        SerializeSample s = (SerializeSample) in.readObject();
        System.out.println(s.name);
        System.out.println(s.age);
        System.out.println(s.zzzz);
        if( Detachable.class.isAssignableFrom(SerializeSample.class))
        {
            System.out.println(s.getClass().getDeclaredField("jdoDetachedState").get(s));
        }
    }

}


Sort Order: Ascending order - Click to sort in descending order
Andy Jefferson added a comment - 02/Jul/07 06:26 AM
Moved to next release since no comment on the status of this