@@ -295,6 +295,49 @@ public MemberSubstitution stub() {
295
295
return replaceWith (Substitution .Stubbing .INSTANCE );
296
296
}
297
297
298
+ /**
299
+ * Replaces any interaction with a matched byte code element with the provided compile-time constant.
300
+ *
301
+ * @param value The compile-time constant to set.
302
+ * @return A member substitution that replaces any interaction with the supplied compile-time constant.
303
+ */
304
+ public MemberSubstitution replaceWithConstant (Object value ) {
305
+ if (value instanceof JavaConstant ) {
306
+ return replaceWith (new Substitution .ForValue (new JavaConstantValue ((JavaConstant ) value ), ((JavaConstant ) value ).getTypeDescription ().asGenericType ()));
307
+ } else if (value instanceof TypeDescription ) {
308
+ return replaceWith (new Substitution .ForValue (ClassConstant .of ((TypeDescription ) value ), TypeDefinition .Sort .describe (Class .class )));
309
+ } else {
310
+ Class <?> type = value .getClass ();
311
+ if (type == String .class ) {
312
+ return replaceWith (new Substitution .ForValue (new TextConstant ((String ) value ), TypeDefinition .Sort .describe (String .class )));
313
+ } else if (type == Class .class ) {
314
+ return replaceWith (new Substitution .ForValue (ClassConstant .of (TypeDescription .ForLoadedType .of ((Class <?>) value )), TypeDefinition .Sort .describe (Class .class )));
315
+ } else if (type == Boolean .class ) {
316
+ return replaceWith (new Substitution .ForValue (IntegerConstant .forValue ((Boolean ) value ), TypeDefinition .Sort .describe (boolean .class )));
317
+ } else if (type == Byte .class ) {
318
+ return replaceWith (new Substitution .ForValue (IntegerConstant .forValue ((Byte ) value ), TypeDefinition .Sort .describe (byte .class )));
319
+ } else if (type == Short .class ) {
320
+ return replaceWith (new Substitution .ForValue (IntegerConstant .forValue ((Short ) value ), TypeDefinition .Sort .describe (short .class )));
321
+ } else if (type == Character .class ) {
322
+ return replaceWith (new Substitution .ForValue (IntegerConstant .forValue ((Character ) value ), TypeDefinition .Sort .describe (char .class )));
323
+ } else if (type == Integer .class ) {
324
+ return replaceWith (new Substitution .ForValue (IntegerConstant .forValue ((Integer ) value ), TypeDefinition .Sort .describe (int .class )));
325
+ } else if (type == Long .class ) {
326
+ return replaceWith (new Substitution .ForValue (LongConstant .forValue ((Long ) value ), TypeDefinition .Sort .describe (long .class )));
327
+ } else if (type == Float .class ) {
328
+ return replaceWith (new Substitution .ForValue (FloatConstant .forValue ((Float ) value ), TypeDefinition .Sort .describe (float .class )));
329
+ } else if (type == Double .class ) {
330
+ return replaceWith (new Substitution .ForValue (DoubleConstant .forValue ((Double ) value ), TypeDefinition .Sort .describe (double .class )));
331
+ } else if (JavaType .METHOD_HANDLE .getTypeStub ().isAssignableFrom (type )) {
332
+ return replaceWith (new Substitution .ForValue (new JavaConstantValue (JavaConstant .MethodHandle .ofLoaded (value )), TypeDefinition .Sort .describe (type )));
333
+ } else if (JavaType .METHOD_TYPE .getTypeStub ().represents (type )) {
334
+ return replaceWith (new Substitution .ForValue (new JavaConstantValue (JavaConstant .MethodType .ofLoaded (value )), TypeDefinition .Sort .describe (type )));
335
+ } else {
336
+ throw new IllegalArgumentException ("Not a constant value: " + value );
337
+ }
338
+ }
339
+ }
340
+
298
341
/**
299
342
* <p>
300
343
* Replaces any interaction with a matched byte code element by an interaction with the specified field. If a field
@@ -869,6 +912,43 @@ public StackManipulation resolve(TypeDescription targetType,
869
912
}
870
913
}
871
914
915
+ class ForValue implements Substitution , Factory {
916
+
917
+ private final StackManipulation stackManipulation ;
918
+
919
+ private final TypeDescription .Generic typeDescription ;
920
+
921
+ public ForValue (StackManipulation stackManipulation , TypeDescription .Generic typeDescription ) {
922
+ this .stackManipulation = stackManipulation ;
923
+ this .typeDescription = typeDescription ;
924
+ }
925
+
926
+ /**
927
+ * {@inheritDoc}
928
+ */
929
+ public Substitution make (TypeDescription instrumentedType , MethodDescription instrumentedMethod , TypePool typePool ) {
930
+ return this ;
931
+ }
932
+
933
+ /**
934
+ * {@inheritDoc}
935
+ */
936
+ public StackManipulation resolve (TypeDescription targetType ,
937
+ ByteCodeElement target ,
938
+ TypeList .Generic parameters ,
939
+ TypeDescription .Generic result ,
940
+ int freeOffset ) {
941
+ List <StackManipulation > stackManipulations = new ArrayList <StackManipulation >(parameters .size ());
942
+ for (int index = parameters .size () - 1 ; index >= 0 ; index --) {
943
+ stackManipulations .add (Removal .of (parameters .get (index )));
944
+ }
945
+ if (!typeDescription .asErasure ().isAssignableTo (result .asErasure ())) {
946
+ throw new IllegalStateException ("Cannot assign " + typeDescription + " to " + result );
947
+ }
948
+ return new StackManipulation .Compound (CompoundList .of (stackManipulations , stackManipulation ));
949
+ }
950
+ }
951
+
872
952
/**
873
953
* A substitution with a field access.
874
954
*/
@@ -1624,7 +1704,7 @@ public Resolution resolve(TypeDescription targetType,
1624
1704
int freeOffset ) {
1625
1705
return targetType .represents (void .class )
1626
1706
? this
1627
- : new Simple (new StackManipulation .Compound (Removal .of (targetType ), stackManipulation ), resultType );
1707
+ : new Simple (new StackManipulation .Compound (Removal .of (current ), stackManipulation ), resultType );
1628
1708
}
1629
1709
1630
1710
/**
@@ -1832,9 +1912,9 @@ class ForArgumentLoading implements Step {
1832
1912
/**
1833
1913
* Creates an argument loading step.
1834
1914
*
1835
- * @param index The index of the argument to load.
1836
- * @param assigner The assigner to use for assigning the argument.
1837
- * @param typing The typing to use for the argument assignment.
1915
+ * @param index The index of the argument to load.
1916
+ * @param assigner The assigner to use for assigning the argument.
1917
+ * @param typing The typing to use for the argument assignment.
1838
1918
*/
1839
1919
protected ForArgumentLoading (int index ,
1840
1920
Assigner assigner ,
0 commit comments