|
| 1 | +package org.utbot.engine.overrides; |
| 2 | + |
| 3 | +import org.utbot.api.annotation.UtClassMock; |
| 4 | + |
| 5 | +import java.security.AccessControlContext; |
| 6 | +import java.security.Permission; |
| 7 | +import java.security.PrivilegedAction; |
| 8 | +import java.security.PrivilegedActionException; |
| 9 | +import java.security.PrivilegedExceptionAction; |
| 10 | + |
| 11 | +@UtClassMock(target = java.security.AccessController.class, internalUsage = true) |
| 12 | +public class AccessController { |
| 13 | + public static <T> T doPrivileged(PrivilegedAction<T> action) { |
| 14 | + return action.run(); |
| 15 | + } |
| 16 | + |
| 17 | + public static <T> T doPrivilegedWithCombiner(PrivilegedAction<T> action) { |
| 18 | + return action.run(); |
| 19 | + } |
| 20 | + |
| 21 | + public static <T> T doPrivileged(PrivilegedAction<T> action, |
| 22 | + AccessControlContext ignoredContext) { |
| 23 | + return action.run(); |
| 24 | + } |
| 25 | + |
| 26 | + public static <T> T doPrivileged(PrivilegedAction<T> action, |
| 27 | + AccessControlContext ignoredContext, Permission... perms) { |
| 28 | + if (perms == null) { |
| 29 | + throw new NullPointerException(); |
| 30 | + } |
| 31 | + |
| 32 | + for (Permission permission : perms) { |
| 33 | + if (permission == null) { |
| 34 | + throw new NullPointerException(); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + return action.run(); |
| 39 | + } |
| 40 | + |
| 41 | + public static <T> T doPrivilegedWithCombiner(PrivilegedAction<T> action, |
| 42 | + AccessControlContext context, Permission... perms) { |
| 43 | + return doPrivileged(action, context, perms); |
| 44 | + } |
| 45 | + |
| 46 | + public static <T> T doPrivileged(PrivilegedExceptionAction<T> action) throws PrivilegedActionException { |
| 47 | + try { |
| 48 | + return action.run(); |
| 49 | + } catch (RuntimeException e) { |
| 50 | + // If the action's run method throws an unchecked exception, it will propagate through this method. |
| 51 | + throw e; |
| 52 | + } catch (Exception e) { |
| 53 | + // Exception is wrapped with the PrivilegedActionException ONLY if this exception is CHECKED |
| 54 | + throw new PrivilegedActionException(e); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public static <T> T doPrivilegedWithCombiner(PrivilegedExceptionAction<T> action) throws PrivilegedActionException { |
| 59 | + return doPrivileged(action); |
| 60 | + } |
| 61 | + |
| 62 | + public static <T> T doPrivileged( |
| 63 | + PrivilegedExceptionAction<T> action, |
| 64 | + AccessControlContext ignoredContext |
| 65 | + ) throws PrivilegedActionException { |
| 66 | + return doPrivileged(action); |
| 67 | + } |
| 68 | + |
| 69 | + public static <T> T doPrivileged( |
| 70 | + PrivilegedExceptionAction<T> action, |
| 71 | + AccessControlContext ignoredContext, |
| 72 | + Permission... perms |
| 73 | + ) throws PrivilegedActionException { |
| 74 | + if (perms == null) { |
| 75 | + throw new NullPointerException(); |
| 76 | + } |
| 77 | + |
| 78 | + for (Permission permission : perms) { |
| 79 | + if (permission == null) { |
| 80 | + throw new NullPointerException(); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return doPrivileged(action); |
| 85 | + } |
| 86 | + |
| 87 | + public static <T> T doPrivilegedWithCombiner( |
| 88 | + PrivilegedExceptionAction<T> action, |
| 89 | + AccessControlContext context, |
| 90 | + Permission... perms |
| 91 | + ) throws PrivilegedActionException { |
| 92 | + return doPrivileged(action, context, perms); |
| 93 | + } |
| 94 | + |
| 95 | + public static void checkPermission(Permission perm) { |
| 96 | + if (perm == null) { |
| 97 | + throw new NullPointerException(); |
| 98 | + } |
| 99 | + |
| 100 | + // We cannot check real permission do determines whether we should throw an AccessControlException, |
| 101 | + // so do nothing more. |
| 102 | + } |
| 103 | +} |
0 commit comments