File tree Expand file tree Collapse file tree 3 files changed +49
-2
lines changed
src/test/java/org/mockito/internal/util/reflection Expand file tree Collapse file tree 3 files changed +49
-2
lines changed Original file line number Diff line number Diff line change 11language : java
22sudo : false
3+ dist : trusty
34jdk :
5+ - oraclejdk9
46 - oraclejdk8
57 - openjdk7
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 >
234234 </plugin >
235235 <plugin >
236236 <artifactId >maven-jar-plugin</artifactId >
237- <version >2.6 </version >
237+ <version >3.0.2 </version >
238238 </plugin >
239239 <plugin >
240240 <artifactId >maven-plugin-plugin</artifactId >
293293-->
294294 </executions >
295295 </plugin >
296+
297+ <plugin >
298+ <groupId >org.apache.maven.plugins</groupId >
299+ <artifactId >maven-jar-plugin</artifactId >
300+ </plugin >
296301 </plugins >
297302 </build >
298303
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