@@ -2195,30 +2195,26 @@ public int RegisterNative(@JavaType(Class.class) StaticObject clazz, @Pointer Tr
21952195 return handleNoSuchMethod (meta );
21962196 }
21972197 Method targetMethod = clazz .getMirrorKlass (getMeta ()).lookupDeclaredMethod (name , signature );
2198- if (targetMethod != null && targetMethod .isNative ()) {
2199- targetMethod .unregisterNative ();
2200- getSubstitutions ().removeRuntimeSubstitution (targetMethod );
2201- } else {
2198+ if (targetMethod == null || !targetMethod .isNative ()) {
22022199 // agents might have set native method prefix, so check with those as well
2203- if (targetMethod == null || getContext ().getJavaAgents () == null ) {
2204- return handleNoSuchMethod (meta );
2205- }
2206- if (findPrefixedNative (name , clazz , signature ) != null ) {
2207- // we found a native wrapper method for the target method
2208- // use the original target method which match the native implementation
2209- targetMethod .unregisterNative ();
2210- getSubstitutions ().removeRuntimeSubstitution (targetMethod );
2211- } else {
2212- return handleNoSuchMethod (meta );
2213- }
2200+ targetMethod = findPrefixedNative (name , clazz , signature );
22142201 }
2202+ if (targetMethod == null ) {
2203+ // OK, nowhere to be found, so give up
2204+ return handleNoSuchMethod (meta );
2205+ }
2206+ // make sure we have the correct method name also for prefixed methods
2207+ name = targetMethod .getName ();
2208+ targetMethod .unregisterNative ();
2209+ getSubstitutions ().removeRuntimeSubstitution (targetMethod );
22152210
22162211 // Lookup known VM methods to shortcut native boundaries.
22172212 Substitutions .EspressoRootNodeFactory factory = lookupKnownVmMethods (closure , targetMethod );
22182213 if (factory == null ) {
22192214 NativeSignature ns = Method .buildJniNativeSignature (targetMethod .getParsedSignature ());
22202215 final TruffleObject boundNative = getNativeAccess ().bindSymbol (closure , ns );
2221- factory = createJniRootNodeFactory (() -> EspressoRootNode .createNative (getContext ().getJNI (closure ), targetMethod .getMethodVersion (), boundNative ), targetMethod );
2216+ final Method finalTargetMethod = targetMethod ;
2217+ factory = createJniRootNodeFactory (() -> EspressoRootNode .createNative (getContext ().getJNI (closure ), finalTargetMethod .getMethodVersion (), boundNative ), targetMethod );
22222218 }
22232219
22242220 Symbol <Type > classType = clazz .getMirrorKlass (getMeta ()).getType ();
@@ -2227,6 +2223,9 @@ public int RegisterNative(@JavaType(Class.class) StaticObject clazz, @Pointer Tr
22272223 }
22282224
22292225 private Method findPrefixedNative (Symbol <Name > name , @ JavaType (Class .class ) StaticObject clazz , Symbol <Signature > signature ) {
2226+ if (getContext ().getJavaAgents () == null ) {
2227+ return null ;
2228+ }
22302229 Symbol <Name >[] allNativePrefixes = getContext ().getJavaAgents ().getAllNativePrefixes ();
22312230 if (allNativePrefixes == Symbol .EMPTY_ARRAY ) {
22322231 return null ;
0 commit comments