polygene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From paulmer...@apache.org
Subject [4/9] polygene-java git commit: Use setAccessible( true ) only when necessary
Date Mon, 10 Apr 2017 11:54:44 GMT
Use setAccessible( true ) only when necessary

See o.a.p.core.api.util.AccessibleObjects
All usages are in the model construction
Keep only potentially necessary calls
Remove duplicated calls on some code paths

This commit retains support for using mixin classes declared as private
or package protected, injection to private or package protected
constructors, methods, fields etc…


Project: http://git-wip-us.apache.org/repos/asf/polygene-java/repo
Commit: http://git-wip-us.apache.org/repos/asf/polygene-java/commit/40d8d6de
Tree: http://git-wip-us.apache.org/repos/asf/polygene-java/tree/40d8d6de
Diff: http://git-wip-us.apache.org/repos/asf/polygene-java/diff/40d8d6de

Branch: refs/heads/develop
Commit: 40d8d6de0c4c459d09ffad31110b3057d175536a
Parents: e029f9b
Author: Paul Merlin <paulmerlin@apache.org>
Authored: Mon Apr 10 12:44:40 2017 +0200
Committer: Paul Merlin <paulmerlin@apache.org>
Committed: Mon Apr 10 12:44:40 2017 +0200

----------------------------------------------------------------------
 .../polygene/api/composite/PropertyMapper.java  |  1 -
 .../polygene/api/util/AccessibleObjects.java    | 57 ++++++++++++++++++++
 .../bootstrap/CompositeAssemblyImpl.java        |  1 -
 .../runtime/composite/CompositeMethodModel.java |  6 ---
 .../runtime/composite/CompositeModel.java       |  4 +-
 .../runtime/composite/ConstructorModel.java     |  4 +-
 .../runtime/composite/ConstructorsModel.java    |  2 -
 .../runtime/composite/FragmentClassLoader.java  |  4 +-
 .../runtime/composite/TransientClassLoader.java |  4 +-
 .../runtime/injection/InjectedFieldModel.java   |  4 +-
 .../runtime/injection/InjectedMethodModel.java  |  8 +--
 .../provider/UsesInjectionProviderFactory.java  |  6 +--
 12 files changed, 69 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/polygene-java/blob/40d8d6de/core/api/src/main/java/org/apache/polygene/api/composite/PropertyMapper.java
----------------------------------------------------------------------
diff --git a/core/api/src/main/java/org/apache/polygene/api/composite/PropertyMapper.java
b/core/api/src/main/java/org/apache/polygene/api/composite/PropertyMapper.java
index 0591bd4..09102d0 100644
--- a/core/api/src/main/java/org/apache/polygene/api/composite/PropertyMapper.java
+++ b/core/api/src/main/java/org/apache/polygene/api/composite/PropertyMapper.java
@@ -103,7 +103,6 @@ public final class PropertyMapper
             {
                 String methodName = objectObjectEntry.getKey().toString();
                 Method propertyMethod = composite.getClass().getInterfaces()[ 0 ].getMethod(
methodName );
-                propertyMethod.setAccessible( true );
                 Object value = objectObjectEntry.getValue();
                 Type propertyType = GenericPropertyInfo.propertyTypeOf( propertyMethod );
 

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/40d8d6de/core/api/src/main/java/org/apache/polygene/api/util/AccessibleObjects.java
----------------------------------------------------------------------
diff --git a/core/api/src/main/java/org/apache/polygene/api/util/AccessibleObjects.java b/core/api/src/main/java/org/apache/polygene/api/util/AccessibleObjects.java
new file mode 100644
index 0000000..73bb6ff
--- /dev/null
+++ b/core/api/src/main/java/org/apache/polygene/api/util/AccessibleObjects.java
@@ -0,0 +1,57 @@
+/*
+ *  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.
+ *
+ *
+ */
+package org.apache.polygene.api.util;
+
+import java.lang.reflect.AccessibleObject;
+import java.lang.reflect.Member;
+import java.lang.reflect.Modifier;
+
+public final class AccessibleObjects
+{
+    /**
+     * Ensure that a Method, Field, Constructor is accessible.
+     *
+     * If it is public, do nothing.
+     * Otherwise, use {@link AccessibleObject#setAccessible(boolean)} if it hasn't already
been done.
+     *
+     * @param accessibleObject The AccessibleObject
+     * @param <T> AccessibleObject type
+     * @return The given AccessibleObject, accessible
+     */
+    public static <T extends AccessibleObject> T accessible( T accessibleObject )
+    {
+        if( accessibleObject instanceof Member )
+        {
+            Member member = (Member) accessibleObject;
+            if( Modifier.isPublic( member.getModifiers() )
+                && Modifier.isPublic( member.getDeclaringClass().getModifiers() )
)
+            {
+                return accessibleObject;
+            }
+        }
+        if( !accessibleObject.isAccessible() )
+        {
+            accessibleObject.setAccessible( true );
+        }
+        return accessibleObject;
+    }
+
+    private AccessibleObjects() {}
+}

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/40d8d6de/core/runtime/src/main/java/org/apache/polygene/runtime/bootstrap/CompositeAssemblyImpl.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/polygene/runtime/bootstrap/CompositeAssemblyImpl.java
b/core/runtime/src/main/java/org/apache/polygene/runtime/bootstrap/CompositeAssemblyImpl.java
index fa71481..5e9b5fc 100644
--- a/core/runtime/src/main/java/org/apache/polygene/runtime/bootstrap/CompositeAssemblyImpl.java
+++ b/core/runtime/src/main/java/org/apache/polygene/runtime/bootstrap/CompositeAssemblyImpl.java
@@ -239,7 +239,6 @@ public abstract class CompositeAssemblyImpl
                                                concat( sideEffectDeclarations( mixinModel.mixinClass()
),
                                                        sideEffectClasses.stream() )
                                                                                         
    );
