java: generated code inline return

pull/14606/head
Ahmed Ashour 6 years ago
parent 1810702bf0
commit ca8a1d2cff
  1. 18
      modules/core/misc/java/src/java/core+KeyPoint.java
  2. 620
      modules/core/misc/java/src/java/core+Mat.java
  3. 147
      modules/java/generator/gen_java.py

@ -33,8 +33,7 @@ public class KeyPoint {
public int 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);
size = _size;
angle = _angle;
@ -44,32 +43,27 @@ public class KeyPoint {
}
// javadoc: KeyPoint::KeyPoint()
public KeyPoint()
{
public KeyPoint() {
this(0, 0, 0, -1, 0, 0, -1);
}
// 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);
}
// 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);
}
// 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);
}
// 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);
}

File diff suppressed because it is too large Load Diff

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

Loading…
Cancel
Save