fixed warnings in generated Java code (accurate imports)
authorAndrey Pavlenko <no@email>
Fri, 22 Jul 2011 13:53:49 +0000 (13:53 +0000)
committerAndrey Pavlenko <no@email>
Fri, 22 Jul 2011 13:53:49 +0000 (13:53 +0000)
modules/java/gen_java.py

index 0358c23..8950119 100644 (file)
@@ -177,14 +177,14 @@ type_dict = {
 \r
 # "complex" : { j_type : "?", jn_args : (("", ""),), jn_name : "", jni_var : "", jni_name : "", "suffix" : "?" },\r
 \r
-    "vector_Point": { "j_type" : "java.util.List<Point>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<Point> %(n)s", "suffix" : "J" },\r
-    "vector_Mat" :  { "j_type" : "java.util.List<Mat>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<Mat> %(n)s", "suffix" : "J" },\r
-    "vector_KeyPoint" : { "j_type" : "java.util.List<KeyPoint>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<KeyPoint> %(n)s", "suffix" : "J" },\r
-    "vector_Rect" : { "j_type" : "java.util.List<Rect>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<Rect> %(n)s", "suffix" : "J" },\r
-    "vector_uchar" : { "j_type" : "java.util.List<Byte>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<uchar> %(n)s", "suffix" : "J" },\r
-    "vector_int" : { "j_type" : "java.util.List<Integer>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<int> %(n)s", "suffix" : "J" },\r
-    "vector_float" : { "j_type" : "java.util.List<Float>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<float> %(n)s", "suffix" : "J" },\r
-    "vector_double" : { "j_type" : "java.util.List<Double>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<double> %(n)s", "suffix" : "J" },\r
+    "vector_Point": { "j_type" : "List<Point>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<Point> %(n)s", "suffix" : "J" },\r
+    "vector_Mat" :  { "j_type" : "List<Mat>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<Mat> %(n)s", "suffix" : "J" },\r
+    "vector_KeyPoint" : { "j_type" : "List<KeyPoint>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<KeyPoint> %(n)s", "suffix" : "J" },\r
+    "vector_Rect" : { "j_type" : "List<Rect>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<Rect> %(n)s", "suffix" : "J" },\r
+    "vector_uchar" : { "j_type" : "List<Byte>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<uchar> %(n)s", "suffix" : "J" },\r
+    "vector_int" : { "j_type" : "List<Integer>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<int> %(n)s", "suffix" : "J" },\r
+    "vector_float" : { "j_type" : "List<Float>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<float> %(n)s", "suffix" : "J" },\r
+    "vector_double" : { "j_type" : "List<Double>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<double> %(n)s", "suffix" : "J" },\r
 \r
     "Mat"     : { "j_type" : "Mat", "jn_type" : "long", "jn_args" : (("__int64", ".nativeObj"),),\r
                   "jni_var" : "Mat& %(n)s = *((Mat*)%(n)s_nativeObj)",\r
@@ -229,15 +229,15 @@ type_dict = {
     "CvSlice" : { "j_type" : "Range",  "jn_args" : (("int", ".start"), ("int", ".end")),\r
                   "jni_var" : "Range %(n)s(%(n)s_start, %(n)s_end)", "jni_type" : "jdoubleArray",\r
                   "suffix" : "II"},\r
-    "string"  : { "j_type" : "java.lang.String",  "jn_type" : "java.lang.String",\r
+    "string"  : { "j_type" : "String",  "jn_type" : "String",\r
                   "jni_type" : "jstring", "jni_name" : "n_%(n)s",\r
                   "jni_var" : 'const char* utf_%(n)s = env->GetStringUTFChars(%(n)s, 0); std::string n_%(n)s( utf_%(n)s ? utf_%(n)s : "" ); env->ReleaseStringUTFChars(%(n)s, utf_%(n)s)',\r
                   "suffix" : "Ljava_lang_String_2"},\r
-    "String"  : { "j_type" : "java.lang.String",  "jn_type" : "java.lang.String",\r
+    "String"  : { "j_type" : "String",  "jn_type" : "String",\r
                   "jni_type" : "jstring", "jni_name" : "n_%(n)s",\r
                   "jni_var" : 'const char* utf_%(n)s = env->GetStringUTFChars(%(n)s, 0); String n_%(n)s( utf_%(n)s ? utf_%(n)s : "" ); env->ReleaseStringUTFChars(%(n)s, utf_%(n)s)',\r
                   "suffix" : "Ljava_lang_String_2"},\r
-    "c_string": { "j_type" : "java.lang.String",  "jn_type" : "java.lang.String",\r
+    "c_string": { "j_type" : "String",  "jn_type" : "String",\r
                   "jni_type" : "jstring", "jni_name" : "n_%(n)s.c_str()",\r
                   "jni_var" : 'const char* utf_%(n)s = env->GetStringUTFChars(%(n)s, 0); std::string n_%(n)s( utf_%(n)s ? utf_%(n)s : "" ); env->ReleaseStringUTFChars(%(n)s, utf_%(n)s)',\r
                   "suffix" : "Ljava_lang_String_2"},\r
@@ -739,9 +739,9 @@ extern "C" {
 \r
         # saving code streams\r
         for cls in self.java_code.keys():\r
-##            imports = "\n".join([ "import %s;" % c for c in \\r
-##                sorted(self.classes[cls].imports) if not c.startswith('org.opencv.'+self.module) ])\r
-            imports = "import org.opencv.core.*;\nimport org.opencv.Converters;\n"\r
+            imports = "\n".join([ "import %s;" % c for c in \\r
+                sorted(self.classes[cls].imports) if not c.startswith('org.opencv.'+self.module) ])\r
+            ##imports = "import org.opencv.core.*;\nimport org.opencv.Converters;\n"\r
             self.java_code[cls]["j_code"].write("\n\n%s\n}\n" % self.java_code[cls]["jn_code"].getvalue())\r
             java_code = self.java_code[cls]["j_code"].getvalue()\r
             java_code = Template(java_code).substitute(imports = imports)\r
@@ -766,6 +766,22 @@ extern "C" {
 \r
 \r
 \r
+    def get_imports(self, scope_classname, ctype):\r
+        imports = self.classes[scope_classname or self.Module].imports\r
+        if ctype.startswith('vector'):\r
+            imports.add("java.util.List")\r
+            imports.add("org.opencv.core.Mat")\r
+            imports.add("org.opencv.Converters")\r
+            ctype = ctype.replace('vector_', '')\r
+        j_type = ''\r
+        if ctype in type_dict:\r
+            j_type = type_dict[ctype]['j_type']\r
+        if j_type in ( "CvType", "Mat", "Point", "Point3", "Range", "Rect", "RotatedRect", "Scalar", "Size", "TermCriteria" ):\r
+            imports.add("org.opencv.core." + j_type)\r
+        if j_type == 'String':\r
+            imports.add("java.lang.String")\r
+\r
+\r
 \r
     def gen_func(self, fi, isoverload, prop_name=''):\r
         j_code   = self.java_code[fi.classname or self.Module]["j_code"]\r
@@ -829,11 +845,7 @@ extern "C" {
             j_epilogue = []\r
             c_prologue = []\r
             c_epilogue = []\r
-            if fi.ctype == 'Mat':\r
-                self.classes[fi.classname or self.Module].imports.add("org.opencv.core.Mat")\r
             if type_dict[fi.ctype]["jni_type"] == "jdoubleArray":\r
-                if "Vec" not in fi.ctype:\r
-                    self.classes[fi.classname or self.Module].imports.add("org.opencv.core." + type_dict[fi.ctype]["j_type"])\r
                 fields = type_dict[fi.ctype]["jn_args"]\r
                 c_epilogue.append( \\r
                     ("jdoubleArray _da_retval_ = env->NewDoubleArray(%(cnt)i);  " +\r
@@ -845,16 +857,12 @@ extern "C" {
                 jn_args.append ( ArgInfo([ "__int64", "nativeObj", "", [], "" ]) )\r
                 jni_args.append( ArgInfo([ "__int64", "self", "", [], "" ]) )\r
                 suffix += "J"\r
+            self.get_imports(fi.classname, fi.ctype)\r
             for a in args:\r
+                self.get_imports(fi.classname, a.ctype)\r
                 suffix += type_dict[a.ctype].get("suffix") or ""\r
 \r
                 if "vector" in a.ctype: # pass as Mat\r
-                    self.classes[fi.classname or self.Module].imports.add("org.opencv.core.Mat")\r
-                    self.classes[fi.classname or self.Module].imports.add("org.opencv.Converters")\r
-                    self.classes[fi.classname or self.Module].imports.add("java.util.List")\r
-                    t = a.ctype.replace("vector_", "")\r
-                    if "Vec" not in t and t in type_dict and "jn_args" in type_dict[t] and type_dict[t]["jni_type"] == "jdoubleArray":\r
-                        self.classes[fi.classname or self.Module].imports.add("org.opencv.core." + type_dict[t]['j_type'])\r
                     jn_args.append  ( ArgInfo([ "__int64", "%s_mat.nativeObj" % a.name, "", [], "" ]) )\r
                     jni_args.append ( ArgInfo([ "__int64", "%s_mat_nativeObj" % a.name, "", [], "" ]) )\r
                     c_prologue.append( type_dict[a.ctype]["jni_var"] % {"n" : a.name} + ";" )\r
@@ -868,12 +876,6 @@ extern "C" {
                         j_epilogue.append("Converters.Mat_to_%(t)s(%(n)s_mat, %(n)s);" % {"t" : a.ctype, "n" : a.name})\r
                         c_epilogue.append( "%(t)s_to_Mat( %(n)s, %(n)s_mat );" % {"n" : a.name, "t" : a.ctype} )\r
                 else:\r
-                    if a.ctype == 'Mat' or \\r
-                        ( "Vec" not in a.ctype and "jn_args" in type_dict[a.ctype] and \\r
-                          type_dict[a.ctype].get("jni_type") == "jdoubleArray" ):\r
-                        self.classes[fi.classname or self.Module].imports.add("org.opencv.core." + type_dict[a.ctype]['j_type'])\r
-                    if 'String' in type_dict[a.ctype]['j_type']:\r
-                        self.classes[fi.classname or self.Module].imports.add("java.lang.String")\r
                     fields = type_dict[a.ctype].get("jn_args", ((a.ctype, ""),))\r
                     if "I" in a.out or not a.out or a.ctype in self.classes: # input arg, pass by primitive fields\r
                         for f in fields:\r