THRIFT-409. java: Add "union" to Thrift
This patch introduces new IDL syntax for creating Unions, explicityly single-valued structs. While the parser changes are portable, this patch only includes the actual generated code changes for the Java library. Other libraries can continue to generate a struct with the same fields and remain compatible until they are able to implement the full shebang.
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@810300 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/java/build.xml b/lib/java/build.xml
index de9b018..dbbaf6b 100644
--- a/lib/java/build.xml
+++ b/lib/java/build.xml
@@ -180,6 +180,8 @@
classpathref="test.classpath" failonerror="true" />
<java classname="org.apache.thrift.test.JavaBeansTest"
classpathref="test.classpath" failonerror="true" />
+ <java classname="org.apache.thrift.test.UnionTest"
+ classpathref="test.classpath" failonerror="true" />
</target>
<target name="generate">
diff --git a/lib/java/src/org/apache/thrift/TBase.java b/lib/java/src/org/apache/thrift/TBase.java
index 7c8978a..3c3b12f 100644
--- a/lib/java/src/org/apache/thrift/TBase.java
+++ b/lib/java/src/org/apache/thrift/TBase.java
@@ -19,13 +19,15 @@
package org.apache.thrift;
+import java.io.Serializable;
+
import org.apache.thrift.protocol.TProtocol;
/**
* Generic base interface for generated Thrift objects.
*
*/
-public interface TBase extends Cloneable {
+public interface TBase extends Serializable {
/**
* Reads the TObject from the given input protocol.
@@ -63,4 +65,6 @@
* @param fieldId The field's id tag as found in the IDL.
*/
public void setFieldValue(int fieldId, Object value);
+
+ public TBase deepCopy();
}
diff --git a/lib/java/src/org/apache/thrift/TUnion.java b/lib/java/src/org/apache/thrift/TUnion.java
new file mode 100644
index 0000000..9375475
--- /dev/null
+++ b/lib/java/src/org/apache/thrift/TUnion.java
@@ -0,0 +1,192 @@
+package org.apache.thrift;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.thrift.protocol.TField;
+import org.apache.thrift.protocol.TProtocol;
+import org.apache.thrift.protocol.TProtocolException;
+import org.apache.thrift.protocol.TStruct;
+
+public abstract class TUnion implements TBase {
+
+ protected Object value_;
+ protected int setField_;
+
+ protected TUnion() {
+ setField_ = 0;
+ value_ = null;
+ }
+
+ protected TUnion(int setField, Object value) {
+ setFieldValue(setField, value);
+ }
+
+ protected TUnion(TUnion other) {
+ if (!other.getClass().equals(this.getClass())) {
+ throw new ClassCastException();
+ }
+ setField_ = other.setField_;
+ value_ = deepCopyObject(other.value_);
+ }
+
+ private static Object deepCopyObject(Object o) {
+ if (o instanceof TBase) {
+ return ((TBase)o).deepCopy();
+ } else if (o instanceof byte[]) {
+ byte[] other_val = (byte[])o;
+ byte[] this_val = new byte[other_val.length];
+ System.arraycopy(other_val, 0, this_val, 0, other_val.length);
+ return this_val;
+ } else if (o instanceof List) {
+ return deepCopyList((List)o);
+ } else if (o instanceof Set) {
+ return deepCopySet((Set)o);
+ } else if (o instanceof Map) {
+ return deepCopyMap((Map)o);
+ } else {
+ return o;
+ }
+ }
+
+ private static Map deepCopyMap(Map<Object, Object> map) {
+ Map copy = new HashMap();
+ for (Map.Entry<Object, Object> entry : map.entrySet()) {
+ copy.put(deepCopyObject(entry.getKey()), deepCopyObject(entry.getValue()));
+ }
+ return copy;
+ }
+
+ private static Set deepCopySet(Set set) {
+ Set copy = new HashSet();
+ for (Object o : set) {
+ copy.add(deepCopyObject(o));
+ }
+ return copy;
+ }
+
+ private static List deepCopyList(List list) {
+ List copy = new ArrayList(list.size());
+ for (Object o : list) {
+ copy.add(deepCopyObject(o));
+ }
+ return copy;
+ }
+
+ public int getSetField() {
+ return setField_;
+ }
+
+ public Object getFieldValue() {
+ return value_;
+ }
+
+ public Object getFieldValue(int fieldId) {
+ if (fieldId != setField_) {
+ throw new IllegalArgumentException("Cannot get the value of field " + fieldId + " because union's set field is " + setField_);
+ }
+
+ return getFieldValue();
+ }
+
+ public boolean isSet() {
+ return setField_ != 0;
+ }
+
+ public boolean isSet(int fieldId) {
+ return setField_ == fieldId;
+ }
+
+ public void read(TProtocol iprot) throws TException {
+ setField_ = 0;
+ value_ = null;
+
+ iprot.readStructBegin();
+
+ TField field = iprot.readFieldBegin();
+
+ value_ = readValue(iprot, field);
+ if (value_ != null) {
+ setField_ = field.id;
+ }
+
+ iprot.readFieldEnd();
+ // this is so that we will eat the stop byte. we could put a check here to
+ // make sure that it actually *is* the stop byte, but it's faster to do it
+ // this way.
+ iprot.readFieldBegin();
+ iprot.readStructEnd();
+ }
+
+ public void setFieldValue(int fieldId, Object value) {
+ checkType((short)fieldId, value);
+ setField_ = (short)fieldId;
+ value_ = value;
+ }
+
+ public void write(TProtocol oprot) throws TException {
+ if (getSetField() == 0 || getFieldValue() == null) {
+ throw new TProtocolException("Cannot write a TUnion with no set value!");
+ }
+ oprot.writeStructBegin(getStructDesc());
+ oprot.writeFieldBegin(getFieldDesc(setField_));
+ writeValue(oprot, (short)setField_, value_);
+ oprot.writeFieldEnd();
+ oprot.writeFieldStop();
+ oprot.writeStructEnd();
+ }
+
+ /**
+ * Implementation should be generated so that we can efficiently type check
+ * various values.
+ * @param setField
+ * @param value
+ */
+ protected abstract void checkType(short setField, Object value) throws ClassCastException;
+
+ /**
+ * Implementation should be generated to read the right stuff from the wire
+ * based on the field header.
+ * @param field
+ * @return
+ */
+ protected abstract Object readValue(TProtocol iprot, TField field) throws TException;
+
+ protected abstract void writeValue(TProtocol oprot, short setField, Object value) throws TException;
+
+ protected abstract TStruct getStructDesc();
+
+ protected abstract TField getFieldDesc(int setField);
+
+ @Override
+ public String toString() {
+ Object v = getFieldValue();
+ String vStr = null;
+ if (v instanceof byte[]) {
+ vStr = bytesToStr((byte[])v);
+ } else {
+ vStr = v.toString();
+ }
+ return "<" + this.getClass().getSimpleName() + " " + getFieldDesc(getSetField()).name + ":" + vStr + ">";
+ }
+
+ private static String bytesToStr(byte[] bytes) {
+ StringBuilder sb = new StringBuilder();
+ int size = Math.min(bytes.length, 128);
+ for (int i = 0; i < size; i++) {
+ if (i != 0) {
+ sb.append(" ");
+ }
+ String digit = Integer.toHexString(bytes[i]);
+ sb.append(digit.length() > 1 ? digit : "0" + digit);
+ }
+ if (bytes.length > 128) {
+ sb.append(" ...");
+ }
+ return sb.toString();
+ }
+}
diff --git a/lib/java/test/org/apache/thrift/test/UnionTest.java b/lib/java/test/org/apache/thrift/test/UnionTest.java
new file mode 100644
index 0000000..85be699
--- /dev/null
+++ b/lib/java/test/org/apache/thrift/test/UnionTest.java
@@ -0,0 +1,131 @@
+package org.apache.thrift.test;
+
+import org.apache.thrift.protocol.TBinaryProtocol;
+import org.apache.thrift.protocol.TProtocol;
+import org.apache.thrift.transport.TMemoryBuffer;
+
+import thrift.test.Empty;
+import thrift.test.StructWithAUnion;
+import thrift.test.TestUnion;
+
+public class UnionTest {
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) throws Exception {
+ testBasic();
+ testEquality();
+ testSerialization();
+ }
+
+ public static void testBasic() throws Exception {
+ TestUnion union = new TestUnion();
+
+ if (union.isSet()) {
+ throw new RuntimeException("new union with default constructor counts as set!");
+ }
+
+ if (union.getFieldValue() != null) {
+ throw new RuntimeException("unset union didn't return null for value");
+ }
+
+ union = new TestUnion(TestUnion.I32_FIELD, 25);
+
+ if ((Integer)union.getFieldValue() != 25) {
+ throw new RuntimeException("set i32 field didn't come out as planned");
+ }
+
+ if ((Integer)union.getFieldValue(TestUnion.I32_FIELD) != 25) {
+ throw new RuntimeException("set i32 field didn't come out of TBase getFieldValue");
+ }
+
+ try {
+ union.getFieldValue(TestUnion.STRING_FIELD);
+ throw new RuntimeException("was expecting an exception around wrong set field");
+ } catch (IllegalArgumentException e) {
+ // cool!
+ }
+
+ System.out.println(union);
+
+ union = new TestUnion();
+ union.setI32_field(1);
+ if (union.getI32_field() != 1) {
+ throw new RuntimeException("didn't get the right value for i32 field!");
+ }
+
+ try {
+ union.getString_field();
+ throw new RuntimeException("should have gotten an exception");
+ } catch (Exception e) {
+ // sweet
+ }
+
+ }
+
+
+ public static void testEquality() throws Exception {
+ TestUnion union = new TestUnion(TestUnion.I32_FIELD, 25);
+
+ TestUnion otherUnion = new TestUnion(TestUnion.STRING_FIELD, "blah!!!");
+
+ if (union.equals(otherUnion)) {
+ throw new RuntimeException("shouldn't be equal");
+ }
+
+ otherUnion = new TestUnion(TestUnion.I32_FIELD, 400);
+
+ if (union.equals(otherUnion)) {
+ throw new RuntimeException("shouldn't be equal");
+ }
+
+ otherUnion = new TestUnion(TestUnion.OTHER_I32_FIELD, 25);
+
+ if (union.equals(otherUnion)) {
+ throw new RuntimeException("shouldn't be equal");
+ }
+ }
+
+
+ public static void testSerialization() throws Exception {
+ TestUnion union = new TestUnion(TestUnion.I32_FIELD, 25);
+
+ TMemoryBuffer buf = new TMemoryBuffer(0);
+ TProtocol proto = new TBinaryProtocol(buf);
+
+ union.write(proto);
+
+ TestUnion u2 = new TestUnion();
+
+ u2.read(proto);
+
+ if (!u2.equals(union)) {
+ throw new RuntimeException("serialization fails!");
+ }
+
+ StructWithAUnion swau = new StructWithAUnion(u2);
+
+ buf = new TMemoryBuffer(0);
+ proto = new TBinaryProtocol(buf);
+
+ swau.write(proto);
+
+ StructWithAUnion swau2 = new StructWithAUnion();
+ if (swau2.equals(swau)) {
+ throw new RuntimeException("objects match before they are supposed to!");
+ }
+ swau2.read(proto);
+ if (!swau2.equals(swau)) {
+ throw new RuntimeException("objects don't match when they are supposed to!");
+ }
+
+ // this should NOT throw an exception.
+ buf = new TMemoryBuffer(0);
+ proto = new TBinaryProtocol(buf);
+
+ swau.write(proto);
+ new Empty().read(proto);
+
+ }
+}