Skip to content

Commit

Permalink
Remove explicit capture of fields (#7282)
Browse files Browse the repository at this point in the history
* Remove explicit capture of fields

Previously we were capturing directly fields of the current class in
a specific way and use them for condition evaluation, while this
was captured as argument. Now we only capture this and all evaluations
are based on implicit this dereferencing when needed.
NB: Serialization of complex classes no longer capture static fields

* Rename error messsage

remote unnecessary comments and dead code
  • Loading branch information
jpbempel committed Jul 9, 2024
1 parent 9e7aa67 commit ff9a04a
Show file tree
Hide file tree
Showing 18 changed files with 224 additions and 659 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public class CapturedContext implements ValueReferenceResolver {
private Map<String, CapturedValue> locals;
private CapturedThrowable throwable;
private Map<String, CapturedValue> staticFields;
private Map<String, CapturedValue> fields;
private Limits limits = Limits.DEFAULT;
private String thisClassName;
private String traceId;
Expand All @@ -43,20 +42,17 @@ public CapturedContext(
CapturedValue[] arguments,
CapturedValue[] locals,
CapturedValue returnValue,
CapturedThrowable throwable,
CapturedValue[] fields) {
CapturedThrowable throwable) {
addArguments(arguments);
addLocals(locals);
addReturn(returnValue);
this.throwable = throwable;
addFields(fields);
}

private CapturedContext(CapturedContext other, Map<String, Object> extensions) {
this.arguments = other.arguments;
this.locals = other.getLocals();
this.throwable = other.throwable;
this.fields = other.fields;
this.extensions.putAll(other.extensions);
this.extensions.putAll(extensions);
}
Expand Down Expand Up @@ -143,7 +139,7 @@ public Object getMember(Object target, String memberName) {
}
target = ReflectiveFieldValueResolver.resolve(target, target.getClass(), memberName);
}
checkUndefined(target, memberName, "Cannot dereference to field: ");
checkUndefined(target, memberName, "Cannot dereference field: ");
return target;
}

Expand All @@ -168,15 +164,16 @@ private Object tryRetrieve(String name) {
if (result != null) {
return result;
}
if (fields != null && !fields.isEmpty()) {
result = fields.get(name);
}
if (result != null) {
return result;
}
if (staticFields != null && !staticFields.isEmpty()) {
result = staticFields.get(name);
}
CapturedValue thisValue;
if (arguments != null && (thisValue = arguments.get("this")) != null) {
result = getMember(thisValue.getValue(), name);
if (result != Values.UNDEFINED_OBJECT) {
return result;
}
}
return result != null ? result : Values.UNDEFINED_OBJECT;
}

Expand Down Expand Up @@ -236,25 +233,15 @@ public void addStaticFields(CapturedValue[] values) {
}
}

public void addFields(CapturedValue[] values) {
if (values == null) {
return;
}
for (CapturedValue value : values) {
putInFields(value.name, value);
}
traceId = extractSpecialId("dd.trace_id");
spanId = extractSpecialId("dd.span_id");
public void addTraceId(CapturedValue capturedValue) {
traceId = extractStringId(capturedValue);
}

private String extractSpecialId(String idName) {
if (fields == null) {
return null;
}
CapturedValue capturedValue = fields.get(idName);
if (capturedValue == null) {
return null;
}
public void addSpanId(CapturedValue capturedValue) {
spanId = extractStringId(capturedValue);
}

private String extractStringId(CapturedValue capturedValue) {
Object value = capturedValue.getValue();
return value instanceof String ? (String) value : null;
}
Expand All @@ -280,10 +267,6 @@ public Map<String, CapturedValue> getStaticFields() {
return staticFields;
}

public Map<String, CapturedValue> getFields() {
return fields;
}

public Limits getLimits() {
return limits;
}
Expand Down Expand Up @@ -314,9 +297,6 @@ public void freeze(TimeoutChecker timeoutChecker) {
if (staticFields != null) {
staticFields.values().forEach(capturedValue -> capturedValue.freeze(timeoutChecker));
}
if (fields != null) {
fields.values().forEach(capturedValue -> capturedValue.freeze(timeoutChecker));
}
}

public Status evaluate(
Expand Down Expand Up @@ -359,13 +339,13 @@ public boolean equals(Object o) {
CapturedContext context = (CapturedContext) o;
return Objects.equals(arguments, context.arguments)
&& Objects.equals(locals, context.locals)
&& Objects.equals(throwable, context.throwable)
&& Objects.equals(fields, context.fields);
&& Objects.equals(staticFields, context.staticFields)
&& Objects.equals(throwable, context.throwable);
}

@Override
public int hashCode() {
return Objects.hash(arguments, locals, throwable, fields);
return Objects.hash(arguments, locals, throwable, staticFields);
}

@Override
Expand All @@ -377,8 +357,8 @@ public String toString() {
+ locals
+ ", throwable="
+ throwable
+ ", fields="
+ fields
+ ", staticFields="
+ staticFields
+ '}';
}

Expand All @@ -396,13 +376,6 @@ private void putInArguments(String name, CapturedValue value) {
arguments.put(name, value);
}

private void putInFields(String name, CapturedValue value) {
if (fields == null) {
fields = new HashMap<>();
}
fields.put(name, value);
}

private void putInStaticFields(String name, CapturedValue value) {
if (staticFields == null) {
staticFields = new HashMap<>();
Expand Down
Loading

0 comments on commit ff9a04a

Please sign in to comment.