-                                           method.setAccessible( true );
                                            ConstraintsModel constraints = constraintsFor(
                                                method,
                                                toList( concat( constraintDeclarations( mixinModel.mixinClass()
),

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/40d8d6de/core/runtime/src/main/java/org/apache/polygene/runtime/composite/CompositeMethodModel.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/polygene/runtime/composite/CompositeMethodModel.java
b/core/runtime/src/main/java/org/apache/polygene/runtime/composite/CompositeMethodModel.java
index e2eded4..6c62ae5 100644
--- a/core/runtime/src/main/java/org/apache/polygene/runtime/composite/CompositeMethodModel.java
+++ b/core/runtime/src/main/java/org/apache/polygene/runtime/composite/CompositeMethodModel.java
@@ -72,13 +72,7 @@ public final class CompositeMethodModel
         sideEffects = sideEffectsModel;
         constraints = constraintsModel;
         constraintsInstance = constraints.newInstance();
-        initialize();
-    }
-
-    private void initialize()
-    {
         annotations = new CompositeMethodAnnotatedElement();
-        this.method.setAccessible( true );
 //        instancePool = new SynchronizedCompositeMethodInstancePool();
     }
 

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/40d8d6de/core/runtime/src/main/java/org/apache/polygene/runtime/composite/CompositeModel.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/polygene/runtime/composite/CompositeModel.java
b/core/runtime/src/main/java/org/apache/polygene/runtime/composite/CompositeModel.java
index 56659de..be159bc 100644
--- a/core/runtime/src/main/java/org/apache/polygene/runtime/composite/CompositeModel.java
+++ b/core/runtime/src/main/java/org/apache/polygene/runtime/composite/CompositeModel.java
@@ -33,6 +33,7 @@ import org.apache.polygene.api.common.Visibility;
 import org.apache.polygene.api.composite.Composite;
 import org.apache.polygene.api.composite.CompositeDescriptor;
 import org.apache.polygene.api.structure.ModuleDescriptor;
+import org.apache.polygene.api.util.AccessibleObjects;
 import org.apache.polygene.api.util.HierarchicalVisitor;
 import org.apache.polygene.api.util.VisitableHierarchy;
 import org.apache.polygene.runtime.injection.Dependencies;
@@ -215,8 +216,7 @@ public abstract class CompositeModel
         {
             constructor = (Constructor<? extends Composite>) proxyClass.getConstructors()[
0 ];
         }
-        constructor.setAccessible( true );
-        return constructor;
+        return AccessibleObjects.accessible( constructor );
     }
 
     // Context

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/40d8d6de/core/runtime/src/main/java/org/apache/polygene/runtime/composite/ConstructorModel.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/polygene/runtime/composite/ConstructorModel.java
b/core/runtime/src/main/java/org/apache/polygene/runtime/composite/ConstructorModel.java
index 3df1ad5..f3f60fb 100644
--- a/core/runtime/src/main/java/org/apache/polygene/runtime/composite/ConstructorModel.java
+++ b/core/runtime/src/main/java/org/apache/polygene/runtime/composite/ConstructorModel.java
@@ -27,6 +27,7 @@ import java.util.stream.Stream;
 import org.apache.polygene.api.common.ConstructionException;
 import org.apache.polygene.api.composite.ConstructorDescriptor;
 import org.apache.polygene.api.composite.InvalidCompositeException;
+import org.apache.polygene.api.util.AccessibleObjects;
 import org.apache.polygene.api.util.HierarchicalVisitor;
 import org.apache.polygene.api.util.VisitableHierarchy;
 import org.apache.polygene.runtime.injection.DependencyModel;
@@ -45,9 +46,8 @@ public final class ConstructorModel
 
     public ConstructorModel( Constructor<?> constructor, InjectedParametersModel parameters
)
     {
-        this.constructor = constructor;
+        this.constructor = AccessibleObjects.accessible( constructor);
         this.parameters = parameters;
-        this.constructor.setAccessible( true );
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/40d8d6de/core/runtime/src/main/java/org/apache/polygene/runtime/composite/ConstructorsModel.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/polygene/runtime/composite/ConstructorsModel.java
b/core/runtime/src/main/java/org/apache/polygene/runtime/composite/ConstructorsModel.java
index e41d40c..56b5d85 100644
--- a/core/runtime/src/main/java/org/apache/polygene/runtime/composite/ConstructorsModel.java
+++ b/core/runtime/src/main/java/org/apache/polygene/runtime/composite/ConstructorsModel.java
@@ -72,11 +72,9 @@ public final class ConstructorsModel
         Class<?> injectionClass = FragmentClassLoader.getSourceClass( fragmentClass
);
         for( Constructor<?> constructor : realConstructors )
         {
-            constructor.setAccessible( true );
             try
             {
                 Constructor<?> injectionConstructor = injectionClass.getDeclaredConstructor(
constructor.getParameterTypes() );
-                injectionConstructor.setAccessible( true );
                 ConstructorModel constructorModel = newConstructorModel( this.fragmentClass,
constructor,
                                                                          injectionConstructor
);
                 if( constructorModel != null )

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/40d8d6de/core/runtime/src/main/java/org/apache/polygene/runtime/composite/FragmentClassLoader.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/polygene/runtime/composite/FragmentClassLoader.java
b/core/runtime/src/main/java/org/apache/polygene/runtime/composite/FragmentClassLoader.java
index 440e023..2603d99 100644
--- a/core/runtime/src/main/java/org/apache/polygene/runtime/composite/FragmentClassLoader.java
+++ b/core/runtime/src/main/java/org/apache/polygene/runtime/composite/FragmentClassLoader.java
@@ -420,7 +420,6 @@ public class FragmentClassLoader
                 {
                     if( isOverridden( method, baseClass ) )
                     {
-                        method.setAccessible( true );
                         Class methodClass;
                         if( Modifier.isAbstract( method.getModifiers() ) )
                         {
@@ -544,8 +543,7 @@ public class FragmentClassLoader
     {
         try
         {
-            Method m = intface.getMethod( method.getName(), method.getParameterTypes() );
-            m.setAccessible( true );
+            intface.getMethod( method.getName(), method.getParameterTypes() );
             return true;
         }
         catch( NoSuchMethodException e )

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/40d8d6de/core/runtime/src/main/java/org/apache/polygene/runtime/composite/TransientClassLoader.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/polygene/runtime/composite/TransientClassLoader.java
b/core/runtime/src/main/java/org/apache/polygene/runtime/composite/TransientClassLoader.java
index 9f63677..68c4fe3 100644
--- a/core/runtime/src/main/java/org/apache/polygene/runtime/composite/TransientClassLoader.java
+++ b/core/runtime/src/main/java/org/apache/polygene/runtime/composite/TransientClassLoader.java
@@ -390,7 +390,6 @@ import static org.objectweb.asm.Type.getInternalName;
             {
                 if( isOverloaded( method, baseClass ) )
                 {
-                    method.setAccessible( true );
                     Class methodClass = method.getDeclaringClass();
 
                     midx++;
@@ -484,8 +483,7 @@ import static org.objectweb.asm.Type.getInternalName;
         return interfacesOf( baseClass ).map( RAW_CLASS ).filter( Methods.HAS_METHODS ).anyMatch(
clazz -> {
             try
             {
-                Method m = clazz.getMethod( method.getName(), method.getParameterTypes()
);
-                m.setAccessible( true );
+                clazz.getMethod( method.getName(), method.getParameterTypes() );
                 return true;
             }
             catch( NoSuchMethodException e )

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/40d8d6de/core/runtime/src/main/java/org/apache/polygene/runtime/injection/InjectedFieldModel.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/polygene/runtime/injection/InjectedFieldModel.java
b/core/runtime/src/main/java/org/apache/polygene/runtime/injection/InjectedFieldModel.java
index e4fd289..553f1ff 100644
--- a/core/runtime/src/main/java/org/apache/polygene/runtime/injection/InjectedFieldModel.java
+++ b/core/runtime/src/main/java/org/apache/polygene/runtime/injection/InjectedFieldModel.java
@@ -29,6 +29,7 @@ import java.util.function.Predicate;
 import java.util.stream.Stream;
 import org.apache.polygene.api.composite.DependencyDescriptor;
 import org.apache.polygene.api.composite.InjectedFieldDescriptor;
+import org.apache.polygene.api.util.AccessibleObjects;
 import org.apache.polygene.api.util.Classes;
 import org.apache.polygene.api.util.HierarchicalVisitor;
 import org.apache.polygene.api.util.VisitableHierarchy;
@@ -51,8 +52,7 @@ public final class InjectedFieldModel
 
     public InjectedFieldModel( Field injectedField, DependencyModel dependencyModel )
     {
-        injectedField.setAccessible( true );
-        this.injectedField = injectedField;
+        this.injectedField = AccessibleObjects.accessible( injectedField );
         this.dependencyModel = dependencyModel;
     }
 

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/40d8d6de/core/runtime/src/main/java/org/apache/polygene/runtime/injection/InjectedMethodModel.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/polygene/runtime/injection/InjectedMethodModel.java
b/core/runtime/src/main/java/org/apache/polygene/runtime/injection/InjectedMethodModel.java
index f1dc19b..9df8692 100644
--- a/core/runtime/src/main/java/org/apache/polygene/runtime/injection/InjectedMethodModel.java
+++ b/core/runtime/src/main/java/org/apache/polygene/runtime/injection/InjectedMethodModel.java
@@ -24,6 +24,7 @@ import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.util.stream.Stream;
 import org.apache.polygene.api.composite.InjectedMethodDescriptor;
+import org.apache.polygene.api.util.AccessibleObjects;
 import org.apache.polygene.api.util.HierarchicalVisitor;
 import org.apache.polygene.api.util.VisitableHierarchy;
 import org.apache.polygene.bootstrap.InjectionException;
@@ -40,8 +41,7 @@ public final class InjectedMethodModel
 
     public InjectedMethodModel( Method method, InjectedParametersModel parameters )
     {
-        this.method = method;
-        this.method.setAccessible( true );
+        this.method = AccessibleObjects.accessible( method );
         this.parameters = parameters;
     }
 
@@ -64,10 +64,6 @@ public final class InjectedMethodModel
         Object[] params = parameters.newParametersInstance( context );
         try
         {
-            if( !method.isAccessible() )
-            {
-                method.setAccessible( true );
-            }
             method.invoke( instance, params );
         }
         catch( IllegalAccessException e )

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/40d8d6de/core/runtime/src/main/java/org/apache/polygene/runtime/injection/provider/UsesInjectionProviderFactory.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/polygene/runtime/injection/provider/UsesInjectionProviderFactory.java
b/core/runtime/src/main/java/org/apache/polygene/runtime/injection/provider/UsesInjectionProviderFactory.java
index f4549c5..6b21d29 100644
--- a/core/runtime/src/main/java/org/apache/polygene/runtime/injection/provider/UsesInjectionProviderFactory.java
+++ b/core/runtime/src/main/java/org/apache/polygene/runtime/injection/provider/UsesInjectionProviderFactory.java
@@ -23,6 +23,7 @@ import java.lang.reflect.Constructor;
 import org.apache.polygene.api.composite.NoSuchTransientException;
 import org.apache.polygene.api.object.NoSuchObjectException;
 import org.apache.polygene.api.structure.Module;
+import org.apache.polygene.api.util.AccessibleObjects;
 import org.apache.polygene.bootstrap.InvalidInjectionException;
 import org.apache.polygene.runtime.composite.UsesInstance;
 import org.apache.polygene.runtime.injection.DependencyModel;
@@ -102,10 +103,7 @@ public final class UsesInjectionProviderFactory
                             {
                                 Constructor constructor = injectionType.getDeclaredConstructor(
context.instance()
                                                                                         
           .getClass() );
-                                if( !constructor.isAccessible() )
-                                {
-                                    constructor.setAccessible( true );
-                                }
+                                AccessibleObjects.accessible( constructor );
                                 usesObject = constructor.newInstance( context.instance()
);
                             }
                             catch( Throwable e3 )


Mime
View raw message