MaintainJ Blog

May 10, 2012

The tricks of Java serialization

Filed under: Uncategorized — maintainj @ 4:59 pm

Whenever I start looking deeper into Java’s serialization mechanism, I seem to learn something new. Here is something I learned yesterday. Read the code snippet below and try to guess the output.

import java.io.*;

class DataHolder implements Serializable{
String data = “data”;
}
public class SerializationTester {
static String serFile = “test.ser”;
static void write() throws Exception{
//Create an object and write to ObjectOutputStream
DataHolder dh = new DataHolder();
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(serFile));
oos.writeObject(dh);

//Change the state and write to the same stream again
dh.data = “New data”;
oos.writeObject(dh);
oos.close();
}
static void read() throws Exception{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(serFile));
DataHolder dh1 = (DataHolder)ois.readObject();
DataHolder dh2 = (DataHolder)ois.readObject();
ois.close();
System.out.println(dh1.data + “–” + dh2.data);
}
public static void main(String[] args) throws Exception{
write();
read();
}
}

What do you think will be the output? It’s ‘data–data’. The new value set to the DataHolder.data field is not written to the stream. To make the Java serialization mechanism write the new value to the stream, we have to reset the stream by calling ObjectOutputStream.stream().

Check this article on Serialization to understand what’s going on. The part that’s of interest to us is below:

—–

… consider the situation in which an object is written to a stream and then written again later. By default, an ObjectOutputStream will maintain a reference to an object written to it. That means that if the state of the written object is written and then written again, the new state will not be saved! Here is a code snippet that shows that problem in action:

10 ObjectOutputStream out = new ObjectOutputStream(...);
20 MyObject obj = new MyObject(); // must be Serializable
30 obj.setState(100);
40 out.writeObject(obj); // saves object with state = 100
50 obj.setState(200);
60 out.writeObject(obj); // does not save new object state

There are two ways to control that situation. First, you could make sure to always close the stream after a write call, ensuring the new object is written out each time. Second, you could call the ObjectOutputStream.reset() method, which would tell the stream to release the cache of references it is holding so all new write calls will actually be written. Be careful, though — the reset flushes the entire object cache, so all objects that have been written could be rewritten.

—–

–Choudary Kothapalli.

April 19, 2012

The class xxx exceeds the maximum class size supported by the JVM

Filed under: Uncategorized — maintainj @ 10:31 am

If you get this error when you start your application with MaintainJ, please exclude that class in the aop.xml. Check this FAQ entry on how to edit the aop.xml and exclude a class.

–choudary Kothapalli.

Unable to continue, this version of AspectJ supports classes built with weaver version 6.0 but the class XXX is version 7.0

Filed under: Uncategorized — maintainj @ 10:19 am

You might see this kind of exception when you start your application with MaintainJ. The reason for this exception is nicely described by the AspectJ lead Andy in his blog.

This exception might occur if you are already using AspectJ in your application, which may conflict with the version of AspectJ that MaintainJ is using. Below are the steps to resolve this:

1. Search if you have aspectjrt.jar or aspectjweaver.jar in your application classpath. Remove them if so.

2. MaintainJ already places aspectjweaver.jar at a higher level in the classpath. So, you don’t need it in your application. aspectjweaver.jar has all the classes in aspectjrt.jar and so you can safely remove it from your classpath.

Do those steps and restart your application. If you still have any problems, contact me.

–Choudary Kothapalli.

April 7, 2012

MaintainJ for Android apps

Filed under: Uncategorized — maintainj @ 8:05 pm

MaintainJ support for Android applications is added.

–Choudary Kothapalli.

March 26, 2012

MaintainJ won the Best Modeling Product award at Eclipse Community Awards 2012

Filed under: Uncategorized — maintainj @ 9:02 pm

Great news! MaintainJ won the Best Modeling Product award at Eclipse Community Awards 2012!

Here are the nomination details.

Below is my picture taking the award from Ian Skerrett of Eclipse foundation!

–Choudary Kothapalli.

March 20, 2012

What a relief!

Filed under: Uncategorized — maintainj @ 11:40 pm

This is the first time I am linking another blog on my blog. I just feel so relieved after reading this particular blog. Read about Imposter Syndrome.

Choudary Kothapalli.

Next Page »

Powered by WordPress