Return-Path: Delivered-To: apmail-ibatis-user-java-archive@www.apache.org Received: (qmail 3377 invoked from network); 25 Mar 2009 11:32:38 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 25 Mar 2009 11:32:38 -0000 Received: (qmail 68861 invoked by uid 500); 25 Mar 2009 11:32:38 -0000 Delivered-To: apmail-ibatis-user-java-archive@ibatis.apache.org Received: (qmail 68817 invoked by uid 500); 25 Mar 2009 11:32:37 -0000 Mailing-List: contact user-java-help@ibatis.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user-java@ibatis.apache.org Delivered-To: mailing list user-java@ibatis.apache.org Received: (qmail 68809 invoked by uid 99); 25 Mar 2009 11:32:37 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 25 Mar 2009 11:32:37 +0000 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests=MISSING_MID,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: local policy) Received: from [85.214.89.56] (HELO h1273841.stratoserver.net) (85.214.89.56) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 25 Mar 2009 11:32:28 +0000 Received: (qmail 3595 invoked from network); 25 Mar 2009 12:34:41 +0100 Received: from poet-egypt.com (HELO insomnia) (62.68.249.236) by bar54.de with SMTP; 25 Mar 2009 12:34:41 +0100 From: "Benjamin Klatt" To: Subject: iBator EqualsHashCodePlugin: NullPointerExceptions in equal method Date: Wed, 25 Mar 2009 13:32:12 +0200 MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook, Build 11.0.5510 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579 Thread-Index: AcmtPSIA3xe1WxCpQKKYBpCO9psjDA== X-Virus-Checked: Checked by ClamAV on apache.org Message-Id: <20090325113235.CB6E281602A@nike.apache.org> Hey all, we found an issue about the equal method. Imagine you have a data object --------- |Product| --------- |id | |name | |desc | --------- And generate the java classes as well as the equal methods (id = Integer, name&desc = String). The Code Product p1 = new Product(); p1.setId(1); p1.setName("p1"); Product p2 = new Product(); p2.setId(1); assertFalse("same id but diff names should return false",p1.equals(p2)); will result in a NullPointerException. This results from the Boolean expression in the equal method. The combination of return nullCheckAttribute1 ? nullCheckObject1 : equalAttribute1 && nullCheckAttribute2 ? nullCheckObject2 : equalAttribute2 && nullCheckAttribute3 ? nullCheckObject3 : equalAttribute3 ... what happens: nullCheckAttribute1 = false -> equalAttribute1 equalAttribute1 = true && nullCheckAttribute2 = false -> equalAttribute2 equalAttribute2 = false && nullCheckAttribute3 == false -> equalAttribute3 equalAttribute3 will result in the NullPointerException because in the example this means this.getDesc().equals(other.getDesc() and this.getDesc() already returns null. So to fix this issue parenthesis should be added in the following way: return nullCheckAttribute1 ? nullCheckObject1 : equalAttribute1 && (nullCheckAttribute2 ? nullCheckObject2 : equalAttribute2 && (nullCheckAttribute3 ? nullCheckObject3 : equalAttribute3 ...)) The required code change in the plugin is: boolean first = true; int numberOfParenthesis = 0; //new counter for parenthesis Iterator iter = introspectedColumns.iterator(); while (iter.hasNext()) { IntrospectedColumn introspectedColumn = iter.next(); ... if (!iter.hasNext()) { for (int i = 0; i < numberOfParenthesis; i++) { sb.append(')'); // append closing parenthesis } sb.append(';'); } I am sorry to posting this on the users mailing list instead of fixing the code directly, but I don't have access to the repository. @Jeff: It would be nice to see this integrated in the next version and to throw away out adopted plugin ;) All the best Benjamin ---------------------- Benjamin Klatt Mob.: +49 (0)179 - 979 55 46 Tel.: +49 (0)721 - 208 84 06 Kentuckyallee. 88 76149 Karlsruhe Germany Bahnhofstr. 21 37445 Walkenried Germany