使用JAVA JNI和C ++进行通用调用 [英] Making generic calls with JAVA JNI and C++

查看:150
本文介绍了使用JAVA JNI和C ++进行通用调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JNI,我必须传递一些通用类型到C ++。

I am working with JNI and I have to pass in some generic types to the C++. I am stuck with how to approach this on the C++ side

HashMap<String, Double[]> data1 ; 
ArrayList<ArrayList<String>> disc ;

我是JNI的新手,看了看,但找不到很多帮助。可以有人帮我怎么写JNI代码这个请。

I am new to JNI and looked around but could not find much help. Can some one help me how to write JNI code for this please. Any reference to material on the net would be very helpful too.

推荐答案

简短的答案:你不能。

长回答:类型擦除: http://download.oracle .com / javase / tutorial / java / generics / erasure.html

考虑一个 ArrayList< Integer> code>。在编译时,编译器检查您在数组列表实例中没有放置任何与 Integer 兼容的东西。

然而,在编译时(以及句法检查之后),编译器剥离类型参数,将 ArrayList 变换为 Arraylist < > 这等效于 ArrayList< Object> 或简单地 ArrayList JDK 5次。)

However, also at compile time (and after syntactic checking), the compiler strips the type parameter, rendering ArrayList<Integer> into Arraylist<?> which is equivalent to ArrayList<Object> or simply ArrayList (as in pre JDK 5 times.)

后面的形式是JNI期望的(因为历史原因以及由于在Java中实现泛型的方式...再次,类型擦除。)

The later form is what JNI expects (because of historical reasons as well as due to the way generics are implemented in Java... again, type erasure.)

请记住, ArrayList <整数> is-a ArrayList 。所以你可以传递 ArrayList< Integer> 到JNI,无论它期望一个 ArrayList 。相反的情况不一定是真的,因为你可能会得到的东西从JNI,不能向上兼容你的很好的参数化泛型。

Remember, an ArrayList<Integer> is-a ArrayList. So you can pass an ArrayList<Integer> to JNI wherever it expects an ArrayList. The opposite is not necessarily true as you might get something out of JNI that is not upwards compatible with your nicely parametrized generics.

在这一点上,你是跨越一个障碍一个类型化的参数化域(你的泛型)和一个无类型的(JNI)。你必须封装这个障碍相当不错,你必须添加胶水代码和错误检查/错误处理代码来检测何时/如果事情不能很好地转换。

At this point, you are crossing a barrier between a typed, parametrized domain (your generics) and an untyped one (JNI). You have to encapsulate that barrier pretty nicely, and you have to add glue code and error checking/error handling code to detect when/if things don't convert well.

这篇关于使用JAVA JNI和C ++进行通用调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