From bb5a2c9dc53f717981993781f6052e5418fcaa18 Mon Sep 17 00:00:00 2001 From: blagoev Date: Thu, 1 Oct 2015 17:14:33 +0300 Subject: [PATCH] fixed https://github.com/NativeScript/android-runtime/issues/180 (calling non existing default constructor) removed asserts for failed registered instances so runtime does not fail --- src/jni/MetadataNode.cpp | 8 ------- src/src/com/tns/MethodResolver.java | 6 +++++ src/src/com/tns/Platform.java | 17 ++++++++------ src/src/com/tns/Util.java | 1 + test-app/assets/app/mainpage.js | 4 ++-- .../app/tests/testIfAbleToRunExternalFile.js | 7 +++--- test-app/assets/app/tests/tests.js | 23 +++++++++++++++++++ 7 files changed, 46 insertions(+), 20 deletions(-) diff --git a/src/jni/MetadataNode.cpp b/src/jni/MetadataNode.cpp index f658e74de..e9b8b8c39 100644 --- a/src/jni/MetadataNode.cpp +++ b/src/jni/MetadataNode.cpp @@ -510,8 +510,6 @@ void MetadataNode::InnerClassConstructorCallback(const v8::FunctionCallbackInfo< string fullClassName = CreateFullClassName(className, extendName); bool success = NativeScriptRuntime::RegisterInstance(thiz, fullClassName, argWrapper, outerThis, false); - - assert(success); } void MetadataNode::InnerClassAccessorGetterCallback(Local property, const PropertyCallbackInfo& info) @@ -717,8 +715,6 @@ void MetadataNode::ExtendedClassConstructorCallback(const v8::FunctionCallbackIn string fullClassName = extData->fullClassName; bool success = NativeScriptRuntime::RegisterInstance(thiz, fullClassName, argWrapper, implementationObject, false); - - assert(success); } @@ -775,8 +771,6 @@ void MetadataNode::InterfaceConstructorCallback(const v8::FunctionCallbackInfo()); auto success = NativeScriptRuntime::RegisterInstance(thiz, fullClassName, argWrapper, implementationObject, true); - - assert(success); } void MetadataNode::ClassConstructorCallback(const v8::FunctionCallbackInfo& info) @@ -796,8 +790,6 @@ void MetadataNode::ClassConstructorCallback(const v8::FunctionCallbackInfo& info) diff --git a/src/src/com/tns/MethodResolver.java b/src/src/com/tns/MethodResolver.java index a0fd89f8e..8287c6515 100644 --- a/src/src/com/tns/MethodResolver.java +++ b/src/src/com/tns/MethodResolver.java @@ -481,6 +481,12 @@ else if (assignTo.equals(char.class)) public static boolean convertConstructorArgs(Constructor ctor, Object[] args) { boolean success = true; + + if (ctor == null) + { + success = false; + return success; + } Class[] paramTypes; if (constructorParamTypeCache.containsKey(ctor)) diff --git a/src/src/com/tns/Platform.java b/src/src/com/tns/Platform.java index 53b330bdb..83eccbaf6 100644 --- a/src/src/com/tns/Platform.java +++ b/src/src/com/tns/Platform.java @@ -289,15 +289,18 @@ private static Object createInstance(Object[] args, int objectId, int constructo { StringBuilder builder = new StringBuilder(); builder.append(constructorId + "("); - for (Object arg : args) + if (args != null) { - if (arg != null) + for (Object arg : args) { - builder.append(arg.toString() + ", "); - } - else - { - builder.append("null, "); + if (arg != null) + { + builder.append(arg.toString() + ", "); + } + else + { + builder.append("null, "); + } } } builder.append(")"); diff --git a/src/src/com/tns/Util.java b/src/src/com/tns/Util.java index ff78cc999..26d9d629e 100644 --- a/src/src/com/tns/Util.java +++ b/src/src/com/tns/Util.java @@ -57,6 +57,7 @@ static boolean runPlugin(Logger logger, Context context) { if (logger.isEnabled()) e.printStackTrace(); } + try { Class liveSyncPluginClass = Class.forName(pluginClassName); diff --git a/test-app/assets/app/mainpage.js b/test-app/assets/app/mainpage.js index 250190b8d..70fa9e2b9 100644 --- a/test-app/assets/app/mainpage.js +++ b/test-app/assets/app/mainpage.js @@ -1,5 +1,5 @@ __disableVerboseLogging(); - +__log("starting tests"); require("./tests/testWeakRef"); require("./tests/tests"); require("./tests/testMethodResolution"); @@ -26,9 +26,9 @@ var MainActivity = { var k = this.toString(); __log("this.toString " + k); this.super.onCreate(bundle); - //this.super.onCreate(null); require("./tests/testsWithContext").run(this); + execute(); //run jasmine var layout = new android.widget.LinearLayout(this); diff --git a/test-app/assets/app/tests/testIfAbleToRunExternalFile.js b/test-app/assets/app/tests/testIfAbleToRunExternalFile.js index d72218c36..83b78711c 100644 --- a/test-app/assets/app/tests/testIfAbleToRunExternalFile.js +++ b/test-app/assets/app/tests/testIfAbleToRunExternalFile.js @@ -1,9 +1,9 @@ describe("Tests running external files", function () { it("When_file_outside_the_project_folder_is_required_it_should_fail", function () { - - __log("When_file_outside_the_project_folder_is_required_it_should_throw_IllegalAccessException"); - + + __log("TEST: When_file_outside_the_project_folder_is_required_it_should_fail"); + var illegalAccesExceptionCaught = false; var fileSeparator = "/"; var nonExistingFileName = "nonExistingFile"; @@ -26,4 +26,5 @@ describe("Tests running external files", function () { expect(exceptionCaught).toBe(true); }); + }); \ No newline at end of file diff --git a/test-app/assets/app/tests/tests.js b/test-app/assets/app/tests/tests.js index 26dc17159..7a01854e6 100644 --- a/test-app/assets/app/tests/tests.js +++ b/test-app/assets/app/tests/tests.js @@ -1863,4 +1863,27 @@ describe("Tests ", function () { expect(value123).toBe(123); }); + + + it("When_calling_non_existent_ctor_it_should_fail", function () { + + __log("TEST: When_calling_non_existent_ctor_it_should_fail: Start"); + + try + { + var textView = android.widget.TextView; + var MyTextView = textView.extend({ + + }); + + var my = new MyTextView(); + } + catch(e) + { + exceptionCaught = true; + } + + expect(exceptionCaught).toBe(true); + }); + }); \ No newline at end of file