mozilla / rhino

Rhino is an open-source implementation of JavaScript written entirely in Java
https://rhino.github.io
Other
4.18k stars 851 forks source link

The choice of Java method java.lang.String.replace matching JavaScript argument types (function,string) is ambiguous #1632

Closed dhavkuma closed 2 months ago

dhavkuma commented 2 months ago

Code resource.getPath().replace(/\W/g, '').replace(/+/g, '_')

ERROR Caused by: org.mozilla.javascript.EvaluatorException: The choice of Java method java.lang.String.replace matching JavaScript argument types (function,string) is ambiguous; candidate methods are: _ class java.lang.String replace(java.lang.CharSequence,java.lang.CharSequence)_ class java.lang.String replace(char,char) (/libs/screens/core/components/content/video/video.js#169) at org.mozilla.javascript.DefaultErrorReporter.runtimeError(DefaultErrorReporter.java:77) at org.mozilla.javascript.Context.reportRuntimeError(Context.java:954) at org.mozilla.javascript.Context.reportRuntimeError(Context.java:1010) at org.mozilla.javascript.Context.reportRuntimeError4(Context.java:997) at org.mozilla.javascript.NativeJavaMethod.findFunction(NativeJavaMethod.java:468) at org.mozilla.javascript.NativeJavaMethod.findCachedFunction(NativeJavaMethod.java:262) at org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:139) at org.mozilla.javascript.optimizer.OptRuntime.call2(OptRuntime.java:42) [org.apache.servicemix.bundles.rhino:1.7.7.1_1] at org.mozilla.javascript.gen._libs_screens_core_components_content_video_video_js_20._c_anonymous_1(/libs/screens/core/components/content/video/video.js:169)

p-bakker commented 2 months ago

See https://rhino.github.io/tutorials/scripting_java/#calling-overloaded-methods for how to select which one you want to call

dhavkuma commented 2 months ago

Thanks. @p-bakker. The doc you shared talks about function overloading. One question still open to me is how does the regex in resource.getPath().replace(/\W/g, '').replace(/+/g, '_') converts to a replace( function, string).