You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@freemarker.apache.org by dd...@apache.org on 2020/06/27 14:27:42 UTC

[freemarker] 01/02: Forward ported from 2.3-gae: [FREEMARKER-145] Fixed bug where methods with "overloaded" return type may become inaccessible on Java 9+, if some overriding subclasses are not public. (This is because java.beans.Introspector behavior has changed with Java 9.)

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

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

commit 40dc20884d4a15d9eb1f67ea96f037c1059ea23b
Author: ddekany <dd...@apache.org>
AuthorDate: Sat Jun 27 16:20:32 2020 +0200

    Forward ported from 2.3-gae: [FREEMARKER-145] Fixed bug where methods with "overloaded" return type may become inaccessible on Java 9+, if some overriding subclasses are not public. (This is because java.beans.Introspector behavior has changed with Java 9.)
---
 .../impl/Java9InstrospectorBugWorkaroundTest.java  | 41 ++++++++++++++++++++++
 .../core/model/impl/ClassIntrospector.java         | 30 +++++++++++++++-
 2 files changed, 70 insertions(+), 1 deletion(-)

diff --git a/freemarker-core-test/src/test/java/org/apache/freemarker/core/model/impl/Java9InstrospectorBugWorkaroundTest.java b/freemarker-core-test/src/test/java/org/apache/freemarker/core/model/impl/Java9InstrospectorBugWorkaroundTest.java
new file mode 100644
index 0000000..a01c022
--- /dev/null
+++ b/freemarker-core-test/src/test/java/org/apache/freemarker/core/model/impl/Java9InstrospectorBugWorkaroundTest.java
@@ -0,0 +1,41 @@
+/*
+ * 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 org.apache.freemarker.core.model.impl;
+
+import java.io.IOException;
+import java.nio.file.Paths;
+
+import org.apache.freemarker.core.Configuration;
+import org.apache.freemarker.core.TemplateException;
+import org.apache.freemarker.test.TemplateTest;
+import org.junit.Test;
+
+public class Java9InstrospectorBugWorkaroundTest extends TemplateTest {
+    @Override
+    protected void setupConfigurationBuilder(Configuration.ExtendableBuilder<?> cb) {
+        cb.setAPIBuiltinEnabled(true);
+    }
+
+    @Test
+    public void test() throws IOException, TemplateException {
+        addToDataModel("path", Paths.get("foo", "bar"));
+        assertOutput("<#assign _ = path?api.parent>", "");
+    }
+}
diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/model/impl/ClassIntrospector.java b/freemarker-core/src/main/java/org/apache/freemarker/core/model/impl/ClassIntrospector.java
index d250259..0199cb7 100644
--- a/freemarker-core/src/main/java/org/apache/freemarker/core/model/impl/ClassIntrospector.java
+++ b/freemarker-core/src/main/java/org/apache/freemarker/core/model/impl/ClassIntrospector.java
@@ -775,6 +775,16 @@ class ClassIntrospector {
         }
     }
 
+    // This is needed as java.bean.Introspector sometimes gives back a method that's actually not accessible,
+    // as it's an override of an accessible method in a non-public subclass. While that's still a public method, calling
+    // it directly via reflection will throw java.lang.IllegalAccessException, and we are supposed to call the overidden
+    // accessible method instead. Like, we might get two PropertyDescriptor-s for the same property name, and only one
+    // will have a reader method that we can actually call. So we have to find that method here.
+    // Furthermore, the return type of the inaccessible method is possibly different (more specific) than the return
+    // type of the overridden accessible method. Also Introspector behavior changed with Java 9, as earlier in such
+    // case the Introspector returned all variants of the method (so the accessible one was amongst them at least),
+    // while in Java 9 it apparently always returns one variant only, but that's sometimes (not sure if it's
+    // predictable) the inaccessible one.
     private static Method getMatchingAccessibleMethod(Method m, Map<ExecutableMemberSignature, List<Method>> accessibles) {
         if (m == null) {
             return null;
@@ -784,8 +794,26 @@ class ClassIntrospector {
         if (ams == null) {
             return null;
         }
+        // Note that this algorithm was different, and more involved in 2.3.31+, as it was paranoid about breaking
+        // applications that worked before. Here we just go for the simplest solution, that should work with sanely
+        // generated classes.
         for (Method am : ams) {
-            if (am.getReturnType() == m.getReturnType()) {
+            if (m == am) {
+                return am;
+            }
+        }
+        Class<?> mReturnType = m.getReturnType();
+        for (Method am : ams) {
+            if (am.getReturnType() == mReturnType) {
+                return am;
+            }
+        }
+        for (Method am : ams) {
+            // An overriding method might narrows the return type. The inaccessible method can be either the overrider,
+            // or the overridden. But fore example none of Number m() and String m() could override the other, as
+            // neither return type is a subtype of the other.
+            Class<?> amReturnType = am.getReturnType();
+            if (amReturnType.isAssignableFrom(mReturnType) || mReturnType.isAssignableFrom(amReturnType)) {
                 return am;
             }
         }