Skip to content

Commit 7258251

Browse files
committed
fix method resolver to support the same behavior of throwing exceptions when method is not found on the target class
1 parent 4c19395 commit 7258251

File tree

1 file changed

+174
-24
lines changed

1 file changed

+174
-24
lines changed

src/jni/NativeScriptRuntime.cpp

Lines changed: 174 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -194,37 +194,187 @@ void NativeScriptRuntime::CallJavaMethod(const Handle<Object>& caller, const str
194194
jmethodID mid;
195195
string sig;
196196

197-
if ((entry != nullptr) && entry->isResolved)
198-
{
199-
isStatic = entry->isStatic;
200-
if (entry->memberId == nullptr)
197+
// if ((entry != nullptr) && entry->isResolved)
198+
// {
199+
// isStatic = entry->isStatic;
200+
//
201+
// if (entry->memberId == nullptr)
202+
// {
203+
// entry->clazz = env.FindClass(className);
204+
// if (entry->clazz == nullptr)
205+
// {
206+
// MetadataNode* callerNode = MetadataNode::GetNodeFromHandle(caller);
207+
// const string callerClassName = callerNode->GetName();
208+
//
209+
// DEBUG_WRITE("Cannot resolve class: %s while calling method: %s callerClassName: %s", className.c_str(), methodName.c_str(), callerClassName.c_str());
210+
// clazz = env.FindClass(callerClassName);
211+
// if (clazz == nullptr)
212+
// {
213+
// DEBUG_WRITE("Cannot resolve caller's class name: %s", callerClassName.c_str());
214+
// return;
215+
// }
216+
//
217+
// mid = isStatic ?
218+
// env.GetStaticMethodID(clazz, methodName, entry->sig) :
219+
// env.GetMethodID(clazz, methodName, entry->sig);
220+
//
221+
// if (mid == nullptr)
222+
// {
223+
// DEBUG_WRITE("Cannot resolve a method %s on caller class: %s", methodName.c_str(), callerClassName.c_str());
224+
// return;
225+
// }
226+
//
227+
// }
228+
// else
229+
// {
230+
// entry->memberId = isStatic ?
231+
// env.GetStaticMethodID(entry->clazz, methodName, entry->sig) :
232+
// env.GetMethodID(entry->clazz, methodName, entry->sig);
233+
//
234+
// if (entry->memberId == nullptr)
235+
// {
236+
// DEBUG_WRITE("Cannot resolve a method %s on class: %s", methodName.c_str(), className.c_str());
237+
// return;
238+
// }
239+
// }
240+
// }
241+
//
242+
// if (entry->clazz != nullptr)
243+
// {
244+
// clazz = entry->clazz;
245+
// mid = reinterpret_cast<jmethodID>(entry->memberId);
246+
// }
247+
//
248+
// sig = entry->sig;
249+
// }
250+
// else
251+
// {
252+
// DEBUG_WRITE("Resolving method: %s.%s on className %s", className.c_str(), methodName.c_str(), className.c_str());
253+
// auto mi = MethodCache::ResolveMethodSignature(className, methodName, args, isStatic);
254+
// if (mi.mid == nullptr)
255+
// {
256+
// MetadataNode* callerNode = MetadataNode::GetNodeFromHandle(caller);
257+
// const string callerClassName = callerNode->GetName();
258+
// DEBUG_WRITE("Resolving method on caller class: %s.%s on className %s", callerClassName.c_str(), methodName.c_str(), className.c_str());
259+
// mi = MethodCache::ResolveMethodSignature(callerClassName, methodName, args, isStatic);
260+
// if (mi.mid == nullptr)
261+
// {
262+
// DEBUG_WRITE("Cannot resolve class=%s, method=%s, isStatic=%d, isSuper=%d, callerClass=%s", className.c_str(), methodName.c_str(), isStatic, isSuper, callerClassName.c_str());
263+
// return;
264+
// }
265+
// }
266+
//
267+
// clazz = mi.clazz;
268+
// mid = mi.mid;
269+
// sig = mi.signature;
270+
// }
271+
272+
273+
274+
275+
276+
if ((entry != nullptr) && entry->isResolved)
201277
{
202-
entry->clazz = env.FindClass(className);
203-
if (entry->clazz == nullptr)
278+
isStatic = entry->isStatic;
279+
280+
if (entry->memberId == nullptr)
281+
{
282+
entry->clazz = env.FindClass(className);
283+
if (entry->clazz == nullptr)
284+
{
285+
MetadataNode* callerNode = MetadataNode::GetNodeFromHandle(caller);
286+
const string callerClassName = callerNode->GetName();
287+
288+
DEBUG_WRITE("Cannot resolve class: %s while calling method: %s callerClassName: %s", className.c_str(), methodName.c_str(), callerClassName.c_str());
289+
clazz = env.FindClass(callerClassName);
290+
if (clazz == nullptr)
291+
{
292+
DEBUG_WRITE("Cannot resolve caller's class name: %s", callerClassName.c_str());
293+
return;
294+
}
295+
296+
mid = isStatic ?
297+
env.GetStaticMethodID(clazz, methodName, entry->sig) :
298+
env.GetMethodID(clazz, methodName, entry->sig);
299+
300+
if (mid == nullptr)
301+
{
302+
DEBUG_WRITE("Cannot resolve a method %s on caller class: %s", methodName.c_str(), callerClassName.c_str());
303+
return;
304+
}
305+
306+
}
307+
else
308+
{
309+
entry->memberId = isStatic ?
310+
env.GetStaticMethodID(entry->clazz, methodName, entry->sig) :
311+
env.GetMethodID(entry->clazz, methodName, entry->sig);
312+
313+
if (entry->memberId == nullptr)
314+
{
315+
DEBUG_WRITE("Cannot resolve a method %s on class: %s", methodName.c_str(), className.c_str());
316+
return;
317+
}
318+
}
319+
}
320+
321+
if (entry->clazz != nullptr)
204322
{
205-
DEBUG_WRITE("Cannot resolve class=%s while calling method %s", className.c_str(), methodName.c_str());
323+
clazz = entry->clazz;
324+
mid = reinterpret_cast<jmethodID>(entry->memberId);
206325
}
207326

208-
entry->memberId = isStatic
209-
? env.GetStaticMethodID(entry->clazz, methodName, entry->sig)
210-
: env.GetMethodID(entry->clazz, methodName, entry->sig);
327+
sig = entry->sig;
211328
}
212-
clazz = entry->clazz;
213-
mid = reinterpret_cast<jmethodID>(entry->memberId);
214-
sig = entry->sig;
215-
}
216-
else
217-
{
218-
auto mi = MethodCache::ResolveMethodSignature(className, methodName, args, isStatic);
219-
if (mi.mid == nullptr)
329+
else
220330
{
221-
DEBUG_WRITE("Cannot resolve class=%s, method=%s, isStatic=%d, isSuper=%d", className.c_str(), methodName.c_str(), isStatic, isSuper);
222-
return;
331+
// auto mi = MethodCache::ResolveMethodSignature(className, methodName, args, isStatic);
332+
// if (mi.mid == nullptr)
333+
// {
334+
// DEBUG_WRITE("Cannot resolve class=%s, method=%s, isStatic=%d, isSuper=%d", className.c_str(), methodName.c_str(), isStatic, isSuper);
335+
// return;
336+
// }
337+
// clazz = mi.clazz;
338+
// mid = mi.mid;
339+
// sig = mi.signature;
340+
341+
342+
//DEBUG_WRITE("Resolving method: %s.%s on className %s", className.c_str(), methodName.c_str(), className.c_str());
343+
MethodCache::CacheMethodInfo mi;
344+
345+
clazz = env.FindClass(className);
346+
if (clazz != nullptr)
347+
{
348+
mi = MethodCache::ResolveMethodSignature(className, methodName, args, isStatic);
349+
if (mi.mid == nullptr)
350+
{
351+
DEBUG_WRITE("Cannot resolve class=%s, method=%s, isStatic=%d, isSuper=%d", className.c_str(), methodName.c_str(), isStatic, isSuper);
352+
return;
353+
}
354+
}
355+
else
356+
{
357+
MetadataNode* callerNode = MetadataNode::GetNodeFromHandle(caller);
358+
const string callerClassName = callerNode->GetName();
359+
DEBUG_WRITE("Resolving method on caller class: %s.%s on className %s", callerClassName.c_str(), methodName.c_str(), className.c_str());
360+
mi = MethodCache::ResolveMethodSignature(callerClassName, methodName, args, isStatic);
361+
if (mi.mid == nullptr)
362+
{
363+
DEBUG_WRITE("Cannot resolve class=%s, method=%s, isStatic=%d, isSuper=%d, callerClass=%s", className.c_str(), methodName.c_str(), isStatic, isSuper, callerClassName.c_str());
364+
return;
365+
}
366+
}
367+
368+
369+
clazz = mi.clazz;
370+
mid = mi.mid;
371+
sig = mi.signature;
223372
}
224-
clazz = mi.clazz;
225-
mid = mi.mid;
226-
sig = mi.signature;
227-
}
373+
374+
375+
376+
377+
228378

229379
if (!isStatic)
230380
{

0 commit comments

Comments
 (0)