内部类中的 Scala 原生方法

标签 scala java-native-interface

我有:

class XCClass
{   
  @native protected def createWin(displayPtr: Long, width: Int, height: Int, backGroundColour: Int = white.value,
    borderColour: Int = darkblue.value, borderWidth: Int = 0, xPosn: Int = 0, yPosn: Int = 0): Long
  @native protected def xOpen(): Long  
  System.load("/sdat/projects/prXCpp/Release/libprXCpp.so")  
//Code edited out

  class Window(width: Int, height: Int, backGroundColour: ColourInt = white, borderColour: ColourInt = darkblue,
    borderWidth: Int = 0, xPosn: Int = 0, yPosn: Int = 0)
  {
  val xWinPtr = createWin(xServPtr, width, height, backGroundColour.value, borderColour.value, borderWidth, xPosn, yPosn)
  @native def drawLine(x1: Int, y1: Int, x2: Int, y2: Int): Unit
  }
}

前两个方法工作正常,但内部类上的 native 方法给出了

object XApp extends App
{
  val xc:XCClass = XCClass()  
  val win: xc.Window = xc.Window(800, 600)
  win.drawLine(20, 20, 40, 40)
  readLine()  
}

线程“main”中出现异常 java.lang.UnsatisfiedLinkError: pXClient.XCClass$Window.drawLine(IIII)V

这是 C++ 签名

extern "C" JNIEXPORT void JNICALL Java_pXClient_XCClass$Window_drawLine(JNIEnv * env, jobject c1, Display *dpy,
    Window win, jint x1, jint y1, jint x2, jint y2)

我尝试使用下划线而不是 $ 符号,并且根本没有内部名称,但也失败了。

Edit2:在看到 Robin 的答案之前我设法让 javah 工作,它给出了

JNIEXPORT void JNICALL Java_pXClient_XCClass_00024Window_drawLine
  (JNIEnv *, jobject, jint, jint, jint, jint);

Edit4:一旦我纠正了代码中的错误,它就工作得很好。看起来,只要名称正确,JVM 就会导入参数签名错误的原生函数。

最佳答案

我刚刚使用 .java 文件和 javah 进行了快速测试,$ 表示为 _00024.

关于内部类中的 Scala 原生方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20033281/

相关文章:

android - 单元测试 JNI 调用

java - 从 Xamarin 中的 Java 绑定(bind)库接口(interface)访问属性

scala - 在退出前等待完成所有 child 工作的 Actor

scala - Play : Run tests with custom GuiceApplicationLoader

scala - 如何在 Scala 中定义排序?

scala - Akka 中的 I/O 是如何工作的?

scala - 带有Scala插件的IntelliJ IDEA找不到scala.concurrent

android - 我将使用什么 OpenCv 函数来检测在 Android 上实现的叶子中的疾病

java - 将来自 AVPacket : problems with output buffers 的字节数据提供给 MediaCodec

java - JDK7 和原生组件的焦点问题