Skip to content

Commit f7e6b4a

Browse files
committed
GH-310 handle renamed fields 'impl' and 'creatorApi'
1 parent 206a96a commit f7e6b4a

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

visualvm/sampler.truffle/libsrc/org/graalvm/visualvm/sampler/truffle/stagent/Truffle.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ private Collection<Engine> getAllEngineInstances() {
186186
if (Engine_findActiveEngines == null) {
187187
Collection<Engine> en = new ArrayList();
188188
for (Object o : engines.keySet()) {
189-
Field cf = o.getClass().getDeclaredField("creatorApi");
189+
Field cf = TruffleJMX.getDeclaredField(o, "creatorApi", "api");
190190
Engine e = (Engine) unsafe.getObject(o, unsafe.objectFieldOffset(cf));
191191
en.add(e);
192192
}

visualvm/sampler.truffle/libsrc/org/graalvm/visualvm/sampler/truffle/stagent/TruffleJMX.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.net.JarURLConnection;
3636
import java.net.URL;
3737
import java.net.URLClassLoader;
38+
import java.util.Arrays;
3839
import java.util.logging.Level;
3940
import java.util.logging.Logger;
4041
import javax.management.InstanceAlreadyExistsException;
@@ -103,8 +104,8 @@ private static Object getContext() throws ClassNotFoundException, NoSuchMethodEx
103104
}
104105

105106
private static Object getContextImpl(Object context) throws IllegalArgumentException, SecurityException, NoSuchFieldException, IllegalAccessException {
106-
// return context.impl
107-
Field implField = context.getClass().getDeclaredField("impl");
107+
// return context.impl or context.dispatch
108+
Field implField = getDeclaredField(context, "impl", "dispatch");
108109
try {
109110
implField.setAccessible(true);
110111
Object impl = implField.get(context);
@@ -119,6 +120,17 @@ private static Object getContextImpl(Object context) throws IllegalArgumentExcep
119120
}
120121
}
121122

123+
static Field getDeclaredField(Object obj, String... names) throws NoSuchFieldException {
124+
for (Field f : obj.getClass().getDeclaredFields()) {
125+
for (String name : names) {
126+
if (name.equals(f.getName())) {
127+
return f;
128+
}
129+
}
130+
}
131+
throw new NoSuchFieldException(Arrays.toString(names));
132+
}
133+
122134
private static URL getJarURL() throws IOException {
123135
URL classUrl = ClassLoader.getSystemResource("org/graalvm/visualvm/sampler/truffle/stagent/Truffle.class");
124136
JarURLConnection connection = (JarURLConnection) classUrl.openConnection();

0 commit comments

Comments
 (0)