1 /* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 package org.mockito.internal.util.reflection; 6 7 import java.lang.reflect.AccessibleObject; 8 9 public class AccessibilityChanger { 10 11 private Boolean wasAccessible = null; 12 13 /** 14 * safely disables access 15 */ safelyDisableAccess(AccessibleObject accessibleObject)16 public void safelyDisableAccess(AccessibleObject accessibleObject) { 17 assert wasAccessible != null : "accessibility info shall not be null"; 18 try { 19 accessibleObject.setAccessible(wasAccessible); 20 } catch (Throwable t) { 21 //ignore 22 } 23 } 24 25 /** 26 * changes the accessibleObject accessibility and returns true if accessibility was changed 27 */ enableAccess(AccessibleObject accessibleObject)28 public void enableAccess(AccessibleObject accessibleObject) { 29 wasAccessible = accessibleObject.isAccessible(); 30 accessibleObject.setAccessible(true); 31 } 32 } 33