44import io .valkey .params .SetParams ;
55
66import java .lang .reflect .Field ;
7+ import java .util .ArrayList ;
8+ import java .util .List ;
79
810public class TestingUtils {
911
1012 private TestingUtils () {}
1113
1214 public static boolean isSetParamsNX (SetParams setParams ) {
1315 if (setParams != null ) {
14- Protocol .Keyword valueExistance = extractPrivateValue ("existance " , SetParams .class , setParams , Protocol .Keyword .class );
16+ Protocol .Keyword valueExistance = extractPrivateValue ("existence " , SetParams .class , setParams , Protocol .Keyword .class );
1517 return valueExistance == Protocol .Keyword .NX ;
1618 } else {
1719 return false ;
@@ -22,8 +24,8 @@ public static boolean isSetParamsNX(SetParams setParams) {
2224
2325 public static Long extractSetParamsExpireTimePX (SetParams setParams ) {
2426 if (setParams != null ) {
25- Protocol .Keyword valueExpiration = extractPrivateValue ("expiration" , SetParams .class , setParams , Protocol .Keyword .class );
26- Long valueExpirationValue = extractPrivateValue ("expirationValue" , SetParams .class , setParams , Long .class );
27+ Protocol .Keyword valueExpiration = extractPrivateValue ("expiration" , SetParams .class , setParams , Protocol .Keyword .class , true );
28+ Long valueExpirationValue = extractPrivateValue ("expirationValue" , SetParams .class , setParams , Long .class , true );
2729 if (valueExpiration == null && valueExpirationValue == null ) {
2830 return null ;
2931 } else if (valueExpiration == Protocol .Keyword .PX ) {
@@ -39,8 +41,17 @@ public static Long extractSetParamsExpireTimePX(SetParams setParams) {
3941
4042
4143 public static <I ,O > O extractPrivateValue (String field , Class <I > originType , I origin , Class <O > resultType ) {
44+ return extractPrivateValue (field , originType , origin , resultType , false );
45+ }
46+
47+ public static <I ,O > O extractPrivateValue (String field , Class <I > originType , I origin , Class <O > resultType , boolean searchInParent ) {
4248 try {
43- Field privateField = origin .getClass ().getDeclaredField (field );
49+ Field privateField ;
50+ if (searchInParent ) {
51+ privateField = getPrivateFieldsFromObjectAndParent (originType , field );
52+ } else {
53+ privateField = origin .getClass ().getDeclaredField (field );
54+ }
4455 privateField .setAccessible (true );
4556 if (resultType == privateField .getType ()){
4657 return (O ) privateField .get (origin );
@@ -56,4 +67,21 @@ public static <I,O> O extractPrivateValue(String field, Class<I> originType, I o
5667 }
5768 }
5869
59- }
70+ public static Field getPrivateFieldsFromObjectAndParent (Class <?> type , String name ) {
71+ List <Field > fieldList = new ArrayList <>();
72+ Class <?> currentType = type ;
73+ while (currentType != null && currentType != Object .class ) {
74+ for (Field field : currentType .getDeclaredFields ()) {
75+ if (!field .isSynthetic ()) {
76+ fieldList .add (field );
77+ }
78+ }
79+ currentType = currentType .getSuperclass ();
80+ }
81+ return fieldList .stream ().
82+ filter (f -> f .getName ().equals (name )).
83+ findFirst ().get ();
84+
85+ }
86+
87+ }
0 commit comments