View Javadoc

1   /*
2   This project provide access to non public fields
3   and methods as help for writing tests.
4   Copyright (C) 2007  Christof Lehmann
5   
6   This program is free software; you can redistribute it and/or
7   modify it under the terms of the GNU General Public License
8   as published by the Free Software Foundation; either version 2
9   of the License, or (at your option) any later version.
10  
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15  
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19   */
20  package net.sf.testextensions;
21  
22  import java.lang.reflect.AccessibleObject;
23  
24  /**
25   * The interface declares methodes for manipulating the SecurityManger.
26   * @author forge-cl
27   */
28  abstract class AccessibilityManipulator {
29  	/**
30  	 * The method <code>befor</code> must be called before accessing a non
31  	 * public field.
32  	 */
33  	public abstract void before();
34  
35  	/**
36  	 * The method <code>after</code> must be called after accessing a non
37  	 * public field.
38  	 *
39  	 */
40  	public abstract void after();
41  
42  	/**
43  	 * The method <code>selectManipulator</code> generate a
44  	 * AccessibilityManipulator, which suites to the member.
45  	 *
46  	 * @param forMember
47  	 *            The member for the AccessibilityManipulator
48  	 * @return the required AccessibilityManipulator
49  	 */
50  	public static AccessibilityManipulator selectManipulator(
51  			final AccessibleObject forMember) {
52  		AccessibilityManipulator manipulator;
53  		if (forMember.isAccessible()) {
54  			manipulator = new AccessibilityManipulator() {
55  				public void after() {
56  				}
57  
58  				public void before() {
59  				}
60  			};
61  		} else {
62  			manipulator = new AccessibilityManipulator() {
63  				public void after() {
64  					forMember.setAccessible(false);
65  				}
66  
67  				public void before() {
68  					forMember.setAccessible(true);
69  				}
70  
71  			};
72  		}
73  		return manipulator;
74  	}
75  }