Skip to content
1 change: 1 addition & 0 deletions test-app/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ NativeScriptActivity.java
NativeScriptApplication.java
# named extend() classes the static binding generator writes for the specs
app/src/main/java/com/tns/tests/defaults/
app/src/main/java/com/tns/tests/extras/
**/com/tns/gen
1 change: 1 addition & 0 deletions test-app/app/src/main/assets/app/mainpage.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ require("./tests/testWorkerNestedConsole");
require("./tests/testMessaging");
require("./tests/testInterfaceDefaultMethods");
require("./tests/testInterfaceDefaultMethodOverrides");
require("./tests/testInterfaceExtraInterfaces");
require("./tests/testInterfaceStaticMethods");
require("./tests/testMetadata");
require("./tests/testAsserts");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Entry for testInterfaceExtraInterfaces: an interface extend with an extra interface, made and
// constructed in a worker's runtime.
var X = com.tns.tests.interfaces.extras;
var F = X.ExtrasFixtures;
var result = {};

try {
var B = X.WorkerBase;
var C = B.extend({
interfaces: [X.Extra],
base: function () { return "worker base"; },
extra: function () { return "worker extra"; }
});
var instance = new C();
result.className = String(F.className(instance));
result.isExtra = F.isInstance(instance, "com.tns.tests.interfaces.extras.Extra");
result.extra = String(F.call(instance, "com.tns.tests.interfaces.extras.Extra", "extra"));
} catch (e) {
result.error = String(e && e.message || e);
}
postMessage(result);
438 changes: 438 additions & 0 deletions test-app/app/src/main/assets/app/tests/testInterfaceExtraInterfaces.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.tns.tests.interfaces.extras;

