You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by br...@apache.org on 2010/02/17 21:01:30 UTC

svn commit: r911162 - in /incubator/thrift/trunk: compiler/cpp/src/generate/t_java_generator.cc lib/java/test/org/apache/thrift/test/UnionTest.java

Author: bryanduxbury
Date: Wed Feb 17 20:01:29 2010
New Revision: 911162

URL: http://svn.apache.org/viewvc?rev=911162&view=rev
Log:
THRIFT-703. Attempting to hash an unset union struct results in NPE

Modified:
    incubator/thrift/trunk/compiler/cpp/src/generate/t_java_generator.cc
    incubator/thrift/trunk/lib/java/test/org/apache/thrift/test/UnionTest.java

Modified: incubator/thrift/trunk/compiler/cpp/src/generate/t_java_generator.cc
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/compiler/cpp/src/generate/t_java_generator.cc?rev=911162&r1=911161&r2=911162&view=diff
==============================================================================
--- incubator/thrift/trunk/compiler/cpp/src/generate/t_java_generator.cc (original)
+++ incubator/thrift/trunk/compiler/cpp/src/generate/t_java_generator.cc Wed Feb 17 20:01:29 2010
@@ -1024,7 +1024,19 @@
   if (gen_hash_code_) {
     indent(out) << "@Override" << endl;
     indent(out) << "public int hashCode() {" << endl;
-    indent(out) << "  return new HashCodeBuilder().append(getSetField().getThriftFieldId()).append((getFieldValue() instanceof TEnum) ? ((TEnum)getFieldValue()).getValue() : getFieldValue()).toHashCode();" << endl;
+    indent(out) << "  HashCodeBuilder hcb = new HashCodeBuilder();" << endl;
+    indent(out) << "  hcb.append(this.getClass().getName());" << endl;
+    indent(out) << "  TFieldIdEnum setField = getSetField();" << endl;
+    indent(out) << "  if (setField != null) {" << endl;
+    indent(out) << "    hcb.append(setField.getThriftFieldId());" << endl;
+    indent(out) << "    Object value = getFieldValue();" << endl;
+    indent(out) << "    if (value instanceof TEnum) {" << endl;
+    indent(out) << "      hcb.append(((TEnum)getFieldValue()).getValue());" << endl;
+    indent(out) << "    } else {" << endl;
+    indent(out) << "      hcb.append(value);" << endl;
+    indent(out) << "    }" << endl;
+    indent(out) << "  }" << endl;
+    indent(out) << "  return hcb.toHashCode();" << endl;
     indent(out) << "}";
   } else {
     indent(out) << "/**" << endl;

Modified: incubator/thrift/trunk/lib/java/test/org/apache/thrift/test/UnionTest.java
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/java/test/org/apache/thrift/test/UnionTest.java?rev=911162&r1=911161&r2=911162&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/java/test/org/apache/thrift/test/UnionTest.java (original)
+++ incubator/thrift/trunk/lib/java/test/org/apache/thrift/test/UnionTest.java Wed Feb 17 20:01:29 2010
@@ -74,6 +74,10 @@
     System.out.println(union);
 
     union = new TestUnion();
+
+    // should not throw an exception here
+    union.hashCode();
+
     union.setI32_field(1);
     if (union.getI32_field() != 1) {
       throw new RuntimeException("didn't get the right value for i32 field!");