You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by si...@apache.org on 2012/01/21 16:12:37 UTC

svn commit: r1234342 - in /commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2: DefaultClassAccessor.java PrivilegedAccessibleAction.java

Author: simonetripodi
Date: Sat Jan 21 15:12:36 2012
New Revision: 1234342

URL: http://svn.apache.org/viewvc?rev=1234342&view=rev
Log:
make constructors accessible via privileged action

Added:
    commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/PrivilegedAccessibleAction.java   (with props)
Modified:
    commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultClassAccessor.java

Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultClassAccessor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultClassAccessor.java?rev=1234342&r1=1234341&r2=1234342&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultClassAccessor.java (original)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultClassAccessor.java Sat Jan 21 15:12:36 2012
@@ -20,6 +20,7 @@ package org.apache.commons.beanutils2;
 import static java.lang.reflect.Modifier.isPublic;
 import static org.apache.commons.beanutils2.Argument.getParameterTypes;
 import static org.apache.commons.beanutils2.Assertions.checkNotNull;
+import static org.apache.commons.beanutils2.PrivilegedAccessibleAction.makeAccessible;
 import static org.apache.commons.beanutils2.TypeUtils.isAssignmentCompatible;
 
 import java.lang.reflect.Constructor;
@@ -130,31 +131,7 @@ final class DefaultClassAccessor<B>
         try
         {
             Constructor<B> ctor = beanClass.getConstructor( parameterTypes );
-            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.
-                //
-                ctor.setAccessible( true );
-            }
-            catch ( SecurityException se )
-            {
-                /* SWALLOW, if workaround fails don't fret. */
-            }
-            return ctor;
+            return makeAccessible( ctor );
         }
         catch ( NoSuchMethodException e )
         {
@@ -187,16 +164,6 @@ final class DefaultClassAccessor<B>
                     ctor = getAccessibleConstructor( ctor );
                     if ( ctor != null )
                     {
-                        try
-                        {
-                            ctor.setAccessible( true );
-                        }
-                        catch ( SecurityException se )
-                        {
-                            /*
-                             * Swallow SecurityException TODO: Why?
-                             */
-                        }
                         @SuppressWarnings( "unchecked" ) // TODO verify, it should be assignable
                         Constructor<? extends B> returned = (Constructor<? extends B>) ctor;
                         return returned;
@@ -232,7 +199,7 @@ final class DefaultClassAccessor<B>
         Class<T> beanClass = ctor.getDeclaringClass();
         if ( isPublic( beanClass.getModifiers() ) )
         {
-            return ( ctor );
+            return makeAccessible( ctor );
         }
 
         // what else can we do?

Added: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/PrivilegedAccessibleAction.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/PrivilegedAccessibleAction.java?rev=1234342&view=auto
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/PrivilegedAccessibleAction.java (added)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/PrivilegedAccessibleAction.java Sat Jan 21 15:12:36 2012
@@ -0,0 +1,70 @@
+package org.apache.commons.beanutils2;
+
+/*
+ * 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.
+ */
+
+import static java.lang.System.getSecurityManager;
+import static java.security.AccessController.doPrivileged;
+
+import java.lang.reflect.AccessibleObject;
+import java.security.PrivilegedAction;
+
+final class PrivilegedAccessibleAction<T extends AccessibleObject>
+    implements PrivilegedAction<T>
+{
+
+    public static <T extends AccessibleObject> T makeAccessible( T accessibleObject )
+    {
+        PrivilegedAccessibleAction<T> action = new PrivilegedAccessibleAction<T>( accessibleObject );
+        if ( getSecurityManager() != null )
+        {
+            return doPrivileged( action );
+        }
+        return action.run();
+    }
+
+    private final T accessibleObject;
+
+    /**
+     * hidden constructor, this class cannot be instantiated directly
+     */
+    private PrivilegedAccessibleAction( T accessibleObject )
+    {
+        this.accessibleObject = accessibleObject;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public T run()
+    {
+        try
+        {
+            accessibleObject.setAccessible( true );
+        }
+        catch ( SecurityException se )
+        {
+            /*
+             * Swallow SecurityException TODO: Why?
+             */
+        }
+        return accessibleObject;
+    }
+
+}

Propchange: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/PrivilegedAccessibleAction.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/PrivilegedAccessibleAction.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/PrivilegedAccessibleAction.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain