I am really confused by all the opinions I find in books and even here so bear with me please!
It is stated in Clay Richardson's Professional Java JDK 6 that
you will notice that MyBean does not implement Serializable. XMLEncoder/Decoder does not require classes it serializes to implement Serializable.
and that
the XMLEncoder/Decoder API serializes object instances without any knowledge of their private data members. It serializes based upon the object’s methods, its JavaBean properties, exposed through the JavaBeans convention > of getters and setters (getXXX and setXXX).
Doesn't that mean that XMLEncoder can serialize non-serializable objects such as a javafx.scence.color if it is a field inside a JavaBean ? Well I tried to create a JavaBean and followed all the stated conventions about the naming and the default no-arg constructor as follows
import javafx.geometry.Point2D;
import javafx.scene.paint.Color;
import java.io.Serializable;
import java.util.Map;
public class PCircle extends Shape implements Serializable {
private Point2D position;
private Map<String, Double> properties;
private Color backColor;
private Color strokeColor;
public void setPosition(Point2D position) { this.position = position; }
public Point2D getPosition() { return this.position; }
public void setProperties(Map<String, Double> properties) { this.properties = properties; }
public Map<String, Double> getProperties() { return this.properties; }
public void setBackColor(Color color) { this.backColor = color; }
public Color getBackColor() { return this.backColor; }
public void setStrokeColor(Color color) { this.strokeColor = color; }
public Color getStrokeColor() { return this.strokeColor; }
}
where Shape is an abstract class implementing IShape interface as shown below
import javafx.geometry.Point2D;
import javafx.scene.paint.Color;
import java.util.Map;
public interface IShape {
void setPosition(Point2D position);
Point2D getPosition();
void setProperties(Map<String, Double> properties);
Map<String, Double> getProperties();
void setBackColor(Color color);
Color getBackColor();
void setStrokeColor(Color color);
Color getStrokeColor();
}
public abstract class Shape implements IShape {
abstract public void setPosition(Point2D position);
abstract public Point2D getPosition();
abstract public void setProperties(Map<String, Double> properties);
abstract public Map<String, Double> getProperties();
abstract public void setBackColor(Color color);
abstract public Color getBackColor();
abstract public void setStrokeColor(Color color);
abstract public Color getStrokeColor();
}
now if I run this main method to test
import javafx.geometry.Point2D;
import javafx.scene.paint.Color;
import java.beans.XMLEncoder;
import java.io.FileOutputStream;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<String, Double> properties = new HashMap<>();
properties.put("First Property", 20.5);
PCircle circle = new PCircle();
circle.setPosition(new Point2D(20, 30));
circle.setProperties(properties);
circle.setBackColor(Color.BLACK);
circle.setStrokeColor(Color.RED);
try {
FileOutputStream fos = new FileOutputStream("output.xml");
XMLEncoder encoder = new XMLEncoder(fos);
encoder.writeObject(circle);
encoder.close();
fos.close();
} catch (Exception ex) {
System.out.println("Exception! " + ex.getMessage());
}
}
}
I get this error
java.lang.InstantiationException: javafx.scene.paint.Color
Continuing ...
java.lang.RuntimeException: failed to evaluate: <unbound>=Class.new();
Continuing ...
java.lang.InstantiationException: javafx.scene.paint.Color
Continuing ...
java.lang.RuntimeException: failed to evaluate: <unbound>=Class.new();
Continuing ...
java.lang.InstantiationException: javafx.scene.paint.Color
Continuing ...
java.lang.RuntimeException: failed to evaluate: <unbound>=Class.new();
Continuing ...
java.lang.InstantiationException: javafx.scene.paint.Color
Continuing ...
java.lang.RuntimeException: failed to evaluate: <unbound>=Class.new();
Continuing ...
The interesting part is that if I use serializable classes such as java.awt.Color, the code works fine and the XML is generated. But how does that even matters even though it is stated clearly that the JavaBean is not required to implement the Serializable interface ?