commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r687221 - /commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/MethodUtils.java
Date Wed, 20 Aug 2008 01:52:58 GMT
Author: niallp
Date: Tue Aug 19 18:52:57 2008
New Revision: 687221

URL: http://svn.apache.org/viewvc?rev=687221&view=rev
Log:
Factor out the common code to call Method.setAccessible(true) into a separate method

Modified:
    commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/MethodUtils.java

Modified: commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/MethodUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/MethodUtils.java?rev=687221&r1=687220&r2=687221&view=diff
==============================================================================
--- commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/MethodUtils.java
(original)
+++ commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/MethodUtils.java
Tue Aug 19 18:52:57 2008
@@ -947,54 +947,8 @@
                 log.trace("isPublic:" + Modifier.isPublic(method.getModifiers()));
             }
             
-            try {
-                //
-                // XXX Default access superclass workaround
-                //
-                // When a public class has a default access superclass
-                // with public methods, these methods are accessible.
-                // Calling them from compiled code works fine.
-                //
-                // Unfortunately, using reflection to invoke these methods
-                // seems to (wrongly) to prevent access even when the method
-                // modifer is public.
-                //
-                // The following workaround solves the problem but will only
-                // work from sufficiently privilages code. 
-                //
-                // Better workarounds would be greatfully accepted.
-                //
-                method.setAccessible(true);
-                
-            } catch (SecurityException se) {
-                // log but continue just in case the method.invoke works anyway
-                if (!loggedAccessibleWarning) {
-                    boolean vulnerableJVM = false;
-                    try {
-                        String specVersion = System.getProperty("java.specification.version");
-                        if (specVersion.charAt(0) == '1' && 
-                                (specVersion.charAt(2) == '0' ||
-                                 specVersion.charAt(2) == '1' ||
-                                 specVersion.charAt(2) == '2' ||
-                                 specVersion.charAt(2) == '3')) {
-                                 
-                            vulnerableJVM = true;
-                        }
-                    } catch (SecurityException e) {
-                        // don't know - so display warning
-                        vulnerableJVM = true;
-                    }
-                    if (vulnerableJVM) {
-                        log.warn(
-                            "Current Security Manager restricts use of workarounds for reflection
bugs "
-                            + " in pre-1.4 JVMs.");
-                    }
-                    loggedAccessibleWarning = true;
-                }
-                log.debug(
-                        "Cannot setAccessible on method. Therefore cannot use jvm access
bug workaround.", 
-                        se);
-            }
+            setMethodAccessible(method); // Default access superclass workaround
+
             cacheMethod(md, method);
             return method;
             
@@ -1042,24 +996,7 @@
                                 log.trace(method + " accessible version of " 
                                             + methods[i]);
                             }
-                            try {
-                                //
-                                // XXX Default access superclass workaround
-                                // (See above for more details.)
-                                //
-                                method.setAccessible(true);
-                                
-                            } catch (SecurityException se) {
-                                // log but continue just in case the method.invoke works
anyway
-                                if (!loggedAccessibleWarning) {
-                                    log.warn(
-            "Cannot use JVM pre-1.4 access bug workaround due to restrictive security manager.");
-                                    loggedAccessibleWarning = true;
-                                }
-                                log.debug(
-            "Cannot setAccessible on method. Therefore cannot use jvm access bug workaround.",

-                                        se);
-                            }
+                            setMethodAccessible(method); // Default access superclass workaround
                             myCost = getTotalTransformationCost(parameterTypes,method.getParameterTypes());
                             if ( myCost < bestMatchCost ) {
                                bestMatch = method;
@@ -1083,6 +1020,60 @@
     }
 
     /**
+     * Try to make the method accessible
+     * @param method The source arguments
+     */
+    private static void setMethodAccessible(Method method) {
+        try {
+            //
+            // XXX Default access superclass workaround
+            //
+            // When a public class has a default access superclass
+            // with public methods, these methods are accessible.
+            // Calling them from compiled code works fine.
+            //
+            // Unfortunately, using reflection to invoke these methods
+            // seems to (wrongly) to prevent access even when the method
+            // modifer is public.
+            //
+            // The following workaround solves the problem but will only
+            // work from sufficiently privilages code. 
+            //
+            // Better workarounds would be greatfully accepted.
+            //
+            method.setAccessible(true);
+            
+        } catch (SecurityException se) {
+            // log but continue just in case the method.invoke works anyway
+            Log log = LogFactory.getLog(MethodUtils.class);
+            if (!loggedAccessibleWarning) {
+                boolean vulnerableJVM = false;
+                try {
+                    String specVersion = System.getProperty("java.specification.version");
+                    if (specVersion.charAt(0) == '1' && 
+                            (specVersion.charAt(2) == '0' ||
+                             specVersion.charAt(2) == '1' ||
+                             specVersion.charAt(2) == '2' ||
+                             specVersion.charAt(2) == '3')) {
+                             
+                        vulnerableJVM = true;
+                    }
+                } catch (SecurityException e) {
+                    // don't know - so display warning
+                    vulnerableJVM = true;
+                }
+                if (vulnerableJVM) {
+                    log.warn(
+                        "Current Security Manager restricts use of workarounds for reflection
bugs "
+                        + " in pre-1.4 JVMs.");
+                }
+                loggedAccessibleWarning = true;
+            }
+            log.debug("Cannot setAccessible on method. Therefore cannot use jvm access bug
workaround.", se);
+        }
+    }
+
+    /**
      * Returns the sum of the object transformation cost for each class in the source
      * argument list.
      * @param srcArgs The source arguments



Mime
View raw message