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

ddekany pushed a commit to branch 2.3-gae
in repository https://gitbox.apache.org/repos/asf/freemarker.git


The following commit(s) were added to refs/heads/2.3-gae by this push:
     new 9cc2b9cb ZeroArgumentNonVoidMethodPolicy: Improved behavior when a 
record has Java Bean property read methods to expose the record components on 
the traditional way too.
9cc2b9cb is described below

commit 9cc2b9cb6d1196fffa010430920331a36e7cbf49
Author: ddekany <[email protected]>
AuthorDate: Wed Apr 3 17:13:03 2024 +0200

    ZeroArgumentNonVoidMethodPolicy: Improved behavior when a record has Java 
Bean property read methods to expose the record components on the traditional 
way too.
---
 .../freemarker/ext/beans/ClassIntrospector.java    | 13 +++-
 ...ntNonVoidMethodPolicyAndClashingBeansProps.java | 81 ++++++++++++++++++++++
 2 files changed, 93 insertions(+), 1 deletion(-)

diff --git 
a/freemarker-core/src/main/java/freemarker/ext/beans/ClassIntrospector.java 
b/freemarker-core/src/main/java/freemarker/ext/beans/ClassIntrospector.java
index 90b109a1..b223868e 100644
--- a/freemarker-core/src/main/java/freemarker/ext/beans/ClassIntrospector.java
+++ b/freemarker-core/src/main/java/freemarker/ext/beans/ClassIntrospector.java
@@ -394,7 +394,7 @@ class ClassIntrospector {
                     PropertyDescriptor propDesc = 
decision.getExposeAsProperty();
                     if (propDesc != null &&
                             (decision.getReplaceExistingProperty()
-                                    || !(introspData.get(propDesc.getName()) 
instanceof FastPropertyDescriptor))) {
+                                    || 
isExistingIntropsDataNotPropertyOrNewAddsMethodSupport(introspData, 
propDesc.getName(), decision))) {
                         boolean methodInsteadOfPropertyValueBeforeCall = 
decision.isMethodInsteadOfPropertyValueBeforeCall();
                         addPropertyDescriptorToClassIntrospectionData(
                                 introspData, propDesc, 
methodInsteadOfPropertyValueBeforeCall,
@@ -441,6 +441,17 @@ class ClassIntrospector {
         } // end if (exposureLevel < EXPOSE_PROPERTIES_ONLY)
     }
 
+    private static boolean 
isExistingIntropsDataNotPropertyOrNewAddsMethodSupport(
+            Map<Object, Object> introspData, String introspDataKey, 
MethodAppearanceDecision decision) {
+        Object prevIntrospDataValue = introspData.get(introspDataKey);
+        if (prevIntrospDataValue instanceof FastPropertyDescriptor) {
+            return decision.isMethodInsteadOfPropertyValueBeforeCall()
+                    && !((FastPropertyDescriptor) 
prevIntrospDataValue).isMethodInsteadOfPropertyValueBeforeCall();
+        } else {
+            return true;
+        }
+    }
+
     private static ZeroArgumentNonVoidMethodPolicy 
getAppliedZeroArgumentNonVoidMethodPolicy(Method method, Set<String> 
beanPropertyReadMethodNameCollector, ZeroArgumentNonVoidMethodPolicy 
zeroArgumentNonVoidMethodPolicy) {
         if (method.getParameterCount() == 0 && method.getReturnType() != 
void.class) {
             return beanPropertyReadMethodNameCollector != null && 
beanPropertyReadMethodNameCollector.contains(method.getName())
diff --git 
a/freemarker-core16/src/test/java/freemarker/ext/beans/TestZeroArgumentNonVoidMethodPolicyAndClashingBeansProps.java
 
b/freemarker-core16/src/test/java/freemarker/ext/beans/TestZeroArgumentNonVoidMethodPolicyAndClashingBeansProps.java
new file mode 100644
index 00000000..98eebcfa
--- /dev/null
+++ 
b/freemarker-core16/src/test/java/freemarker/ext/beans/TestZeroArgumentNonVoidMethodPolicyAndClashingBeansProps.java
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package freemarker.ext.beans;
+
+import java.io.IOException;
+
+import org.junit.Test;
+
+import freemarker.template.Configuration;
+import freemarker.template.TemplateException;
+import freemarker.test.TemplateTest;
+
+public class TestZeroArgumentNonVoidMethodPolicyAndClashingBeansProps extends 
TemplateTest {
+    @Override
+    protected Configuration createConfiguration() throws Exception {
+        Configuration cfg = super.createConfiguration();
+        // Don't use default, as then the object wrapper is a shared static 
mutable object:
+        cfg.setIncompatibleImprovements(Configuration.VERSION_2_3_33);
+        cfg.setBooleanFormat("c");
+        return cfg;
+    }
+
+    @Test
+    public void testAdditionalBeanPropertyReadMethod() throws 
TemplateException, IOException {
+        addToDataModel("r", new 
RecordWithAdditionalBeanPropertyReadMethod(true, "N"));
+
+        assertOutput("${r.active}", "true");
+        assertOutput("${r.active()}", "true");
+        assertErrorContains("${r.isActive}", "convertible to string");
+        assertOutput("${r.isActive()}", "true");
+
+        assertOutput("${r.name}", "N");
+        assertOutput("${r.name()}", "N");
+        assertErrorContains("${r.getName}", "convertible to string");
+        assertOutput("${r.getName()}", "N");
+    }
+
+    @Test
+    public void testAccidentalBeanPropertyCreatorComponent() throws 
TemplateException, IOException {
+        addToDataModel("r", new 
RecordWithAccidentalBeanPropertyCreatorComponent(true, "N"));
+
+        assertOutput("${r.active}", "true");
+        assertErrorContains("${r.active()}", "Expected a method");
+        assertErrorContains("${r.isActive}", "convertible to string"); // Not 
intuitive...
+        assertOutput("${r.isActive()}", "true");
+
+        assertOutput("${r.name}", "N");
+        assertErrorContains("${r.name()}", "Expected a method");
+        assertErrorContains("${r.getName}", "convertible to string"); // Not 
intuitive...
+        assertOutput("${r.getName()}", "N");
+    }
+
+    public record RecordWithAdditionalBeanPropertyReadMethod(boolean active, 
String name) {
+        public boolean isActive() {
+            return active();
+        }
+        public String getName() {
+            return name();
+        }
+    }
+
+    public record RecordWithAccidentalBeanPropertyCreatorComponent(boolean 
isActive, String getName) {
+    }
+}

Reply via email to