cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ntimof...@apache.org
Subject [cayenne] branch master updated: CAY-2544 Possible name clash of ivar and property name in generated class
Date Fri, 01 Mar 2019 07:28:09 GMT
This is an automated email from the ASF dual-hosted git repository.

ntimofeev pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cayenne.git


The following commit(s) were added to refs/heads/master by this push:
     new 653ba7a  CAY-2544 Possible name clash of ivar and property name in generated class
653ba7a is described below

commit 653ba7ad454974db3c026d44382ddfc2d3ea42f0
Author: Nikita Timofeev <stariy95@gmail.com>
AuthorDate: Fri Mar 1 10:28:05 2019 +0300

    CAY-2544 Possible name clash of ivar and property name in generated class
---
 RELEASE-NOTES.txt                                       |  1 +
 .../main/java/org/apache/cayenne/gen/PropertyUtils.java | 17 +++++++++++++++--
 2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt
index f792a72..62aad0a 100644
--- a/RELEASE-NOTES.txt
+++ b/RELEASE-NOTES.txt
@@ -29,6 +29,7 @@ Bug Fixes:
 
 CAY-2332 Property API: unable to use eq() and in() methods for toMany relationships
 CAY-2509 Result of resolving lazily faulted relationships can be out-of-date
+CAY-2544 Possible name clash of ivar and property name in generated class
 CAY-2547 CayenneDataObject serialization issue
 
 ----------------------------------
diff --git a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/PropertyUtils.java b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/PropertyUtils.java
index 624f42f..06e0422 100644
--- a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/PropertyUtils.java
+++ b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/PropertyUtils.java
@@ -48,6 +48,7 @@ import org.apache.cayenne.map.Embeddable;
 import org.apache.cayenne.map.EmbeddableAttribute;
 import org.apache.cayenne.map.EmbeddedAttribute;
 import org.apache.cayenne.map.ObjAttribute;
+import org.apache.cayenne.map.ObjEntity;
 import org.apache.cayenne.map.ObjRelationship;
 
 /**
@@ -56,6 +57,7 @@ import org.apache.cayenne.map.ObjRelationship;
 public class PropertyUtils {
 
     private static final String PK_PROPERTY_SUFFIX = "_PK_PROPERTY";
+    private static final char DUPLICATE_NAME_SUFFIX = '_';
     private static final Map<String, String> FACTORY_METHODS = new HashMap<>();
 
     static {
@@ -164,7 +166,7 @@ public class PropertyUtils {
         return String.format("public static final %s<%s> %s = PropertyFactory.%s(ExpressionFactory.dbPathExp(\"%s\"),
%s.class);",
                 importUtils.formatJavaType(propertyType),
                 attributeType,
-                utils.capitalizedAsConstant(attribute.getName() + PK_PROPERTY_SUFFIX),
+                utils.capitalizedAsConstant(attribute.getName()) + PK_PROPERTY_SUFFIX,
                 propertyFactoryMethod,
                 attribute.getName(),
                 attributeType
@@ -182,13 +184,24 @@ public class PropertyUtils {
         return String.format("public static final %s<%s> %s = %s(\"%s\", %s.class);",
                 importUtils.formatJavaType(propertyDescriptor.getPropertyType()),
                 attributeType,
-                utils.capitalizedAsConstant(attribute.getName()),
+                generatePropertyName(attribute),
                 propertyDescriptor.getPropertyFactoryMethod(),
                 attribute.getName(),
                 attributeType
         );
     }
 
+    protected String generatePropertyName(ObjAttribute attribute) {
+        StringUtils utils = StringUtils.getInstance();
+        ObjEntity entity = attribute.getEntity();
+        String name = utils.capitalizedAsConstant(attribute.getName());
+        // ensure that final name is unique
+        while(entity.getAttribute(name) != null) {
+            name = name + DUPLICATE_NAME_SUFFIX;
+        }
+        return name;
+    }
+
     public String propertyDefinition(ObjAttribute attribute) throws ClassNotFoundException
{
         return propertyDefinition(attribute, false);
     }


Mime
View raw message