arkanovicz commented on code in PR #54:
URL: https://github.com/apache/velocity-engine/pull/54#discussion_r1793058421


##########
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:
   So this call is only for Java 8, right? And it may throw, whereas the name 
of the method is `trySetAccessible()`, which would imply it does not throw. Why 
not move the try/catch from `canAccess()` here?



##########
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);

Review Comment:
   The accessible flag is specific to the Method instance. Do we need to revert 
it to false?



-- 
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