Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove explicit capture of fields #7282

Merged
merged 2 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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="
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch

+ 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
Loading