// emit a message with the call made appended to the message void emitErrorMessage(const std::string& msg) {
std::string nm = classname;
std::replace(nm.begin(), nm.end(), '/', '.');
::fprintf(stderr, "ERROR: %s::%s, %s\n", nm.c_str(), method.c_str(), msg.c_str());
}
// check the given object which is expected to be null void checkReturnNull(jobject obj) { if (obj != NULL) {
emitErrorMessage("Null return expected");
::exit(-1);
}
}
// check the given object which is expected to NOT be null void checkReturnNotNull(jobject obj) { if (obj == NULL) {
emitErrorMessage("Non-Null return expected");
::exit(-1);
}
}
// check if any unexpected exceptions were thrown void checkException() { if (env->ExceptionOccurred() != NULL) {
emitErrorMessage("Exception was thrown");
env->ExceptionDescribe();
::exit(-1);
}
}
// check if an expected exception was thrown void checkExpectedExceptionThrown(const std::string& exception) {
jclass expected = env->FindClass(exception.c_str());
assert(expected != NULL);
jthrowable t = env->ExceptionOccurred(); if (env->IsInstanceOf(t, expected) == JNI_FALSE) {
emitErrorMessage("Didn't get the expected " + exception);
::exit(-1);
}
env->ExceptionClear();
}
/* *supportformakingcheckedcallsoninstancesofanobject
*/ class InstanceCall : public CallHelper { public:
InstanceCall(JNIEnv* e, const std::string& cname, const std::string& mname, const std::string& sig)
: CallHelper(e, cname, mname, sig) {
m = env->GetMethodID(c, method.c_str(), signature.c_str());
assert(m != NULL);
}
// call on the given object, checking for exceptions and that the return is not null
jobject callReturnNotNull(jobject obj) {
jobject robj = call(obj);
checkReturnNotNull(robj); return robj;
}
// call on the given object with an argument, // checking for exceptions and that the return is not null
jobject callReturnNotNull(jobject obj, jobject arg) {
jobject robj = call(obj, arg);
checkReturnNotNull(robj); return robj;
}
// call on the given object, checking for exceptions and that the return is null
jobject callReturnIsNull(jobject obj) {
jobject robj = call(obj);
checkReturnNull(robj); return robj;
}
// call on the given object with an argument, // checking for exceptions and that the return is null
jobject callReturnIsNull(jobject obj, jobject arg) {
jobject robj = call(obj, arg);
checkReturnNull(robj); return robj;
}
// call a void method checking if exceptions were thrown void callVoidMethod(jobject obj) {
env->CallVoidMethod(obj, m);
checkException();
}
/* *supportformakingcheckedstaticcalls
*/ class StaticCall : public CallHelper { public:
StaticCall(JNIEnv* e, const std::string& cname, const std::string& mname, const std::string& sig)
: CallHelper(e, cname, mname, sig) {
m = env->GetStaticMethodID(c, method.c_str(), signature.c_str());
assert(m != NULL);
}
// call a method returning an object checking for exceptions and // the return value is not null.
jobject callReturnNotNull(jobject arg) {
jobject robj = env->CallStaticObjectMethod(c, m, arg);
checkException();
checkReturnNotNull(robj); return robj;
}
// call a void method checking if any exceptions thrown void callVoidMethod() {
env->CallStaticVoidMethod(c, m);
checkException();
}
// call method returning boolean that is expected to throw the // given exception void callBooleanMethodWithException(const std::string& exception) {
env->CallStaticBooleanMethod(c, m);
checkExpectedExceptionThrown(exception);
}
// call method returning an object that is expected to throw the // given exception void callObjectMethodWithException(const std::string& exception) {
env->CallStaticObjectMethod(c, m);
checkExpectedExceptionThrown(exception);
}
};
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.