Despite that manual implementation of the Externalizable interface gives you complete control in reading and writing objects during Java serialization, it also requires attentiveness and additional work in support.
However if used correctly it can help to improve serialization process performance.
The plugin allows to generate externalizable code for Java classes. Easy and safe.
You may install the plugin following official guide or download it from JetBrains Plugin Repository.
After installation you can find Externalizable
action in the Generate menu (Alt + Ins
, by default).
For example our class is:
public class Example {
private int intValue;
private Integer boxedInteger;
private Object object;
/*...*/
}
The plugin will generate following code:
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
public class Example implements Externalizable {
private int intValue;
private Integer boxedInteger;
private Object object;
public Example() {
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
intValue = in.readInt();
if (in.readBoolean()) {
boxedInteger = in.readInt();
}
if (in.readBoolean()) {
object = in.readObject();
}
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeInt(intValue);
if (boxedInteger == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
out.writeInt(boxedInteger);
}
if (object == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
out.writeObject(object);
}
}
}
...to be described