From: Bryan Duxbury Date: Fri, 25 Sep 2009 20:28:35 +0000 (+0000) Subject: THRIFT-588. java: Generated .equals method throws NPE for thrift object X-Git-Tag: 0.2.0~27 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=315a5dbe29213aa70750f4e93542e1d72f3b0bb9;p=common%2Fthrift.git THRIFT-588. java: Generated .equals method throws NPE for thrift object If other is null, then they're not equal. git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@818998 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/compiler/cpp/src/generate/t_java_generator.cc b/compiler/cpp/src/generate/t_java_generator.cc index 4166f3c7..40f897bc 100644 --- a/compiler/cpp/src/generate/t_java_generator.cc +++ b/compiler/cpp/src/generate/t_java_generator.cc @@ -951,7 +951,7 @@ void t_java_generator::generate_union_comparisons(ofstream& out, t_struct* tstru out << endl; indent(out) << "public boolean equals(" << tstruct->get_name() << " other) {" << endl; - indent(out) << " return getSetField() == other.getSetField() && ((value_ instanceof byte[]) ? " << endl; + indent(out) << " return other != null && getSetField() == other.getSetField() && ((value_ instanceof byte[]) ? " << endl; indent(out) << " Arrays.equals((byte[])getFieldValue(), (byte[])other.getFieldValue()) : getFieldValue().equals(other.getFieldValue()));" << endl; indent(out) << "}" << endl; out << endl; diff --git a/lib/java/test/org/apache/thrift/test/UnionTest.java b/lib/java/test/org/apache/thrift/test/UnionTest.java index 85be6993..c2c8791b 100644 --- a/lib/java/test/org/apache/thrift/test/UnionTest.java +++ b/lib/java/test/org/apache/thrift/test/UnionTest.java @@ -62,6 +62,11 @@ public class UnionTest { // sweet } + union = TestUnion.i32_field(1); + + if (union.equals((TestUnion)null)) { + throw new RuntimeException("uh oh, union.equals(null)!"); + } }