Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8253762: JFR: getField(String) should be able to access subfields
Reviewed-by: mgronlun
  • Loading branch information
egahlin committed Dec 8, 2020
1 parent 39b8a2e commit cef606f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/jdk.jfr/share/classes/jdk/jfr/EventType.java
Expand Up @@ -66,6 +66,9 @@ public List<ValueDescriptor> getFields() {
/**
* Returns the field with the specified name, or {@code null} if it doesn't
* exist.
* <p>
* It's possible to index into a nested field by using {@code "."} (for
* instance {@code "thread.group.parent.name}").
*
* @param name of the field to get, not {@code null}
*
Expand All @@ -82,7 +85,12 @@ public ValueDescriptor getField(String name) {
}
cache = newCache;
}
return cache.get(name);
ValueDescriptor result = cache.get(name);
if (result == null) {
// Cache doesn't contain subfields
result = platformEventType.getField(name);
}
return result;
}

/**
Expand Down
Expand Up @@ -189,6 +189,9 @@ final <T> T getTyped(String name, Class<T> clazz, T defaultValue) {
/**
* Returns {@code true} if a field with the given name exists, {@code false}
* otherwise.
* <p>
* It's possible to index into a nested field by using {@code "."} (for
* instance {@code "thread.group.parent.name}").
*
* @param name name of the field to get, not {@code null}
*
Expand Down
20 changes: 20 additions & 0 deletions src/jdk.jfr/share/classes/jdk/jfr/internal/Type.java
Expand Up @@ -191,6 +191,26 @@ public String getLogName() {
return getName() + "(" + getId() + ")";
}

public ValueDescriptor getField(String name) {
int dotIndex = name.indexOf(".");
if (dotIndex > 0) {
String pre = name.substring(0, dotIndex);
String post = name.substring(dotIndex + 1);
ValueDescriptor subField = getField(pre);
if (subField != null) {
Type type = PrivateAccess.getInstance().getType(subField);
return type.getField(post);
}
} else {
for (ValueDescriptor v : getFields()) {
if (name.equals(v.getName())) {
return v;
}
}
}
return null;
}

public List<ValueDescriptor> getFields() {
if (fields instanceof ArrayList) {
((ArrayList<ValueDescriptor>) fields).trimToSize();
Expand Down
9 changes: 7 additions & 2 deletions test/jdk/jdk/jfr/api/metadata/eventtype/TestGetField.java
Expand Up @@ -44,13 +44,17 @@ public static void main(String[] args) throws Throwable {
EventType type = EventType.getEventType(MyEvent.class);

ValueDescriptor v = type.getField("myByte");
Asserts.assertNotNull(v, "getFiled(myByte) was null");
Asserts.assertNotNull(v, "getField(myByte) was null");
Asserts.assertEquals(v.getTypeName(), "byte", "myByte was not type byte");

v = type.getField("myInt");
Asserts.assertNotNull(v, "getFiled(myInt) was null");
Asserts.assertNotNull(v, "getField(myInt) was null");
Asserts.assertEquals(v.getTypeName(), "int", "myInt was not type int");

v = type.getField("eventThread.group.name");
Asserts.assertNotNull(v, "getField(eventThread.group.name) was null");
Asserts.assertEquals(v.getTypeName(), "java.lang.String", "eventThread.group.name was not type java.lang.String");

v = type.getField("myStatic");
Asserts.assertNull(v, "got static field");

Expand All @@ -60,6 +64,7 @@ public static void main(String[] args) throws Throwable {
v = type.getField("");
Asserts.assertNull(v, "got field for empty name");


try {
v = type.getField(null);
Asserts.fail("No Exception when getField(null)");
Expand Down

1 comment on commit cef606f

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.