Skip to content

Commit 294e86f

Browse files
authored
Merge pull request #1207 from NativeScript/trifonov/suppress-exceptions
Added a setting to wrap calls to CallJSMethod in try catch
2 parents 13450fb + 69ac687 commit 294e86f

File tree

4 files changed

+78
-5
lines changed

4 files changed

+78
-5
lines changed

test-app/app/src/main/assets/app/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"freeMemoryRatio": 0.50,
99
"markingMode": "full",
1010
"maxLogcatObjectSize": 1024,
11-
"forceLog": false
11+
"forceLog": false,
12+
"suppressCallJSMethodExceptions": false
1213
},
1314
"discardUncaughtJsExceptions": false
1415
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.nativescript.staticbindinggenerator;
2+
3+
import org.apache.bcel.generic.Type;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
class DefaultValues {
9+
static final Map<Type, String> defaultValues = new HashMap<>();
10+
11+
// load
12+
static {
13+
defaultValues.put(Type.BOOLEAN, "false");
14+
defaultValues.put(Type.BYTE, "0");
15+
defaultValues.put(Type.SHORT, "0");
16+
defaultValues.put(Type.INT, "0");
17+
defaultValues.put(Type.LONG, "0L");
18+
defaultValues.put(Type.CHAR, "'\u0000'");
19+
defaultValues.put(Type.FLOAT, "0.0F");
20+
defaultValues.put(Type.DOUBLE, "0.0");
21+
}
22+
23+
public static final String defaultStringValueFor(Type type) {
24+
if(defaultValues.containsKey(type)) {
25+
return defaultValues.get(type);
26+
} else {
27+
return "null";
28+
}
29+
}
30+
}

test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/Generator.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.bcel.classfile.ClassParser;
2727
import org.apache.bcel.classfile.JavaClass;
2828
import org.apache.bcel.classfile.Method;
29+
import org.apache.bcel.generic.BasicType;
2930
import org.apache.bcel.generic.Type;
3031

3132
public class Generator {
@@ -47,15 +48,17 @@ public class Generator {
4748
private final File outputDir;
4849
private final List<DataRow> libs;
4950
private final Map<String, JavaClass> classes;
51+
private final boolean suppressCallJSMethodExceptions;
5052

5153
public Generator(File outputDir, List<DataRow> libs) throws IOException {
52-
this(outputDir, libs, false);
54+
this(outputDir, libs, false, false);
5355
}
5456

55-
public Generator(File outputDir, List<DataRow> libs, boolean throwOnError) throws IOException {
57+
public Generator(File outputDir, List<DataRow> libs, boolean suppressCallJSMethodExceptions, boolean throwOnError) throws IOException {
5658
this.outputDir = outputDir;
5759
this.libs = libs;
5860
this.classes = readClasses(libs, throwOnError);
61+
this.suppressCallJSMethodExceptions = suppressCallJSMethodExceptions;
5962
}
6063

6164
public void writeBindings(String filename) throws IOException, ClassNotFoundException {
@@ -638,6 +641,12 @@ private void writeMethodBody(Method m, boolean isConstructor, boolean isApplicat
638641
}
639642
w.write("\t\t");
640643
Type ret = m.getReturnType();
644+
645+
if(this.suppressCallJSMethodExceptions) {
646+
w.writeln("try {");
647+
w.write("\t\t\t");
648+
}
649+
641650
if (!ret.equals(Type.VOID)) {
642651
w.write("return (");
643652
writeType(ret, w);
@@ -648,6 +657,19 @@ private void writeMethodBody(Method m, boolean isConstructor, boolean isApplicat
648657
w.write("\", ");
649658
writeType(ret, w);
650659
w.writeln(".class, args);");
660+
661+
if(this.suppressCallJSMethodExceptions) {
662+
w.writeln("\t\t} catch (Throwable t) {");
663+
w.writeln("\t\t\tandroid.util.Log.w(\"Error\", t);");
664+
if (!ret.equals(Type.VOID)) {
665+
w.write("\t\t\t");
666+
w.write("return ");
667+
w.write(DefaultValues.defaultStringValueFor(ret));
668+
w.writeln(";");
669+
}
670+
w.writeln("\t\t}");
671+
}
672+
651673
if (m.getName().equals("onCreate") && isApplicationClass) {
652674
w.writeln("\t\tif (runtime != null) {");
653675
w.writeln("\t\t\truntime.run();");

test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/Main.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public class Main {
3333
}
3434

3535
public static void main(String[] args) throws IOException, ClassNotFoundException {
36-
3736
validateInput();
3837

3938
getWorkerExcludeFile();
@@ -45,7 +44,7 @@ public static void main(String[] args) throws IOException, ClassNotFoundExceptio
4544

4645
// generate java bindings
4746
String inputBindingFilename = Paths.get(System.getProperty("user.dir"), SBG_BINDINGS_NAME).toString();
48-
new Generator(outputDir, rows).writeBindings(inputBindingFilename);
47+
new Generator(outputDir, rows, isSuppressCallJSMethodExceptionsEnabled(), false).writeBindings(inputBindingFilename);
4948
}
5049

5150
/*
@@ -67,6 +66,27 @@ private static void generateJsInputFile() throws IOException {
6766
pw.close();
6867
}
6968

69+
private static boolean isSuppressCallJSMethodExceptionsEnabled() throws IOException{
70+
File jsonFile = new File(inputDir, "package.json");
71+
if (!jsonFile.exists()) {
72+
return false;
73+
}
74+
String jsonContent = FileUtils.readFileToString(jsonFile, "UTF-8");
75+
JSONObject pjson = null;
76+
try {
77+
pjson = new JSONObject(jsonContent);
78+
if (pjson.has("android")) {
79+
JSONObject androidSettings = (JSONObject) pjson.get("android");
80+
if(androidSettings.has("suppressCallJSMethodExceptions") && androidSettings.get("suppressCallJSMethodExceptions").toString().equals("true")) {
81+
return true;
82+
}
83+
}
84+
} catch (JSONException e) {
85+
e.printStackTrace();
86+
}
87+
return false;
88+
}
89+
7090
private static void validateInput() throws IOException {
7191
dependenciesFile = "sbg-java-dependencies.txt";
7292
if (!(new File(dependenciesFile).exists())) {

0 commit comments

Comments
 (0)