Java Code Examples for sun.reflect.Reflection#getCallerClass()

The following examples show how to use sun.reflect.Reflection#getCallerClass() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DriverManager.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Retrieves an Enumeration with all of the currently loaded JDBC drivers
 * to which the current caller has access.
 *
 * <P><B>Note:</B> The classname of a driver can be found using
 * <CODE>d.getClass().getName()</CODE>
 *
 * @return the list of JDBC Drivers loaded by the caller's class loader
 */
@CallerSensitive
public static java.util.Enumeration<Driver> getDrivers() {
    java.util.Vector<Driver> result = new java.util.Vector<>();

    Class<?> callerClass = Reflection.getCallerClass();

    // Walk through the loaded registeredDrivers.
    for(DriverInfo aDriver : registeredDrivers) {
        // If the caller does not have permission to load the driver then
        // skip it.
        if(isDriverAllowed(aDriver.driver, callerClass)) {
            result.addElement(aDriver.driver);
        } else {
            println("    skipping: " + aDriver.getClass().getName());
        }
    }
    return (result.elements());
}
 
Example 2
Source File: DriverManager.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Retrieves an Enumeration with all of the currently loaded JDBC drivers
 * to which the current caller has access.
 *
 * <P><B>Note:</B> The classname of a driver can be found using
 * <CODE>d.getClass().getName()</CODE>
 *
 * @return the list of JDBC Drivers loaded by the caller's class loader
 */
@CallerSensitive
public static java.util.Enumeration<Driver> getDrivers() {
    java.util.Vector<Driver> result = new java.util.Vector<>();

    Class<?> callerClass = Reflection.getCallerClass();

    // Walk through the loaded registeredDrivers.
    for(DriverInfo aDriver : registeredDrivers) {
        // If the caller does not have permission to load the driver then
        // skip it.
        if(isDriverAllowed(aDriver.driver, callerClass)) {
            result.addElement(aDriver.driver);
        } else {
            println("    skipping: " + aDriver.getClass().getName());
        }
    }
    return (result.elements());
}
 
Example 3
Source File: Proxy.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the invocation handler for the specified proxy instance.
 *
 * @param   proxy the proxy instance to return the invocation handler for
 * @return  the invocation handler for the proxy instance
 * @throws  IllegalArgumentException if the argument is not a
 *          proxy instance
 * @throws  SecurityException if a security manager, <em>s</em>, is present
 *          and the caller's class loader is not the same as or an
 *          ancestor of the class loader for the invocation handler
 *          and invocation of {@link SecurityManager#checkPackageAccess
 *          s.checkPackageAccess()} denies access to the invocation
 *          handler's class.
 */
@CallerSensitive
public static InvocationHandler getInvocationHandler(Object proxy)
    throws IllegalArgumentException
{
    /*
     * Verify that the object is actually a proxy instance.
     */
    if (!isProxyClass(proxy.getClass())) {
        throw new IllegalArgumentException("not a proxy instance");
    }

    final Proxy p = (Proxy) proxy;
    final InvocationHandler ih = p.h;
    if (System.getSecurityManager() != null) {
        Class<?> ihClass = ih.getClass();
        Class<?> caller = Reflection.getCallerClass();
        if (ReflectUtil.needsPackageAccessCheck(caller.getClassLoader(),
                                                ihClass.getClassLoader()))
        {
            ReflectUtil.checkPackageAccess(ihClass);
        }
    }

    return ih;
}
 
Example 4
Source File: ObjectStreamField.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the type of the field.  If the type is non-primitive and this
 * <code>ObjectStreamField</code> was obtained from a deserialized {@link
 * ObjectStreamClass} instance, then <code>Object.class</code> is returned.
 * Otherwise, the <code>Class</code> object for the type of the field is
 * returned.
 *
 * @return  a <code>Class</code> object representing the type of the
 *          serializable field
 */
@CallerSensitive
public Class<?> getType() {
    if (System.getSecurityManager() != null) {
        Class<?> caller = Reflection.getCallerClass();
        if (ReflectUtil.needsPackageAccessCheck(caller.getClassLoader(), type.getClassLoader())) {
            ReflectUtil.checkPackageAccess(type);
        }
    }
    return type;
}
 
