35
35
import java .net .JarURLConnection ;
36
36
import java .net .URL ;
37
37
import java .net .URLClassLoader ;
38
+ import java .util .Arrays ;
38
39
import java .util .logging .Level ;
39
40
import java .util .logging .Logger ;
40
41
import javax .management .InstanceAlreadyExistsException ;
@@ -103,8 +104,8 @@ private static Object getContext() throws ClassNotFoundException, NoSuchMethodEx
103
104
}
104
105
105
106
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 " );
108
109
try {
109
110
implField .setAccessible (true );
110
111
Object impl = implField .get (context );
@@ -119,6 +120,17 @@ private static Object getContextImpl(Object context) throws IllegalArgumentExcep
119
120
}
120
121
}
121
122
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
+
122
134
private static URL getJarURL () throws IOException {
123
135
URL classUrl = ClassLoader .getSystemResource ("org/graalvm/visualvm/sampler/truffle/stagent/Truffle.class" );
124
136
JarURLConnection connection = (JarURLConnection ) classUrl .openConnection ();
0 commit comments