Skip to content

Commit 3aeaa47

Browse files
authored
Merge pull request #1239 from NativeScript/vmutafov/sbg-interfaces-handling-fix
Fix handling of user implemented interfaces
2 parents 6aa1b74 + dd4482b commit 3aeaa47

1 file changed

Lines changed: 26 additions & 21 deletions

File tree

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

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

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ public class Generator {
4040
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
4141

4242
private static final String AUTO_GENERATED_FILE_PROLOGUE =
43-
"/* AUTO-GENERATED FILE. DO NOT MODIFY." + LINE_SEPARATOR +
44-
" * This class was automatically generated by the" + LINE_SEPARATOR +
45-
" * static binding generator from the resources it found." + LINE_SEPARATOR +
46-
" * Please do not modify by hand." + LINE_SEPARATOR +
47-
" */" + LINE_SEPARATOR;
43+
"/* AUTO-GENERATED FILE. DO NOT MODIFY." + LINE_SEPARATOR +
44+
" * This class was automatically generated by the" + LINE_SEPARATOR +
45+
" * static binding generator from the resources it found." + LINE_SEPARATOR +
46+
" * Please do not modify by hand." + LINE_SEPARATOR +
47+
" */" + LINE_SEPARATOR;
4848

4949
private final File outputDir;
50-
private final List<DataRow> libs;
50+
private final List<DataRow> libs;
5151
private final Map<String, JavaClass> classes;
5252
private final boolean suppressCallJSMethodExceptions;
5353

@@ -213,7 +213,7 @@ private String getNormalizedName(String filename) {
213213
return sb.toString();
214214
}
215215

216-
private Map<String, MethodGroup> getPublicApi(JavaClass clazz) throws ClassNotFoundException {
216+
private Map<String, MethodGroup> getPublicApi(JavaClass clazz, List<String> userImplementedInterfacesNames) throws ClassNotFoundException {
217217
Map<String, MethodGroup> api = new HashMap<String, MethodGroup>();
218218
JavaClass currentClass = clazz;
219219
String clazzName = clazz.getClassName();
@@ -225,7 +225,7 @@ private Map<String, MethodGroup> getPublicApi(JavaClass clazz) throws ClassNotFo
225225
methods.add(m);
226226
}
227227

228-
collectInterfaceMethods(clazz, methods);
228+
collectInterfaceMethods(clazz, userImplementedInterfacesNames, methods);
229229
for (Method m : methods) {
230230
if (!m.isSynthetic() && (m.isPublic() || m.isProtected()) && !m.isStatic()) {
231231
String name = m.getName();
@@ -303,7 +303,7 @@ private Map<String, JavaClass> readDir(String path, boolean throwOnError) throws
303303
while (!d.isEmpty()) {
304304
File cur = d.pollFirst();
305305
File[] files = cur.listFiles();
306-
for (File f: files) {
306+
for (File f : files) {
307307
if (f.isFile() && f.getName().endsWith(CLASS_EXT)) {
308308
ClassParser cp = new ClassParser(f.getAbsolutePath());
309309
JavaClass clazz = cp.parse();
@@ -338,9 +338,8 @@ private String getFullMethodSignature(Method m) {
338338

339339
private void writeBinding(Writer w, DataRow dataRow, JavaClass clazz, String packageName, String name) throws ClassNotFoundException {
340340
String[] implInterfaces = dataRow.getInterfaces();
341-
collectImplementedInterfaces(implInterfaces, clazz);
342341

343-
Map<String, MethodGroup> api = getPublicApi(clazz);
342+
Map<String, MethodGroup> api = getPublicApi(clazz, Arrays.asList(dataRow.getInterfaces()));
344343

345344
w.writeln("package " + packageName + ";");
346345
w.writeln();
@@ -404,7 +403,7 @@ private void writeBinding(Writer w, DataRow dataRow, JavaClass clazz, String pac
404403
}
405404
Set<Method> notImplementedObjectMethods = new HashSet<Method>();
406405
Method[] currentIfaceMethods = clazz.getMethods();
407-
ArrayList<Method> ifaceMethods = new ArrayList<Method>();
406+
Set<Method> ifaceMethods = new HashSet<>();
408407
for (Method m : currentIfaceMethods) {
409408
if (!m.getName().equals("<clinit>")) {
410409
ifaceMethods.add(m);
@@ -446,7 +445,7 @@ private void writeBinding(Writer w, DataRow dataRow, JavaClass clazz, String pac
446445
}
447446
} else {
448447
List<Method> interfaceMethods = new ArrayList<Method>();
449-
collectInterfaceMethods(clazz, interfaceMethods);
448+
collectInterfaceMethods(clazz, Arrays.asList(dataRow.getInterfaces()), interfaceMethods);
450449
for (String methodName : dataRow.getMethods()) {
451450
if (api.containsKey(methodName)) {
452451
List<Method> methodGroup = api.get(methodName).getList();
@@ -481,8 +480,8 @@ private void writeBinding(Writer w, DataRow dataRow, JavaClass clazz, String pac
481480
private boolean isClassApplication(JavaClass clazz) {
482481
String className = clazz.getClassName();
483482
return className.equals("android.app.Application") ||
484-
className.equals("android.support.multidex.MultiDexApplication") ||
485-
className.equals("android.test.mock.MockApplication");
483+
className.equals("android.support.multidex.MultiDexApplication") ||
484+
className.equals("android.test.mock.MockApplication");
486485
}
487486

488487
private void writeMethodBody(Method m, Writer w, boolean isApplicationClass, boolean isActivityClass, boolean isInterfaceMethod) {
@@ -643,7 +642,7 @@ private void writeMethodBody(Method m, boolean isConstructor, boolean isApplicat
643642
w.write("\t\t");
644643
Type ret = m.getReturnType();
645644

646-
if(this.suppressCallJSMethodExceptions) {
645+
if (this.suppressCallJSMethodExceptions) {
647646
w.writeln("try {");
648647
w.write("\t\t\t");
649648
}
@@ -659,7 +658,7 @@ private void writeMethodBody(Method m, boolean isConstructor, boolean isApplicat
659658
writeType(ret, w);
660659
w.writeln(".class, args);");
661660

662-
if(this.suppressCallJSMethodExceptions) {
661+
if (this.suppressCallJSMethodExceptions) {
663662
w.writeln("\t\t} catch (Throwable t) {");
664663
w.writeln("\t\t\tcom.tns.Runtime.passSuppressedExceptionToJs(t, \"" + m.getName() + "\");");
665664
w.writeln("\t\t\tandroid.util.Log.w(\"Warning\", \"NativeScript discarding uncaught JS exception!\");");
@@ -689,7 +688,7 @@ private void writeType(Type t, Writer w) {
689688
w.write(type);
690689
}
691690

692-
private void collectInterfaceMethods(JavaClass clazz, List<Method> methods) throws ClassNotFoundException {
691+
private void collectInterfaceMethods(JavaClass clazz, List<String> userImplementedInterfacesNames, List<Method> methods) throws ClassNotFoundException {
693692
JavaClass currentClass = clazz;
694693

695694
while (true) {
@@ -700,6 +699,12 @@ private void collectInterfaceMethods(JavaClass clazz, List<Method> methods) thro
700699
queue.add(name);
701700
}
702701

702+
for (String userImplementedInterfaceName : userImplementedInterfacesNames) {
703+
if (userImplementedInterfaceName != null && !userImplementedInterfaceName.equals("")) {
704+
queue.add(userImplementedInterfaceName);
705+
}
706+
}
707+
703708
while (!queue.isEmpty()) {
704709
String ifaceName = queue.poll();
705710
JavaClass currentInterface = getClass(ifaceName);
@@ -755,7 +760,7 @@ private boolean isActivityClass(JavaClass clazz, Map<String, JavaClass> classes)
755760
while (true) {
756761
String currentClassname = currentClass.getClassName();
757762

758-
for (String activityClassName: activityClassNames) {
763+
for (String activityClassName : activityClassNames) {
759764
isActivityClass = currentClassname.equals(activityClassName);
760765
if (isActivityClass) {
761766
return true;
@@ -826,7 +831,7 @@ private void cleanPreviouslyAutoGeneratedFiles(File folder) {
826831
// some javascript files containing native extends are removed,
827832
// their corresponding java classes will also be removed.
828833
File[] files = folder.listFiles();
829-
for (File file: files) {
834+
for (File file : files) {
830835
if (file.isDirectory()) {
831836
this.cleanPreviouslyAutoGeneratedFiles(file);
832837
} else if ("java".equalsIgnoreCase(this.getFileExtension(file.toString()))) {
@@ -869,4 +874,4 @@ private String readFile(File file) {
869874
}
870875
}
871876
}
872-
}
877+
}

0 commit comments

Comments
 (0)