Example 5
Source File: AccessController.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
/**
 * Performs the specified {@code PrivilegedAction} with privileges
 * enabled and restricted by the specified
 * {@code AccessControlContext} and with a privilege scope limited
 * by specified {@code Permission} arguments.
 *
 * The action is performed with the intersection of the permissions
 * possessed by the caller's protection domain, and those possessed
 * by the domains represented by the specified
 * {@code AccessControlContext}.
 * <p>
 * If the action's {@code run} method throws an (unchecked) exception,
 * it will propagate through this method.
 *
 * <p> This method preserves the current AccessControlContext's
 * DomainCombiner (which may be null) while the action is performed.
 * <p>
 * If a security manager is installed and the specified
 * {@code AccessControlContext} was not created by system code and the
 * caller's {@code ProtectionDomain} has not been granted the
 * {@literal "createAccessControlContext"}
 * {@link java.security.SecurityPermission}, then the action is performed
 * with no permissions.
 *
 * @param <T> the type of the value returned by the PrivilegedAction's
 *                  {@code run} method.
 * @param action the action to be performed.
 * @param context an <i>access control context</i>
 *                representing the restriction to be applied to the
 *                caller's domain's privileges before performing
 *                the specified action.  If the context is
 *                {@code null},
 *                then no additional restriction is applied.
 * @param perms the {@code Permission} arguments which limit the
 *              scope of the caller's privileges. The number of arguments
 *              is variable.
 *
 * @return the value returned by the action's {@code run} method.
 *
 * @throws NullPointerException if action or perms or any element of
 *         perms is {@code null}
 *
 * @see #doPrivileged(PrivilegedAction)
 * @see #doPrivileged(PrivilegedExceptionAction,AccessControlContext)
 * @see java.security.DomainCombiner
 *
 * @since 1.8
 */
@CallerSensitive
public static <T> T doPrivilegedWithCombiner(PrivilegedAction<T> action,
    AccessControlContext context, Permission... perms) {

    AccessControlContext parent = getContext();
    DomainCombiner dc = parent.getCombiner();
    if (dc == null && context != null) {
        dc = context.getCombiner();
    }
    if (perms == null) {
        throw new NullPointerException("null permissions parameter");
    }
    Class <?> caller = Reflection.getCallerClass();
    return AccessController.doPrivileged(action, createWrapper(dc, caller,
        parent, context, perms));
}
 
Example 6
Source File: AccessController.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
/**
 * Performs the specified {@code PrivilegedExceptionAction} with
 * privileges enabled and restricted by the specified
 * {@code AccessControlContext} and with a privilege scope limited by
 * specified {@code Permission} arguments.
 *
 * The action is performed with the intersection of the permissions
 * possessed by the caller's protection domain, and those possessed
 * by the domains represented by the specified
 * {@code AccessControlContext}.
 * <p>
 * If the action's {@code run} method throws an (unchecked) exception,
 * it will propagate through this method.
 *
 * <p> This method preserves the current AccessControlContext's
 * DomainCombiner (which may be null) while the action is performed.
 * <p>
 * If a security manager is installed and the specified
 * {@code AccessControlContext} was not created by system code and the
 * caller's {@code ProtectionDomain} has not been granted the
 * {@literal "createAccessControlContext"}
 * {@link java.security.SecurityPermission}, then the action is performed
 * with no permissions.
 *
 * @param <T> the type of the value returned by the
 *                  PrivilegedExceptionAction's {@code run} method.
 * @param action the action to be performed.
 * @param context an <i>access control context</i>
 *                representing the restriction to be applied to the
 *                caller's domain's privileges before performing
 *                the specified action.  If the context is
 *                {@code null},
 *                then no additional restriction is applied.
 * @param perms the {@code Permission} arguments which limit the
 *              scope of the caller's privileges. The number of arguments
 *              is variable.
 *
 * @return the value returned by the action's {@code run} method.
 *
 * @throws PrivilegedActionException if the specified action's
 *         {@code run} method threw a <i>checked</i> exception
 * @throws NullPointerException if action or perms or any element of
 *         perms is {@code null}
 *
 * @see #doPrivileged(PrivilegedAction)
 * @see #doPrivileged(PrivilegedAction,AccessControlContext)
 * @see java.security.DomainCombiner
 *
 * @since 1.8
 */
