Commit 59a49e30 authored by Thomas Guillem's avatar Thomas Guillem

libvlcjni: rework exceptions throwing from jni

Throw OutOfMemoryError instead of IllegalStateException for allocation errors.
parent c96b6635
......@@ -226,7 +226,7 @@ Java_org_videolan_libvlc_Dialog_00024LoginDialog_nativePostLogin(
if (!username
|| !(psz_username = (*env)->GetStringUTFChars(env, username, 0)))
{
throw_IllegalArgumentException(env, "username invalid");
throw_Exception(env, VLCJNI_EX_ILLEGAL_ARGUMENT, "username invalid");
return;
}
......@@ -234,7 +234,7 @@ Java_org_videolan_libvlc_Dialog_00024LoginDialog_nativePostLogin(
|| !(psz_password = (*env)->GetStringUTFChars(env, password, 0)))
{
(*env)->ReleaseStringUTFChars(env, username, psz_username);
throw_IllegalArgumentException(env, "password invalid");
throw_Exception(env, VLCJNI_EX_ILLEGAL_ARGUMENT, "password invalid");
return;
}
......
......@@ -104,7 +104,9 @@ Media_nativeNewCommon(JNIEnv *env, jobject thiz, vlcjni_object *p_obj)
{
free(p_obj->p_sys);
VLCJniObject_release(env, thiz, p_obj);
throw_IllegalStateException(env, "can't create Media instance");
throw_Exception(env,
!p_obj->u.p_m ? VLCJNI_EX_ILLEGAL_STATE : VLCJNI_EX_OUT_OF_MEMORY,
"can't create Media instance");
return;
}
......@@ -125,7 +127,7 @@ Media_nativeNewFromCb(JNIEnv *env, jobject thiz, jobject libVlc, jstring jmrl,
if (!jmrl || !(p_mrl = (*env)->GetStringUTFChars(env, jmrl, 0)))
{
throw_IllegalArgumentException(env, "path or location invalid");
throw_Exception(env, VLCJNI_EX_ILLEGAL_ARGUMENT, "path or location invalid");
return;
}
......@@ -173,7 +175,7 @@ Java_org_videolan_libvlc_Media_nativeNewFromFd(JNIEnv *env, jobject thiz,
}
if (fd == -1)
{
throw_IllegalArgumentException(env, "fd invalid");
throw_Exception(env, VLCJNI_EX_ILLEGAL_ARGUMENT, "fd invalid");
return;
}
......@@ -486,7 +488,7 @@ Java_org_videolan_libvlc_Media_nativeAddOption(JNIEnv *env, jobject thiz,
if (!joption || !(p_option = (*env)->GetStringUTFChars(env, joption, 0)))
{
throw_IllegalArgumentException(env, "option invalid");
throw_Exception(env, VLCJNI_EX_ILLEGAL_ARGUMENT, "option invalid");
return;
}
......@@ -508,7 +510,7 @@ Java_org_videolan_libvlc_Media_nativeAddSlave(JNIEnv *env, jobject thiz,
if (!juri || !(psz_uri = (*env)->GetStringUTFChars(env, juri, 0)))
{
throw_IllegalArgumentException(env, "uri invalid");
throw_Exception(env, VLCJNI_EX_ILLEGAL_ARGUMENT, "uri invalid");
return;
}
......@@ -516,7 +518,8 @@ Java_org_videolan_libvlc_Media_nativeAddSlave(JNIEnv *env, jobject thiz,
(*env)->ReleaseStringUTFChars(env, juri, psz_uri);
if (i_ret != 0)
throw_IllegalStateException(env, "can't add slaves to libvlc_media");
throw_Exception(env, VLCJNI_EX_ILLEGAL_ARGUMENT,
"can't add slaves to libvlc_media");
}
void
......
......@@ -30,7 +30,7 @@ Java_org_videolan_libvlc_MediaDiscoverer_nativeNew(JNIEnv *env,
if (!jname || !(p_name = (*env)->GetStringUTFChars(env, jname, 0)))
{
throw_IllegalArgumentException(env, "jname invalid");
throw_Exception(env, VLCJNI_EX_ILLEGAL_STATE, "jname invalid");
return;
}
......@@ -48,7 +48,8 @@ Java_org_videolan_libvlc_MediaDiscoverer_nativeNew(JNIEnv *env,
if (!p_obj->u.p_md)
{
VLCJniObject_release(env, thiz, p_obj);
throw_IllegalStateException(env, "can't create MediaDiscoverer instance");
throw_Exception(env, VLCJNI_EX_ILLEGAL_STATE,
"can't create MediaDiscoverer instance");
return;
}
}
......
......@@ -61,7 +61,8 @@ Equalizer_getInstance(JNIEnv *env, jobject thiz)
(*env)->GetLongField(env, thiz,
fields.MediaPlayer.Equalizer.mInstanceID);
if (!i_ptr)
throw_IllegalStateException(env, "can't get Equalizer instance");
throw_Exception(env, VLCJNI_EX_ILLEGAL_STATE,
"can't get Equalizer instance");
return (libvlc_equalizer_t*) i_ptr;
}
......@@ -120,14 +121,17 @@ MediaPlayer_newCommon(JNIEnv *env, jobject thiz, vlcjni_object *p_obj,
if (!p_obj->u.p_mp || !p_obj->p_sys)
{
VLCJniObject_release(env, thiz, p_obj);
throw_IllegalStateException(env, "can't create MediaPlayer instance");
throw_Exception(env,
!p_obj->u.p_mp ? VLCJNI_EX_ILLEGAL_STATE : VLCJNI_EX_OUT_OF_MEMORY,
"can't create MediaPlayer instance");
return;
}
p_obj->p_sys->jwindow = (*env)->NewGlobalRef(env, jwindow);
if (!p_obj->p_sys->jwindow)
{
VLCJniObject_release(env, thiz, p_obj);
throw_IllegalStateException(env, "can't create MediaPlayer instance");
throw_Exception(env, VLCJNI_EX_ILLEGAL_STATE,
"can't create MediaPlayer instance");
return;
}
libvlc_media_player_set_android_context(p_obj->u.p_mp, p_obj->p_sys->jwindow);
......@@ -522,7 +526,7 @@ Java_org_videolan_libvlc_MediaPlayer_nativeSetAudioOutput(JNIEnv *env,
if (!jaout || !(psz_aout = (*env)->GetStringUTFChars(env, jaout, 0)))
{
throw_IllegalArgumentException(env, "aout invalid");
throw_Exception(env, VLCJNI_EX_ILLEGAL_ARGUMENT, "aout invalid");
return false;
}
......@@ -546,7 +550,7 @@ Java_org_videolan_libvlc_MediaPlayer_nativeSetAudioOutputDevice(JNIEnv *env,
if (!jid || !(psz_id = (*env)->GetStringUTFChars(env, jid, 0)))
{
throw_IllegalArgumentException(env, "aout invalid");
throw_Exception(env, VLCJNI_EX_ILLEGAL_ARGUMENT, "aout invalid");
return false;
}
......@@ -987,7 +991,7 @@ Java_org_videolan_libvlc_MediaPlayer_nativeSetAspectRatio(JNIEnv *env,
}
if (!(psz_aspect = (*env)->GetStringUTFChars(env, jaspect, 0)))
{
throw_IllegalArgumentException(env, "aspect invalid");
throw_Exception(env, VLCJNI_EX_ILLEGAL_ARGUMENT, "aspect invalid");
return;
}
......@@ -1036,7 +1040,7 @@ Java_org_videolan_libvlc_MediaPlayer_nativeAddSlave(JNIEnv *env,
if (!jmrl || !(psz_mrl = (*env)->GetStringUTFChars(env, jmrl, 0)))
{
throw_IllegalArgumentException(env, "mrl invalid");
throw_Exception(env, VLCJNI_EX_ILLEGAL_ARGUMENT, "mrl invalid");
return false;
}
......@@ -1083,7 +1087,7 @@ Java_org_videolan_libvlc_MediaPlayer_00024Equalizer_nativeGetPresetName(JNIEnv *
if (index < 0)
{
throw_IllegalArgumentException(env, "index invalid");
throw_Exception(env, VLCJNI_EX_ILLEGAL_ARGUMENT, "index invalid");
return NULL;
}
......@@ -1106,7 +1110,7 @@ Java_org_videolan_libvlc_MediaPlayer_00024Equalizer_nativeGetBandFrequency(JNIEn
{
if (index < 0)
{
throw_IllegalArgumentException(env, "index invalid");
throw_Exception(env, VLCJNI_EX_ILLEGAL_ARGUMENT, "index invalid");
return 0.0;
}
......@@ -1119,7 +1123,7 @@ Java_org_videolan_libvlc_MediaPlayer_00024Equalizer_nativeNew(JNIEnv *env,
{
libvlc_equalizer_t *p_eq = libvlc_audio_equalizer_new();
if (!p_eq)
throw_IllegalStateException(env, "can't create Equalizer instance");
throw_Exception(env, VLCJNI_EX_OUT_OF_MEMORY, "Equalizer");
VLCJniObject_setInstance(env, thiz, p_eq);
}
......@@ -1131,7 +1135,7 @@ Java_org_videolan_libvlc_MediaPlayer_00024Equalizer_nativeNewFromPreset(JNIEnv *
{
libvlc_equalizer_t *p_eq = libvlc_audio_equalizer_new_from_preset(index);
if (!p_eq)
throw_IllegalStateException(env, "can't create Equalizer instance");
throw_Exception(env, VLCJNI_EX_OUT_OF_MEMORY, "Equalizer");
VLCJniObject_setInstance(env, thiz, p_eq);
}
......
......@@ -54,7 +54,7 @@ Java_org_videolan_libvlc_RendererDiscoverer_nativeNew(JNIEnv *env,
if (!jname || !(p_name = (*env)->GetStringUTFChars(env, jname, 0)))
{
throw_IllegalArgumentException(env, "jname invalid");
throw_Exception(env, VLCJNI_EX_ILLEGAL_ARGUMENT, "jname invalid");
return;
}
......@@ -72,7 +72,7 @@ Java_org_videolan_libvlc_RendererDiscoverer_nativeNew(JNIEnv *env,
if (!p_obj->u.p_rd)
{
VLCJniObject_release(env, thiz, p_obj);
throw_IllegalStateException(env, "can't create RendererDiscoverer instance");
throw_Exception(env, VLCJNI_EX_OUT_OF_MEMORY, "RendererDiscoverer");
return;
}
VLCJniObject_attachEvents(p_obj, RendererDiscoverer_event_cb,
......
......@@ -51,7 +51,8 @@ VLCJniObject_getInstance(JNIEnv *env, jobject thiz)
{
vlcjni_object *p_obj = VLCJniObject_getInstanceInternal(env, thiz);
if (!p_obj)
throw_IllegalStateException(env, "can't get VLCObject instance");
throw_Exception(env, VLCJNI_EX_ILLEGAL_STATE,
"can't get VLCObject instance");
return p_obj;
}
......@@ -71,6 +72,7 @@ VLCJniObject_newFromLibVlc(JNIEnv *env, jobject thiz,
vlcjni_object *p_obj = NULL;
libvlc_event_manager_t *ev;
const char *p_error;
enum vlcjni_exception ex = VLCJNI_EX_OUT_OF_MEMORY;
if (VLCJniObject_getInstanceInternal(env, thiz))
{
......@@ -110,6 +112,7 @@ VLCJniObject_newFromLibVlc(JNIEnv *env, jobject thiz,
p_obj->p_owner->weak = (*env)->NewWeakGlobalRef(env, thiz);
if (!p_obj->p_owner->weak && !p_obj->p_owner->weakCompat)
{
ex = VLCJNI_EX_ILLEGAL_STATE;
p_error = "No VLCObject weak reference";
goto error;
}
......@@ -121,25 +124,25 @@ VLCJniObject_newFromLibVlc(JNIEnv *env, jobject thiz,
error:
if (p_obj)
VLCJniObject_release(env, thiz, p_obj);
throw_IllegalStateException(env, p_error);
throw_Exception(env, ex, p_error);
return NULL;
}
vlcjni_object *
VLCJniObject_newFromJavaLibVlc(JNIEnv *env, jobject thiz, jobject libVlc)
{
vlcjni_object *p_lib_obj = VLCJniObject_getInstance(env, libVlc);
vlcjni_object *p_lib_obj = VLCJniObject_getInstanceInternal(env, libVlc);
if (!p_lib_obj)
{
const char *p_error = libVlc ? "Can't get mInstance from libVlc"
: "libVlc is NULL";
throw_IllegalStateException(env, p_error);
throw_Exception(env, VLCJNI_EX_ILLEGAL_STATE, p_error);
return NULL;
}
if (p_lib_obj->p_libvlc || !p_lib_obj->u.p_libvlc)
{
/* The initial LibVLC object shouldn't have a parent libvlc */
throw_IllegalStateException(env, "Invalid LibVLC object");
throw_Exception(env, VLCJNI_EX_ILLEGAL_STATE, "Invalid LibVLC object");
return NULL;
}
return VLCJniObject_newFromLibVlc(env, thiz, p_lib_obj->u.p_libvlc);
......
......@@ -89,14 +89,31 @@ void VLCJniObject_attachEvents(vlcjni_object *p_obj, event_cb pf_event_cb,
libvlc_event_manager_t *p_event_manager,
const int *p_events);
static inline void throw_IllegalStateException(JNIEnv *env, const char *p_error)
enum vlcjni_exception
{
(*env)->ThrowNew(env, fields.IllegalStateException.clazz, p_error);
}
VLCJNI_EX_ILLEGAL_STATE,
VLCJNI_EX_ILLEGAL_ARGUMENT,
VLCJNI_EX_OUT_OF_MEMORY,
};
static inline void throw_IllegalArgumentException(JNIEnv *env, const char *p_error)
static inline void throw_Exception(JNIEnv *env, enum vlcjni_exception type,
const char *p_error)
{
(*env)->ThrowNew(env, fields.IllegalArgumentException.clazz, p_error);
jclass clazz;
switch (type)
{
case VLCJNI_EX_ILLEGAL_STATE:
clazz = fields.IllegalStateException.clazz;
break;
case VLCJNI_EX_OUT_OF_MEMORY:
clazz = fields.OutOfMemoryError.clazz;
break;
case VLCJNI_EX_ILLEGAL_ARGUMENT:
default:
clazz = fields.IllegalArgumentException.clazz;
break;
}
(*env)->ThrowNew(env, clazz, p_error);
}
#endif // LIBVLCJNI_VLCOBJECT_H
......@@ -145,6 +145,8 @@ int VLCJNI_OnLoad(JavaVM *vm, JNIEnv* env)
"java/lang/IllegalStateException", true);
GET_CLASS(fields.IllegalArgumentException.clazz,
"java/lang/IllegalArgumentException", true);
GET_CLASS(fields.OutOfMemoryError.clazz,
"java/lang/OutOfMemoryError", true);
GET_CLASS(fields.String.clazz,
"java/lang/String", true);
GET_CLASS(fields.FileDescriptor.clazz,
......@@ -345,6 +347,7 @@ void VLCJNI_OnUnload(JavaVM *vm, JNIEnv *env)
{
(*env)->DeleteGlobalRef(env, fields.IllegalStateException.clazz);
(*env)->DeleteGlobalRef(env, fields.IllegalArgumentException.clazz);
(*env)->DeleteGlobalRef(env, fields.OutOfMemoryError.clazz);
(*env)->DeleteGlobalRef(env, fields.String.clazz);
(*env)->DeleteGlobalRef(env, fields.VLCObject.clazz);
(*env)->DeleteGlobalRef(env, fields.Media.clazz);
......@@ -422,7 +425,8 @@ error:
if (!p_libvlc)
{
throw_IllegalStateException(env, "can't create LibVLC instance");
throw_Exception(env, VLCJNI_EX_ILLEGAL_STATE,
"can't create LibVLC instance");
return;
}
......@@ -480,5 +484,5 @@ void Java_org_videolan_libvlc_LibVLC_nativeSetUserAgent(JNIEnv* env,
(*env)->ReleaseStringUTFChars(env, jhttp, psz_http);
if (!psz_name || !psz_http)
throw_IllegalArgumentException(env, "name or http invalid");
throw_Exception(env, VLCJNI_EX_ILLEGAL_ARGUMENT, "name or http invalid");
}
......@@ -33,6 +33,9 @@ struct fields {
struct {
jclass clazz;
} IllegalArgumentException;
struct {
jclass clazz;
} OutOfMemoryError;
struct {
jclass clazz;
} String;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment