Merge pull request #14606 from asashour:java_inline_return

This commit is contained in:
Alexander Alekhin 2019-05-29 20:03:54 +00:00
commit 29b3f66507
3 changed files with 231 additions and 554 deletions

View File

@ -33,8 +33,7 @@ public class KeyPoint {
public int class_id; public int class_id;
// javadoc:KeyPoint::KeyPoint(x,y,_size,_angle,_response,_octave,_class_id) // javadoc:KeyPoint::KeyPoint(x,y,_size,_angle,_response,_octave,_class_id)
public KeyPoint(float x, float y, float _size, float _angle, float _response, int _octave, int _class_id) public KeyPoint(float x, float y, float _size, float _angle, float _response, int _octave, int _class_id) {
{
pt = new Point(x, y); pt = new Point(x, y);
size = _size; size = _size;
angle = _angle; angle = _angle;
@ -44,32 +43,27 @@ public class KeyPoint {
} }
// javadoc: KeyPoint::KeyPoint() // javadoc: KeyPoint::KeyPoint()
public KeyPoint() public KeyPoint() {
{
this(0, 0, 0, -1, 0, 0, -1); this(0, 0, 0, -1, 0, 0, -1);
} }
// javadoc: KeyPoint::KeyPoint(x, y, _size, _angle, _response, _octave) // javadoc: KeyPoint::KeyPoint(x, y, _size, _angle, _response, _octave)
public KeyPoint(float x, float y, float _size, float _angle, float _response, int _octave) public KeyPoint(float x, float y, float _size, float _angle, float _response, int _octave) {
{
this(x, y, _size, _angle, _response, _octave, -1); this(x, y, _size, _angle, _response, _octave, -1);
} }
// javadoc: KeyPoint::KeyPoint(x, y, _size, _angle, _response) // javadoc: KeyPoint::KeyPoint(x, y, _size, _angle, _response)
public KeyPoint(float x, float y, float _size, float _angle, float _response) public KeyPoint(float x, float y, float _size, float _angle, float _response) {
{
this(x, y, _size, _angle, _response, 0, -1); this(x, y, _size, _angle, _response, 0, -1);
} }
// javadoc: KeyPoint::KeyPoint(x, y, _size, _angle) // javadoc: KeyPoint::KeyPoint(x, y, _size, _angle)
public KeyPoint(float x, float y, float _size, float _angle) public KeyPoint(float x, float y, float _size, float _angle) {
{
this(x, y, _size, _angle, 0, 0, -1); this(x, y, _size, _angle, 0, 0, -1);
} }
// javadoc: KeyPoint::KeyPoint(x, y, _size) // javadoc: KeyPoint::KeyPoint(x, y, _size)
public KeyPoint(float x, float y, float _size) public KeyPoint(float x, float y, float _size) {
{
this(x, y, _size, -1, 0, 0, -1); this(x, y, _size, -1, 0, 0, -1);
} }

File diff suppressed because it is too large Load Diff

View File

@ -222,7 +222,8 @@ class ClassInfo(GeneralInfo):
return Template("CLASS $namespace::$classpath.$name : $base").substitute(**self.__dict__) return Template("CLASS $namespace::$classpath.$name : $base").substitute(**self.__dict__)
def getAllImports(self, module): def getAllImports(self, module):
return ["import %s;" % c for c in sorted(self.imports) if not c.startswith('org.opencv.'+module)] return ["import %s;" % c for c in sorted(self.imports) if not c.startswith('org.opencv.'+module)
and (not c.startswith('java.lang.') or c.count('.') != 2)]
def addImports(self, ctype): def addImports(self, ctype):
if ctype in type_dict: if ctype in type_dict:
@ -285,8 +286,8 @@ class ClassInfo(GeneralInfo):
self.cpp_code.close() self.cpp_code.close()
def generateJavaCode(self, m, M): def generateJavaCode(self, m, M):
return Template(self.j_code.getvalue() + "\n\n" + \ return Template(self.j_code.getvalue() + "\n\n" +
self.jn_code.getvalue() + "\n}\n").substitute(\ self.jn_code.getvalue() + "\n}\n").substitute(
module = m, module = m,
name = self.name, name = self.name,
jname = self.jname, jname = self.jname,
@ -685,7 +686,7 @@ class JavaWrapperGenerator(object):
jn_args.append ( ArgInfo([ "double[]", "%s_out" % a.name, "", [], "" ]) ) jn_args.append ( ArgInfo([ "double[]", "%s_out" % a.name, "", [], "" ]) )
jni_args.append ( ArgInfo([ "double[]", "%s_out" % a.name, "", [], "" ]) ) jni_args.append ( ArgInfo([ "double[]", "%s_out" % a.name, "", [], "" ]) )
j_prologue.append( "double[] %s_out = new double[%i];" % (a.name, len(fields)) ) j_prologue.append( "double[] %s_out = new double[%i];" % (a.name, len(fields)) )
c_epilogue.append( \ c_epilogue.append(
"jdouble tmp_%(n)s[%(cnt)i] = {%(args)s}; env->SetDoubleArrayRegion(%(n)s_out, 0, %(cnt)i, tmp_%(n)s);" % "jdouble tmp_%(n)s[%(cnt)i] = {%(args)s}; env->SetDoubleArrayRegion(%(n)s_out, 0, %(cnt)i, tmp_%(n)s);" %
{ "n" : a.name, "cnt" : len(fields), "args" : ", ".join(["(jdouble)" + a.name + f[1] for f in fields]) } ) { "n" : a.name, "cnt" : len(fields), "args" : ", ".join(["(jdouble)" + a.name + f[1] for f in fields]) } )
if type_dict[a.ctype]["j_type"] in ('bool', 'int', 'long', 'float', 'double'): if type_dict[a.ctype]["j_type"] in ('bool', 'int', 'long', 'float', 'double'):
@ -724,10 +725,10 @@ class JavaWrapperGenerator(object):
# private java NATIVE method decl # private java NATIVE method decl
# e.g. # e.g.
# private static native void add_0(long src1, long src2, long dst, long mask, int dtype); # private static native void add_0(long src1, long src2, long dst, long mask, int dtype);
jn_code.write( Template(\ jn_code.write( Template(
" private static native $type $name($args);\n").substitute(\ " private static native $type $name($args);\n").substitute(
type = type_dict[fi.ctype].get("jn_type", "double[]"), \ type = type_dict[fi.ctype].get("jn_type", "double[]"),
name = fi.jname + '_' + str(suffix_counter), \ name = fi.jname + '_' + str(suffix_counter),
args = ", ".join(["%s %s" % (type_dict[a.ctype]["jn_type"], normalize_field_name(a.name)) for a in jn_args]) args = ", ".join(["%s %s" % (type_dict[a.ctype]["jn_type"], normalize_field_name(a.name)) for a in jn_args])
) ); ) );
@ -754,9 +755,9 @@ class JavaWrapperGenerator(object):
ret_type = fi.ctype ret_type = fi.ctype
if fi.ctype.endswith('*'): if fi.ctype.endswith('*'):
ret_type = ret_type[:-1] ret_type = ret_type[:-1]
ret_val = type_dict[ret_type]["j_type"] + " retVal = " ret_val = type_dict[ret_type]["j_type"] + " retVal = " if j_epilogue else "return "
tail = "" tail = ""
ret = "return retVal;" ret = "return retVal;" if j_epilogue else ""
if "v_type" in type_dict[ret_type]: if "v_type" in type_dict[ret_type]:
j_type = type_dict[ret_type]["j_type"] j_type = type_dict[ret_type]["j_type"]
if type_dict[ret_type]["v_type"] in ("Mat", "vector_Mat"): if type_dict[ret_type]["v_type"] in ("Mat", "vector_Mat"):
@ -767,70 +768,77 @@ class JavaWrapperGenerator(object):
ret_val = "Mat retValMat = new Mat(" ret_val = "Mat retValMat = new Mat("
j_prologue.append( j_type + ' retVal = new Array' + j_type+'();') j_prologue.append( j_type + ' retVal = new Array' + j_type+'();')
j_epilogue.append('Converters.Mat_to_' + ret_type + '(retValMat, retVal);') j_epilogue.append('Converters.Mat_to_' + ret_type + '(retValMat, retVal);')
ret = "return retVal;"
elif ret_type.startswith("Ptr_"): elif ret_type.startswith("Ptr_"):
ret_val = type_dict[fi.ctype]["j_type"] + " retVal = " + type_dict[ret_type]["j_type"] + ".__fromPtr__(" constructor = type_dict[ret_type]["j_type"] + ".__fromPtr__(";
if j_epilogue:
ret_val = type_dict[fi.ctype]["j_type"] + " retVal = " + constructor
else:
ret_val = "return " + constructor
tail = ")" tail = ")"
elif ret_type == "void": elif ret_type == "void":
ret_val = "" ret_val = ""
ret = "return;" ret = ""
elif ret_type == "": # c-tor elif ret_type == "": # c-tor
if fi.classname and ci.base: if fi.classname and ci.base:
ret_val = "super(" ret_val = "super("
tail = ")" tail = ")"
else: else:
ret_val = "nativeObj = " ret_val = "nativeObj = "
ret = "return;" ret = ""
elif self.isWrapped(ret_type): # wrapped class elif self.isWrapped(ret_type): # wrapped class
ret_val = type_dict[ret_type]["j_type"] + " retVal = new " + self.getClass(ret_type).jname + "(" constructor = self.getClass(ret_type).jname + "(";
if j_epilogue:
ret_val = type_dict[ret_type]["j_type"] + " retVal = new " + constructor
else:
ret_val = "return new " + constructor
tail = ")" tail = ")"
elif "jn_type" not in type_dict[ret_type]: elif "jn_type" not in type_dict[ret_type]:
ret_val = type_dict[fi.ctype]["j_type"] + " retVal = new " + type_dict[ret_type]["j_type"] + "(" constructor = type_dict[ret_type]["j_type"] + "(";
if j_epilogue:
ret_val = type_dict[fi.ctype]["j_type"] + " retVal = new " + constructor
else:
ret_val = "return new " + constructor
tail = ")" tail = ")"
static = "static" static = "static"
if fi.classname: if fi.classname:
static = fi.static static = fi.static
j_code.write( Template(\ j_code.write( Template(
""" public $static $j_type $j_name($j_args) """ public $static$j_type $j_name($j_args) {$prologue
{ $ret_val$jn_name($jn_args_call)$tail;$epilogue$ret
$prologue
$ret_val$jn_name($jn_args_call)$tail;
$epilogue
$ret
} }
""" """
).substitute(\ ).substitute(
ret = ret, \ ret = "\n " + ret if ret else "",
ret_val = ret_val, \ ret_val = ret_val,
tail = tail, \ tail = tail,
prologue = "\n ".join(j_prologue), \ prologue = "\n " + "\n ".join(j_prologue) if j_prologue else "",
epilogue = "\n ".join(j_epilogue), \ epilogue = "\n " + "\n ".join(j_epilogue) if j_epilogue else "",
static=static, \ static = static + " " if static else "",
j_type=type_dict[fi.ctype]["j_type"], \ j_type=type_dict[fi.ctype]["j_type"],
j_name=fi.jname, \ j_name=fi.jname,
j_args=", ".join(j_args), \ j_args=", ".join(j_args),
jn_name=fi.jname + '_' + str(suffix_counter), \ jn_name=fi.jname + '_' + str(suffix_counter),
jn_args_call=", ".join( [a.name for a in jn_args] ),\ jn_args_call=", ".join( [a.name for a in jn_args] ),
) )
) )
# cpp part: # cpp part:
# jni_func(..) { _retval_ = cv_func(..); return _retval_; } # jni_func(..) { _retval_ = cv_func(..); return _retval_; }
ret = "return _retval_;" ret = "return _retval_;" if c_epilogue else ""
default = "return 0;" default = "return 0;"
if fi.ctype == "void": if fi.ctype == "void":
ret = "return;" ret = ""
default = "return;" default = ""
elif not fi.ctype: # c-tor elif not fi.ctype: # c-tor
ret = "return (jlong) _retval_;" ret = "return (jlong) _retval_;"
elif "v_type" in type_dict[fi.ctype]: # c-tor elif "v_type" in type_dict[fi.ctype]: # c-tor
if type_dict[fi.ctype]["v_type"] in ("Mat", "vector_Mat"): if type_dict[fi.ctype]["v_type"] in ("Mat", "vector_Mat"):
ret = "return (jlong) _retval_;" ret = "return (jlong) _retval_;"
else: # returned as jobject
ret = "return _retval_;"
elif fi.ctype == "String": elif fi.ctype == "String":
ret = "return env->NewStringUTF(_retval_.c_str());" ret = "return env->NewStringUTF(_retval_.c_str());"
default = 'return env->NewStringUTF("");' default = 'return env->NewStringUTF("");'
@ -853,19 +861,19 @@ class JavaWrapperGenerator(object):
name = prop_name + ";//" name = prop_name + ";//"
cvname = fi.fullName(isCPP=True) cvname = fi.fullName(isCPP=True)
retval = self.fullTypeName(fi.ctype) + " _retval_ = " retval = self.fullTypeName(fi.ctype) + " _retval_ = " if ret else "return "
if fi.ctype == "void": if fi.ctype == "void":
retval = "" retval = ""
elif fi.ctype == "String": elif fi.ctype == "String":
retval = "cv::" + retval retval = "cv::" + self.fullTypeName(fi.ctype) + " _retval_ = "
elif "v_type" in type_dict[fi.ctype]: # vector is returned elif "v_type" in type_dict[fi.ctype]: # vector is returned
retval = type_dict[fi.ctype]['jni_var'] % {"n" : '_ret_val_vector_'} + " = " retval = type_dict[fi.ctype]['jni_var'] % {"n" : '_ret_val_vector_'} + " = "
if type_dict[fi.ctype]["v_type"] in ("Mat", "vector_Mat"): if type_dict[fi.ctype]["v_type"] in ("Mat", "vector_Mat"):
c_epilogue.append("Mat* _retval_ = new Mat();") c_epilogue.append("Mat* _retval_ = new Mat();")
c_epilogue.append(fi.ctype+"_to_Mat(_ret_val_vector_, *_retval_);") c_epilogue.append(fi.ctype+"_to_Mat(_ret_val_vector_, *_retval_);")
else: else:
c_epilogue.append("jobject _retval_ = " + fi.ctype + "_to_List(env, _ret_val_vector_);") c_epilogue.append("return " + fi.ctype + "_to_List(env, _ret_val_vector_);")
if len(fi.classname)>0: if fi.classname:
if not fi.ctype: # c-tor if not fi.ctype: # c-tor
retval = fi.fullClass(isCPP=True) + "* _retval_ = " retval = fi.fullClass(isCPP=True) + "* _retval_ = "
cvname = "new " + fi.fullClass(isCPP=True) cvname = "new " + fi.fullClass(isCPP=True)
@ -873,9 +881,9 @@ class JavaWrapperGenerator(object):
cvname = fi.fullName(isCPP=True) cvname = fi.fullName(isCPP=True)
else: else:
cvname = ("me->" if not self.isSmartClass(ci) else "(*me)->") + name cvname = ("me->" if not self.isSmartClass(ci) else "(*me)->") + name
c_prologue.append(\ c_prologue.append(
"%(cls)s* me = (%(cls)s*) self; //TODO: check for NULL" \ "%(cls)s* me = (%(cls)s*) self; //TODO: check for NULL"
% { "cls" : self.smartWrap(ci, fi.fullClass(isCPP=True))} \ % { "cls" : self.smartWrap(ci, fi.fullClass(isCPP=True))}
) )
cvargs = [] cvargs = []
for a in args: for a in args:
@ -898,7 +906,7 @@ class JavaWrapperGenerator(object):
rtype = type_dict[fi.ctype].get("jni_type", "jdoubleArray") rtype = type_dict[fi.ctype].get("jni_type", "jdoubleArray")
clazz = ci.jname clazz = ci.jname
cpp_code.write ( Template( \ cpp_code.write ( Template(
""" """
${namespace} ${namespace}
@ -909,37 +917,34 @@ JNIEXPORT $rtype JNICALL Java_org_opencv_${module}_${clazz}_$fname
{ {
static const char method_name[] = "$module::$fname()"; static const char method_name[] = "$module::$fname()";
try { try {
LOGD("%s", method_name); LOGD("%s", method_name);$prologue
$prologue $retval$cvname($cvargs);$epilogue$ret
$retval$cvname( $cvargs );
$epilogue$ret
} catch(const std::exception &e) { } catch(const std::exception &e) {
throwJavaException(env, &e, method_name); throwJavaException(env, &e, method_name);
} catch (...) { } catch (...) {
throwJavaException(env, 0, method_name); throwJavaException(env, 0, method_name);
} }$default
$default
} }
""" ).substitute( \ """ ).substitute(
rtype = rtype, \ rtype = rtype,
module = self.module.replace('_', '_1'), \ module = self.module.replace('_', '_1'),
clazz = clazz.replace('_', '_1'), \ clazz = clazz.replace('_', '_1'),
fname = (fi.jname + '_' + str(suffix_counter)).replace('_', '_1'), \ fname = (fi.jname + '_' + str(suffix_counter)).replace('_', '_1'),
args = ", ".join(["%s %s" % (type_dict[a.ctype].get("jni_type"), a.name) for a in jni_args]), \ args = ", ".join(["%s %s" % (type_dict[a.ctype].get("jni_type"), a.name) for a in jni_args]),
argst = ", ".join([type_dict[a.ctype].get("jni_type") for a in jni_args]), \ argst = ", ".join([type_dict[a.ctype].get("jni_type") for a in jni_args]),
prologue = "\n ".join(c_prologue), \ prologue = "\n " + "\n ".join(c_prologue) if c_prologue else "",
epilogue = " ".join(c_epilogue) + ("\n " if c_epilogue else ""), \ epilogue = "\n " + "\n ".join(c_epilogue) if c_epilogue else "",
ret = ret, \ ret = "\n " + ret if ret else "",
cvname = cvname, \ cvname = cvname,
cvargs = ", ".join(cvargs), \ cvargs = " " + ", ".join(cvargs) + " " if cvargs else "",
default = default, \ default = "\n " + default if default else "",
retval = retval, \ retval = retval,
namespace = ('using namespace ' + ci.namespace.replace('.', '::') + ';') if ci.namespace else '' namespace = ('using namespace ' + ci.namespace.replace('.', '::') + ';') if ci.namespace else ''
) ) ) )
# adding method signature to dictionarry # adding method signature to dictionary
j_signatures.append(j_signature) j_signatures.append(j_signature)
# processing args with default values # processing args with default values
@ -1036,7 +1041,7 @@ JNIEXPORT $rtype JNICALL Java_org_opencv_${module}_${clazz}_$fname
""" ) """ )
# native support for java finalize() # native support for java finalize()
ci.cpp_code.write( \ ci.cpp_code.write(
""" """
// //
// native support for java finalize() // native support for java finalize()