You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@grails.apache.org by ma...@apache.org on 2026/09/29 12:43:04 UTC

(grails-core) 03/04: Skip interfaces when collecting static property values from a class hierarchy

This is an automated email from the ASF dual-hosted git repository.

matrei pushed a commit to branch chore/groovy-6-forward-compat
in repository https://gitbox.apache.org/repos/asf/grails-core.git

commit e1a2fc6c4e9f213a5d6b45995cb1a3d1bc4ed8b0
Author: Mattias Reichel <ma...@gmail.com>
AuthorDate: Tue Sep 29 14:42:05 2026 +0200

    Skip interfaces when collecting static property values from a class hierarchy
    
    getStaticPropertyValuesFromInheritanceHierarchy is meant to skip the
    interfaces in the hierarchy, but it tested whether the target class itself
    was an interface, which is never the case for the classes it is used with.
    Interfaces were therefore visited, so a constant such as `transients` or
    `mapping` declared on an implemented interface was returned as if a class
    in the hierarchy had declared it.
    
    The check now tests each hierarchy entry. A static property a trait
    contributes is still read from the implementing class. Visiting the trait
    interface instead fails on Groovy 6, where it no longer carries the static
    accessors.
---
 .../mapping/reflect/ClassPropertyFetcher.java      |  2 +-
 .../reflect/ClassPropertyFetcherTests.groovy       | 35 ++++++++++++++++++++++
 2 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/reflect/ClassPropertyFetcher.java b/grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/reflect/ClassPropertyFetcher.java
index a429285376..f962392898 100644
--- a/grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/reflect/ClassPropertyFetcher.java
+++ b/grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/reflect/ClassPropertyFetcher.java
@@ -225,7 +225,7 @@ public class ClassPropertyFetcher {
         Class javaClass = cachedClass.getTheClass();
         List<T> values = new ArrayList<>(hierarchy.size());
         for (ClassInfo current : hierarchy) {
-            if (cachedClass.isInterface()) continue;
+            if (current.getCachedClass().isInterface()) continue;
             MetaProperty metaProperty = current.getMetaClass().getMetaProperty(name);
             if (metaProperty != null && Modifier.isStatic(metaProperty.getModifiers())) {
                 Class type = metaProperty.getType();
diff --git a/grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/reflect/ClassPropertyFetcherTests.groovy b/grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/reflect/ClassPropertyFetcherTests.groovy
index afd4abf658..6b384c993c 100644
--- a/grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/reflect/ClassPropertyFetcherTests.groovy
+++ b/grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/reflect/ClassPropertyFetcherTests.groovy
@@ -51,6 +51,21 @@ class ClassPropertyFetcherTests  {
         assert tsc.getStaticPropertyValuesFromInheritanceHierarchy(GormProperties.TRANSIENT, Collection) == [[], ["transientProperty"], ["bar"]]
     }
 
+    @Test
+    void testGetStaticPropertyInheritanceSkipsInterfaces() {
+        def cpf = ClassPropertyFetcher.forClass(TransientsNextToInterfaceConstant)
+
+        assert cpf.getStaticPropertyValuesFromInheritanceHierarchy(GormProperties.TRANSIENT, Collection) == [['own']]
+        assert ClassPropertyFetcher.getStaticPropertyValuesFromInheritanceHierarchy(TransientsNextToInterfaceConstant, GormProperties.TRANSIENT, Collection) == [['own']]
+    }
+
+    @Test
+    void testGetStaticPropertyInheritanceReadsATraitStaticPropertyFromTheImplementingClass() {
+        def cpf = ClassPropertyFetcher.forClass(TransientsFromTrait)
+
+        assert cpf.getStaticPropertyValuesFromInheritanceHierarchy(GormProperties.TRANSIENT, Collection) == [['fromTrait']]
+    }
+
 
     @Test
     void testClassPropertyFetcherWithTraitProperty() {
@@ -150,6 +165,26 @@ class TransientSubChild extends TransientChild {
     static transients = ["bar"]
 }
 
+interface TransientsConstant {
+
+    List transients = ['fromInterface']
+}
+
+class TransientsNextToInterfaceConstant implements TransientsConstant {
+
+    static transients = ['own']
+}
+
+trait TransientsTrait {
+
+    static List transients = ['fromTrait']
+}
+
+class TransientsFromTrait implements TransientsTrait {
+
+    String name
+}
+
 class DomainWithMultipleSetter {
     Long id
     String name