A "lightweight" implementation of {@link Subject#doAs(Subject, PrivilegedExceptionAction)} which uses a ThreadLocal
{@link Subject} instead of modifying the current {@link AccessControlContext}.

@see Subject#doAs(Subject, PrivilegedExceptionAction)
553022
public static <T> T doAs(final Subject subject,
         final java.security.PrivilegedExceptionAction<T> action)
         throws java.security.PrivilegedActionException {
      Stack<Subject> stack = SUBJECT.get();
      if (stack == null) {
         stack = new Stack<>();
         SUBJECT.set(stack);
      }
      stack.push(subject);
      try {
         return action.run();
      } catch (Exception e) {
         throw new PrivilegedActionException(e);
      } finally {
         stack.pop();
         if (stack.isEmpty()) {
            SUBJECT.remove();
         }
      }
   }
380346
public static <T> T runInVm(PrivilegedExceptionAction<T> action) throws PrivilegedActionException {
        checkPermission(PERFORM_IN_VM_CALL);

        if (action == null) return null;
        Boolean originalValue = IN_VM_CALL.get();

        try {
            IN_VM_CALL.set(Boolean.TRUE);
            return action.run();
        } catch (RuntimeException | PrivilegedActionException e) {
            throw e;
        } catch (Exception e) {
            throw new PrivilegedActionException(e);
        } finally {
            IN_VM_CALL.set(originalValue);
        }
    }
642302
@FFDCIgnore(PrivilegedActionException.class)
    private Object createResourceWithFilter(final String filter, final ResourceInfo resourceRefInfo) throws Exception {
        try {
            return AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
                @Override
                public Object run() throws Exception {
                    return createResourceWithFilterPrivileged(filter, resourceRefInfo);
                }
            });
        } catch (PrivilegedActionException paex) {
            Throwable cause = paex.getCause();
            if (cause instanceof Exception) {
                throw (Exception) cause;
            }
            throw new Error(cause);
        }
    }
1061252
private void syncToOSThread(WebSecurityContext webSecurityContext) throws SecurityViolationException {
        try {
            Object token = ThreadIdentityManager.setAppThreadIdentity(subjectManager.getInvocationSubject());
            webSecurityContext.setSyncToOSThreadToken(token);
        } catch (ThreadIdentityException tie) {
            SecurityViolationException secVE = convertWebSecurityException(new WebSecurityCollaboratorException(tie.getMessage(), DENY_AUTHZ_FAILED, webSecurityContext));
            throw secVE;
        }
    }
257274
private void setSubject(Object val) throws CertificateException {
        if (!(val instanceof X500Name)) {
            throw new CertificateException(
                             "Subject class type invalid.");
        }
        subject = (X500Name)val;
    }
337035
static void checkAccess(final int access, final int possibleAccess) {
        if ((access & ~possibleAccess) != 0) {
            throw new IllegalArgumentException("Invalid access flags: "
                    + access);
        }
        int pub = (access & Opcodes.ACC_PUBLIC) == 0 ? 0 : 1;
        int pri = (access & Opcodes.ACC_PRIVATE) == 0 ? 0 : 1;
        int pro = (access & Opcodes.ACC_PROTECTED) == 0 ? 0 : 1;
        if (pub + pri + pro > 1) {
            throw new IllegalArgumentException(
                    "public private and protected are mutually exclusive: "
                            + access);
        }
        int fin = (access & Opcodes.ACC_FINAL) == 0 ? 0 : 1;
        int abs = (access & Opcodes.ACC_ABSTRACT) == 0 ? 0 : 1;
        if (fin + abs > 1) {
            throw new IllegalArgumentException(
                    "final and abstract are mutually exclusive: " + access);
        }
    }
185880
@FFDCIgnore(PrivilegedActionException.class)
    private Object getJNDIServiceObjectInstance(final String className, final String bindingName, final Hashtable<?, ?> envmt) throws Exception {
        try {
            return AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
                @Override
                public Object run() throws Exception {
                    return getJNDIServiceObjectInstancePrivileged(className, bindingName, envmt);
                }
            });
        } catch (PrivilegedActionException paex) {
            Throwable cause = paex.getCause();
            if (cause instanceof Exception) {
                throw (Exception) cause;
            }
            throw new Error(cause);
        }
    }