@CallerSensitive
public static <T> T doPrivilegedWithCombiner(PrivilegedExceptionAction<T> action,
                                             AccessControlContext context,
                                             Permission... perms)
    throws PrivilegedActionException
{
    AccessControlContext parent = getContext();
    DomainCombiner dc = parent.getCombiner();
    if (dc == null && context != null) {
        dc = context.getCombiner();
    }
    if (perms == null) {
        throw new NullPointerException("null permissions parameter");
    }
    Class <?> caller = Reflection.getCallerClass();
    return AccessController.doPrivileged(action, createWrapper(dc, caller,
        parent, context, perms));
}
 
Example 7
Source File: GetCallerClassWithDepth.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
static void deepest() {
    // 0: Reflection 1: deepest 2: deeper 3: deep 4: Test.selfTest
    Class<?> c = Reflection.getCallerClass(4);
    assertEquals(c, Test.class);
}
 
Example 8
Source File: Method.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Invokes the underlying method represented by this {@code Method}
 * object, on the specified object with the specified parameters.
 * Individual parameters are automatically unwrapped to match
 * primitive formal parameters, and both primitive and reference
 * parameters are subject to method invocation conversions as
 * necessary.
 *
 * <p>If the underlying method is static, then the specified {@code obj}
 * argument is ignored. It may be null.
 *
 * <p>If the number of formal parameters required by the underlying method is
 * 0, the supplied {@code args} array may be of length 0 or null.
 *
 * <p>If the underlying method is an instance method, it is invoked
 * using dynamic method lookup as documented in The Java Language
 * Specification, Second Edition, section 15.12.4.4; in particular,
 * overriding based on the runtime type of the target object will occur.
 *
 * <p>If the underlying method is static, the class that declared
 * the method is initialized if it has not already been initialized.
 *
 * <p>If the method completes normally, the value it returns is
 * returned to the caller of invoke; if the value has a primitive
 * type, it is first appropriately wrapped in an object. However,
 * if the value has the type of an array of a primitive type, the
 * elements of the array are <i>not</i> wrapped in objects; in
 * other words, an array of primitive type is returned.  If the
 * underlying method return type is void, the invocation returns
 * null.
 *
 * @param obj  the object the underlying method is invoked from
 * @param args the arguments used for the method call
 * @return the result of dispatching the method represented by
 * this object on {@code obj} with parameters
 * {@code args}
 *
 * @exception IllegalAccessException    if this {@code Method} object
 *              is enforcing Java language access control and the underlying
 *              method is inaccessible.
 * @exception IllegalArgumentException  if the method is an
 *              instance method and the specified object argument
 *              is not an instance of the class or interface
 *              declaring the underlying method (or of a subclass
 *              or implementor thereof); if the number of actual
 *              and formal parameters differ; if an unwrapping
 *              conversion for primitive arguments fails; or if,
 *              after possible unwrapping, a parameter value
 *              cannot be converted to the corresponding formal
 *              parameter type by a method invocation conversion.
 * @exception InvocationTargetException if the underlying method
 *              throws an exception.
 * @exception NullPointerException      if the specified object is null
 *              and the method is an instance method.
 * @exception ExceptionInInitializerError if the initialization
 * provoked by this method fails.
 */
@CallerSensitive
public Object invoke(Object obj, Object... args)
    throws IllegalAccessException, IllegalArgumentException,
       InvocationTargetException
{
    if (!override) {
        if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
            Class<?> caller = Reflection.getCallerClass();
            checkAccess(caller, clazz, obj, modifiers);
        }
    }
    MethodAccessor ma = methodAccessor;             // read volatile
    if (ma == null) {
        ma = acquireMethodAccessor();
    }
    return ma.invoke(obj, args);
}
 
