1
1
package io .avaje .inject .test ;
2
2
3
3
import java .lang .annotation .Annotation ;
4
+ import java .lang .invoke .VarHandle ;
4
5
import java .lang .reflect .Field ;
5
6
import java .lang .reflect .Modifier ;
6
7
import java .lang .reflect .ParameterizedType ;
14
15
import org .mockito .Captor ;
15
16
import org .mockito .Mock ;
16
17
import org .mockito .Spy ;
17
- import org .mockito .internal .configuration .plugins .Plugins ;
18
- import org .mockito .internal .util .reflection .GenericMaster ;
19
18
20
19
import io .avaje .inject .BeanScope ;
21
20
import io .avaje .inject .BeanScopeBuilder ;
26
25
final class MetaReader {
27
26
28
27
private final SetupMethods methodFinder ;
28
+ final Class <?> testClass ;
29
29
final List <Field > captors = new ArrayList <>();
30
30
final List <FieldTarget > mocks = new ArrayList <>();
31
31
final List <FieldTarget > spies = new ArrayList <>();
@@ -41,6 +41,7 @@ final class MetaReader {
41
41
boolean instancePlugin ;
42
42
43
43
MetaReader (Class <?> testClass , Plugin plugin ) {
44
+ this .testClass = testClass ;
44
45
this .plugin = plugin ;
45
46
final var hierarchy = typeHierarchy (testClass );
46
47
this .methodFinder = new SetupMethods (hierarchy );
@@ -54,9 +55,8 @@ final class MetaReader {
54
55
boolean hasMocksOrSpies (Object testInstance ) {
55
56
if (testInstance == null ) {
56
57
return hasStaticMocksOrSpies () || methodFinder .hasStaticMethods ();
57
- } else {
58
- return hasInstanceMocksOrSpies (testInstance ) || methodFinder .hasInstanceMethods ();
59
58
}
59
+ return hasInstanceMocksOrSpies (testInstance ) || methodFinder .hasInstanceMethods ();
60
60
}
61
61
62
62
private boolean hasInstanceMocksOrSpies (Object testInstance ) {
@@ -154,7 +154,7 @@ private void add(FieldTarget target, List<FieldTarget> instanceList, List<FieldT
154
154
}
155
155
156
156
private FieldTarget newTarget (Field field ) {
157
- return new FieldTarget (field , name (field ));
157
+ return new FieldTarget (field , name (field ), Lookups . getVarhandle ( testClass , field ) );
158
158
}
159
159
160
160
private String name (Field field ) {
@@ -178,9 +178,8 @@ private String name(Field field) {
178
178
TestBeans setFromScope (TestBeans metaScope , Object testInstance ) {
179
179
if (testInstance != null ) {
180
180
return setForInstance (metaScope , testInstance );
181
- } else {
182
- return setForStatics (metaScope );
183
181
}
182
+ return setForStatics (metaScope );
184
183
}
185
184
186
185
private TestBeans setForInstance (TestBeans metaScope , Object testInstance ) {
@@ -189,7 +188,11 @@ private TestBeans setForInstance(TestBeans metaScope, Object testInstance) {
189
188
BeanScope beanScope = metaScope .beanScope ();
190
189
191
190
for (Field field : captors ) {
192
- set (field , captorFor (field ), testInstance );
191
+ set (
192
+ Modifier .isStatic (field .getModifiers ()),
193
+ Lookups .getVarhandle (testClass , field ),
194
+ captorFor (field ),
195
+ testInstance );
193
196
}
194
197
for (FieldTarget target : mocks ) {
195
198
target .setFromScope (beanScope , testInstance );
@@ -239,9 +242,12 @@ private TestBeans setForStatics(TestBeans metaScope) {
239
242
private Object captorFor (Field field ) {
240
243
Class <?> type = field .getType ();
241
244
if (!ArgumentCaptor .class .isAssignableFrom (type )) {
242
- throw new IllegalStateException ("@Captor field must be of the type ArgumentCaptor.\n Field: '" + field .getName () + "' has wrong type" );
245
+ throw new IllegalStateException (
246
+ "@Captor field must be of the type ArgumentCaptor.\n Field: '"
247
+ + field .getName ()
248
+ + "' has wrong type" );
243
249
}
244
- Class <?> cls = new GenericMaster () .getGenericType (field );
250
+ Class <?> cls = Lookups . getClassFromType ( field .getGenericType () );
245
251
return ArgumentCaptor .forClass (cls );
246
252
}
247
253
@@ -308,8 +314,12 @@ private static void registerAsTestDouble(BeanScopeBuilder builder, FieldTarget t
308
314
builder .bean (target .name (), target .type (), value );
309
315
}
310
316
311
- void set (Field field , Object val , Object testInstance ) throws IllegalAccessException {
312
- Plugins .getMemberAccessor ().set (field , testInstance , val );
317
+ void set (boolean isStatic , VarHandle fieldHandle , Object val , Object testInstance ) {
318
+ if (isStatic ) {
319
+ fieldHandle .set (val );
320
+ } else {
321
+ fieldHandle .set (testInstance , val );
322
+ }
313
323
}
314
324
315
325
class FieldTarget {
@@ -319,11 +329,13 @@ class FieldTarget {
319
329
private final boolean isStatic ;
320
330
private boolean pluginInjection ;
321
331
private boolean valueAlreadyProvided ;
332
+ private final VarHandle fieldHandle ;
322
333
323
- FieldTarget (Field field , String name ) {
334
+ FieldTarget (Field field , String name , VarHandle fieldHandle ) {
324
335
this .field = field ;
325
336
this .isStatic = Modifier .isStatic (field .getModifiers ());
326
337
this .name = name ;
338
+ this .fieldHandle = fieldHandle ;
327
339
}
328
340
329
341
@ Override
@@ -344,40 +356,35 @@ boolean isStatic() {
344
356
}
345
357
346
358
Object get (Object instance ) {
347
- try {
348
- return Plugins .getMemberAccessor ().get (field , instance );
349
- } catch (IllegalAccessException e ) {
350
- throw new RuntimeException (e );
351
- }
359
+ return isStatic ? fieldHandle .get () : fieldHandle .get (instance );
352
360
}
353
361
354
362
void setFromScope (BeanScope beanScope , Object testInstance ) throws IllegalAccessException {
355
363
if (valueAlreadyProvided ) {
356
364
return ;
357
365
}
358
366
final var type = type ();
359
-
360
367
if (type instanceof ParameterizedType ) {
361
368
final var parameterizedType = (ParameterizedType ) type ;
362
369
final var rawType = parameterizedType .getRawType ();
363
370
final var typeArguments = parameterizedType .getActualTypeArguments ();
364
371
365
372
if (rawType .equals (List .class )) {
366
- set (field , beanScope .list (typeArguments [0 ]), testInstance );
373
+ set (isStatic , fieldHandle , beanScope .list (typeArguments [0 ]), testInstance );
367
374
return ;
368
375
}
369
376
370
377
if (rawType .equals (Optional .class )) {
371
- set (field , beanScope .getOptional (typeArguments [0 ], name ), testInstance );
378
+ set (isStatic , fieldHandle , beanScope .getOptional (typeArguments [0 ], name ), testInstance );
372
379
return ;
373
380
}
374
381
}
375
382
376
- set (field , beanScope .get (type , name ), testInstance );
383
+ set (isStatic , fieldHandle , beanScope .get (type , name ), testInstance );
377
384
}
378
385
379
386
void setFromPlugin (Object value , Object testInstance ) throws IllegalAccessException {
380
- set (field , value , testInstance );
387
+ set (isStatic , fieldHandle , value , testInstance );
381
388
}
382
389
383
390
void markForPluginInjection () {
0 commit comments