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


The following commit(s) were added to refs/heads/3 by this push:
     new dd29de93 Forward ported from 2.3-gae: FREEMARKER-216: Fixed some 
IllegalAccessException-s appearing since Java 16 (JEP 396), because invoke 
public methods on public, but internal JDK classes
dd29de93 is described below

commit dd29de9352f9b265e885547a59755ef512dea506
Author: ddekany <[email protected]>
AuthorDate: Sat Mar 9 10:57:54 2024 +0100

    Forward ported from 2.3-gae: FREEMARKER-216: Fixed some 
IllegalAccessException-s appearing since Java 16 (JEP 396), because invoke 
public methods on public, but internal JDK classes
---
 .../model/impl/NotExportedInternalPackageTest.java | 59 ++++++++++++++++++++++
 .../core/model/impl/ClassIntrospector.java         |  6 ++-
 .../core/model/impl/ExecutableMemberSignature.java |  5 ++
 3 files changed, 69 insertions(+), 1 deletion(-)

diff --git 
a/freemarker-core-test/src/test/java/org/apache/freemarker/core/model/impl/NotExportedInternalPackageTest.java
 
b/freemarker-core-test/src/test/java/org/apache/freemarker/core/model/impl/NotExportedInternalPackageTest.java
new file mode 100644
index 00000000..11badaf6
--- /dev/null
+++ 
b/freemarker-core-test/src/test/java/org/apache/freemarker/core/model/impl/NotExportedInternalPackageTest.java
@@ -0,0 +1,59 @@
+/*
+ * 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 org.apache.freemarker.core.Configuration;
+import org.apache.freemarker.core.NonTemplateCallPlace;
+import org.apache.freemarker.core.model.TemplateHashModel;
+import org.apache.freemarker.core.model.TemplateModel;
+import org.hamcrest.Matchers;
+import org.junit.Test;
+import org.w3c.dom.Document;
+import org.xml.sax.InputSource;
+
+import javax.xml.parsers.DocumentBuilderFactory;
+import java.io.StringReader;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+
+import static org.junit.Assert.*;
+
+/**
+ * FREEMARKER-216: IllegalAccessException because of JEP 396 - Strongly 
Encapsulate JDK Internals by Default
+ */
+public class NotExportedInternalPackageTest {
+    @Test
+    public void java16InternalClassAvoidanceTest() throws Exception {
+        DefaultObjectWrapper bw = new 
DefaultObjectWrapper.Builder(Configuration.VERSION_3_0_0).build();
+
+        Document document = 
DocumentBuilderFactory.newInstance().newDocumentBuilder()
+                .parse(new InputSource(new StringReader("<a></a>")));
+        TemplateHashModel documentModel = (TemplateHashModel) 
bw.wrap(document);
+
+        Method internalClassMethod = 
document.getClass().getMethod("getDocumentElement");
+        assertTrue(Modifier.isPublic(internalClassMethod.getModifiers()));
+        assertThat(internalClassMethod.getDeclaringClass().getName(), 
Matchers.startsWith("com.")); // Internal class
+
+        JavaMethodModel methodModel = (JavaMethodModel) 
documentModel.get("getDocumentElement");
+        assertNotNull(methodModel);
+
+        assertNotNull(methodModel.execute(new TemplateModel[0], 
NonTemplateCallPlace.INSTANCE)); // No IllegalAccessException
+    }
+}
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 20571f2e..27abbfde 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
@@ -61,6 +61,8 @@ class ClassIntrospector {
     private static final String JREBEL_INTEGRATION_ERROR_MSG
             = "Error initializing JRebel integration. JRebel integration 
disabled.";
 
+    private static final Module ACCESSOR_MODULE = 
ClassIntrospector.class.getModule();
+
     private static final ExecutableMemberSignature GET_STRING_SIGNATURE =
             new ExecutableMemberSignature("get", new Class[] { String.class });
     private static final ExecutableMemberSignature GET_OBJECT_SIGNATURE =
@@ -69,6 +71,7 @@ class ClassIntrospector {
             new ExecutableMemberSignature("toString", new Class[0]);
 
     private static final ClassChangeNotifier CLASS_CHANGE_NOTIFIER;
+
     static {
         boolean jRebelAvailable;
         try {
@@ -706,7 +709,8 @@ class ClassIntrospector {
 
     private static void discoverAccessibleMethods(
             Class<?> clazz, Map<ExecutableMemberSignature, List<Method>> 
accessibles) {
-        if (Modifier.isPublic(clazz.getModifiers())) {
+        if (Modifier.isPublic(clazz.getModifiers())
+                && clazz.getModule().isExported(clazz.getPackage().getName(), 
ACCESSOR_MODULE)) {
             try {
                 Method[] methods = clazz.getMethods();
                 for (Method method : methods) {
diff --git 
a/freemarker-core/src/main/java/org/apache/freemarker/core/model/impl/ExecutableMemberSignature.java
 
b/freemarker-core/src/main/java/org/apache/freemarker/core/model/impl/ExecutableMemberSignature.java
index db63d31b..5a696470 100644
--- 
a/freemarker-core/src/main/java/org/apache/freemarker/core/model/impl/ExecutableMemberSignature.java
+++ 
b/freemarker-core/src/main/java/org/apache/freemarker/core/model/impl/ExecutableMemberSignature.java
@@ -64,4 +64,9 @@ final class ExecutableMemberSignature {
     public int hashCode() {
         return name.hashCode() + args.length * 31;
     }
+
+    @Override
+    public String toString() {
+        return this.getClass().getSimpleName() + "(" + name + ", " + 
Arrays.toString(args) + ")";
+    }
 }

Reply via email to