You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@freemarker.apache.org by wo...@apache.org on 2017/10/16 01:34:17 UTC

[5/8] incubator-freemarker git commit: Test and version history entry for edge case when one of the read methods of a bean property is inaccessible, and the other (indexed VS non-indexed) is accessible.

Test and version history entry for edge case when one of the read methods of a bean property is inaccessible, and the other (indexed VS non-indexed) is accessible.


Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/03106619
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/03106619
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/03106619

Branch: refs/heads/2.3
Commit: 03106619036c0f427a5838b4e795f139fdf30703
Parents: f09918d
Author: ddekany <dd...@apache.org>
Authored: Sun Oct 15 21:31:10 2017 +0200
Committer: ddekany <dd...@apache.org>
Committed: Sun Oct 15 21:51:45 2017 +0200

----------------------------------------------------------------------
 src/manual/en_US/book.xml                       | 16 +++++++
 .../ext/beans/BeansWrapperMiscTest.java         | 48 ++++++++++++++++++++
 2 files changed, 64 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/03106619/src/manual/en_US/book.xml
----------------------------------------------------------------------
diff --git a/src/manual/en_US/book.xml b/src/manual/en_US/book.xml
index 421a14a..a66096e 100644
--- a/src/manual/en_US/book.xml
+++ b/src/manual/en_US/book.xml
@@ -27249,6 +27249,22 @@ TemplateModel x = env.getVariable("x");  // get variable x</programlisting>
             </listitem>
 
             <listitem>
+              <para>Bug fixed (affects Java 8 and later): Regardless of the
+              value of the <literal>preferIndexedReadMethod</literal> setting
+              (see previous point), if one of the indexed read method and the
+              non-indexed read method is inaccessible (i.e., it's declared in
+              a non-public type, and wasn't inherited by a public type), while
+              the other read method is accessible, we will use the accessible
+              one. Earlier, if there was an indexed read method but it was
+              inaccessible, we have given up, and that bean property wasn't
+              visible. Such properties will now be visible again, just as
+              before Java 8. (Before Java 8
+              <literal>java.beans.Inrospector</literal> has only exposed the
+              non-indexed read method in this case, so we didn't have this
+              problem.)</para>
+            </listitem>
+
+            <listitem>
               <para>Bug fixed: On OpenJDK 9 (but not on earlier versions, nor
               on Oracle Java 9 (tested with <quote>build 9+181</quote>)), when
               you try to use the DOM-based XML support

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/03106619/src/test/java/freemarker/ext/beans/BeansWrapperMiscTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/freemarker/ext/beans/BeansWrapperMiscTest.java b/src/test/java/freemarker/ext/beans/BeansWrapperMiscTest.java
index 917b2b3..c3ff772 100644
--- a/src/test/java/freemarker/ext/beans/BeansWrapperMiscTest.java
+++ b/src/test/java/freemarker/ext/beans/BeansWrapperMiscTest.java
@@ -22,12 +22,14 @@ package freemarker.ext.beans;
 import static org.hamcrest.Matchers.*;
 import static org.junit.Assert.*;
 
+import java.lang.reflect.Modifier;
 import java.util.Collections;
 
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.JUnit4;
 
+import freemarker.core._JavaVersions;
 import freemarker.template.Configuration;
 import freemarker.template.TemplateBooleanModel;
 import freemarker.template.TemplateHashModel;
@@ -36,6 +38,7 @@ import freemarker.template.TemplateModel;
 import freemarker.template.TemplateModelException;
 import freemarker.template.TemplateScalarModel;
 import freemarker.template.TemplateSequenceModel;
+import freemarker.template.Version;
 import freemarker.template.utility.Constants;
 
 @RunWith(JUnit4.class)
@@ -95,6 +98,27 @@ public class BeansWrapperMiscTest {
             assertEquals(2, ((TemplateSequenceModel) fooTM).size());
         }
     }
+
+    @Test
+    public void java8InaccessibleIndexedAccessibleNonIndexedReadMethodTest() throws TemplateModelException {
+        assertTrue("This test case must be ran on Java 8 or later", _JavaVersions.JAVA_8 != null);
+        assertFalse(Modifier.isPublic(BeanWithInaccessibleIndexedProperty.class.getModifiers()));
+        
+        for (Version ici : new Version[] { Configuration.VERSION_2_3_26, Configuration.VERSION_2_3_27 }) {
+            BeansWrapper bw = new BeansWrapper(ici);
+            TemplateHashModel beanTM = (TemplateHashModel) bw.wrap(new BeanWithInaccessibleIndexedProperty());
+            TemplateModel fooTM = beanTM.get("foo");
+            
+            assertThat(fooTM, instanceOf(TemplateSequenceModel.class));
+            assertEquals("b",
+                    ((TemplateScalarModel) ((TemplateSequenceModel) fooTM).get(1)).getAsString());
+            // Even with 2.3.26, where the indexed reader was preferred, as it's inaccessible, we use the normal reader:
+            assertEquals(2, ((TemplateSequenceModel) fooTM).size());
+            
+            TemplateModel barTM = beanTM.get("bar");
+            assertNull(barTM); // all read methods inaccessible
+        }
+    }
     
     public static class BeanWithBothIndexedAndArrayProperty {
         
@@ -110,4 +134,28 @@ public class BeansWrapperMiscTest {
         
     }
     
+    public interface HasFoo {
+        String[] getFoo();
+    }
+
+    // Note: This class is deliberately not public
+    static class BeanWithInaccessibleIndexedProperty implements HasFoo {
+        
+        private final static String[] FOO = new String[] { "a", "b" };
+        
+        public String getFoo(int index) {
+            return FOO[index];
+        }
+
+        // This will be accessible
+        public String[] getFoo() {
+            return FOO;
+        }
+        
+        public String getBar(int index) {
+            return FOO[index];
+        }
+        
+    }
+    
 }