File tree Expand file tree Collapse file tree 2 files changed +41
-1
lines changed
src/test/java/org/mockito/internal/util/reflection Expand file tree Collapse file tree 2 files changed +41
-1
lines changed Original file line number Diff line number Diff line change 5959
6060 <jgit .version>4.5.2.201704071617-r</jgit .version>
6161 <junit .version>4.12</junit .version>
62- <mockito .version>2.0.35-beta </mockito .version>
62+ <mockito .version>2.8.47 </mockito .version>
6363
6464 <fest-assert .version>1.4</fest-assert .version>
6565 </properties >
Original file line number Diff line number Diff line change 1+ package org .mockito .internal .util .reflection ;
2+
3+ import java .lang .reflect .Field ;
4+
5+ @ Deprecated
6+ public class Whitebox {
7+ public static void setInternalState (Object target , String field , Object value ) {
8+ Class <?> c = target .getClass ();
9+ try {
10+ Field f = getFieldFromHierarchy (c , field );
11+ f .setAccessible (true );
12+ f .set (target , value );
13+ } catch (Exception e ) {
14+ throw new RuntimeException ("Unable to set internal state on a private field. Please report to mockito mailing list." , e );
15+ }
16+ }
17+
18+ private static Field getFieldFromHierarchy (Class <?> clazz , String field ) {
19+ Field f = getField (clazz , field );
20+ while (f == null && clazz != Object .class ) {
21+ clazz = clazz .getSuperclass ();
22+ f = getField (clazz , field );
23+ }
24+ if (f == null ) {
25+ throw new RuntimeException (
26+ "You want me to get this field: '" + field +
27+ "' on this class: '" + clazz .getSimpleName () +
28+ "' but this field is not declared withing hierarchy of this class!" );
29+ }
30+ return f ;
31+ }
32+
33+ private static Field getField (Class <?> clazz , String field ) {
34+ try {
35+ return clazz .getDeclaredField (field );
36+ } catch (NoSuchFieldException e ) {
37+ return null ;
38+ }
39+ }
40+ }
You can’t perform that action at this time.
0 commit comments