You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@causeway.apache.org by ah...@apache.org on 2024/09/26 08:09:45 UTC

(causeway) branch master updated: CAUSEWAY-3814: fixes potential false positive with check explicit-logical-type-names

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 16a582da3a CAUSEWAY-3814: fixes potential false positive with check explicit-logical-type-names
16a582da3a is described below

commit 16a582da3abacdad2ce1fa2f264460f5f0335a81
Author: Andi Huber <ah...@apache.org>
AuthorDate: Thu Sep 26 10:09:39 2024 +0200

    CAUSEWAY-3814: fixes potential false positive with check
    explicit-logical-type-names
    
    - adds check for presence of Named annotation
---
 .../commons/internal/reflection/_ClassCache.java   | 24 ++++++++++++++++------
 .../LogicalTypeFacetFromClassNameFactory.java      |  5 +++--
 2 files changed, 21 insertions(+), 8 deletions(-)

diff --git a/commons/src/main/java/org/apache/causeway/commons/internal/reflection/_ClassCache.java b/commons/src/main/java/org/apache/causeway/commons/internal/reflection/_ClassCache.java
index 05fae46ec4..3611183610 100644
--- a/commons/src/main/java/org/apache/causeway/commons/internal/reflection/_ClassCache.java
+++ b/commons/src/main/java/org/apache/causeway/commons/internal/reflection/_ClassCache.java
@@ -31,6 +31,7 @@ import java.util.stream.Stream;
 
 import javax.annotation.PostConstruct;
 import javax.inject.Inject;
+import javax.inject.Named;
 import javax.xml.bind.annotation.XmlRootElement;
 
 import org.springframework.beans.factory.annotation.Autowired;
@@ -94,10 +95,20 @@ public final class _ClassCache implements AutoCloseable {
 
     // -- TYPE SPECIFIC SEMANTICS
 
+    /**
+     * whether type is annotated with {@link XmlRootElement}
+     */
     public boolean hasJaxbRootElementSemantics(final Class<?> type) {
         return classModel(type).hasJaxbRootElementSemantics;
     }
 
+    /**
+     * whether type is annotated with {@link Named}
+     */
+    public boolean isAnnotatedWithNamed(final Class<?> type) {
+        return classModel(type).isAnnotatedWithNamed;
+    }
+
     // -- CONSTRUCTOR SEMANTICS
 
     public <T> Stream<ResolvedConstructor> streamPublicConstructors(final Class<T> type) {
@@ -217,9 +228,9 @@ public final class _ClassCache implements AutoCloseable {
             .stream();
         }
     }
-    
+
     // -- ATTRIBUTES
-    
+
     public _ClassCache setAttribute(
             final Class<?> type,
             final String attributeName,
@@ -228,7 +239,7 @@ public final class _ClassCache implements AutoCloseable {
         classModel.attributeMap.put(attributeName, value);
         return this;
     }
-    
+
     public Optional<String> lookupAttribute(
             final Class<?> type,
             final String attributeName) {
@@ -241,6 +252,8 @@ public final class _ClassCache implements AutoCloseable {
     @RequiredArgsConstructor
     private static class ClassModel {
         private final Can<Field> declaredFields;
+        private final boolean hasJaxbRootElementSemantics;
+        private final boolean isAnnotatedWithNamed;
 
         private final Map<ConstructorKey, ResolvedConstructor> publicConstructorsByKey = new HashMap<>();
         private final Map<ConstructorKey, ResolvedConstructor> constructorsWithInjectSemanticsByKey = new HashMap<>();
@@ -251,7 +264,6 @@ public final class _ClassCache implements AutoCloseable {
 
         private final Map<String, Can<ResolvedMethod>> declaredMethodsByAttribute = new HashMap<>();
         private final Map<String, String> attributeMap = new ConcurrentHashMap<>();
-        private final boolean hasJaxbRootElementSemantics;
     }
 
     private final Map<Class<?>, ClassModel> inspectedTypes = new HashMap<>();
@@ -340,7 +352,8 @@ public final class _ClassCache implements AutoCloseable {
 
         val model = new ClassModel(
                 Can.ofArray(type.getDeclaredFields()),
-                _Annotations.isPresent(type, XmlRootElement.class));
+                _Annotations.isPresent(type, XmlRootElement.class),
+                _Annotations.isPresent(type, Named.class));
 
         // process public constructors
         val publicConstr = type.getConstructors();
@@ -512,5 +525,4 @@ public final class _ClassCache implements AutoCloseable {
         return _Strings.decapitalize(fieldName);
     }
 
-
 }
diff --git a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/facets/object/logicaltype/classname/LogicalTypeFacetFromClassNameFactory.java b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/facets/object/logicaltype/classname/LogicalTypeFacetFromClassNameFactory.java
index 43a863cb02..6d7cc18756 100644
--- a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/facets/object/logicaltype/classname/LogicalTypeFacetFromClassNameFactory.java
+++ b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/facets/object/logicaltype/classname/LogicalTypeFacetFromClassNameFactory.java
@@ -21,6 +21,7 @@ package org.apache.causeway.core.metamodel.facets.object.logicaltype.classname;
 import javax.inject.Inject;
 import javax.xml.bind.annotation.XmlType;
 
+import org.apache.causeway.commons.internal.reflection._ClassCache;
 import org.apache.causeway.core.config.progmodel.ProgrammingModelConstants.MessageTemplate;
 import org.apache.causeway.core.metamodel.context.MetaModelContext;
 import org.apache.causeway.core.metamodel.facetapi.FeatureType;
@@ -74,8 +75,8 @@ implements
 
             val logicalType = objectSpec.getLogicalType();
 
-            //XXX has a slight chance to be a false positive; would need to check whether annotated with @Named
-            if(logicalType.getClassName().equals(logicalType.getLogicalTypeName())) {
+            if(logicalType.getClassName().equals(logicalType.getLogicalTypeName())
+                    && !_ClassCache.getInstance().isAnnotatedWithNamed(objectSpec.getCorrespondingClass())) {
                 ValidationFailure.raise(objectSpec, MessageTemplate.LOGICAL_TYPE_NAME_IS_NOT_EXPLICIT
                         .builder()
                         .addVariable("type", objectSpec.getFullIdentifier())