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

Follow the class hierarchy to lookup a field #584

Merged
merged 5 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -40,6 +40,7 @@
import java.util.Deque;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
Expand Down Expand Up @@ -3035,9 +3036,24 @@ private static Field getField(Class<?> clazz, String name, boolean throwError) {
return AccessController.doPrivileged(
(PrivilegedAction<Field>)
() -> {
Field field = null;
Class<?> cClass = clazz;
try {
Field field = clazz.getDeclaredField(name);
while (Objects.isNull(field) && Objects.nonNull(cClass)) {
try {
field = cClass.getDeclaredField(name);
} catch (NoSuchFieldException exp) {
// Ignore the exception and continue looking for the parent class
cClass = cClass.getSuperclass();
}
}

if (Objects.isNull(cClass)) {
throw new NoSuchFieldException(name);
}

field.setAccessible(true);

return field;
} catch (Exception exp) {
if (throwError) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.openjdk.btrace.core;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

class BTraceUtilsTest {
GGGGGHT marked this conversation as resolved.
Show resolved Hide resolved

@Test
public void getIntTest() {
D a = new D();
RuntimeException exception = assertThrows(RuntimeException.class, () -> BTraceUtils.Reflective.getInt("notExist", a));
assertTrue(exception.getMessage().contains("notExist"));
}

static class A {
int a;
}

static class B extends A {

}

static class C extends A {
}

static class D extends C {
}
}