Example 9
Source File: Constructor.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
/**
 * Uses the constructor represented by this {@code Constructor} object to
 * create and initialize a new instance of the constructor's
 * declaring class, with the specified initialization parameters.
 * Individual parameters are automatically unwrapped to match
 * primitive formal parameters, and both primitive and reference
 * parameters are subject to method invocation conversions as necessary.
 *
 * <p>If the number of formal parameters required by the underlying constructor
 * is 0, the supplied {@code initargs} array may be of length 0 or null.
 *
 * <p>If the constructor's declaring class is an inner class in a
 * non-static context, the first argument to the constructor needs
 * to be the enclosing instance; see section 15.9.3 of
 * <cite>The Java&trade; Language Specification</cite>.
 *
 * <p>If the required access and argument checks succeed and the
 * instantiation will proceed, the constructor's declaring class
 * is initialized if it has not already been initialized.
 *
 * <p>If the constructor completes normally, returns the newly
 * created and initialized instance.
 *
 * @param initargs array of objects to be passed as arguments to
 * the constructor call; values of primitive types are wrapped in
 * a wrapper object of the appropriate type (e.g. a {@code float}
 * in a {@link java.lang.Float Float})
 *
 * @return a new object created by calling the constructor
 * this object represents
 *
 * @exception IllegalAccessException    if this {@code Constructor} object
 *              is enforcing Java language access control and the underlying
 *              constructor is inaccessible.
 * @exception IllegalArgumentException  if the number of actual
 *              and formal parameters differ; if an unwrapping
 *              conversion for primitive arguments fails; or if,
 *              after possible unwrapping, a parameter value
 *              cannot be converted to the corresponding formal
 *              parameter type by a method invocation conversion; if
 *              this constructor pertains to an enum type.
 * @exception InstantiationException    if the class that declares the
 *              underlying constructor represents an abstract class.
 * @exception InvocationTargetException if the underlying constructor
 *              throws an exception.
 * @exception ExceptionInInitializerError if the initialization provoked
 *              by this method fails.
 */
@CallerSensitive
public T newInstance(Object ... initargs)
    throws InstantiationException, IllegalAccessException,
           IllegalArgumentException, InvocationTargetException
{
    if (!override) {
        if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
            Class<?> caller = Reflection.getCallerClass();
            checkAccess(caller, clazz, null, modifiers);
        }
    }
    if ((clazz.getModifiers() & Modifier.ENUM) != 0)
        throw new IllegalArgumentException("Cannot reflectively create enum objects");
    ConstructorAccessor ca = constructorAccessor;   // read volatile
    if (ca == null) {
        ca = acquireConstructorAccessor();
    }
    @SuppressWarnings("unchecked")
    T inst = (T) ca.newInstance(initargs);
    return inst;
}
 
Example 10
Source File: Field.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Sets the value of a field as a {@code short} on the specified object.
 * This method is equivalent to
 * {@code set(obj, sObj)},
 * where {@code sObj} is a {@code Short} object and
 * {@code sObj.shortValue() == s}.
 *
 * @param obj the object whose field should be modified
 * @param s   the new value for the field of {@code obj}
 * being modified
 *
 * @exception IllegalAccessException    if this {@code Field} object
 *              is enforcing Java language access control and the underlying
 *              field is either inaccessible or final.
 * @exception IllegalArgumentException  if the specified object is not an
 *              instance of the class or interface declaring the underlying
 *              field (or a subclass or implementor thereof),
 *              or if an unwrapping conversion fails.
 * @exception NullPointerException      if the specified object is null
 *              and the field is an instance field.
 * @exception ExceptionInInitializerError if the initialization provoked
 *              by this method fails.
 * @see       Field#set
 */
@CallerSensitive
public void setShort(Object obj, short s)
    throws IllegalArgumentException, IllegalAccessException
{
    if (!override) {
        if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
            Class<?> caller = Reflection.getCallerClass();
            checkAccess(caller, clazz, obj, modifiers);
        }
    }
    getFieldAccessor(obj).setShort(obj, s);
}
 
