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 if List.mem DangerWillRobinson flags then
117 doc ^ "\n\n" ^ danger_will_robinson
120 match deprecation_notice flags with
122 | Some txt -> doc ^ "\n\n" ^ txt in
123 let doc = pod2text ~width:60 name doc in
124 let doc = List.map ( (* RHBZ#501883 *)
127 | nonempty -> nonempty
129 let doc = String.concat "\n * " doc in
132 pr " * %s\n" shortdesc;
135 pr " * @throws LibGuestFSException\n";
139 generate_java_prototype ~public:true ~semicolon:false name style;
143 pr " throw new LibGuestFSException (\"%s: handle is closed\");\n"
145 if optargs <> [] then (
147 pr " /* Unpack optional args. */\n";
148 pr " Object _optobj;\n";
149 pr " long _optargs_bitmask = 0;\n";
152 let t, boxed_t, convert, n, default =
154 | Bool n -> "boolean", "Boolean", ".booleanValue()", n, "false"
155 | Int n -> "int", "Integer", ".intValue()", n, "0"
156 | Int64 n -> "long", "Long", ".longValue()", n, "0"
157 | String n -> "String", "String", "", n, "\"\""
158 | _ -> assert false in
159 pr " %s %s = %s;\n" t n default;
160 pr " _optobj = null;\n";
161 pr " if (optargs != null)\n";
162 pr " _optobj = optargs.get (\"%s\");\n" n;
163 pr " if (_optobj != null) {\n";
164 pr " %s = ((%s) _optobj)%s;\n" n boxed_t convert;
165 pr " _optargs_bitmask |= %Ld;\n"
166 (Int64.shift_left Int64.one i);
174 generate_java_call_args ~handle:"g" style;
177 pr " String[] r = _%s " name;
178 generate_java_call_args ~handle:"g" style;
181 pr " HashMap rhash = new HashMap ();\n";
182 pr " for (int i = 0; i < r.length; i += 2)\n";
183 pr " rhash.put (r[i], r[i+1]);\n";
184 pr " return rhash;\n"
186 pr " return _%s " name;
187 generate_java_call_args ~handle:"g" style;
193 generate_java_prototype ~privat:true ~native:true name style;
200 (* Generate Java call arguments, eg "(handle, foo, bar)" *)
201 and generate_java_call_args ~handle (_, args, optargs) =
203 List.iter (fun arg -> pr ", %s" (name_of_argt arg)) args;
204 if optargs <> [] then (
205 pr ", _optargs_bitmask";
206 List.iter (fun arg -> pr ", %s" (name_of_argt arg)) optargs
210 and generate_java_prototype ?(public=false) ?(privat=false) ?(native=false)
211 ?(semicolon=true) name (ret, args, optargs) =
212 if privat then pr "private ";
213 if public then pr "public ";
214 if native then pr "native ";
218 | RErr -> pr "void ";
219 | RInt _ -> pr "int ";
220 | RInt64 _ -> pr "long ";
221 | RBool _ -> pr "boolean ";
222 | RConstString _ | RConstOptString _ | RString _
223 | RBufferOut _ -> pr "String ";
224 | RStringList _ -> pr "String[] ";
225 | RStruct (_, typ) ->
226 let name = java_name_of_struct typ in
228 | RStructList (_, typ) ->
229 let name = java_name_of_struct typ in
233 pr "Map<String,String> "
238 if native then pr "_%s " name else pr "%s " name;
240 let needs_comma = ref false in
249 if !needs_comma then pr ", ";
254 | Device n | Dev_or_Path n
263 | StringList n | DeviceList n ->
269 | Int64 n | Pointer (_, n) ->
273 if optargs <> [] then (
274 if !needs_comma then pr ", ";
278 pr "Map<String, Object> optargs"
280 pr "long _optargs_bitmask";
284 | Bool n -> pr ", boolean %s" n
285 | Int n -> pr ", int %s" n
286 | Int64 n -> pr ", long %s" n
287 | String n -> pr ", String %s" n
294 pr " throws LibGuestFSException";
295 if semicolon then pr ";"
297 and generate_java_struct jtyp cols () =
298 generate_header CStyle LGPLv2plus;
301 package com.redhat.et.libguestfs;
304 * Libguestfs %s structure.
316 | name, FBuffer -> pr " public String %s;\n" name
317 | name, (FBytes|FUInt64|FInt64) -> pr " public long %s;\n" name
318 | name, (FUInt32|FInt32) -> pr " public int %s;\n" name
319 | name, FChar -> pr " public char %s;\n" name
320 | name, FOptPercent ->
321 pr " /* The next field is [0..100] or -1 meaning 'not present': */\n";
322 pr " public float %s;\n" name
327 and generate_java_c () =
328 generate_header CStyle LGPLv2plus;
335 #include \"com_redhat_et_libguestfs_GuestFS.h\"
336 #include \"guestfs.h\"
338 /* Note that this function returns. The exception is not thrown
339 * until after the wrapper function returns.
342 throw_exception (JNIEnv *env, const char *msg)
345 cl = (*env)->FindClass (env,
346 \"com/redhat/et/libguestfs/LibGuestFSException\");
347 (*env)->ThrowNew (env, cl, msg);
350 JNIEXPORT jlong JNICALL
351 Java_com_redhat_et_libguestfs_GuestFS__1create
352 (JNIEnv *env, jobject obj)
356 g = guestfs_create ();
358 throw_exception (env, \"GuestFS.create: failed to allocate handle\");
361 guestfs_set_error_handler (g, NULL, NULL);
362 return (jlong) (long) g;
365 JNIEXPORT void JNICALL
366 Java_com_redhat_et_libguestfs_GuestFS__1close
367 (JNIEnv *env, jobject obj, jlong jg)
369 guestfs_h *g = (guestfs_h *) (long) jg;
376 fun (name, (ret, args, optargs as style), _, _, _, _, _) ->
379 | RErr -> pr "void ";
380 | RInt _ -> pr "jint ";
381 | RInt64 _ -> pr "jlong ";
382 | RBool _ -> pr "jboolean ";
383 | RConstString _ | RConstOptString _ | RString _
384 | RBufferOut _ -> pr "jstring ";
385 | RStruct _ | RHashtable _ ->
387 | RStringList _ | RStructList _ ->
391 pr "Java_com_redhat_et_libguestfs_GuestFS_";
392 pr "%s" (replace_str ("_" ^ name) "_" "_1");
393 pr " (JNIEnv *env, jobject obj, jlong jg";
397 | Device n | Dev_or_Path n
405 pr ", jbyteArray j%s" n
406 | StringList n | DeviceList n ->
407 pr ", jobjectArray j%s" n
409 pr ", jboolean j%s" n
412 | Int64 n | Pointer (_, n) ->
415 if optargs <> [] then (
416 pr ", jlong joptargs_bitmask";
419 | Bool n -> pr ", jboolean j%s" n
420 | Int n -> pr ", jint j%s" n
421 | Int64 n -> pr ", jlong j%s" n
422 | String n -> pr ", jstring j%s" n
428 pr " guestfs_h *g = (guestfs_h *) (long) jg;\n";
430 | RErr -> pr " int r;\n"
432 | RInt _ -> pr " int r;\n"
433 | RInt64 _ -> pr " int64_t r;\n"
434 | RConstString _ -> pr " const char *r;\n"
435 | RConstOptString _ -> pr " const char *r;\n"
441 pr " jobjectArray jr;\n";
442 pr " size_t r_len;\n";
444 pr " jstring jstr;\n";
446 | RStruct (_, typ) ->
449 pr " jfieldID fl;\n";
450 pr " struct guestfs_%s *r;\n" typ
451 | RStructList (_, typ) ->
452 pr " jobjectArray jr;\n";
454 pr " jfieldID fl;\n";
455 pr " jobject jfl;\n";
456 pr " struct guestfs_%s_list *r;\n" typ
466 | Device n | Dev_or_Path n
472 pr " const char *%s;\n" n
475 pr " size_t %s_size;\n" n
476 | StringList n | DeviceList n ->
477 pr " size_t %s_len;\n" n;
483 pr " int64_t %s;\n" n
488 if optargs <> [] then (
489 pr " struct guestfs_%s_argv optargs_s;\n" name;
490 pr " const struct guestfs_%s_argv *optargs = &optargs_s;\n" name
495 | RStringList _ | RStructList _ | RHashtable _ -> true
496 | RErr | RBool _ | RInt _ | RInt64 _ | RConstString _
498 | RString _ | RBufferOut _ | RStruct _ -> false) ||
499 List.exists (function
500 | StringList _ -> true
501 | DeviceList _ -> true
502 | _ -> false) args in
508 (* Get the parameters. *)
512 | Device n | Dev_or_Path n
517 pr " %s = (*env)->GetStringUTFChars (env, j%s, NULL);\n" n n
519 (* This is completely undocumented, but Java null becomes
522 pr " %s = j%s ? (*env)->GetStringUTFChars (env, j%s, NULL) : NULL;\n" n n n
524 pr " %s = (char *) (*env)->GetByteArrayElements (env, j%s, NULL);\n" n n;
525 pr " %s_size = (*env)->GetArrayLength (env, j%s);\n" n n
526 | StringList n | DeviceList n ->
527 pr " %s_len = (*env)->GetArrayLength (env, j%s);\n" n n;
528 pr " %s = guestfs_safe_malloc (g, sizeof (char *) * (%s_len+1));\n" n n;
529 pr " for (i = 0; i < %s_len; ++i) {\n" n;
530 pr " jobject o = (*env)->GetObjectArrayElement (env, j%s, i);\n"
532 pr " %s[i] = (char *) (*env)->GetStringUTFChars (env, o, NULL);\n" n;
534 pr " %s[%s_len] = NULL;\n" n n;
538 pr " %s = j%s;\n" n n
540 pr " %s = (%s) j%s;\n" n t n
543 if optargs <> [] then (
544 pr " optargs_s.bitmask = joptargs_bitmask;\n";
550 pr " optargs_s.%s = j%s;\n" n n
552 pr " optargs_s.%s = (*env)->GetStringUTFChars (env, j%s, NULL);\n"
562 pr " r = guestfs_%s " name
564 pr " r = guestfs_%s_argv " name;
565 generate_c_call_args ~handle:"g" style;
570 (* Release the parameters. *)
574 | Device n | Dev_or_Path n
579 pr " (*env)->ReleaseStringUTFChars (env, j%s, %s);\n" n n
582 pr " (*env)->ReleaseStringUTFChars (env, j%s, %s);\n" n n
584 pr " (*env)->ReleaseByteArrayElements (env, j%s, (jbyte *) %s, 0);\n" n n
585 | StringList n | DeviceList n ->
586 pr " for (i = 0; i < %s_len; ++i) {\n" n;
587 pr " jobject o = (*env)->GetObjectArrayElement (env, j%s, i);\n"
589 pr " (*env)->ReleaseStringUTFChars (env, o, %s[i]);\n" n;
604 pr " (*env)->ReleaseStringUTFChars (env, j%s, optargs_s.%s);\n" n n
610 (* Check for errors. *)
611 (match errcode_of_ret ret with
612 | `CannotReturnError -> ()
613 | (`ErrorIsMinusOne|`ErrorIsNULL) as errcode ->
615 | `ErrorIsMinusOne ->
616 pr " if (r == -1) {\n";
618 pr " if (r == NULL) {\n";
620 pr " throw_exception (env, guestfs_last_error (g));\n";
628 | RConstString _ | RConstOptString _ | RString _
630 | RStruct _ | RHashtable _
631 | RStringList _ | RStructList _ ->
640 | RInt _ -> pr " return (jint) r;\n"
641 | RBool _ -> pr " return (jboolean) r;\n"
642 | RInt64 _ -> pr " return (jlong) r;\n"
643 | RConstString _ -> pr " return (*env)->NewStringUTF (env, r);\n"
644 | RConstOptString _ ->
645 pr " return (*env)->NewStringUTF (env, r); /* XXX r NULL? */\n"
647 pr " jr = (*env)->NewStringUTF (env, r);\n";
652 pr " for (r_len = 0; r[r_len] != NULL; ++r_len) ;\n";
653 pr " cl = (*env)->FindClass (env, \"java/lang/String\");\n";
654 pr " jstr = (*env)->NewStringUTF (env, \"\");\n";
655 pr " jr = (*env)->NewObjectArray (env, r_len, cl, jstr);\n";
656 pr " for (i = 0; i < r_len; ++i) {\n";
657 pr " jstr = (*env)->NewStringUTF (env, r[i]);\n";
658 pr " (*env)->SetObjectArrayElement (env, jr, i, jstr);\n";
659 pr " free (r[i]);\n";
663 | RStruct (_, typ) ->
664 let jtyp = java_name_of_struct typ in
665 let cols = cols_of_struct typ in
666 generate_java_struct_return typ jtyp cols
667 | RStructList (_, typ) ->
668 let jtyp = java_name_of_struct typ in
669 let cols = cols_of_struct typ in
670 generate_java_struct_list_return typ jtyp cols
672 pr " jr = (*env)->NewStringUTF (env, r); // XXX size\n";
681 and generate_java_struct_return typ jtyp cols =
682 pr " cl = (*env)->FindClass (env, \"com/redhat/et/libguestfs/%s\");\n" jtyp;
683 pr " jr = (*env)->AllocObject (env, cl);\n";
687 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
688 pr " (*env)->SetObjectField (env, jr, fl, (*env)->NewStringUTF (env, r->%s));\n" name;
692 pr " memcpy (s, r->%s, 32);\n" name;
694 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
695 pr " (*env)->SetObjectField (env, jr, fl, (*env)->NewStringUTF (env, s));\n";
699 pr " int len = r->%s_len;\n" name;
700 pr " char s[len+1];\n";
701 pr " memcpy (s, r->%s, len);\n" name;
703 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
704 pr " (*env)->SetObjectField (env, jr, fl, (*env)->NewStringUTF (env, s));\n";
706 | name, (FBytes|FUInt64|FInt64) ->
707 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"J\");\n" name;
708 pr " (*env)->SetLongField (env, jr, fl, r->%s);\n" name;
709 | name, (FUInt32|FInt32) ->
710 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"I\");\n" name;
711 pr " (*env)->SetLongField (env, jr, fl, r->%s);\n" name;
712 | name, FOptPercent ->
713 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"F\");\n" name;
714 pr " (*env)->SetFloatField (env, jr, fl, r->%s);\n" name;
716 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"C\");\n" name;
717 pr " (*env)->SetLongField (env, jr, fl, r->%s);\n" name;
722 and generate_java_struct_list_return typ jtyp cols =
723 pr " cl = (*env)->FindClass (env, \"com/redhat/et/libguestfs/%s\");\n" jtyp;
724 pr " jr = (*env)->NewObjectArray (env, r->len, cl, NULL);\n";
725 pr " for (i = 0; i < r->len; ++i) {\n";
726 pr " jfl = (*env)->AllocObject (env, cl);\n";
730 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
731 pr " (*env)->SetObjectField (env, jfl, fl, (*env)->NewStringUTF (env, r->val[i].%s));\n" name;
735 pr " memcpy (s, r->val[i].%s, 32);\n" name;
737 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
738 pr " (*env)->SetObjectField (env, jfl, fl, (*env)->NewStringUTF (env, s));\n";
742 pr " int len = r->val[i].%s_len;\n" name;
743 pr " char s[len+1];\n";
744 pr " memcpy (s, r->val[i].%s, len);\n" name;
746 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
747 pr " (*env)->SetObjectField (env, jfl, fl, (*env)->NewStringUTF (env, s));\n";
749 | name, (FBytes|FUInt64|FInt64) ->
750 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"J\");\n" name;
751 pr " (*env)->SetLongField (env, jfl, fl, r->val[i].%s);\n" name;
752 | name, (FUInt32|FInt32) ->
753 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"I\");\n" name;
754 pr " (*env)->SetLongField (env, jfl, fl, r->val[i].%s);\n" name;
755 | name, FOptPercent ->
756 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"F\");\n" name;
757 pr " (*env)->SetFloatField (env, jfl, fl, r->val[i].%s);\n" name;
759 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"C\");\n" name;
760 pr " (*env)->SetLongField (env, jfl, fl, r->val[i].%s);\n" name;
762 pr " (*env)->SetObjectArrayElement (env, jfl, i, jfl);\n";
764 pr " guestfs_free_%s_list (r);\n" typ;
767 and generate_java_makefile_inc () =
768 generate_header HashStyle GPLv2plus;
770 pr "java_built_sources = \\\n";
773 pr "\tcom/redhat/et/libguestfs/%s.java \\\n" jtyp;
775 pr "\tcom/redhat/et/libguestfs/GuestFS.java\n"