338190
@Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        String methodName = method.getName();
        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
            Tr.entry(this, tc, methodName, args);
        Object r = null;
        try {
            if (args == null || args.length == 0) {
                if ("canRegisterSynchronization".equals(methodName))
                    r = canRegisterSynchronization();
                else if ("getCurrentStatus".equals(methodName))
                    r = getCurrentStatus();
                else if ("hashCode".equals(methodName))
                    r = System.identityHashCode(this);
                else if ("retrieveTransactionManager".equals(methodName))
                    r = retrieveTransactionManager();
                else if ("retrieveUserTransaction".equals(methodName))
                    r = retrieveUserTransaction();
                else if ("toString".equals(methodName))
                    r = new StringBuilder(getClass().getName()).append('@').append(Integer.toHexString(System.identityHashCode(this))).toString();
            } else {
                if ("equals".equals(methodName))
                    r = proxy == args[0]; // assumes one proxy per invocation handler
                else if ("getTransactionIdentifier".equals(methodName))
                    r = getTransactionIdentifier((Transaction) args[0]);
                else if ("registerSynchronization".equals(methodName))
                    registerSynchronization((Synchronization) args[0]);
            }
        } catch (Throwable x) {
            if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
                Tr.exit(this, tc, methodName, x);
            throw x;
        }

        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
            Tr.exit(this, tc, methodName, r);
        return r;
    }
206563
static void setAccessible(final Method m, final boolean value)
   {
      AccessController.doPrivileged(new PrivilegedAction<Object>() 
      {
         public Object run()
         {
            m.setAccessible(value);
            return null;
         }
      });
   }
480004
protected void setUpSubject() throws WSLoginFailedException {
        try {
            AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
                @Override
                public Object run() throws Exception {
                    updateSubjectWithTemporarySubjectContents();
                    return null;
                }
            });
        } catch (PrivilegedActionException e) {
            throw new WSLoginFailedException("Unable to setup the Subject: " + e.getLocalizedMessage());
        }
    }
1031965
private void optimize(final Subject subject) {
        if (System.getSecurityManager() == null)
            subject.setReadOnly();
        else
            AccessController.doPrivileged(new PrivilegedAction<Void>() {
                @Override
                public Void run() {
                    subject.setReadOnly();
                    return null;
                }
            });
    }
1278527
public <R> R accessContext(ContextAccess<C,R> access)
    {
        contextLock.lock();
        try
        {
            return access.access(context);
        }
        finally
        {
            contextLock.unlock();
        }
    }
1122852
public static <T> T
        doPrivileged(PrivilegedExceptionAction<T> action,
                     AccessControlContext context)
        throws PrivilegedActionException {
        return doPrivileged(action);
    }
458166
public static PrivilegedAction<Method> wrapException(Class<?> javaClass, String methodName, Class<?>... parameterTypes) {
        return new WrappingAction(javaClass, methodName, parameterTypes);
    }
353960
public static Callable<Object> callable(final PrivilegedExceptionAction<?> action) {
        if (action == null)
            throw new NullPointerException();
        return new Callable<Object>() {
            public Object call() throws Exception { return action.run(); }};
    }
1121357
NullSafeAccumulator dotAccess(FieldAccess access, boolean nullSafe) {
    if (access instanceof ProtoCall) {
      ProtoCall protoCall = (ProtoCall) access;
      Expression maybeUnpack = protoCall.unpackFunction();
      if (maybeUnpack != null) {
        Preconditions.checkState(
            unpackFunction == null, "this chain will already unpack with %s", unpackFunction);
        unpackFunction = maybeUnpack;
        accessType = protoCall.accessType();
      }
    }
    chain.add(access.toChainAccess(nullSafe));
    return this;
  }
863413
public <T> T doAs(PrivilegedAction<T> action) {
    return Subject.doAs(null, action);
  }