2 * Copyright (C) 2009-2011 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 (* Please read generator/README first. *)
26 open Generator_docstrings
27 open Generator_optgroups
28 open Generator_actions
29 open Generator_structs
32 (* Generate Java bindings GuestFS.java file. *)
33 let rec generate_java_java () =
34 generate_header CStyle LGPLv2plus;
37 package com.redhat.et.libguestfs;
39 import java.util.HashMap;
41 import com.redhat.et.libguestfs.LibGuestFSException;
42 import com.redhat.et.libguestfs.PV;
43 import com.redhat.et.libguestfs.VG;
44 import com.redhat.et.libguestfs.LV;
45 import com.redhat.et.libguestfs.Stat;
46 import com.redhat.et.libguestfs.StatVFS;
47 import com.redhat.et.libguestfs.IntBool;
48 import com.redhat.et.libguestfs.Dirent;
51 * The GuestFS object is a libguestfs handle.
55 public class GuestFS {
56 // Load the native code.
58 System.loadLibrary (\"guestfs_jni\");
62 * The native guestfs_h pointer.
67 * Create a libguestfs handle.
69 * @throws LibGuestFSException
71 public GuestFS () throws LibGuestFSException
75 private native long _create () throws LibGuestFSException;
78 * Close a libguestfs handle.
80 * You can also leave handles to be collected by the garbage
81 * collector, but this method ensures that the resources used
82 * by the handle are freed up immediately. If you call any
83 * other methods after closing the handle, you will get an
86 * @throws LibGuestFSException
88 public void close () throws LibGuestFSException
94 private native void _close (long g) throws LibGuestFSException;
96 public void finalize () throws LibGuestFSException
104 fun (name, (ret, args, optargs as style), _, flags, _, shortdesc, longdesc) ->
105 if not (List.mem NotInDocs flags); then (
106 let doc = replace_str longdesc "C<guestfs_" "C<g." in
108 if optargs <> [] then
109 doc ^ "\n\nOptional arguments are supplied in the final Map<String,Object> parameter, which is a hash of the argument name to its value (cast to Object). Pass an empty Map or null for no optional arguments."
112 if List.mem ProtocolLimitWarning flags then
113 doc ^ "\n\n" ^ protocol_limit_warning
116 match deprecation_notice flags with
118 | Some txt -> doc ^ "\n\n" ^ txt in
119 let doc = pod2text ~width:60 name doc in
120 let doc = List.map ( (* RHBZ#501883 *)
123 | nonempty -> nonempty
125 let doc = String.concat "\n * " doc in
128 pr " * %s\n" shortdesc;
131 pr " * @throws LibGuestFSException\n";
135 generate_java_prototype ~public:true ~semicolon:false name style;
139 pr " throw new LibGuestFSException (\"%s: handle is closed\");\n"
141 if optargs <> [] then (
143 pr " /* Unpack optional args. */\n";
144 pr " Object _optobj;\n";
145 pr " long _optargs_bitmask = 0;\n";
148 let t, boxed_t, convert, n, default =
150 | Bool n -> "boolean", "Boolean", ".booleanValue()", n, "false"
151 | Int n -> "int", "Integer", ".intValue()", n, "0"
152 | Int64 n -> "long", "Long", ".longValue()", n, "0"
153 | String n -> "String", "String", "", n, "\"\""
154 | _ -> assert false in
155 pr " %s %s = %s;\n" t n default;
156 pr " _optobj = null;\n";
157 pr " if (optargs != null)\n";
158 pr " _optobj = optargs.get (\"%s\");\n" n;
159 pr " if (_optobj != null) {\n";
160 pr " %s = ((%s) _optobj)%s;\n" n boxed_t convert;
161 pr " _optargs_bitmask |= %Ld;\n"
162 (Int64.shift_left Int64.one i);
170 generate_java_call_args ~handle:"g" style;
173 pr " String[] r = _%s " name;
174 generate_java_call_args ~handle:"g" style;
177 pr " HashMap rhash = new HashMap ();\n";
178 pr " for (int i = 0; i < r.length; i += 2)\n";
179 pr " rhash.put (r[i], r[i+1]);\n";
180 pr " return rhash;\n"
182 pr " return _%s " name;
183 generate_java_call_args ~handle:"g" style;
189 generate_java_prototype ~privat:true ~native:true name style;
196 (* Generate Java call arguments, eg "(handle, foo, bar)" *)
197 and generate_java_call_args ~handle (_, args, optargs) =
199 List.iter (fun arg -> pr ", %s" (name_of_argt arg)) args;
200 if optargs <> [] then (
201 pr ", _optargs_bitmask";
202 List.iter (fun arg -> pr ", %s" (name_of_argt arg)) optargs
206 and generate_java_prototype ?(public=false) ?(privat=false) ?(native=false)
207 ?(semicolon=true) name (ret, args, optargs) =
208 if privat then pr "private ";
209 if public then pr "public ";
210 if native then pr "native ";
214 | RErr -> pr "void ";
215 | RInt _ -> pr "int ";
216 | RInt64 _ -> pr "long ";
217 | RBool _ -> pr "boolean ";
218 | RConstString _ | RConstOptString _ | RString _
219 | RBufferOut _ -> pr "String ";
220 | RStringList _ -> pr "String[] ";
221 | RStruct (_, typ) ->
222 let name = java_name_of_struct typ in
224 | RStructList (_, typ) ->
225 let name = java_name_of_struct typ in
229 pr "Map<String,String> "
234 if native then pr "_%s " name else pr "%s " name;
236 let needs_comma = ref false in
245 if !needs_comma then pr ", ";
250 | Device n | Dev_or_Path n
259 | StringList n | DeviceList n ->
265 | Int64 n | Pointer (_, n) ->
269 if optargs <> [] then (
270 if !needs_comma then pr ", ";
274 pr "Map<String, Object> optargs"
276 pr "long _optargs_bitmask";
280 | Bool n -> pr ", boolean %s" n
281 | Int n -> pr ", int %s" n
282 | Int64 n -> pr ", long %s" n
283 | String n -> pr ", String %s" n
290 pr " throws LibGuestFSException";
291 if semicolon then pr ";"
293 and generate_java_struct jtyp cols () =
294 generate_header CStyle LGPLv2plus;
297 package com.redhat.et.libguestfs;
300 * Libguestfs %s structure.
312 | name, FBuffer -> pr " public String %s;\n" name
313 | name, (FBytes|FUInt64|FInt64) -> pr " public long %s;\n" name
314 | name, (FUInt32|FInt32) -> pr " public int %s;\n" name
315 | name, FChar -> pr " public char %s;\n" name
316 | name, FOptPercent ->
317 pr " /* The next field is [0..100] or -1 meaning 'not present': */\n";
318 pr " public float %s;\n" name
323 and generate_java_c () =
324 generate_header CStyle LGPLv2plus;
331 #include \"com_redhat_et_libguestfs_GuestFS.h\"
332 #include \"guestfs.h\"
334 /* Note that this function returns. The exception is not thrown
335 * until after the wrapper function returns.
338 throw_exception (JNIEnv *env, const char *msg)
341 cl = (*env)->FindClass (env,
342 \"com/redhat/et/libguestfs/LibGuestFSException\");
343 (*env)->ThrowNew (env, cl, msg);
346 JNIEXPORT jlong JNICALL
347 Java_com_redhat_et_libguestfs_GuestFS__1create
348 (JNIEnv *env, jobject obj)
352 g = guestfs_create ();
354 throw_exception (env, \"GuestFS.create: failed to allocate handle\");
357 guestfs_set_error_handler (g, NULL, NULL);
358 return (jlong) (long) g;
361 JNIEXPORT void JNICALL
362 Java_com_redhat_et_libguestfs_GuestFS__1close
363 (JNIEnv *env, jobject obj, jlong jg)
365 guestfs_h *g = (guestfs_h *) (long) jg;
372 fun (name, (ret, args, optargs as style), _, _, _, _, _) ->
375 | RErr -> pr "void ";
376 | RInt _ -> pr "jint ";
377 | RInt64 _ -> pr "jlong ";
378 | RBool _ -> pr "jboolean ";
379 | RConstString _ | RConstOptString _ | RString _
380 | RBufferOut _ -> pr "jstring ";
381 | RStruct _ | RHashtable _ ->
383 | RStringList _ | RStructList _ ->
387 pr "Java_com_redhat_et_libguestfs_GuestFS_";
388 pr "%s" (replace_str ("_" ^ name) "_" "_1");
389 pr " (JNIEnv *env, jobject obj, jlong jg";
393 | Device n | Dev_or_Path n
401 pr ", jbyteArray j%s" n
402 | StringList n | DeviceList n ->
403 pr ", jobjectArray j%s" n
405 pr ", jboolean j%s" n
408 | Int64 n | Pointer (_, n) ->
411 if optargs <> [] then (
412 pr ", jlong joptargs_bitmask";
415 | Bool n -> pr ", jboolean j%s" n
416 | Int n -> pr ", jint j%s" n
417 | Int64 n -> pr ", jlong j%s" n
418 | String n -> pr ", jstring j%s" n
424 pr " guestfs_h *g = (guestfs_h *) (long) jg;\n";
426 | RErr -> pr " int r;\n"
428 | RInt _ -> pr " int r;\n"
429 | RInt64 _ -> pr " int64_t r;\n"
430 | RConstString _ -> pr " const char *r;\n"
431 | RConstOptString _ -> pr " const char *r;\n"
437 pr " jobjectArray jr;\n";
438 pr " size_t r_len;\n";
440 pr " jstring jstr;\n";
442 | RStruct (_, typ) ->
445 pr " jfieldID fl;\n";
446 pr " struct guestfs_%s *r;\n" typ
447 | RStructList (_, typ) ->
448 pr " jobjectArray jr;\n";
450 pr " jfieldID fl;\n";
451 pr " jobject jfl;\n";
452 pr " struct guestfs_%s_list *r;\n" typ
462 | Device n | Dev_or_Path n
468 pr " const char *%s;\n" n
471 pr " size_t %s_size;\n" n
472 | StringList n | DeviceList n ->
473 pr " size_t %s_len;\n" n;
479 pr " int64_t %s;\n" n
484 if optargs <> [] then (
485 pr " struct guestfs_%s_argv optargs_s;\n" name;
486 pr " const struct guestfs_%s_argv *optargs = &optargs_s;\n" name
491 | RStringList _ | RStructList _ | RHashtable _ -> true
492 | RErr | RBool _ | RInt _ | RInt64 _ | RConstString _
494 | RString _ | RBufferOut _ | RStruct _ -> false) ||
495 List.exists (function
496 | StringList _ -> true
497 | DeviceList _ -> true
498 | _ -> false) args in
504 (* Get the parameters. *)
508 | Device n | Dev_or_Path n
513 pr " %s = (*env)->GetStringUTFChars (env, j%s, NULL);\n" n n
515 (* This is completely undocumented, but Java null becomes
518 pr " %s = j%s ? (*env)->GetStringUTFChars (env, j%s, NULL) : NULL;\n" n n n
520 pr " %s = (char *) (*env)->GetByteArrayElements (env, j%s, NULL);\n" n n;
521 pr " %s_size = (*env)->GetArrayLength (env, j%s);\n" n n
522 | StringList n | DeviceList n ->
523 pr " %s_len = (*env)->GetArrayLength (env, j%s);\n" n n;
524 pr " %s = guestfs_safe_malloc (g, sizeof (char *) * (%s_len+1));\n" n n;
525 pr " for (i = 0; i < %s_len; ++i) {\n" n;
526 pr " jobject o = (*env)->GetObjectArrayElement (env, j%s, i);\n"
528 pr " %s[i] = (char *) (*env)->GetStringUTFChars (env, o, NULL);\n" n;
530 pr " %s[%s_len] = NULL;\n" n n;
534 pr " %s = j%s;\n" n n
536 pr " %s = (%s) j%s;\n" n t n
539 if optargs <> [] then (
540 pr " optargs_s.bitmask = joptargs_bitmask;\n";
546 pr " optargs_s.%s = j%s;\n" n n
548 pr " optargs_s.%s = (*env)->GetStringUTFChars (env, j%s, NULL);\n"
558 pr " r = guestfs_%s " name
560 pr " r = guestfs_%s_argv " name;
561 generate_c_call_args ~handle:"g" style;
566 (* Release the parameters. *)
570 | Device n | Dev_or_Path n
575 pr " (*env)->ReleaseStringUTFChars (env, j%s, %s);\n" n n
578 pr " (*env)->ReleaseStringUTFChars (env, j%s, %s);\n" n n
580 pr " (*env)->ReleaseByteArrayElements (env, j%s, (jbyte *) %s, 0);\n" n n
581 | StringList n | DeviceList n ->
582 pr " for (i = 0; i < %s_len; ++i) {\n" n;
583 pr " jobject o = (*env)->GetObjectArrayElement (env, j%s, i);\n"
585 pr " (*env)->ReleaseStringUTFChars (env, o, %s[i]);\n" n;
600 pr " (*env)->ReleaseStringUTFChars (env, j%s, optargs_s.%s);\n" n n
606 (* Check for errors. *)
607 (match errcode_of_ret ret with
608 | `CannotReturnError -> ()
609 | (`ErrorIsMinusOne|`ErrorIsNULL) as errcode ->
611 | `ErrorIsMinusOne ->
612 pr " if (r == -1) {\n";
614 pr " if (r == NULL) {\n";
616 pr " throw_exception (env, guestfs_last_error (g));\n";
624 | RConstString _ | RConstOptString _ | RString _
626 | RStruct _ | RHashtable _
627 | RStringList _ | RStructList _ ->
636 | RInt _ -> pr " return (jint) r;\n"
637 | RBool _ -> pr " return (jboolean) r;\n"
638 | RInt64 _ -> pr " return (jlong) r;\n"
639 | RConstString _ -> pr " return (*env)->NewStringUTF (env, r);\n"
640 | RConstOptString _ ->
641 pr " return (*env)->NewStringUTF (env, r); /* XXX r NULL? */\n"
643 pr " jr = (*env)->NewStringUTF (env, r);\n";
648 pr " for (r_len = 0; r[r_len] != NULL; ++r_len) ;\n";
649 pr " cl = (*env)->FindClass (env, \"java/lang/String\");\n";
650 pr " jstr = (*env)->NewStringUTF (env, \"\");\n";
651 pr " jr = (*env)->NewObjectArray (env, r_len, cl, jstr);\n";
652 pr " for (i = 0; i < r_len; ++i) {\n";
653 pr " jstr = (*env)->NewStringUTF (env, r[i]);\n";
654 pr " (*env)->SetObjectArrayElement (env, jr, i, jstr);\n";
655 pr " free (r[i]);\n";
659 | RStruct (_, typ) ->
660 let jtyp = java_name_of_struct typ in
661 let cols = cols_of_struct typ in
662 generate_java_struct_return typ jtyp cols
663 | RStructList (_, typ) ->
664 let jtyp = java_name_of_struct typ in
665 let cols = cols_of_struct typ in
666 generate_java_struct_list_return typ jtyp cols
668 pr " jr = (*env)->NewStringUTF (env, r); // XXX size\n";
677 and generate_java_struct_return typ jtyp cols =
678 pr " cl = (*env)->FindClass (env, \"com/redhat/et/libguestfs/%s\");\n" jtyp;
679 pr " jr = (*env)->AllocObject (env, cl);\n";
683 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
684 pr " (*env)->SetObjectField (env, jr, fl, (*env)->NewStringUTF (env, r->%s));\n" name;
688 pr " memcpy (s, r->%s, 32);\n" name;
690 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
691 pr " (*env)->SetObjectField (env, jr, fl, (*env)->NewStringUTF (env, s));\n";
695 pr " int len = r->%s_len;\n" name;
696 pr " char s[len+1];\n";
697 pr " memcpy (s, r->%s, len);\n" name;
699 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
700 pr " (*env)->SetObjectField (env, jr, fl, (*env)->NewStringUTF (env, s));\n";
702 | name, (FBytes|FUInt64|FInt64) ->
703 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"J\");\n" name;
704 pr " (*env)->SetLongField (env, jr, fl, r->%s);\n" name;
705 | name, (FUInt32|FInt32) ->
706 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"I\");\n" name;
707 pr " (*env)->SetLongField (env, jr, fl, r->%s);\n" name;
708 | name, FOptPercent ->
709 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"F\");\n" name;
710 pr " (*env)->SetFloatField (env, jr, fl, r->%s);\n" name;
712 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"C\");\n" name;
713 pr " (*env)->SetLongField (env, jr, fl, r->%s);\n" name;
718 and generate_java_struct_list_return typ jtyp cols =
719 pr " cl = (*env)->FindClass (env, \"com/redhat/et/libguestfs/%s\");\n" jtyp;
720 pr " jr = (*env)->NewObjectArray (env, r->len, cl, NULL);\n";
721 pr " for (i = 0; i < r->len; ++i) {\n";
722 pr " jfl = (*env)->AllocObject (env, cl);\n";
726 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
727 pr " (*env)->SetObjectField (env, jfl, fl, (*env)->NewStringUTF (env, r->val[i].%s));\n" name;
731 pr " memcpy (s, r->val[i].%s, 32);\n" name;
733 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
734 pr " (*env)->SetObjectField (env, jfl, fl, (*env)->NewStringUTF (env, s));\n";
738 pr " int len = r->val[i].%s_len;\n" name;
739 pr " char s[len+1];\n";
740 pr " memcpy (s, r->val[i].%s, len);\n" name;
742 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
743 pr " (*env)->SetObjectField (env, jfl, fl, (*env)->NewStringUTF (env, s));\n";
745 | name, (FBytes|FUInt64|FInt64) ->
746 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"J\");\n" name;
747 pr " (*env)->SetLongField (env, jfl, fl, r->val[i].%s);\n" name;
748 | name, (FUInt32|FInt32) ->
749 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"I\");\n" name;
750 pr " (*env)->SetLongField (env, jfl, fl, r->val[i].%s);\n" name;
751 | name, FOptPercent ->
752 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"F\");\n" name;
753 pr " (*env)->SetFloatField (env, jfl, fl, r->val[i].%s);\n" name;
755 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"C\");\n" name;
756 pr " (*env)->SetLongField (env, jfl, fl, r->val[i].%s);\n" name;
758 pr " (*env)->SetObjectArrayElement (env, jfl, i, jfl);\n";
760 pr " guestfs_free_%s_list (r);\n" typ;
763 and generate_java_makefile_inc () =
764 generate_header HashStyle GPLv2plus;
766 pr "java_built_sources = \\\n";
769 pr "\tcom/redhat/et/libguestfs/%s.java \\\n" jtyp;
771 pr "\tcom/redhat/et/libguestfs/GuestFS.java\n"