Class PrivilegedAccessHelper


  • public class PrivilegedAccessHelper
    extends java.lang.Object
    INTERNAL: Privileged Access Helper provides a utility so all calls that require privileged access can use the same code. Do privileged blocks can be used with a security manager to grant a code base (eclipselink.jar) access to certain Java operations such as reflection. Generally a security manager is not enabled in a JVM, so this is not an issue. If a security manager is enabled, then either the application can be configured to have access to operations such as reflection, or only EclipseLink can be given access. If only EclipseLink is desired to be given access then do privileged must be enabled through the System property "eclipselink.security.usedoprivileged"=true. Note the usage of do privileged has major impacts on performance, so should normally be avoided.
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static <T> T callDoPrivileged​(PrivilegedAccessHelper.PrivilegedCallable<T> task)
      INTERNAL Executes provided PrivilegedAccessHelper.PrivilegedCallable task using AccessController.doPrivileged(PrivilegedAction) when privileged access is enabled.
      static <T> T callDoPrivilegedWithException​(PrivilegedAccessHelper.PrivilegedExceptionCallable<T> task)
      INTERNAL Executes provided PrivilegedAccessHelper.PrivilegedExceptionCallable task using AccessController.doPrivileged(PrivilegedExceptionAction) when privileged access is enabled.
      static <T,​E extends java.lang.Exception>
      T
      callDoPrivilegedWithException​(PrivilegedAccessHelper.PrivilegedExceptionCallable<T> task, PrivilegedAccessHelper.CallableExceptionSupplier<E> exception)
      INTERNAL Executes provided PrivilegedAccessHelper.PrivilegedExceptionCallable task using AccessController.doPrivileged(PrivilegedExceptionAction) when privileged access is enabled.
      static <T> java.lang.Class<T> getClassForName​(java.lang.String className)
      Execute a java Class.forName().
      static <T> java.lang.Class<T> getClassForName​(java.lang.String className, boolean initialize, java.lang.ClassLoader loader)
      Execute a java Class.forName() wrap the call in a doPrivileged block if necessary.
      static java.lang.ClassLoader getClassLoaderForClass​(java.lang.Class<?> clazz)
      Gets the class loader for a given class.
      static <T> java.lang.reflect.Constructor<T> getConstructorFor​(java.lang.Class<T> javaClass, java.lang.Class<?>[] args, boolean shouldSetAccessible)
      Get the public constructor for the given class and given arguments and wrap it in doPrivileged if necessary.
      static java.lang.ClassLoader getContextClassLoader​(java.lang.Thread thread)
      Get the context ClassLoader for a thread.
      static <T> java.lang.reflect.Constructor<T> getDeclaredConstructorFor​(java.lang.Class<T> javaClass, java.lang.Class<?>[] args, boolean shouldSetAccessible)
      Get the constructor for the given class and given arguments (regardless of whether it is public or private))and wrap it in doPrivileged if necessary.
      static java.lang.reflect.Field getDeclaredField​(java.lang.Class<?> javaClass, java.lang.String fieldName, boolean shouldSetAccessible)
      Get a field actually declared in a class and wrap the call in doPrivileged if necessary.
      static java.lang.reflect.Field[] getDeclaredFields​(java.lang.Class<?> clazz)
      Get the list of fields in a class.
      static java.lang.reflect.Method getDeclaredMethod​(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Class<?>[] methodParameterTypes)
      Return a method on a given class with the given method name and parameter types.
      static java.lang.reflect.Method[] getDeclaredMethods​(java.lang.Class<?> clazz)
      Get the list of methods in a class.
      static java.lang.reflect.Field getField​(java.lang.Class<?> javaClass, java.lang.String fieldName, boolean shouldSetAccessible)
      Get a field in a class or its superclasses and wrap the call in doPrivileged if necessary.
      static java.lang.reflect.Field[] getFields​(java.lang.Class<?> clazz)
      Get the list of public fields in a class.
      static <T> java.lang.Class<T> getFieldType​(java.lang.reflect.Field field)
      Get the return type for a given method.
      static java.lang.String getLineSeparator()
      INTERNAL: Get the line separator character.
      static java.lang.reflect.Method getMethod​(java.lang.Class<?> javaClass, java.lang.String methodName, java.lang.Class<?>[] methodParameterTypes, boolean shouldSetAccessible)
      Get a method declared in the given class.
      static java.lang.Class<?>[] getMethodParameterTypes​(java.lang.reflect.Method method)
      Get the list of parameter types for a given method.
      static <T> java.lang.Class<T> getMethodReturnType​(java.lang.reflect.Method method)
      Get the return type for a given method.
      static java.lang.reflect.Method[] getMethods​(java.lang.Class<?> clazz)
      Get the list of methods in a class.
      static java.lang.reflect.Method getPublicMethod​(java.lang.Class<?> javaClass, java.lang.String methodName, java.lang.Class<?>[] methodParameterTypes, boolean shouldSetAccessible)
      Get a public method declared in the given class.
      static java.lang.String getSystemProperty​(java.lang.String key)
      INTERNAL: Get the System property indicated by the specified key.
      static java.lang.String getSystemProperty​(java.lang.String key, java.lang.String def)
      INTERNAL: Get the System property indicated by the specified key.
      static boolean getSystemPropertyBoolean​(java.lang.String key, boolean def)
      INTERNAL: Get boolean value of the System property indicated by the specified key.
      static <T> T getValueFromField​(java.lang.reflect.Field field, java.lang.Object object)
      Get the value of the given field in the given object.
      static <T> T invokeConstructor​(java.lang.reflect.Constructor<T> constructor, java.lang.Object[] args)
      Construct an object with the given Constructor and the given array of arguments.
      static <T> T invokeMethod​(java.lang.reflect.Method method, java.lang.Object object)
      Invoke the givenMethod on a givenObject.
      static <T> T invokeMethod​(java.lang.reflect.Method method, java.lang.Object object, java.lang.Object[] parameters)
      Invoke the givenMethod on a givenObject using the array of parameters given.
      static <T> T newInstanceFromClass​(java.lang.Class<T> clazz)
      Get a new instance of a class using the default constructor.
      static java.lang.ClassLoader privilegedGetClassLoaderForClass​(java.lang.Class<?> clazz)
      Deprecated.
      Will be removed in next version.
      static void setDefaultUseDoPrivilegedValue​(boolean def)
      INTERNAL It will be used to set default value of property "eclipselink.security.usedoprivileged" if not passed as system property.
      static void setValueInField​(java.lang.reflect.Field field, java.lang.Object object, java.lang.Object value)
      Set the value of a given field in the given object with the value given.
      static boolean shouldUsePrivilegedAccess()
      This method checks to see if calls should be made to doPrivileged.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • PrivilegedAccessHelper

        public PrivilegedAccessHelper()
    • Method Detail

      • setDefaultUseDoPrivilegedValue

        public static void setDefaultUseDoPrivilegedValue​(boolean def)
        INTERNAL It will be used to set default value of property "eclipselink.security.usedoprivileged" if not passed as system property. This is used by GlassfishPlatform.
      • getClassForName

        public static <T> java.lang.Class<T> getClassForName​(java.lang.String className)
                                                      throws java.lang.ClassNotFoundException
        Execute a java Class.forName(). Wrap the call in a doPrivileged block if necessary.
        Parameters:
        className -
        Throws:
        java.lang.ClassNotFoundException
      • getClassForName

        public static <T> java.lang.Class<T> getClassForName​(java.lang.String className,
                                                             boolean initialize,
                                                             java.lang.ClassLoader loader)
                                                      throws java.lang.ClassNotFoundException
        Execute a java Class.forName() wrap the call in a doPrivileged block if necessary.
        Throws:
        java.lang.ClassNotFoundException
      • getClassLoaderForClass

        public static java.lang.ClassLoader getClassLoaderForClass​(java.lang.Class<?> clazz)
        Gets the class loader for a given class. Wraps the call in a privileged block if necessary
      • privilegedGetClassLoaderForClass

        @Deprecated
        public static java.lang.ClassLoader privilegedGetClassLoaderForClass​(java.lang.Class<?> clazz)
        Deprecated.
        Will be removed in next version.
        Gets the class loader for a given class. Wraps the call in a privileged block if necessary
      • getConstructorFor

        public static <T> java.lang.reflect.Constructor<T> getConstructorFor​(java.lang.Class<T> javaClass,
                                                                             java.lang.Class<?>[] args,
                                                                             boolean shouldSetAccessible)
                                                                      throws java.lang.NoSuchMethodException
        Get the public constructor for the given class and given arguments and wrap it in doPrivileged if necessary. The shouldSetAccessible parameter allows the the setAccessible API to be called as well. This option was added to avoid making multiple doPrivileged calls within InstantiationPolicy.
        Parameters:
        javaClass - The class to get the Constructor for
        args - An array of classes representing the argument types of the constructor
        shouldSetAccessible - whether or not to call the setAccessible API
        Throws:
        java.lang.NoSuchMethodException
      • getContextClassLoader

        public static java.lang.ClassLoader getContextClassLoader​(java.lang.Thread thread)
        Get the context ClassLoader for a thread. Wrap the call in a doPrivileged block if necessary.
      • getDeclaredConstructorFor

        public static <T> java.lang.reflect.Constructor<T> getDeclaredConstructorFor​(java.lang.Class<T> javaClass,
                                                                                     java.lang.Class<?>[] args,
                                                                                     boolean shouldSetAccessible)
                                                                              throws java.lang.NoSuchMethodException
        Get the constructor for the given class and given arguments (regardless of whether it is public or private))and wrap it in doPrivileged if necessary. The shouldSetAccessible parameter allows the the setAccessible API to be called as well. This option was added to avoid making multiple doPrivileged calls within InstantiationPolicy.
        Parameters:
        javaClass - The class to get the Constructor for
        args - An array of classes representing the argument types of the constructor
        shouldSetAccessible - whether or not to call the setAccessible API
        Throws:
        java.lang.NoSuchMethodException
      • getField

        public static java.lang.reflect.Field getField​(java.lang.Class<?> javaClass,
                                                       java.lang.String fieldName,
                                                       boolean shouldSetAccessible)
                                                throws java.lang.NoSuchFieldException
        Get a field in a class or its superclasses and wrap the call in doPrivileged if necessary. The shouldSetAccessible parameter allows the the setAccessible API to be called as well. This option was added to avoid making multiple doPrivileged calls within InstanceVariableAttributeAccessor.
        Parameters:
        javaClass - The class to get the field from
        fieldName - The name of the field
        shouldSetAccessible - whether or not to call the setAccessible API
        Throws:
        java.lang.NoSuchFieldException
      • getDeclaredField

        public static java.lang.reflect.Field getDeclaredField​(java.lang.Class<?> javaClass,
                                                               java.lang.String fieldName,
                                                               boolean shouldSetAccessible)
                                                        throws java.lang.NoSuchFieldException
        Get a field actually declared in a class and wrap the call in doPrivileged if necessary. The shouldSetAccessible parameter allows the the setAccessible API to be called as well. This option was added to avoid making multiple doPrivileged calls within InstanceVariableAttributeAccessor.
        Parameters:
        javaClass - The class to get the field from
        fieldName - The name of the field
        shouldSetAccessible - whether or not to call the setAccessible API
        Throws:
        java.lang.NoSuchFieldException
      • getDeclaredFields

        public static java.lang.reflect.Field[] getDeclaredFields​(java.lang.Class<?> clazz)
        Get the list of fields in a class. Wrap the call in doPrivileged if necessary Excludes inherited fields.
        Parameters:
        clazz - the class to get the fields from.
      • getFields

        public static java.lang.reflect.Field[] getFields​(java.lang.Class<?> clazz)
        Get the list of public fields in a class. Wrap the call in doPrivileged if necessary
        Parameters:
        clazz - the class to get the fields from.
      • getDeclaredMethod

        public static java.lang.reflect.Method getDeclaredMethod​(java.lang.Class<?> clazz,
                                                                 java.lang.String methodName,
                                                                 java.lang.Class<?>[] methodParameterTypes)
                                                          throws java.lang.NoSuchMethodException
        Return a method on a given class with the given method name and parameter types. This call will NOT traverse the superclasses. Wrap the call in doPrivileged if necessary.
        Parameters:
        method - the class to get the method from
        methodName - the name of the method to get
        methodParameters - a list of classes representing the classes of the parameters of the method.
        Throws:
        java.lang.NoSuchMethodException
      • getMethod

        public static java.lang.reflect.Method getMethod​(java.lang.Class<?> javaClass,
                                                         java.lang.String methodName,
                                                         java.lang.Class<?>[] methodParameterTypes,
                                                         boolean shouldSetAccessible)
                                                  throws java.lang.NoSuchMethodException
        Get a method declared in the given class. Wrap the call in doPrivileged if necessary. This call will traverse the superclasses. The shouldSetAccessible parameter allows the the setAccessible API to be called as well. This option was added to avoid making multiple doPrivileged calls within MethodBasedAttributeAccessor.
        Parameters:
        javaClass - The class to get the method from
        methodName - The name of the method to get
        methodParameterTypes - A list of classes representing the classes of the parameters of the mthod
        shouldSetAccessible - whether or not to call the setAccessible API
        Throws:
        java.lang.NoSuchMethodException
      • getPublicMethod

        public static java.lang.reflect.Method getPublicMethod​(java.lang.Class<?> javaClass,
                                                               java.lang.String methodName,
                                                               java.lang.Class<?>[] methodParameterTypes,
                                                               boolean shouldSetAccessible)
                                                        throws java.lang.NoSuchMethodException
        Get a public method declared in the given class. Wrap the call in doPrivileged if necessary. This call will traverse the superclasses. The shouldSetAccessible parameter allows the the setAccessible API to be called as well. This option was added to avoid making multiple doPrivileged calls within MethodBasedAttributeAccessor.
        Parameters:
        javaClass - The class to get the method from
        methodName - The name of the method to get
        methodParameterTypes - A list of classes representing the classes of the parameters of the method
        shouldSetAccessible - whether or not to call the setAccessible API
        Throws:
        java.lang.NoSuchMethodException
      • getDeclaredMethods

        public static java.lang.reflect.Method[] getDeclaredMethods​(java.lang.Class<?> clazz)
        Get the list of methods in a class. Wrap the call in doPrivileged if necessary. Excludes inherited methods.
        Parameters:
        clazz - the class to get the methods from.
      • getFieldType

        public static <T> java.lang.Class<T> getFieldType​(java.lang.reflect.Field field)
        Get the return type for a given method. Wrap the call in doPrivileged if necessary.
        Parameters:
        field -
      • getSystemProperty

        public static final java.lang.String getSystemProperty​(java.lang.String key)
        INTERNAL: Get the System property indicated by the specified key.
        Parameters:
        key - The name of the System property.
        Returns:
        The String value of the system property or null if property identified by key does not exist.
        Since:
        2.6.2
      • getSystemProperty

        public static final java.lang.String getSystemProperty​(java.lang.String key,
                                                               java.lang.String def)
        INTERNAL: Get the System property indicated by the specified key.
        Parameters:
        key - The name of the System property.
        def - The default value.
        Returns:
        The String value of the system property or def if property identified by key does not exist.
        Since:
        2.6.2
      • getSystemPropertyBoolean

        public static final boolean getSystemPropertyBoolean​(java.lang.String key,
                                                             boolean def)
        INTERNAL: Get boolean value of the System property indicated by the specified key.
        Parameters:
        key - The name of the System property.
        def - The default value.
        Returns:
        true if the property value is "true" (case insensitive) or the property is not defined and def is true; false otherwise.
        Since:
        2.6.3
      • getLineSeparator

        public static final java.lang.String getLineSeparator()
        INTERNAL: Get the line separator character.
        Returns:
        The String containing the platform-appropriate characters for line separator.
      • getMethodParameterTypes

        public static java.lang.Class<?>[] getMethodParameterTypes​(java.lang.reflect.Method method)
        Get the list of parameter types for a given method. Wrap the call in doPrivileged if necessary.
        Parameters:
        method - The method to get the parameter types of
      • getMethodReturnType

        public static <T> java.lang.Class<T> getMethodReturnType​(java.lang.reflect.Method method)
        Get the return type for a given method. Wrap the call in doPrivileged if necessary.
        Parameters:
        method -
      • getMethods

        public static java.lang.reflect.Method[] getMethods​(java.lang.Class<?> clazz)
        Get the list of methods in a class. Wrap the call in doPrivileged if necessary. This call will traverse the superclasses.
        Parameters:
        clazz - the class to get the methods from.
      • getValueFromField

        public static <T> T getValueFromField​(java.lang.reflect.Field field,
                                              java.lang.Object object)
                                       throws java.lang.IllegalAccessException
        Get the value of the given field in the given object.
        Throws:
        java.lang.IllegalAccessException
      • invokeConstructor

        public static <T> T invokeConstructor​(java.lang.reflect.Constructor<T> constructor,
                                              java.lang.Object[] args)
                                       throws java.lang.IllegalAccessException,
                                              java.lang.reflect.InvocationTargetException,
                                              java.lang.InstantiationException
        Construct an object with the given Constructor and the given array of arguments. Wrap the call in a doPrivileged block if necessary.
        Throws:
        java.lang.IllegalAccessException
        java.lang.reflect.InvocationTargetException
        java.lang.InstantiationException
      • invokeMethod

        public static <T> T invokeMethod​(java.lang.reflect.Method method,
                                         java.lang.Object object)
                                  throws java.lang.IllegalAccessException,
                                         java.lang.reflect.InvocationTargetException
        Invoke the givenMethod on a givenObject. Assumes method does not take parameters. Wrap in a doPrivileged block if necessary.
        Throws:
        java.lang.IllegalAccessException
        java.lang.reflect.InvocationTargetException
      • invokeMethod

        public static <T> T invokeMethod​(java.lang.reflect.Method method,
                                         java.lang.Object object,
                                         java.lang.Object[] parameters)
                                  throws java.lang.IllegalAccessException,
                                         java.lang.reflect.InvocationTargetException
        Invoke the givenMethod on a givenObject using the array of parameters given. Wrap in a doPrivileged block if necessary.
        Throws:
        java.lang.IllegalAccessException
        java.lang.reflect.InvocationTargetException
      • newInstanceFromClass

        public static <T> T newInstanceFromClass​(java.lang.Class<T> clazz)
                                          throws java.lang.IllegalAccessException,
                                                 java.lang.InstantiationException
        Get a new instance of a class using the default constructor. Wrap the call in a privileged block if necessary.
        Throws:
        java.lang.IllegalAccessException
        java.lang.InstantiationException
      • setValueInField

        public static void setValueInField​(java.lang.reflect.Field field,
                                           java.lang.Object object,
                                           java.lang.Object value)
                                    throws java.lang.IllegalAccessException
        Set the value of a given field in the given object with the value given. Wrap the call in a privileged block if necessary.
        Throws:
        java.lang.IllegalAccessException
      • shouldUsePrivilegedAccess

        public static boolean shouldUsePrivilegedAccess()
        This method checks to see if calls should be made to doPrivileged. It will only return true if a security manager is enabled, and the "eclipselink.security.usedoprivileged" property is set.

        Note: it is not possible to run EclipseLink using doPrivileged blocks when there is no SecurityManager enabled.