This is an automated email from the ASF dual-hosted git repository.
sergeykamov pushed a commit to branch NLPCRAFT-283
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/NLPCRAFT-283 by this push:
new ee144fd WIP.
ee144fd is described below
commit ee144fdbc22cde907bb4c7639c81e3d491cf37e6
Author: Sergey Kamov <[email protected]>
AuthorDate: Thu Mar 25 12:56:08 2021 +0300
WIP.
---
.../org/apache/nlpcraft/common/util/NCUtils.scala | 73 ++++++++++++++++------
.../idl/compiler/functions/NCIdlFunctions.scala | 9 +--
.../compiler/functions/NCIdlFunctionsCustom.scala | 26 +++++---
3 files changed, 73 insertions(+), 35 deletions(-)
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
index df50ab1..f9a4eae 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/util/NCUtils.scala
@@ -860,7 +860,7 @@ object NCUtils extends LazyLogging {
* @param path File path.
*/
@throws[NCE]
- def serializePath(path: String, obj: Any): Unit = {
+ def serializePath(path: String, obj: Any): Unit =
try {
manageOutput(new FileOutputStream(path)) acquireAndGet { out ⇒
out.writeObject(obj)
@@ -871,7 +871,6 @@ object NCUtils extends LazyLogging {
catch {
case e: IOException ⇒ throw new NCE(s"Error writing file: $path",
e)
}
- }
/**
* Serializes data from file.
@@ -1952,28 +1951,61 @@ object NCUtils extends LazyLogging {
* @tparam T
* @return
*/
- def callMethod[T: ClassTag, R](objFac: () ⇒ Object, mtdName: String, arg:
T): R = {
- val obj = objFac()
- val mtd = obj.getClass.getMethod(mtdName, classTag[T].runtimeClass)
-
- var flag = mtd.canAccess(obj)
-
+ @throws[NCE]
+ def callMethod[T: ClassTag, R: ClassTag](objFac: () ⇒ Object, mtdName:
String, arg: T): R =
try {
- if (!flag) {
- mtd.setAccessible(true)
-
- flag = true
+ val obj = objFac()
+
+ val argClazz = classTag[T].runtimeClass
+ val retClazz = classTag[R].runtimeClass
+
+ def mkErrors = s"[name=$mtdName, argType=$argClazz,
retType=$retClazz]"
+
+ val mtd =
+ try {
+ val mtd = obj.getClass.getMethod(mtdName, argClazz)
+
+ if (mtd.getAnnotatedReturnType.getType != retClazz)
+ throw new NCE(s"Expected method not found $mkErrors")
+
+ mtd
+ }
+ catch {
+ case e: NoSuchMethodException ⇒ throw new NCE(s"Expected
method not found $mkErrors", e)
+ }
+
+ var flag = mtd.canAccess(obj)
+
+ try {
+ if (!flag) {
+ mtd.setAccessible(true)
+
+ flag = true
+ }
+ else
+ flag = false
+
+ val res =
+ try
+ mtd.invoke(obj,
arg.asInstanceOf[Object]).asInstanceOf[R]
+ catch {
+ case e: Throwable ⇒ throw new NCE(s"Method execution
error $mkErrors", e)
+ }
+
+ if (res == null)
+ throw new NCE(s"Unexpected 'null' result for method
execution $mkErrors")
+
+ res
+ }
+ finally {
+ if (flag)
+ mtd.setAccessible(false)
}
- else
- flag = false
-
- mtd.invoke(obj, arg.asInstanceOf[Object]).asInstanceOf[R]
}
- finally {
- if (flag)
- mtd.setAccessible(false)
+ catch {
+ case e: NCE ⇒ throw e
+ case e: Throwable ⇒ throw new NCE("Unexpected error.", e)
}
- }
/**
*
@@ -1981,6 +2013,7 @@ object NCUtils extends LazyLogging {
* @tparam T Type of the object to create.
* @return New instance of the specified type.
*/
+ @throws[NCE]
def mkObject[T](clsName: String): T = {
try
// Try Java reflection first.
diff --git
a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctions.scala
b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctions.scala
index 2674401..9e83290 100644
---
a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctions.scala
+++
b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctions.scala
@@ -167,7 +167,7 @@ private[functions] trait NCIdlFunctions {
f.term.pred.apply(f.token.getOrElse(tkn()), f.idlCtx)
}
catch {
- case e: NCE ⇒ throw new NCE(s"Execution error processing:
$f", e)
+ case e: NCE ⇒ throw e
case e: Exception ⇒ throw new Exception(s"Execution error
processing: $f", e)
}
@@ -206,12 +206,7 @@ private[functions] trait NCIdlFunctions {
require(false)
}
catch {
- case e: Exception ⇒
- e.getCause match {
- case cause: NCE ⇒ println(s"Expected error:
${cause.getLocalizedMessage}")
- case _ ⇒ println(s"Expected error:
${e.getLocalizedMessage}")
- }
-
+ case e: Exception ⇒ println(s"Expected error:
${e.getLocalizedMessage}")
}
protected implicit def convert(pred: String): TestDesc = TestDesc(truth =
pred)
diff --git
a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsCustom.scala
b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsCustom.scala
index cc2e543..65c8820 100644
---
a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsCustom.scala
+++
b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsCustom.scala
@@ -23,6 +23,14 @@ import org.junit.jupiter.api.Test
class NCIdlFunctionsCustomImpl {
def trueOn123(ctx: NCTokenPredicateContext): NCTokenPredicateResult =
new NCTokenPredicateResult(ctx.getToken.getOriginalText == "123", 1)
+
+ def wrongParams1(): NCTokenPredicateResult = null
+ def wrongParams2(s: String): NCTokenPredicateResult = null
+ def wrongParams3(s: String, ctx: NCTokenPredicateContext):
NCTokenPredicateResult = null
+ def wrongRet1(ctx: NCTokenPredicateContext): String = ""
+ def wrongRet2(ctx: NCTokenPredicateContext): Unit = {}
+ def wrongExec1(ctx: NCTokenPredicateContext): NCTokenPredicateResult = null
+ def wrongExec2(ctx: NCTokenPredicateContext): NCTokenPredicateResult =
throw new NullPointerException
}
/**
@@ -31,14 +39,16 @@ class NCIdlFunctionsCustomImpl {
class NCIdlFunctionsCustom extends NCIdlFunctions {
@Test
def testErrors(): Unit = {
- expectError(
- TestDesc(
- truth =
"org.apache.nlpcraft.model.intent.idl.compiler.functions.NCIdlFunctionsCustomImpl#missed",
- isCustom = true,
- token = Some(tkn(txt = "123")),
- expectedTokensUsed = Some(1)
- ),
- )
+ def test(truth: String): Unit = expectError(TestDesc(truth = truth,
isCustom = true))
+
+
test("org.apache.nlpcraft.model.intent.idl.compiler.functions.NCIdlFunctionsCustomImpl#missed")
+
test("org.apache.nlpcraft.model.intent.idl.compiler.functions.NCIdlFunctionsCustomImpl#wrongParams1")
+
test("org.apache.nlpcraft.model.intent.idl.compiler.functions.NCIdlFunctionsCustomImpl#wrongParams2")
+
test("org.apache.nlpcraft.model.intent.idl.compiler.functions.NCIdlFunctionsCustomImpl#wrongParams3")
+
test("org.apache.nlpcraft.model.intent.idl.compiler.functions.NCIdlFunctionsCustomImpl#wrongRet1")
+
test("org.apache.nlpcraft.model.intent.idl.compiler.functions.NCIdlFunctionsCustomImpl#wrongRet2")
+
test("org.apache.nlpcraft.model.intent.idl.compiler.functions.NCIdlFunctionsCustomImpl#wrongExec1")
+
test("org.apache.nlpcraft.model.intent.idl.compiler.functions.NCIdlFunctionsCustomImpl#wrongExec2")
}
@Test