可以访问java运行时中的本机空隙,就像这样吗?
因此,我正在四处寻找另一个答案,我注意到ObjectInputStream.java中有这两个方法(和)可以访问java运行时中的本机空隙,就像这样吗?,java,Java,因此,我正在四处寻找另一个答案,我注意到ObjectInputStream.java中有这两个方法(和) /** * Converts specified span of bytes into float values. */ // REMIND: remove once hotspot inlines Float.intBitsToFloat private static native void bytesToFloats(byte[] src, int srcpos,
/**
* Converts specified span of bytes into float values.
*/
// REMIND: remove once hotspot inlines Float.intBitsToFloat
private static native void bytesToFloats(byte[] src, int srcpos,
float[] dst, int dstpos,
int nfloats);
/**
* Converts specified span of bytes into double values.
*/
// REMIND: remove once hotspot inlines Double.longBitsToDouble
private static native void bytesToDoubles(byte[] src, int srcpos,
double[] dst, int dstpos,
int ndoubles);
一开始我很沮丧,因为它们是私人的,土生土长的,我认为我的实验到此为止了。然而,我在放弃之前做了一个实验,只是在我自己的课堂上复制了这些方法的定义,并且成功了
所以我的问题是:为什么它会起作用?为什么本机方法可以从“任何地方”这样访问?在访问本机方法时,我希望有一些守卫或门柱
为了进一步澄清我的问题:我很好奇,为什么java.io的本机方法可以从任何地方访问,而不需要任何指针来指明访问的确切内容。对我来说,它有点像一个奇怪的超全局。编译器“假设”您提供了本机库
class Ideone
{
private static native void bytesToDoubles(byte[] src, int srcpos,
double[] dst, int dstpos,
int ndoubles);
public static void main (String[] args) throws java.lang.Exception
{
byte[] bytes = { 1, 2, 3 ,4, 5, 6, 7, 8};
double[] dst = new double[1];
bytesToDoubles(bytes, 0, dst, 0, 1);
System.out.println(dst[0]);
}
}
有了这个答案,我们可以看到,当我们运行程序时,将不会有本机库
class Ideone
{
private static native void bytesToDoubles(byte[] src, int srcpos,
double[] dst, int dstpos,
int ndoubles);
public static void main (String[] args) throws java.lang.Exception
{
byte[] bytes = { 1, 2, 3 ,4, 5, 6, 7, 8};
double[] dst = new double[1];
bytesToDoubles(bytes, 0, dst, 0, 1);
System.out.println(dst[0]);
}
}
线程“main”java.lang.UnsatifiedLinkError中出现异常:>Ideone.ByTestDoubles([BI[DII)V在Ideone.bytestodoobles(本机方法)
在Ideone.main(main.java:17)
因此,要访问另一个类本机库,您需要使用完全限定的类名。因此,它将获得private提供的“保护”。您复制的类是否具有相同的完全限定的名称?@TavianBarnes No.一点也不。什么是“有效的”是吗?它编译了吗?还是你在你的类中成功地调用了那些方法?考虑发布一个最小但完整的例子。@ TsCalkaa,我不知道你在哪里调用这些方法。除非你调用它们,否则不会出错。@ TrackaCa编译器不能检查它,因为本地库在运行时W被加载。ith
System.loadLibrary()