summaryrefslogtreecommitdiff
path: root/tutorial/examples/serialize
diff options
context:
space:
mode:
authorKenny Ballou <kballou@devnulllabs.io>2018-11-19 22:59:50 -0700
committerKenny Ballou <kballou@devnulllabs.io>2018-11-19 22:59:50 -0700
commitea3e1b949dcbdc09518f17eee0bcf21d41d76896 (patch)
tree7ec7a7fb4df67815a9b7bb0e4d95d67c4050e2a2 /tutorial/examples/serialize
downloadopenjava-ea3e1b949dcbdc09518f17eee0bcf21d41d76896.tar.gz
openjava-ea3e1b949dcbdc09518f17eee0bcf21d41d76896.tar.xz
OJ (aka OpenJava) modernization/mirroring
Signed-off-by: Kenny Ballou <kballou@devnulllabs.io>
Diffstat (limited to 'tutorial/examples/serialize')
-rw-r--r--tutorial/examples/serialize/BIND5
-rw-r--r--tutorial/examples/serialize/Color.oj44
-rw-r--r--tutorial/examples/serialize/Colored3DPoint.oj19
-rw-r--r--tutorial/examples/serialize/ColoredPoint.oj28
-rw-r--r--tutorial/examples/serialize/Marshalable.java9
-rw-r--r--tutorial/examples/serialize/OIOTest.java56
-rw-r--r--tutorial/examples/serialize/ObjectIn.java34
-rw-r--r--tutorial/examples/serialize/ObjectOut.java36
-rw-r--r--tutorial/examples/serialize/Point.oj29
-rw-r--r--tutorial/examples/serialize/SerializableClass.oj121
10 files changed, 381 insertions, 0 deletions
diff --git a/tutorial/examples/serialize/BIND b/tutorial/examples/serialize/BIND
new file mode 100644
index 0000000..7a57297
--- /dev/null
+++ b/tutorial/examples/serialize/BIND
@@ -0,0 +1,5 @@
+examples.serialize.SerializableClass examples.serialize.Point
+examples.serialize.SerializableClass examples.serialize.Color
+examples.serialize.SerializableClass examples.serialize.ColoredPoint
+examples.serialize.SerializableClass examples.serialize.Colored3DPoint
+examples.serialize.SerializableClass examples.serialize.*
diff --git a/tutorial/examples/serialize/Color.oj b/tutorial/examples/serialize/Color.oj
new file mode 100644
index 0000000..dbce37f
--- /dev/null
+++ b/tutorial/examples/serialize/Color.oj
@@ -0,0 +1,44 @@
+package examples.serialize;
+
+import java.io.*;
+
+public class Color
+{
+ public Color(byte r, byte g, byte b) {
+ this.r = r; this.g = g; this.b = b;
+ }
+ byte r;
+ byte g;
+ byte b;
+}
+
+/*
+public class Color implements Marshalable
+{
+ byte r;
+ byte g;
+ byte b;
+
+ public Color(byte r, byte g, byte b) {
+ this.r = r; this.g = g; this.b = b;
+ }
+
+ public Color() {
+ }
+
+ public void readObject(ObjectIn is) throws IOException {
+ r = is.readByte();
+ g = is.readByte();
+ b = is.readByte();
+ }
+
+ public void writeObject(ObjectOut os) throws IOException {
+ os.writeUTF("Color");
+ os.writeByte(r);
+ os.writeByte(g);
+ os.writeByte(b);
+ }
+
+}
+*/
+
diff --git a/tutorial/examples/serialize/Colored3DPoint.oj b/tutorial/examples/serialize/Colored3DPoint.oj
new file mode 100644
index 0000000..54c0db0
--- /dev/null
+++ b/tutorial/examples/serialize/Colored3DPoint.oj
@@ -0,0 +1,19 @@
+package examples.serialize;
+
+public class Colored3DPoint
+{
+ public Color color = new Color((byte) 0, (byte) 0, (byte) 0);
+ public int x;
+ public int y;
+ public int z;
+}
+
+/*
+public class Colored3DPoint
+{
+ public Color color = new Color((byte) 0, (byte) 0, (byte) 0);
+ private int x;
+ private int y;
+ private int z;
+}
+*/
diff --git a/tutorial/examples/serialize/ColoredPoint.oj b/tutorial/examples/serialize/ColoredPoint.oj
new file mode 100644
index 0000000..30e534c
--- /dev/null
+++ b/tutorial/examples/serialize/ColoredPoint.oj
@@ -0,0 +1,28 @@
+package examples.serialize;
+
+import java.io.*;
+
+public class ColoredPoint
+{
+ public Color color = new Color((byte) 0, (byte) 0, (byte) 0);
+ public Point point = new Point();
+}
+
+/*
+public class ColoredPoint implements Marshalable
+{
+ public Color color = new Color((byte) 1, (byte) 3, (byte) 5);
+ public Point point = new Point();
+
+ public void writeObject(ObjectOut os) throws IOException {
+ os.writeUTF("ColoredPoint");
+ os.writeObject(color);
+ os.writeObject(point);
+ }
+
+ public void readObject(ObjectIn is) throws IOException {
+ color = (Color) is.readObject();
+ point = (Point) is.readObject();
+ }
+}
+*/
diff --git a/tutorial/examples/serialize/Marshalable.java b/tutorial/examples/serialize/Marshalable.java
new file mode 100644
index 0000000..5924c4b
--- /dev/null
+++ b/tutorial/examples/serialize/Marshalable.java
@@ -0,0 +1,9 @@
+package examples.serialize;
+
+import java.io.*;
+
+public interface Marshalable
+{
+ void readObject(ObjectIn is) throws IOException;
+ void writeObject(ObjectOut os) throws IOException;
+}
diff --git a/tutorial/examples/serialize/OIOTest.java b/tutorial/examples/serialize/OIOTest.java
new file mode 100644
index 0000000..717707f
--- /dev/null
+++ b/tutorial/examples/serialize/OIOTest.java
@@ -0,0 +1,56 @@
+package examples.serialize;
+
+import javax.microedition.midlet.MIDlet;
+import java.io.*;
+
+public class OIOTest extends MIDlet
+{
+
+ public void startApp() {
+ try {
+ main();
+ } catch (IOException ex) {
+ ex.printStackTrace();
+ }
+ }
+
+ public static void main() throws IOException {
+ ColoredPoint p = new ColoredPoint();
+ p.point.x = 10; p.point.y = 20;
+
+ /* Writes objects */
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ ObjectOut oos = new ObjectOut(baos);
+
+ oos.writeObject(p);
+ p.color.g = (byte)2;
+ oos.writeObject(p);
+ oos.flush();
+
+ byte[] buf = baos.toByteArray();
+
+ /* Reads objects */
+ ByteArrayInputStream bais = new ByteArrayInputStream(buf);
+ ObjectIn ois = new ObjectIn(bais);
+
+ ColoredPoint p2 = (ColoredPoint) ois.readObject();
+ ColoredPoint p3 = (ColoredPoint) ois.readObject();
+
+ System.out.println("p2.color.r = " + p2.color.r);
+ System.out.println("p2.color.g = " + p2.color.g);
+ System.out.println("p2.color.b = " + p2.color.b);
+ System.out.println("p2.point.x = " + p2.point.x);
+ System.out.println("p2.point.y = " + p2.point.y);
+ System.out.println("p3.color.r = " + p3.color.r);
+ System.out.println("p3.color.g = " + p3.color.g);
+ System.out.println("p3.color.b = " + p3.color.b);
+
+ }
+
+ public void pauseApp() {
+ }
+
+ public void destroyApp(boolean unconditional) {
+ }
+
+}
diff --git a/tutorial/examples/serialize/ObjectIn.java b/tutorial/examples/serialize/ObjectIn.java
new file mode 100644
index 0000000..4a3e1f1
--- /dev/null
+++ b/tutorial/examples/serialize/ObjectIn.java
@@ -0,0 +1,34 @@
+package examples.serialize;
+
+import java.io.*;
+import java.util.*;
+
+public class ObjectIn extends DataInputStream
+{
+ private Vector readObjects = new Vector();
+
+ public ObjectIn(InputStream is) {
+ super(is);
+ }
+
+ public Marshalable readObject() throws IOException {
+ int id = readInt();
+ if (id == -1) return null; /* -1 implies null */
+ if (id == -2) {
+ id = readInt();
+ return (Marshalable) readObjects.elementAt(id);
+ }
+
+ String classname = readUTF();
+ Marshalable obj;
+ try {
+ obj = (Marshalable) Class.forName(classname).newInstance();
+ } catch (Exception ex) {
+ throw new IOException(ex.toString());
+ }
+ readObjects.addElement(obj);
+ obj.readObject(this);
+
+ return obj;
+ }
+}
diff --git a/tutorial/examples/serialize/ObjectOut.java b/tutorial/examples/serialize/ObjectOut.java
new file mode 100644
index 0000000..eb482c2
--- /dev/null
+++ b/tutorial/examples/serialize/ObjectOut.java
@@ -0,0 +1,36 @@
+package examples.serialize;
+
+import java.io.*;
+import java.util.*;
+
+class ObjectOut extends DataOutputStream
+{
+ private Vector writtenObjects = new Vector();
+
+ public ObjectOut(OutputStream os) {
+ super(os);
+ }
+
+ public void writeObject(Marshalable obj) throws IOException {
+ if (obj == null) {
+ writeInt(-1);
+ } else {
+ int id = 0;
+ int size = writtenObjects.size();
+ for (; id < size; ++id) {
+ if (writtenObjects.elementAt(id) == obj) break;
+ }
+ if (id < size) {
+ /* this object is already written to this stream */
+ writeInt(-2);
+ writeInt(id);
+ } else {
+ /* this object is not regisered in the table */
+ writeInt(id);
+ writtenObjects.addElement(obj);
+ obj.writeObject(this);
+ }
+ }
+ }
+
+}
diff --git a/tutorial/examples/serialize/Point.oj b/tutorial/examples/serialize/Point.oj
new file mode 100644
index 0000000..e7db5f9
--- /dev/null
+++ b/tutorial/examples/serialize/Point.oj
@@ -0,0 +1,29 @@
+package examples.serialize;
+
+import java.io.*;
+
+public class Point
+{
+ int x;
+ int y;
+}
+
+/*
+public class Point implements Marshalable
+{
+ int x;
+ int y;
+
+ public void readObject(ObjectIn is) throws IOException {
+ x = is.readInt();
+ y = is.readInt();
+ }
+
+ public void writeObject(ObjectOut os) throws IOException {
+ os.writeUTF("Point");
+ os.writeInt(x);
+ os.writeInt(y);
+
+ }
+}
+*/
diff --git a/tutorial/examples/serialize/SerializableClass.oj b/tutorial/examples/serialize/SerializableClass.oj
new file mode 100644
index 0000000..b054a78
--- /dev/null
+++ b/tutorial/examples/serialize/SerializableClass.oj
@@ -0,0 +1,121 @@
+package examples.serialize;
+
+import openjava.mop.*;
+import openjava.ptree.*;
+import openjava.syntax.*;
+
+
+/**
+ * The class <code>CapsuleClass</code> is a metaclass which
+ * adds methods for serialization of the instances of the class.
+ */
+public class SerializableClass instantiates Metaclass extends OJClass
+{
+ /** Overrides to translate definition of the class */
+ public void translateDefinition() throws MOPException {
+ OJClass baseClass = getSuperclass();
+
+ OJField[] fields = getDeclaredFields();
+
+ addReaderMethod(fields);
+ addWriterMethod(fields);
+ }
+
+ convenient void addReaderMethod(OJField[] fieldsToWrite)
+ throws MOPException
+ {
+ OJClass declaringClass = this;
+ OJModifier modifier = OJModifier.constantEmpty().setPublic();
+ OJClass returnType = OJSystem.VOID;
+ String methodName = "readObject";
+ OJClass[] paramTypes = new OJClass[]{ObjectIn.class};
+ String[] paramNames = new String[]{"in"};
+ OJClass[] exepTypes = new OJClass[]{java.io.IOException.class};
+ OJMethod reader
+ = new OJMethod(this, modifier, returnType, methodName,
+ paramTypes, paramNames, exepTypes, null);
+ addMethod(reader);
+
+ Environment env = reader.getEnvironment();
+ StatementList body = new StatementList();
+ for (int i = 0; i < fieldsToWrite.length; ++i) {
+ body.add(createReadExprFor(env, fieldsToWrite[i]));
+ }
+ reader.setBody(body);
+ }
+
+ Statement createReadExprFor(Environment env, OJField field)
+ throws MOPException
+ {
+ OJClass type = field.getType();
+ String name = field.getName();
+ if (type == OJSystem.STRING) {
+ return makeStatement(env, name + " = in.readUTF();");
+ } else if (type == OJSystem.BYTE) {
+ return makeStatement(env, name + " = in.readByte();");
+ } else if (type == OJSystem.BOOLEAN) {
+ return makeStatement(env, name + " = in.readBoolean();");
+ } else if (type == OJSystem.CHAR) {
+ return makeStatement(env, name + " = in.readChar();");
+ } else if (type == OJSystem.INT) {
+ return makeStatement(env, name + " = in.readInt();");
+ } else if (type == OJSystem.LONG) {
+ return makeStatement(env, name + " = in.readLong();");
+ } else if (type.isArray()) {
+ return null; /* not implemented yet. */
+ } else if (! type.isPrimitive()) {
+ return makeStatement(env, name + " = in.readObject();");
+ }
+ return null;
+ }
+
+ convenient void addWriterMethod(OJField[] fieldsToWrite)
+ throws MOPException
+ {
+ OJClass declaringClass = this;
+ OJModifier modifier = OJModifier.constantEmpty().setPublic();
+ OJClass returnType = OJSystem.VOID;
+ String methodName = "writeObject";
+ OJClass[] paramTypes = new OJClass[]{ObjectOut.class};
+ String[] paramNames = new String[]{"out"};
+ OJClass[] exepTypes = new OJClass[]{java.io.IOException.class};
+ OJMethod writer
+ = new OJMethod(this, modifier, returnType, methodName,
+ paramTypes, paramNames, exepTypes, null);
+ addMethod(writer);
+
+ Environment env = writer.getEnvironment();
+ StatementList body = new StatementList();
+ body.add(makeStatement(env, "out.writeUTF(\"" + getName() + "\");"));
+ for (int i = 0; i < fieldsToWrite.length; ++i) {
+ body.add(createWriteExprFor(env, fieldsToWrite[i]));
+ }
+ writer.setBody(body);
+ }
+
+ Statement createWriteExprFor(Environment env, OJField field)
+ throws MOPException
+ {
+ OJClass type = field.getType();
+ String name = field.getName();
+ if (type == OJSystem.STRING) {
+ return makeStatement(env, "out.writeUTF(" + name + ");");
+ } else if (type == OJSystem.BYTE) {
+ return makeStatement(env, "out.writeByte(" + name + ");");
+ } else if (type == OJSystem.BOOLEAN) {
+ return makeStatement(env, "out.writeBoolean(" + name + ");");
+ } else if (type == OJSystem.CHAR) {
+ return makeStatement(env, "out.writeChar(" + name + ");");
+ } else if (type == OJSystem.INT) {
+ return makeStatement(env, "out.writeInt(" + name + ");");
+ } else if (type == OJSystem.LONG) {
+ return makeStatement(env, "out.writeLong(" + name + ");");
+ } else if (type.isArray()) {
+ return null; /* not implemented yet. */
+ } else if (! type.isPrimitive()) {
+ return makeStatement(env, "out.writeObject(" + name + ");");
+ }
+ return null;
+ }
+
+}