是否有可能覆盖在Android中/ Dalvik的Java类中的本地方法? [英] Is it possible to override a native method in a Java class in Android/dalvik?
问题描述
我是单元测试使用了EasyMock类 TESTME
,以及它的一个方法(比如办法(N N)
)预计类型的参数 N
其中有一个本地方法(比如 nativeMethod()
)。
I am unit testing a class TestMe
using EasyMock, and one of its methods (say method(N n)
) expects a parameter of type N
which has a native method (say nativeMethod()
).
class TestMe {
void method(N n) {
// Do stuff
n.nativeMethod();
// Do more stuff
}
}
()方法
需要调用 N.nativeMethod()
在某些时候,我的问题其是,对于N EasyMock的我的模拟对象是无法覆盖本地方法。我不拥有N级,但是我可以重构 TESTME
以任何方式必要的。
method()
needs to invoke N.nativeMethod()
at some point, and the problem I'm having is that my Easymock mock object for N is unable to override the native method. I do not own class N but I can refactor TestMe
in any way necessary.
我决定把我自己的类 FakeN延伸ñ
将覆盖 nativeMethod
来什么都不做:
I decided to make my own class FakeN extends N
which overrides nativeMethod
to do nothing:
class FakeN extends N {
FakeN(int pointer) {
super(pointer);
}
@Override
public void nativeMethod(Object o) {
// super.nativeMethod() is an actual native method defined as:
// public native void nativeMethod(Object o)
//
// IGNORE
}
}
但在编译器不抱怨,当我运行测试,似乎 N.nativeMethod()
是正在调用的一个而不是 FakeN
S版。
but while the compiler does not complain, when I run the test it appears that N.nativeMethod()
is the one being invoked and not FakeN
s version.
有一种变通方法在这里,我可以使用?
Is there a workaround here that I can use?
推荐答案
的本地
方法可以覆盖,就像任何其他的方法,除非它们被声明最后
。只要确保你调用 TestMe.method(N N)
与 FakeN
的一个实例。
The native
methods can be overridden just like any other methods, unless they are declared final
. Just be sure that you're calling TestMe.method(N n)
with an instance of FakeN
.
这篇关于是否有可能覆盖在Android中/ Dalvik的Java类中的本地方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!