Example 11
Source File: Field.java    From jdk8u-dev-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Gets the value of a static or instance field of type
 * {@code char} or of another primitive type convertible to
 * type {@code char} via a widening conversion.
 *
 * @param obj the object to extract the {@code char} value
 * from
 * @return the value of the field converted to type {@code char}
 *
 * @exception IllegalAccessException    if this {@code Field} object
 *              is enforcing Java language access control and the underlying
 *              field is inaccessible.
 * @exception IllegalArgumentException  if the specified object is not
 *              an instance of the class or interface declaring the
 *              underlying field (or a subclass or implementor
 *              thereof), or if the field value cannot be
 *              converted to the type {@code char} by a
 *              widening conversion.
 * @exception NullPointerException      if the specified object is null
 *              and the field is an instance field.
 * @exception ExceptionInInitializerError if the initialization provoked
 *              by this method fails.
 * @see Field#get
 */
@CallerSensitive
public char getChar(Object obj)
    throws IllegalArgumentException, IllegalAccessException
{
    if (!override) {
        if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
            Class<?> caller = Reflection.getCallerClass();
            checkAccess(caller, clazz, obj, modifiers);
        }
    }
    return getFieldAccessor(obj).getChar(obj);
}
 
Example 12
Source File: Field.java    From JDKSourceCode1.8 with MIT License 3 votes vote down vote up
/**
 * Gets the value of a static or instance {@code boolean} field.
 *
 * @param obj the object to extract the {@code boolean} value
 * from
 * @return the value of the {@code boolean} field
 *
 * @exception IllegalAccessException    if this {@code Field} object
 *              is enforcing Java language access control and the underlying
 *              field is inaccessible.
 * @exception IllegalArgumentException  if the specified object is not
 *              an instance of the class or interface declaring the
 *              underlying field (or a subclass or implementor
 *              thereof), or if the field value cannot be
 *              converted to the type {@code boolean} by a
 *              widening conversion.
 * @exception NullPointerException      if the specified object is null
 *              and the field is an instance field.
 * @exception ExceptionInInitializerError if the initialization provoked
 *              by this method fails.
 * @see       Field#get
 */
@CallerSensitive
public boolean getBoolean(Object obj)
    throws IllegalArgumentException, IllegalAccessException
{
    if (!override) {
        if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
            Class<?> caller = Reflection.getCallerClass();
            checkAccess(caller, clazz, obj, modifiers);
        }
    }
    return getFieldAccessor(obj).getBoolean(obj);
}
 
Example 13
Source File: Field.java    From TencentKona-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Sets the value of a field as a {@code char} on the specified object.
 * This method is equivalent to
 * {@code set(obj, cObj)},
 * where {@code cObj} is a {@code Character} object and
 * {@code cObj.charValue() == c}.
 *
 * @param obj the object whose field should be modified
 * @param c   the new value for the field of {@code obj}
 * being modified
 *
 * @exception IllegalAccessException    if this {@code Field} object
 *              is enforcing Java language access control and the underlying
 *              field is either inaccessible or final.
 * @exception IllegalArgumentException  if the specified object is not an
 *              instance of the class or interface declaring the underlying
 *              field (or a subclass or implementor thereof),
 *              or if an unwrapping conversion fails.
 * @exception NullPointerException      if the specified object is null
 *              and the field is an instance field.
 * @exception ExceptionInInitializerError if the initialization provoked
 *              by this method fails.
 * @see       Field#set
 */
@CallerSensitive
public void setChar(Object obj, char c)
    throws IllegalArgumentException, IllegalAccessException
{
    if (!override) {
        if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
            Class<?> caller = Reflection.getCallerClass();
            checkAccess(caller, clazz, obj, modifiers);
        }
    }
    getFieldAccessor(obj).setChar(obj, c);
}
 
Example 14
Source File: Field.java    From dragonwell8_jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Gets the value of a static or instance field of type
 * {@code long} or of another primitive type convertible to
 * type {@code long} via a widening conversion.
 *
 * @param obj the object to extract the {@code long} value
 * from
 * @return the value of the field converted to type {@code long}
 *
 * @exception IllegalAccessException    if this {@code Field} object
 *              is enforcing Java language access control and the underlying
 *              field is inaccessible.
 * @exception IllegalArgumentException  if the specified object is not
 *              an instance of the class or interface declaring the
 *              underlying field (or a subclass or implementor
 *              thereof), or if the field value cannot be
 *              converted to the type {@code long} by a
 *              widening conversion.
 * @exception NullPointerException      if the specified object is null
 *              and the field is an instance field.
 * @exception ExceptionInInitializerError if the initialization provoked
 *              by this method fails.
 * @see       Field#get
 */
