Skip to content

Classes can implement multiple interfaces #501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 45 additions & 23 deletions binding-generator/src/main/java/com/tns/bindings/Dump.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,34 +199,36 @@ public void generateProxy(ApplicationWriter aw, String proxyName, ClassDescripto
String methodOverride = methodOverrides[i];
methodOverridesSet.add(methodOverride);
}
generateProxy(aw, proxyName, classTo, methodOverridesSet);
}

public void generateProxy(ApplicationWriter aw, ClassDescriptor classTo, String[] methodOverrides, int ignored)
{
HashSet<String> methodOverridesSet = new HashSet<String>();
generateProxy(aw, proxyName, classTo, methodOverridesSet, null);
}

public void generateProxy(ApplicationWriter aw, ClassDescriptor classTo, String[] methodOverrides, int ignored)
{
HashSet<String> methodOverridesSet = new HashSet<String>();

for (int i = 0; i < methodOverrides.length; i++)
{
String methodOverride = methodOverrides[i];
methodOverridesSet.add(methodOverride);
}
generateProxy(aw, "0", classTo, methodOverridesSet);

generateProxy(aw, "0", classTo, methodOverridesSet, null);
}

public void generateProxy(ApplicationWriter aw, String proxyName, ClassDescriptor classTo)
{
generateProxy(aw, proxyName, classTo, null);
generateProxy(aw, proxyName, classTo, null, null);
}

public void generateProxy(ApplicationWriter aw, ClassDescriptor classTo)
{
generateProxy(aw, "0", classTo, null);
}
generateProxy(aw, "0", classTo, null, null);
}

