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: dev-unsubscr...@velocity.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@velocity.apache.org
For additional commands, e-mail: dev-h...@velocity.apache.org

Reply via email to