@CallerSensitive
public long getLong(Object obj)
    throws IllegalArgumentException, IllegalAccessException
{
    if (!override) {
        if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
            Class<?> caller = Reflection.getCallerClass();
            checkAccess(caller, clazz, obj, modifiers);
        }
    }
    return getFieldAccessor(obj).getLong(obj);
}
 
Example 15
Source File: Field.java    From TencentKona-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Gets the value of a static or instance field of type
 * {@code long} or of another primitive type convertible to
 * type {@code long} via a widening conversion.
 *
 * @param obj the object to extract the {@code long} value
 * from
 * @return the value of the field converted to type {@code long}
 *
 * @exception IllegalAccessException    if this {@code Field} object
 *              is enforcing Java language access control and the underlying
 *              field is inaccessible.
 * @exception IllegalArgumentException  if the specified object is not
 *              an instance of the class or interface declaring the
 *              underlying field (or a subclass or implementor
 *              thereof), or if the field value cannot be
 *              converted to the type {@code long} by a
 *              widening conversion.
 * @exception NullPointerException      if the specified object is null
 *              and the field is an instance field.
 * @exception ExceptionInInitializerError if the initialization provoked
 *              by this method fails.
 * @see       Field#get
 */
@CallerSensitive
public long getLong(Object obj)
    throws IllegalArgumentException, IllegalAccessException
{
    if (!override) {
        if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
            Class<?> caller = Reflection.getCallerClass();
            checkAccess(caller, clazz, obj, modifiers);
        }
    }
    return getFieldAccessor(obj).getLong(obj);
}
 
Example 16
Source File: AccessController.java    From openjdk-8-source with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Performs the specified {@code PrivilegedAction} with privileges
 * enabled and restricted by the specified
 * {@code AccessControlContext} and with a privilege scope limited
 * by specified {@code Permission} arguments.
 *
 * The action is performed with the intersection of the permissions
 * possessed by the caller's protection domain, and those possessed
 * by the domains represented by the specified
 * {@code AccessControlContext}.
 * <p>
 * If the action's {@code run} method throws an (unchecked) exception,
 * it will propagate through this method.
 * <p>
 * If a security manager is installed and the specified
 * {@code AccessControlContext} was not created by system code and the
 * caller's {@code ProtectionDomain} has not been granted the
 * {@literal "createAccessControlContext"}
 * {@link java.security.SecurityPermission}, then the action is performed
 * with no permissions.
 *
 * @param <T> the type of the value returned by the PrivilegedAction's
 *                  {@code run} method.
 * @param action the action to be performed.
 * @param context an <i>access control context</i>
 *                representing the restriction to be applied to the
 *                caller's domain's privileges before performing
 *                the specified action.  If the context is
 *                {@code null},
 *                then no additional restriction is applied.
 * @param perms the {@code Permission} arguments which limit the
 *              scope of the caller's privileges. The number of arguments
 *              is variable.
 *
 * @return the value returned by the action's {@code run} method.
 *
 * @throws NullPointerException if action or perms or any element of
 *         perms is {@code null}
 *
 * @see #doPrivileged(PrivilegedAction)
 * @see #doPrivileged(PrivilegedExceptionAction,AccessControlContext)
 *
 * @since 1.8
 */
@CallerSensitive
public static <T> T doPrivileged(PrivilegedAction<T> action,
    AccessControlContext context, Permission... perms) {

    AccessControlContext parent = getContext();
    if (perms == null) {
        throw new NullPointerException("null permissions parameter");
    }
    Class <?> caller = Reflection.getCallerClass();
    return AccessController.doPrivileged(action, createWrapper(null,
        caller, parent, context, perms));
}
 
Example 17
Source File: AtomicLongFieldUpdater.java    From openjdk-jdk8u with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates and returns an updater for objects with the given field.
 * The Class argument is needed to check that reflective types and
 * generic types match.
 *
 * @param tclass the class of the objects holding the field
 * @param fieldName the name of the field to be updated
 * @param <U> the type of instances of tclass
 * @return the updater
 * @throws IllegalArgumentException if the field is not a
 * volatile long type
 * @throws RuntimeException with a nested reflection-based
 * exception if the class does not hold field or is the wrong type,
 * or the field is inaccessible to the caller according to Java language
 * access control
 */
