Re: [PR] VELOCITY-984: Velocity calls method of abstract s uperclass leading to an IllegalAccessException [velocity-engine ]

tmortagne (via GitHub) <[email protected]>
Newsgroups gmane.comp.jakarta.velocity.devel
Message-ID <PR_kwDOAN72MM59-hKK-c330098d-9b2f-4f6a-9791-9b71fcd8e39f@gitbox.apache.org>
tmortagne commented on code in PR #54:
URL: https://github.com/apache/velocity-engine/pull/54#discussion_r1793112912


##########
velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/MethodMap.java:
##########
@@ -387,9 +393,52 @@ public static Method getTopMostMethodDeclaration(Method method)
             }
             clazz = superClass;
         }
+
         return method;
     }
 
+    private static boolean canAccess(Method method)
+    {
+        // Check if the method is public
+        if (Modifier.isPublic(method.getModifiers())) {
+            if (method.isAccessible()) {
+                // The method accessible flag was already set to true so we assume we can call it
+                return true;
+            } else {
+                try {
+                    // Check if we are able to change the accessible flag
+                    if (trySetAccessible(method)) {
+                        // Restore the accessible flag to its former value
+                        method.setAccessible(false);
+
+                        // We were able to modify the accessible flag, so we should be able to call it
+                        return true;
+                    }
+                } catch (Exception e) {
+                    // We failed to change the accessible flag, so we won't be able to call the method
+                }
+            }
+        }
+
+        return false;
+    }
+
+    private static boolean trySetAccessible(Method method)
+    {
+        boolean accessible = false;
+        if (trySetAccessible != null) {
+            try {
+                accessible = ((Boolean) trySetAccessible.invoke(method)).booleanValue();
+            } catch (Exception e) {
+                // Failed to call the trySetAccessible method, assume not accessible
+            }
+        } else {
+            method.setAccessible(true);

Review Comment:
   Yes indeed, I remember thinking about it and then I totally forgot about it. Improving that.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.