Skip to content

Commit 9eccd66

Browse files
committed
Add support for non-string context fields
1 parent a29d052 commit 9eccd66

File tree

5 files changed

+49
-17
lines changed

5 files changed

+49
-17
lines changed

src/jdk.jfr/share/classes/jdk/jfr/internal/TypeLibrary.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,12 +352,18 @@ private static void addFields(Type type, boolean requestable, boolean hasDuratio
352352
type.add(STACK_TRACE_FIELD);
353353
}
354354
if (hasContext) {
355-
var fldSuffix = 1;
356355
for (var descriptor : ContextRepository.registrations()) {
357356
String name = descriptor.holderId() + "_" + descriptor.name();
358357
var annos = createStandardAnnotations(name, descriptor.holderId() + ":" + descriptor.label(), descriptor.description());
359-
type.add(PrivateAccess.getInstance().newValueDescriptor(name, Type.STRING, annos, 0, true, name));
360-
fldSuffix++;
358+
Class<?> varType = descriptor.access().varType();
359+
Type fType = Type.LONG;
360+
if (varType == String.class) {
361+
fType = Type.STRING;
362+
} else if (varType == boolean.class) {
363+
fType = Type.BOOLEAN;
364+
}
365+
System.err.println("===> " + name + ": " + fType + ", " + (fType == Type.STRING));
366+
type.add(PrivateAccess.getInstance().newValueDescriptor(name, fType, annos, 0, fType == Type.STRING, name));
361367
}
362368
}
363369
}

src/jdk.jfr/share/classes/jdk/jfr/internal/context/ContextRepository.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,19 @@ private static <T extends BaseContextType> Set<ContextDescriptor> descriptorsOf(
5858
MethodHandles.Lookup lookup = MethodHandles.publicLookup();
5959
int order = 0;
6060
for (Field f : contextTypeClass.getFields()) {
61-
Name nameAnnot = f.getAnnotation(Name.class);
62-
Label labelAnnot = f.getAnnotation(Label.class);
63-
Description descAnnot = f.getAnnotation(Description.class);
64-
if (nameAnnot != null || labelAnnot != null || descAnnot != null) {
65-
String name = nameAnnot != null ? nameAnnot.value() : f.getName();
66-
String label = labelAnnot != null ? labelAnnot.value() : name;
67-
String desc = descAnnot != null ? descAnnot.value() : "";
68-
ctxDescriptors.add(new ContextDescriptor(order++, id, name, label, desc, lookup.unreflectVarHandle(f)));
61+
Class<?> fType = f.getType();
62+
if (fType == String.class || (fType.isPrimitive() && fType != float.class && fType != double.class)) {
63+
Name nameAnnot = f.getAnnotation(Name.class);
64+
Label labelAnnot = f.getAnnotation(Label.class);
65+
Description descAnnot = f.getAnnotation(Description.class);
66+
if (nameAnnot != null || labelAnnot != null || descAnnot != null) {
67+
String name = nameAnnot != null ? nameAnnot.value() : f.getName();
68+
String label = labelAnnot != null ? labelAnnot.value() : name;
69+
String desc = descAnnot != null ? descAnnot.value() : "";
70+
ctxDescriptors.add(new ContextDescriptor(order++, id, name, label, desc, lookup.unreflectVarHandle(f)));
71+
}
72+
} else {
73+
throw new IllegalArgumentException("Only context fields of String, boolean, int, long, byte, short and char types are supported");
6974
}
7075
}
7176
return ctxDescriptors;

src/jdk.jfr/share/classes/jdk/jfr/internal/context/ContextWriter.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package jdk.jfr.internal.context;
22

3+
import java.lang.invoke.VarHandle;
34
import java.nio.LongBuffer;
45
import java.util.Collections;
56
import java.util.Map;
@@ -36,8 +37,16 @@ void write(BaseContextType target) {
3637
}
3738
for (ContextDescriptor cd : descriptors) {
3839
if (cd.order() < 8) {
39-
String value = (String) cd.access().get(target);
40-
context.put(offset + cd.order(), value != null ? StringPool.addString(value, false) : 0);
40+
VarHandle access= cd.access();
41+
Class<?> vType = access.varType();
42+
if (vType == String.class) {
43+
String value = (String) access.get(target);
44+
context.put(offset + cd.order(), value != null ? StringPool.addString(value, false) : 0);
45+
} else if (vType == boolean.class) {
46+
context.put(offset + cd.order(), ((boolean) access.get(target) ? 0 : 1));
47+
} else {
48+
context.put(offset + cd.order(), (long) access.get(target));
49+
}
4150
}
4251
}
4352
}
@@ -53,7 +62,12 @@ void clear(BaseContextType target) {
5362
for (ContextDescriptor cd : descriptors) {
5463
if (cd.order() < 8) {
5564
context.put(offset + cd.order(), 0L);
56-
cd.access().set(target, null);
65+
VarHandle access = cd.access();
66+
if (access.varType() == String.class) {
67+
access.set(target, null);
68+
} else {
69+
access.set(target, 0);
70+
}
5771
}
5872
}
5973
}

test/jdk/jdk/jfr/api/context/TestJfrContext.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ public static class TestContextType extends ContextType {
5454

5555
@Name("result")
5656
@Description("TRUE/FALSE")
57-
public String result;
57+
public boolean result;
58+
59+
@Name("value")
60+
public byte value;
5861
}
5962

6063
@Name("too_large")
@@ -99,7 +102,8 @@ public static void main(String[] args) throws Throwable {
99102

100103
TestContextType captured = new TestContextType();
101104
captured.state = "enabled";
102-
captured.result = "FALSE";
105+
captured.result = false;
106+
captured.value = -1;
103107
captured.set();
104108

105109
ClassLoader classLoader = TestJfrContext.class.getClassLoader();
@@ -115,7 +119,8 @@ public static void main(String[] args) throws Throwable {
115119
}
116120
r.enable(eventClass).withThreshold(Duration.ofMillis(0)).withoutStackTrace();
117121
captured.state = "disabled";
118-
captured.result = "TRUE";
122+
captured.result = true;
123+
captured.value = 1;
119124
captured.set();
120125
synchronized (captured) {
121126
TooLargeContextType tlct = new TooLargeContextType();

test/lib/jdk/test/lib/jfr/ContextAwareEvent.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727

2828
import jdk.jfr.ContextAware;
2929
import jdk.jfr.Event;
30+
import jdk.jfr.Name;
3031

3132
@ContextAware
33+
@Name("test.ContextAware")
3234
public class ContextAwareEvent extends Event {
3335
public ContextAwareEvent(int id) {
3436
this.id = id;

0 commit comments

Comments
 (0)