@CallerSensitive
public static <U> AtomicLongFieldUpdater<U> newUpdater(Class<U> tclass,
                                                       String fieldName) {
    Class<?> caller = Reflection.getCallerClass();
    if (AtomicLong.VM_SUPPORTS_LONG_CAS)
        return new CASUpdater<U>(tclass, fieldName, caller);
    else
        return new LockedUpdater<U>(tclass, fieldName, caller);
}
 
Example 18
Source File: Field.java    From openjdk-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Sets the value of a field as an {@code int} on the specified object.
 * This method is equivalent to
 * {@code set(obj, iObj)},
 * where {@code iObj} is a {@code Integer} object and
 * {@code iObj.intValue() == i}.
 *
 * @param obj the object whose field should be modified
 * @param i   the new value for the field of {@code obj}
 * being modified
 *
 * @exception IllegalAccessException    if this {@code Field} object
 *              is enforcing Java language access control and the underlying
 *              field is either inaccessible or final.
 * @exception IllegalArgumentException  if the specified object is not an
 *              instance of the class or interface declaring the underlying
 *              field (or a subclass or implementor thereof),
 *              or if an unwrapping conversion fails.
 * @exception NullPointerException      if the specified object is null
 *              and the field is an instance field.
 * @exception ExceptionInInitializerError if the initialization provoked
 *              by this method fails.
 * @see       Field#set
 */
@CallerSensitive
public void setInt(Object obj, int i)
    throws IllegalArgumentException, IllegalAccessException
{
    if (!override) {
        if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
            Class<?> caller = Reflection.getCallerClass();
            checkAccess(caller, clazz, obj, modifiers);
        }
    }
    getFieldAccessor(obj).setInt(obj, i);
}
 
Example 19
Source File: AtomicReferenceFieldUpdater.java    From TencentKona-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates and returns an updater for objects with the given field.
 * The Class arguments are needed to check that reflective types and
 * generic types match.
 *
 * @param tclass the class of the objects holding the field
 * @param vclass the class of the field
 * @param fieldName the name of the field to be updated
 * @param <U> the type of instances of tclass
 * @param <W> the type of instances of vclass
 * @return the updater
 * @throws ClassCastException if the field is of the wrong type
 * @throws IllegalArgumentException if the field is not volatile
 * @throws RuntimeException with a nested reflection-based
 * exception if the class does not hold field or is the wrong type,
 * or the field is inaccessible to the caller according to Java language
 * access control
 */
@CallerSensitive
public static <U,W> AtomicReferenceFieldUpdater<U,W> newUpdater(Class<U> tclass,
                                                                Class<W> vclass,
                                                                String fieldName) {
    return new AtomicReferenceFieldUpdaterImpl<U,W>
        (tclass, vclass, fieldName, Reflection.getCallerClass());
}
 
Example 20
Source File: MethodHandles.java    From dragonwell8_jdk with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns a {@link Lookup lookup object} with
 * full capabilities to emulate all supported bytecode behaviors of the caller.
 * These capabilities include <a href="MethodHandles.Lookup.html#privacc">private access</a> to the caller.
 * Factory methods on the lookup object can create
 * <a href="MethodHandleInfo.html#directmh">direct method handles</a>
 * for any member that the caller has access to via bytecodes,
 * including protected and private fields and methods.
 * This lookup object is a <em>capability</em> which may be delegated to trusted agents.
 * Do not store it in place where untrusted code can access it.
 * <p>
 * This method is caller sensitive, which means that it may return different
 * values to different callers.
 * <p>
 * For any given caller class {@code C}, the lookup object returned by this call
 * has equivalent capabilities to any lookup object
 * supplied by the JVM to the bootstrap method of an
 * <a href="package-summary.html#indyinsn">invokedynamic instruction</a>
 * executing in the same caller class {@code C}.
 * @return a lookup object for the caller of this method, with private access
 */
@CallerSensitive
public static Lookup lookup() {
    return new Lookup(Reflection.getCallerClass());
}