Return-Path: X-Original-To: apmail-cayenne-dev-archive@www.apache.org Delivered-To: apmail-cayenne-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 4D4817E17 for ; Tue, 4 Oct 2011 08:20:13 +0000 (UTC) Received: (qmail 16113 invoked by uid 500); 4 Oct 2011 08:20:13 -0000 Delivered-To: apmail-cayenne-dev-archive@cayenne.apache.org Received: (qmail 16086 invoked by uid 500); 4 Oct 2011 08:20:13 -0000 Mailing-List: contact dev-help@cayenne.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cayenne.apache.org Delivered-To: mailing list dev@cayenne.apache.org Received: (qmail 16044 invoked by uid 99); 4 Oct 2011 08:20:13 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 04 Oct 2011 08:20:13 +0000 X-ASF-Spam-Status: No, hits=-0.0 required=5.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: local policy) Received: from [208.78.103.231] (HELO vorsha.objectstyle.org) (208.78.103.231) by apache.org (qpsmtpd/0.29) with SMTP; Tue, 04 Oct 2011 08:20:05 +0000 Received: (qmail 20945 invoked from network); 4 Oct 2011 08:19:43 -0000 Received: from unknown (HELO ?192.168.1.95?) (194.158.197.10) by vorsha.objectstyle.org with SMTP; 4 Oct 2011 08:19:43 -0000 Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Apple Message framework v1084) Subject: Re: [jira] [Commented] (CAY-1622) Generated classes shouldn't produce serialVersionUID compiler warning From: Andrus Adamchik In-Reply-To: <4E8ABF70.1040201@gmail.com> Date: Tue, 4 Oct 2011 11:19:42 +0300 Content-Transfer-Encoding: quoted-printable Message-Id: References: <494298374.2716.1317638673955.JavaMail.tomcat@hel.zones.apache.org> <2142707061.5551.1317685293986.JavaMail.tomcat@hel.zones.apache.org> <9DB2CC1C-2A3A-4D71-8CFC-2B1CA5CD4FA7@objectstyle.org> <4E8ABF70.1040201@gmail.com> To: dev@cayenne.apache.org X-Mailer: Apple Mail (2.1084) X-Virus-Checked: Checked by ClamAV on apache.org Very cool.=20 Can we also create some unit tests for this to ensure it works as = advertised. E.g. a possible caveat is consistent alphabetic ordering of = relationships and attributes (IIRC they are ordered by default, but it = is worth creating some tests to see how entity creation affects the = result). Also I think in relationships loop we may add a check for the = target entity type. Andrus On Oct 4, 2011, at 11:10 AM, Dzmitry Kazimirchyk wrote: > Just committed fix to the trunk (r1178712). UID generation algorithm = uses same approach as java.io.ObjectStreamClass.getSerialVersionUID(). = Generated id depends on ObjEntity's superclass, ObjAttributes and = ObjRelationships names and types. >=20 > Here is the diff: >=20 > Modified: = cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/= apache/cayenne/map/ObjEntity.java > = URL:http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1= .5-unpublished/src/main/java/org/apache/cayenne/map/ObjEntity.java?rev=3D1= 178712&r1=3D1178711&r2=3D1178712&view=3Ddiff > = =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D > --- = cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/= apache/cayenne/map/ObjEntity.java (original) > +++ = cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/= apache/cayenne/map/ObjEntity.java Tue Oct 4 07:57:40 2011 > @@ -19,6 +19,9 @@ >=20 > package org.apache.cayenne.map; >=20 > +import java.io.ByteArrayOutputStream; > +import java.io.DataOutputStream; > +import java.security.MessageDigest; > import java.util.ArrayList; > import java.util.Arrays; > import java.util.Collection; > @@ -1250,5 +1253,42 @@ public class ObjEntity extends Entity im > public void setExcludingSuperclassListeners(boolean = excludingSuperclassListeners) { > this.excludingSuperclassListeners =3D = excludingSuperclassListeners; > } > + > + /** > + * Returns unique id for this ObjEntity. > + * > + * @since 3.1 > + */ > + public long getSerialVersionUID() throws Exception { > + ByteArrayOutputStream bout =3D new ByteArrayOutputStream(); > + DataOutputStream dout =3D new DataOutputStream(bout); > + > + dout.writeUTF(getClassName()); > + > + if (getSuperClassName() !=3D null) { > + dout.writeUTF(getSuperClassName()); > + } > + > + for (ObjAttribute attr : getAttributes()) { > + dout.writeUTF(attr.getName()); > + dout.writeUTF(attr.getType()); > + } > + > + for (ObjRelationship rel : getRelationships()) { > + dout.writeUTF(rel.getName()); > + dout.writeUTF(rel.getCollectionType() !=3D null > + ? rel.getCollectionType() : "to-one"); > + } > + > + dout.flush(); > + > + MessageDigest md =3D MessageDigest.getInstance("SHA"); > + byte[] hashBytes =3D md.digest(bout.toByteArray()); > + long hash =3D 0; > + for (int i =3D Math.min(hashBytes.length, 8) - 1; i>=3D 0; = i--) { > + hash =3D (hash<< 8) | (hashBytes[i]& 0xFF); > + } > + return hash; > + } >=20 > } >=20 >=20 > Dzmitry >=20 > On 10/04/2011 08:22 AM, Andrus Adamchik wrote: >> +1. We had a brief discussion with Dzmitry to that extent last night. = Here is an extract from that chat: >>=20 >>> Generated classes (the ones with _XXX) are made from ObjEntities, so = if we can use an alg. based on ObjEntity state, I think that should be = enough just need to account for all its parts (superclass, objattribute = names and types; objrelationship names and types) >> So yeah, IMO it does have to be like hashcode of sorts based on = ObjEntity state... >>=20 >> Andrus >>=20 >> On Oct 4, 2011, at 2:53 AM, Mike Kienenberger wrote: >>=20 >>> No, it should be more clever than that. Otherwise, every time you >>> regenerate the template, the generated code will change even if >>> nothing else had changed. That will also break serialization. >>>=20 >>> Ideally, it should return the same number whenever the same fields = and >>> types are used for the generated class. >>>=20 >>> Maybe build the number by iterating over the attributes and >>> relationships and feeding it into >>> org.apache.commons.lang.builder.HashCodeBuilder, basing it on the >>> order and type >>>=20 >>>=20 >>> On Mon, Oct 3, 2011 at 7:41 PM, Ari Maniatis (Commented) (JIRA) >>> wrote: >>>> [ = https://issues.apache.org/jira/browse/CAY-1622?page=3Dcom.atlassian.jira.p= lugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=3D13119757#c= omment-13119757 ] >>>>=20 >>>> Ari Maniatis commented on CAY-1622: >>>> ----------------------------------- >>>>=20 >>>> Do we just want to add this to the velocity templates: >>>>=20 >>>> private static final long serialVersionUID =3D = ${random(1,9223372036854775807)}; >>>>=20 >>>>> Generated classes shouldn't produce serialVersionUID compiler = warning >>>>> = --------------------------------------------------------------------- >>>>>=20 >>>>> Key: CAY-1622 >>>>> URL: https://issues.apache.org/jira/browse/CAY-1622 >>>>> Project: Cayenne >>>>> Issue Type: Bug >>>>> Components: Core Library >>>>> Affects Versions: 3.0 >>>>> Reporter: Bob Harner >>>>> Priority: Minor >>>>>=20 >>>>> It is probably a good idea for Cayenne not to produce any compiler = warnings, but auto-generated entity classes always have the "The = serializable class _Foo does not declare a static final serialVersionUID = field of type long" compiler warning. >>>>> The @SuppressWarnings("serial") annotation will get rid of the = warnings, but of course it is not recommended to manually update the = generated classes. >>>>> Users could configure the IDE to ignore these warnings (yuk), but = then other team members will still see them. >>>>> Another option is to for users to set up a custom velocity = template to include this annotation. >>>>> But the best solution would be for Cayenne to either automatically = generate either the serialVersionUID itself or the = @SuppressWarnings("serial") annotation that suppresses the compiler = warning. >>>> -- >>>> This message is automatically generated by JIRA. >>>> If you think it was sent incorrectly, please contact your JIRA = administrators: = https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa >>>> For more information on JIRA, see: = http://www.atlassian.com/software/jira >>>>=20 >>>>=20 >>>>=20 >=20 >=20