// Listed in an extend's `interfaces` although it is a class.
public abstract class AbstractExtra {
public abstract String abstractExtra();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.tns.tests.interfaces.extras;

public final class Bases {
private Bases() {
}

public interface Nested {
String base();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.tns.tests.interfaces.extras;

public interface CacheBase {
String base();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.tns.tests.interfaces.extras;

// A class listed in an extend's `interfaces` that implements the base's default method.
public final class ClassListed {
private ClassListed() {
}

public interface Base {
default String value() {
return "java";
}
}

public abstract static class Implementor implements Base {
@Override
public String value() {
return "class";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.tns.tests.interfaces.extras;

// shared() conflicts with ConflictExtra's: a class implementing both has to override it.
public interface ConflictBase {
String base();

default String shared() {
return "base";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.tns.tests.interfaces.extras;

public interface ConflictExtra {
default String shared() {
return "extra";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.tns.tests.interfaces.extras;

// Unrelated interfaces declaring the same method with different return types: a class
// implementing two of them is called through a different method descriptor for each.
public final class Covariant {
private Covariant() {
}

public interface NumberBase {
default Number value() {
return 1;
}
}

public interface NamedNumberBase {
default Number value() {
return 1;
}
}

public interface IntegerExtra {
default Integer value() {
return 2;
}
}

public interface AbstractNumberBase {
Number amount();
}

public interface AbstractIntegerExtra {
Integer amount();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.tns.tests.interfaces.extras;

public interface DefaultsBase {
String base();

default String baseDefault() {
return "java";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.tns.tests.interfaces.extras;

public interface DuplicateBase {
String base();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.tns.tests.interfaces.extras;

public interface Extra {
String extra();

default String extraDefault() {
return "java";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.tns.tests.interfaces.extras;

public interface ExtrasFirstBase {
String base();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.tns.tests.interfaces.extras;

import android.content.Context;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

// Java callers for testInterfaceExtraInterfaces.js. Every method but runExtra takes Object, so
// an instance that lacks an interface reaches Java and is reported instead of crossing JNI
// as an argument of a type it does not have.
public final class ExtrasFixtures {
private ExtrasFixtures() {
}

public static String className(Object instance) {
return instance.getClass().getName();
}

public static boolean isInstance(Object instance, String interfaceName) throws ClassNotFoundException {
return Class.forName(interfaceName).isInstance(instance);
}

// The interfaces the instance's class declares, sorted, without the runtime's own.
public static String interfaceNames(Object instance) {
List<String> names = new ArrayList<String>();
for (Class<?> iface : instance.getClass().getInterfaces()) {
if (!iface.getName().startsWith("com.tns.NativeScript")) {
names.add(iface.getName());
}
}
Collections.sort(names);
StringBuilder joined = new StringBuilder();
for (String name : names) {
if (joined.length() > 0) {
joined.append(',');
}
joined.append(name);
}
return joined.toString();
}

// Calls the interface's no-argument method through the interface, as a Java caller holding
// the instance as that type would.
public static String call(Object instance, String interfaceName, String methodName) {
try {
Class<?> iface = Class.forName(interfaceName);
if (!iface.isInstance(instance)) {
return "not a " + interfaceName;
}
return String.valueOf(iface.getMethod(methodName).invoke(instance));
} catch (java.lang.reflect.InvocationTargetException e) {
return e.getCause().getClass().getName();
} catch (Exception e) {
return e.getClass().getName();
}
}

public static String runExtra(Extra extra) {
return extra.extra();
}

// Below minSdk 24, D8 turns this module's default methods into abstract ones, whose
// bodies a class generated on the device cannot reach. Method.isDefault needs API 24.
public static boolean areDefaultsDesugared() throws NoSuchMethodException {
return !Extra.class.getMethod("extraDefault").isDefault();
}

public static boolean isRuntimeGenerated(Object instance) {
try {
instance.getClass().getDeclaredField("__initialized");
return true;
} catch (NoSuchFieldException e) {
return false;
}
}

// The runtime's cached dex files whose names start with prefix.
public static String[] cachedDexFiles(String prefix) throws Exception {
Context context = (Context) Class.forName("android.app.ActivityThread").getMethod("currentApplication").invoke(null);
List<String> found = new ArrayList<String>();
File[] dirs = {
new File(context.getApplicationInfo().dataDir, "code_cache/secondary-dexes"),
new File(context.getFilesDir(), "secondary-dexes")
};
for (File dir : dirs) {
String[] names = dir.list();
if (names == null) {
continue;
}
for (String name : names) {
if (name.startsWith(prefix) && name.endsWith(".dex")) {
found.add(name);
}
}
}
Collections.sort(found);
return found.toArray(new String[0]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.tns.tests.interfaces.extras;

public interface MainBase {
String base();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.tns.tests.interfaces.extras;

public interface NamedBase {
String base();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.tns.tests.interfaces.extras;

public interface NewWithInterfacesBase {
String base();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.tns.tests.interfaces.extras;

public interface NotAnInterfaceBase {
String base();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.tns.tests.interfaces.extras;

public interface OtherExtra {
String other();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.tns.tests.interfaces.extras;

public interface PlainFirstBase {
String base();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.tns.tests.interfaces.extras;

public interface TwoSitesBase {
String base();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.tns.tests.interfaces.extras;

public interface TypeScriptBase {
String base();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.tns.tests.interfaces.extras;

public interface WorkerBase {
String base();
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,15 @@ public Binding generateBinding(DataRow dataRow, HashSet<String> interfaceNames)

String name = "";
Boolean isInterface = clazz.isInterface();
// An anonymous implementation of an interface that lists interfaces of its own is its
// call site's class, named like a class extend's, as the runtime asks for it; the
// interface's one shared class cannot implement them for a single call site.
boolean isSharedInterfaceClass = isInterface && !hasSpecifiedName && !hasImplementedInterfaces(dataRow);

if (hasSpecifiedName) {
name = getSimpleClassname(dataRow.getFilename());
} else {
if (isInterface) {
if (isSharedInterfaceClass) {
name = getSimpleClassname(clazz.getClassName());
} else {
name = getSimpleClassname(clazz.getClassName().replace("$", "_")) + "_";
Expand All @@ -162,9 +166,10 @@ public Binding generateBinding(DataRow dataRow, HashSet<String> interfaceNames)
}
}

if (isInterface && interfaceNames.contains(name)) {
boolean isOneClassPerInterface = isInterface && (hasSpecifiedName || isSharedInterfaceClass);
if (isOneClassPerInterface && interfaceNames.contains(name)) {
return null;
} else if (isInterface) {
} else if (isOneClassPerInterface) {
interfaceNames.add(name);
}

Expand Down Expand Up @@ -578,6 +583,18 @@ private boolean areAllArgumentsAndReturnTypePublic(ReifiedJavaMethod method) {
return true;
}

private static boolean hasImplementedInterfaces(DataRow dataRow) {
if (dataRow.getInterfaces() == null) {
return false;
}
for (String interfaceName : dataRow.getInterfaces()) {
if (interfaceName != null && !interfaceName.isEmpty()) {
return true;
}
}
return false;
}

private List<JavaClass> getInterfacesFromCache(List<String> interfacesNames) {
List<JavaClass> interfaces = new ArrayList<>(interfacesNames.size());

Expand Down
Loading
Loading