@@ -24,7 +24,7 @@ public class CapturedContext implements ValueReferenceResolver {
2424 public static final CapturedContext EMPTY_CONTEXT = new CapturedContext (null );
2525 public static final CapturedContext EMPTY_CAPTURING_CONTEXT =
2626 new CapturedContext (ProbeImplementation .UNKNOWN );
27- private final transient Map <String , Object > extensions = new HashMap <>();
27+ private final transient Map <String , CapturedValue > extensions = new HashMap <>();
2828
2929 private Map <String , CapturedValue > arguments ;
3030 private Map <String , CapturedValue > locals ;
@@ -49,7 +49,7 @@ public CapturedContext(
4949 this .throwable = throwable ;
5050 }
5151
52- private CapturedContext (CapturedContext other , Map <String , Object > extensions ) {
52+ private CapturedContext (CapturedContext other , Map <String , CapturedValue > extensions ) {
5353 this .arguments = other .arguments ;
5454 this .locals = other .getLocals ();
5555 this .throwable = other .throwable ;
@@ -85,11 +85,11 @@ public boolean isCapturing() {
8585 }
8686
8787 @ Override
88- public Object lookup (String name ) {
88+ public CapturedValue lookup (String name ) {
8989 if (name == null || name .isEmpty ()) {
9090 throw new IllegalArgumentException ("empty name for lookup operation" );
9191 }
92- Object target ;
92+ CapturedValue target ;
9393 if (name .startsWith (ValueReferences .SYNTHETIC_PREFIX )) {
9494 String rawName = name .substring (ValueReferences .SYNTHETIC_PREFIX .length ());
9595 target = tryRetrieveSynthetic (rawName );
@@ -98,36 +98,39 @@ public Object lookup(String name) {
9898 target = tryRetrieve (name );
9999 checkUndefined (target , name , "Cannot find symbol: " );
100100 }
101- return target instanceof CapturedValue ? (( CapturedValue ) target ). getValue () : target ;
101+ return target ;
102102 }
103103
104- private void checkUndefined (Object target , String name , String msg ) {
105- if (target == Values . UNDEFINED_OBJECT ) {
104+ private void checkUndefined (CapturedValue target , String name , String msg ) {
105+ if (target == CapturedValue . UNDEFINED || target . notCapturedReason != null ) {
106106 String errorMsg = msg + name ;
107107 throw new RuntimeException (errorMsg );
108108 }
109109 }
110110
111111 @ Override
112- public Object getMember (Object target , String memberName ) {
112+ public CapturedValue getMember (Object target , String memberName ) {
113113 if (target == Values .UNDEFINED_OBJECT ) {
114- return target ;
114+ return CapturedValue . UNDEFINED ;
115115 }
116116 if (Redaction .isRedactedKeyword (memberName )) {
117- return REDACTED_VALUE ;
117+ return CapturedValue . redacted ( memberName , null ) ;
118118 }
119+ CapturedValue result ;
119120 if (target instanceof CapturedValue ) {
120121 Map <String , CapturedValue > fields = ((CapturedValue ) target ).fields ;
121122 if (fields .containsKey (memberName )) {
122- target = fields .get (memberName );
123+ result = fields .get (memberName );
123124 } else {
124125 CapturedValue capturedTarget = ((CapturedValue ) target );
125- target = capturedTarget .getValue ();
126- if (target != null ) {
126+ Object targetedValue = capturedTarget .getValue ();
127+ if (targetedValue != null ) {
127128 // resolve to a CapturedValue instance
128- target = ReflectiveFieldValueResolver .resolve (target , target .getClass (), memberName );
129+ result =
130+ ReflectiveFieldValueResolver .getFieldAsCapturedValue (
131+ targetedValue .getClass (), targetedValue , memberName );
129132 } else {
130- target = Values . UNDEFINED_OBJECT ;
133+ result = CapturedValue . UNDEFINED ;
131134 }
132135 }
133136 } else {
@@ -138,25 +141,27 @@ public Object getMember(Object target, String memberName) {
138141 if (specialFieldAccess != null ) {
139142 CapturedValue specialField = specialFieldAccess .apply (target );
140143 if (specialField != null && specialField .getName ().equals (memberName )) {
141- return specialField . getValue () ;
144+ return specialField ;
142145 }
143146 }
144147 }
145- target = ReflectiveFieldValueResolver .resolve (target , target .getClass (), memberName );
148+ result =
149+ ReflectiveFieldValueResolver .getFieldAsCapturedValue (
150+ target .getClass (), target , memberName );
146151 }
147- checkUndefined (target , memberName , "Cannot dereference field: " );
148- return target ;
152+ checkUndefined (result , memberName , "Cannot dereference field: " );
153+ return result ;
149154 }
150155
151- private Object tryRetrieveSynthetic (String name ) {
156+ private CapturedValue tryRetrieveSynthetic (String name ) {
152157 if (extensions == null || extensions .isEmpty ()) {
153- return Values . UNDEFINED_OBJECT ;
158+ return CapturedValue . UNDEFINED ;
154159 }
155- return extensions .getOrDefault (name , Values . UNDEFINED_OBJECT );
160+ return extensions .getOrDefault (name , CapturedValue . UNDEFINED );
156161 }
157162
158- private Object tryRetrieve (String name ) {
159- Object result = null ;
163+ private CapturedValue tryRetrieve (String name ) {
164+ CapturedValue result = null ;
160165 if (arguments != null && !arguments .isEmpty ()) {
161166 result = arguments .get (name );
162167 }
@@ -174,12 +179,12 @@ private Object tryRetrieve(String name) {
174179 }
175180 CapturedValue thisValue ;
176181 if (arguments != null && (thisValue = arguments .get ("this" )) != null ) {
177- result = getMember (thisValue . getValue () , name );
178- if (result != Values . UNDEFINED_OBJECT ) {
182+ result = getMember (thisValue , name );
183+ if (result != CapturedValue . UNDEFINED ) {
179184 return result ;
180185 }
181186 }
182- return result != null ? result : Values . UNDEFINED_OBJECT ;
187+ return result != null ? result : CapturedValue . UNDEFINED ;
183188 }
184189
185190 public CapturedContext copyWithoutCaptureExpressions () {
@@ -191,12 +196,12 @@ public CapturedContext copyWithoutCaptureExpressions() {
191196 }
192197
193198 @ Override
194- public ValueReferenceResolver withExtensions (Map <String , Object > extensions ) {
199+ public ValueReferenceResolver withExtensions (Map <String , CapturedValue > extensions ) {
195200 return new CapturedContext (this , extensions );
196201 }
197202
198203 @ Override
199- public void addExtension (String name , Object value ) {
204+ public void addExtension (String name , CapturedValue value ) {
200205 extensions .put (name , value );
201206 }
202207
@@ -235,8 +240,9 @@ public void addReturn(CapturedValue retValue) {
235240 public void addThrowable (Throwable t ) {
236241 addThrowable (new CapturedThrowable (t ));
237242 // special local name for throwable
238- putInLocals (ValueReferences .EXCEPTION_REF , CapturedValue .of (t .getClass ().getTypeName (), t ));
239- extensions .put (ValueReferences .EXCEPTION_EXTENSION_NAME , t );
243+ CapturedValue capturedException = CapturedValue .of (t .getClass ().getTypeName (), t );
244+ putInLocals (ValueReferences .EXCEPTION_REF , capturedException );
245+ extensions .put (ValueReferences .EXCEPTION_EXTENSION_NAME , capturedException );
240246 }
241247
242248 public void addThrowable (CapturedThrowable capturedThrowable ) {
@@ -319,7 +325,8 @@ public Status evaluate(
319325 if (methodLocation == MethodLocation .EXIT && startTimestamp > 0 ) {
320326 duration = System .nanoTime () - startTimestamp ;
321327 addExtension (
322- ValueReferences .DURATION_EXTENSION_NAME , duration / 1_000_000.0 ); // convert to ms
328+ ValueReferences .DURATION_EXTENSION_NAME ,
329+ CapturedValue .of (duration / 1_000_000.0 )); // convert to ms
323330 }
324331 this .thisClassName = thisClassName ;
325332 boolean shouldEvaluate =
@@ -446,7 +453,7 @@ public boolean isCapturing() {
446453
447454 /** Stores a captured value */
448455 public static class CapturedValue {
449- public static final CapturedValue UNDEFINED = CapturedValue .of (null , Values .UNDEFINED_OBJECT );
456+ public static final CapturedValue UNDEFINED = CapturedValue .of (Values .UNDEFINED_OBJECT );
450457
451458 private String name ;
452459 private final String declaredType ;
@@ -517,6 +524,10 @@ public void setName(String name) {
517524 this .name = name ;
518525 }
519526
527+ public static CapturedValue of (Object value ) {
528+ return of (null , value );
529+ }
530+
520531 public static CapturedValue of (String declaredType , Object value ) {
521532 return build (null , declaredType , value , Limits .DEFAULT , null );
522533 }
@@ -525,10 +536,6 @@ public static CapturedValue of(String name, String declaredType, Object value) {
525536 return build (name , declaredType , value , Limits .DEFAULT , null );
526537 }
527538
528- public CapturedValue derive (String name , String type , Object value ) {
529- return build (name , type , value , limits , null );
530- }
531-
532539 public static CapturedValue of (
533540 String name ,
534541 String declaredType ,
0 commit comments