summaryrefslogtreecommitdiff
path: root/platform/java/mupdf_native.c
diff options
context:
space:
mode:
authorSebastian Rasmussen <sebras@gmail.com>2016-09-06 10:03:14 +0800
committerSebastian Rasmussen <sebras@gmail.com>2016-09-08 18:53:00 +0800
commit793ae6a3c2d23743e66039b124f826b232a5a04e (patch)
treeb5787042f5104f4d26abd78f9508ca5e951fd3b7 /platform/java/mupdf_native.c
parente6fbde08bf73d9cd72edf0393feeb061cf907ede (diff)
downloadmupdf-793ae6a3c2d23743e66039b124f826b232a5a04e.tar.xz
JNI: Separating big fz_try()s into smaller scopes.
By making the scope smaller fitz exceptions and Java exceptions can be disentangled. This makes it clearer what happens in failure cases.
Diffstat (limited to 'platform/java/mupdf_native.c')
-rw-r--r--platform/java/mupdf_native.c147
1 files changed, 74 insertions, 73 deletions
diff --git a/platform/java/mupdf_native.c b/platform/java/mupdf_native.c
index cae64439..87e57e57 100644
--- a/platform/java/mupdf_native.c
+++ b/platform/java/mupdf_native.c
@@ -4027,44 +4027,47 @@ FUN(Page_getAnnotations)(JNIEnv *env, jobject self)
if (!ctx) return NULL;
+ /* count the annotations */
fz_try(ctx)
{
annots = fz_first_annot(ctx, page);
- /* Count the annotations */
annot = annots;
for (annot_count = 0; annot; annot_count++)
annot = fz_next_annot(ctx, annot);
+ }
+ fz_catch(ctx)
+ {
+ jni_rethrow(env, ctx);
+ return NULL;
+ }
+
+ /* no annotations, return NULL instead of empty array */
+ if (annot_count == 0)
+ return NULL;
- if (annot_count == 0)
- break; /* No annotations! */
+ /* now run through actually creating the annotation objects */
+ jannots = (*env)->NewObjectArray(env, annot_count, cls_Annot, NULL);
+ if (!jannots) return NULL;
- jannots = (*env)->NewObjectArray(env, annot_count, cls_Annot, NULL);
- if (!jannots)
- fz_throw(ctx, FZ_ERROR_GENERIC, "getAnnotations failed (1)");
+ annot = annots;
+ for (i = 0; annot && i < annot_count; i++)
+ {
+ jobject jannot = to_Annotation(ctx, env, annot);
+ if (!jannot) return NULL;
- /* Now run through actually creating the annotation objects */
- annot = annots;
- for (i = 0; annot && i < annot_count; i++)
- {
- jobject jannot = to_Annotation(ctx, env, annot);
- if (!jannot)
- fz_throw(ctx, FZ_ERROR_GENERIC, "getAnnotations failed (2)");
+ (*env)->SetObjectArrayElement(env, jannots, i, jannot);
+ if ((*env)->ExceptionCheck(env)) return NULL;
- (*env)->SetObjectArrayElement(env, jannots, i, jannot);
- if ((*env)->ExceptionCheck(env))
- fz_throw(ctx, FZ_ERROR_GENERIC, "getAnnotations failed (3)");
+ (*env)->DeleteLocalRef(env, jannot);
- (*env)->DeleteLocalRef(env, jannot);
+ fz_try(ctx)
annot = fz_next_annot(ctx, annot);
+ fz_catch(ctx)
+ {
+ jni_rethrow(env, ctx);
+ return NULL;
}
- if (annot || i != annot_count)
- fz_throw(ctx, FZ_ERROR_GENERIC, "getAnnotations failed (4)");
- }
- fz_catch(ctx)
- {
- jni_rethrow(env, ctx);
- return NULL;
}
return jannots;
@@ -4086,66 +4089,64 @@ FUN(Page_getLinks)(JNIEnv *env, jobject self)
fz_var(links);
fz_try(ctx)
- {
links = fz_load_links(ctx, page);
+ fz_catch(ctx)
+ {
+ fz_drop_link(ctx, links);
+ jni_rethrow(env, ctx);
+ return NULL;
+ }
- /* Count the links */
- link = links;
- for (link_count = 0; link; link_count++)
- link = link->next;
+ /* count the links */
+ link = links;
+ for (link_count = 0; link; link_count++)
+ link = link->next;
- if (link_count == 0)
- break; /* No links! */
+ /* no links, return NULL instead of empty array */
+ if (link_count == 0)
+ {
+ fz_drop_link(ctx, links);
+ return NULL;
+ }
- jlinks = (*env)->NewObjectArray(env, link_count, cls_Link, NULL);
- if (!jlinks)
- fz_throw(ctx, FZ_ERROR_GENERIC, "getLinks failed (1)");
+ /* now run through actually creating the link objects */
+ jlinks = (*env)->NewObjectArray(env, link_count, cls_Link, NULL);
+ if (!jlinks) return NULL;
- /* Now run through actually creating the link objects */
- link = links;
- for (i = 0; link && i < link_count; i++)
+ link = links;
+ for (i = 0; link && i < link_count; i++)
+ {
+ jobject jbounds = NULL;
+ jobject jlink = NULL;
+ jobject juri = NULL;
+ int page = 0;
+
+ jbounds = to_Rect_safe(ctx, env, &link->rect);
+ if (!jbounds) return NULL;
+
+ if (link->dest.kind == FZ_LINK_GOTO)
+ page = link->dest.ld.gotor.page;
+ else if (link->dest.kind == FZ_LINK_URI)
{
- jobject jbounds = NULL;
- jobject jlink = NULL;
- jobject juri = NULL;
- int page = 0;
-
- jbounds = to_Rect_safe(ctx, env, &link->rect);
- if (!jbounds)
- break;
- if (link->dest.kind == FZ_LINK_GOTO)
- page = link->dest.ld.gotor.page;
- else if (link->dest.kind == FZ_LINK_URI)
- {
- juri = (*env)->NewStringUTF(env, link->dest.ld.uri.uri);
- if (!juri) break;
- }
+ juri = (*env)->NewStringUTF(env, link->dest.ld.uri.uri);
+ if (!juri) return NULL;
+ }
- jlink = (*env)->NewObject(env, cls_Link, mid_Link_init, jbounds, page, juri);
- (*env)->DeleteLocalRef(env, jbounds);
- if (juri)
- (*env)->DeleteLocalRef(env, juri);
- if (!jlink)
- break;
+ jlink = (*env)->NewObject(env, cls_Link, mid_Link_init, jbounds, page, juri);
+ (*env)->DeleteLocalRef(env, jbounds);
+ if (!jlink) return NULL;
+ if (juri)
+ (*env)->DeleteLocalRef(env, juri);
- (*env)->SetObjectArrayElement(env, jlinks, i, jlink);
- if ((*env)->ExceptionCheck(env))
- break;
+ (*env)->SetObjectArrayElement(env, jlinks, i, jlink);
+ if ((*env)->ExceptionCheck(env)) return NULL;
- (*env)->DeleteLocalRef(env, jlink);
- link = link->next;
- }
- if (link || i != link_count)
- fz_throw(ctx, FZ_ERROR_GENERIC, "getLinks failed (2)");
- }
- fz_always(ctx)
- fz_drop_link(ctx, links);
- fz_catch(ctx)
- {
- jni_rethrow(env, ctx);
- return NULL;
+ (*env)->DeleteLocalRef(env, jlink);
+ link = link->next;
}
+ fz_drop_link(ctx, links);
+
return jlinks;
}