public void generateProxy(ApplicationWriter aw, String proxyName, ClassDescriptor classTo, HashSet<String> methodOverrides)
public void generateProxy(ApplicationWriter aw, String proxyName, ClassDescriptor classTo, HashSet<String> methodOverrides, HashSet<ClassDescriptor> implementedInterfaces)
{
String classSignature = getAsmDescriptor(classTo);
//String methodSignature = org.objectweb.asm.Type.getMethodDescriptor(Object.class.getMethods()[0]);

String tnsClassSignature = LCOM_TNS +
classSignature.substring(1, classSignature.length() - 1).replace("$", "_");
Expand All @@ -237,8 +239,8 @@ public void generateProxy(ApplicationWriter aw, String proxyName, ClassDescripto

tnsClassSignature += ";";

ClassVisitor cv = generateClass(aw, classTo, classSignature, tnsClassSignature);
MethodDescriptor[] methods = getSupportedMethods(classTo, methodOverrides);
ClassVisitor cv = generateClass(aw, classTo, classSignature, tnsClassSignature, implementedInterfaces);
MethodDescriptor[] methods = getSupportedMethods(classTo, methodOverrides, implementedInterfaces);

methods = groupMethodsByNameAndSignature(methods);

Expand Down Expand Up @@ -332,12 +334,17 @@ private void collectInterfaceMethods(ClassDescriptor clazz, HashSet<String> meth
}
}

private MethodDescriptor[] getSupportedMethods(ClassDescriptor clazz, HashSet<String> methodOverrides)
private MethodDescriptor[] getSupportedMethods(ClassDescriptor clazz, HashSet<String> methodOverrides, HashSet<ClassDescriptor> interfacesToImplement)
{
ArrayList<MethodDescriptor> result = new ArrayList<MethodDescriptor>();

collectInterfaceMethods(clazz, methodOverrides, result);

for (ClassDescriptor iface : interfacesToImplement)
{
collectInterfaceMethods(iface, methodOverrides, result);
}

if (!clazz.isInterface())
{
HashMap<String, MethodDescriptor> finalMethods = new HashMap<String, MethodDescriptor>();
Expand Down Expand Up @@ -601,13 +608,13 @@ private void generateHashCodeSuper(ClassVisitor cv)

private void generateMethod(ClassVisitor cv, ClassDescriptor classTo, MethodDescriptor method, int methodNumber, String classSignature, String tnsClassSignature, int fieldBit)
{
if (ProxyGenerator.IsLogEnabled) Log.d("Generator", "generatingMethod " + method.getName());
if (ProxyGenerator.IsLogEnabled) {
Log.d("Generator", "generatingMethod " + method.getName());
}

//TODO: handle checked exceptions
String methodDexSignature = getDexMethodDescriptor(method);
String[] exceptions = new String[0];


MethodVisitor mv;
int methodModifiers = getDexModifiers(method);

Expand All @@ -620,6 +627,7 @@ private void generateMethod(ClassVisitor cv, ClassDescriptor classTo, MethodDesc
{
generateInitializedBlock(mv, thisRegister, classSignature, tnsClassSignature);
}

generateCallOverrideBlock(mv, method, thisRegister, classSignature, tnsClassSignature, methodDexSignature, fieldBit);

mv.visitEnd();
Expand Down Expand Up @@ -919,25 +927,39 @@ private void generateInitializedField(ClassVisitor cv)

static final String[] classImplentedInterfaces = new String[] { "Lcom/tns/NativeScriptHashCodeProvider;" };
static final String[] interfaceImplementedInterfaces = new String[] { "Lcom/tns/NativeScriptHashCodeProvider;", "" };
private ClassVisitor generateClass(ApplicationWriter aw, ClassDescriptor classTo, String classSignature, String tnsClassSignature)

private ClassVisitor generateClass(ApplicationWriter aw, ClassDescriptor classTo, String classSignature, String tnsClassSignature, HashSet<ClassDescriptor> implementedInterfaces)
{
ClassVisitor cv;

int classModifiers = getDexModifiers(classTo);
String[] implentedInterfaces = classImplentedInterfaces;
ArrayList<String> interfacesToImplement = new ArrayList(Arrays.asList(classImplentedInterfaces));

if (classTo.isInterface())
{
interfaceImplementedInterfaces[1] = classSignature; //new String[] { "Lcom/tns/NativeScriptHashCodeProvider;", classSignature };
implentedInterfaces = interfaceImplementedInterfaces;
for(String interfaceToImpl : interfaceImplementedInterfaces) {
if(!interfacesToImplement.contains(interfaceToImpl)) {
interfacesToImplement.add(interfaceToImpl);
}
}

classSignature = objectClass;
}
else
{
implentedInterfaces = classImplentedInterfaces;
if(implementedInterfaces != null) {
for(ClassDescriptor interfaceToImpl : implementedInterfaces) {
interfacesToImplement.add(getAsmDescriptor(interfaceToImpl));
}
}
}

cv = aw.visitClass(classModifiers, tnsClassSignature, null, classSignature, implentedInterfaces);
cv.visit(0, classModifiers, tnsClassSignature, null, classSignature, implentedInterfaces);
String[] interfacesToImplementArr = new String[interfacesToImplement.size()];
interfacesToImplementArr = interfacesToImplement.toArray(interfacesToImplementArr);

cv = aw.visitClass(classModifiers, tnsClassSignature, null, classSignature, interfacesToImplementArr);
cv.visit(0, classModifiers, tnsClassSignature, null, classSignature, interfacesToImplementArr);
cv.visitSource(classTo.getName() + ".java", null);
return cv;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,16 @@ public String generateProxy(String proxyName, ClassDescriptor classToProxy, Stri
methodOverridesSet.add(methodOverride);
}
}
return generateProxy(proxyName, classToProxy, methodOverridesSet, isInterface);

return generateProxy(proxyName, classToProxy, methodOverridesSet, null, isInterface);
}

public String generateProxy(String proxyName, ClassDescriptor classToProxy, HashSet<String> methodOverrides, boolean isInterface) throws IOException
public String generateProxy(String proxyName, ClassDescriptor classToProxy, HashSet<String> methodOverrides, HashSet<ClassDescriptor> implementedInterfaces, boolean isInterface) throws IOException
{
ApplicationWriter aw = new ApplicationWriter();
aw.visit();

dump.generateProxy(aw, proxyName, classToProxy, methodOverrides);
dump.generateProxy(aw, proxyName, classToProxy, methodOverrides, implementedInterfaces);

aw.visitEnd();
byte[] generatedBytes = aw.toByteArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@

__extends_ts(Child, Parent);


if (Parent.extend) {
Child.__isPrototypeImplementationObject = true;
Child.__proto__ = Parent;
Expand All @@ -105,9 +106,19 @@
return extended;
};
}

function Interfaces(interfacesArr) {
return function (target) {
if(interfacesArr instanceof Array) {
// attach interfaces: [] to the object
target.prototype.interfaces = interfacesArr;
}
}
}

global.__native = __native;
global.__extends = __extends;
global.__decorate = __decorate;
global.JavaProxy = JavaProxy;
global.Interfaces = Interfaces;
})()
11 changes: 6 additions & 5 deletions runtime/src/main/java/com/tns/ClassResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public ClassResolver(Runtime runtime)
this.runtime = runtime;
}

public Class<?> resolveClass(String fullClassName, DexFactory dexFactory, String[] methodOverrides, boolean isInterface) throws ClassNotFoundException, IOException
public Class<?> resolveClass(String fullClassName, DexFactory dexFactory, String[] methodOverrides, String[] implementedInterfaces, boolean isInterface) throws ClassNotFoundException, IOException
{
String cannonicalClassName = fullClassName.replace('/', '.');
String name = null;
Expand All @@ -33,15 +33,16 @@ public Class<?> resolveClass(String fullClassName, DexFactory dexFactory, String
if (name == null || name == "")
{
if(isInterface)
{
{
name = "";
} else
{
}
else
{
name = "0";
}
}

clazz = dexFactory.resolveClass(name, className, methodOverrides, isInterface);
clazz = dexFactory.resolveClass(name, className, methodOverrides, implementedInterfaces, isInterface);
}

if (clazz == null)
Expand Down
22 changes: 18 additions & 4 deletions runtime/src/main/java/com/tns/DexFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.zip.ZipOutputStream;

import com.tns.bindings.ProxyGenerator;
import com.tns.bindings.desc.ClassDescriptor;
import com.tns.bindings.desc.reflection.ClassInfo;

import dalvik.system.DexClassLoader;
Expand Down Expand Up @@ -63,7 +64,7 @@ public DexFactory(Logger logger, ClassLoader classLoader, File dexBaseDir, Strin
static long totalMultiDexTime = 0;
static long totalLoadDexTime = 0;

public Class<?> resolveClass(String name, String className, String[] methodOverrides, boolean isInterface) throws ClassNotFoundException, IOException
public Class<?> resolveClass(String name, String className, String[] methodOverrides, String[] implementedInterfaces, boolean isInterface) throws ClassNotFoundException, IOException
{
String fullClassName = className.replace("$", "_");

Expand Down Expand Up @@ -115,7 +116,7 @@ public Class<?> resolveClass(String name, String className, String[] methodOverr
logger.write("generating proxy in place");
}

dexFilePath = this.generateDex(name, classToProxy, methodOverrides, isInterface);
dexFilePath = this.generateDex(name, classToProxy, methodOverrides, implementedInterfaces, isInterface);
dexFile = new File(dexFilePath);
long stopGenTime = System.nanoTime();
totalGenTime += stopGenTime - startGenTime;
Expand Down Expand Up @@ -187,6 +188,7 @@ public Class<?> findClass(String className) throws ClassNotFoundException
{
return existingClass;
}

return classLoader.loadClass(canonicalName);
}

Expand Down Expand Up @@ -250,25 +252,37 @@ private File getDexFile(String className) throws InvalidClassException
{
logger.write("Looking for proxy file: " + dexFilePath + " Result: NOT Found. Proxy Gen needed. ClassName: " + className);
}

return null;
}

private String generateDex(String proxyName, String className, String[] methodOverrides, boolean isInterface) throws ClassNotFoundException, IOException
private String generateDex(String proxyName, String className, String[] methodOverrides, String[] implementedInterfaces, boolean isInterface) throws ClassNotFoundException, IOException
{
Class<?> classToProxy = Class.forName(className);

HashSet<String> methodOverridesSet = null;
HashSet<ClassDescriptor> implementedInterfacesSet = new HashSet<ClassDescriptor>();

if (methodOverrides != null)
{
methodOverridesSet = new HashSet<String>();
for (int i = 0; i < methodOverrides.length; i++)
{
String methodOverride = methodOverrides[i];

methodOverridesSet.add(methodOverride);
}
}

return proxyGenerator.generateProxy(proxyName, new ClassInfo(classToProxy) , methodOverridesSet, isInterface);
if (implementedInterfaces.length > 0) {
for(int j = 0; j < implementedInterfaces.length; j++) {
if(!implementedInterfaces[j].isEmpty()) {
implementedInterfacesSet.add(new ClassInfo(Class.forName(implementedInterfaces[j])));
}
}
}

return proxyGenerator.generateProxy(proxyName, new ClassInfo(classToProxy) , methodOverridesSet, implementedInterfacesSet, isInterface);
}

private void updateDexThumbAndPurgeCache()
Expand Down
5 changes: 3 additions & 2 deletions runtime/src/main/java/com/tns/Runtime.java
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,11 @@ public void run()
return result;
}

// TODO: Pete: new param - implementedInterfaces
@RuntimeCallable
private Class<?> resolveClass(String fullClassName, String[] methodOverrides, boolean isInterface) throws ClassNotFoundException, IOException
private Class<?> resolveClass(String fullClassName, String[] methodOverrides, String[] implementedInterfaces, boolean isInterface) throws ClassNotFoundException, IOException
{
Class<?> javaClass = classResolver.resolveClass(fullClassName, dexFactory, methodOverrides, isInterface);
Class<?> javaClass = classResolver.resolveClass(fullClassName, dexFactory, methodOverrides, implementedInterfaces, isInterface);

return javaClass;
}
Expand Down
Loading