I try to build a Delphi bridge file to access an Android library written in Kotlin. The decompiled .jar has the following declarations:
public interface Function<R> {}
public interface Function2<P1, P2, R> extends Function<R> {
R invoke(P1 var1, P2 var2);
}
public final class Unit {
// snip
}
public interface Controller {
void getNearbyStops(@NotNull Function2<? super List<Stop>, ? super Error, Unit> var1);
}
The best result I can get with Java2OP is this Delphi declaration:
Jkotlin_FunctionClass = interface(IJavaClass)
['{8F273534-5758-45AB-BC0F-2851C32D3350}']
end;
[JavaSignature('kotlin/Function')]
Jkotlin_Function = interface(IJavaInstance)
['{8DFE502E-1AD9-4AD1-8940-BE903549AFC9}']
end;
TJkotlin_Function = class(TJavaGenericImport<Jkotlin_FunctionClass, Jkotlin_Function>) end;
JFunction2Class = interface(Jkotlin_FunctionClass)
['{630CDE10-8184-4A7C-9F6C-0F084112415D}']
end;
[JavaSignature('kotlin/jvm/functions/Function2')]
JFunction2 = interface(Jkotlin_Function)
['{8090FA02-E298-4606-8035-17A4FB3A4456}']
//function invoke(p1: J; p2: J): JObject; cdecl;
end;
TJFunction2 = class(TJavaGenericImport<JFunction2Class, JFunction2>) end;
JControllerClass = interface(IJavaClass)
['{66D15F2C-39DE-40EE-99F3-3A10A45A93D3}']
end;
[JavaSignature('net/company/bla/Controller')]
JController = interface(IJavaInstance)
['{20455085-2AD1-43F1-B9CD-455E8FB7EAD9}']
procedure getNearbyStops(function2: JFunction2); cdecl;
end;
TJController = class(TJavaGenericImport<JControllerClass, JController>) end;
TRegTypes.RegisterType('Android.JNI.MyBridge.Jkotlin_Function', TypeInfo(Android.JNI.MyBridge.Jkotlin_Function));
TRegTypes.RegisterType('Android.JNI.MyBridge.JFunction2', TypeInfo(Android.JNI.MyBridge.JFunction2));
TRegTypes.RegisterType('Android.JNI.MyBridge.JController', TypeInfo(Android.JNI.MyBridge.JController));
You see that it has generated Function2
, but without its generic parameters. The invoke
method is therefore commented.
Is it possible to modify the Delphi code to support the generic parameters?
Thanks for your help! Dominik
CodePudding user response:
The generic parameters are only in the Java source code.
The Java compiler ensures type safety at compile time and then discards the element type information. This is called type erasure.
This is standard behaviour, and your Delphi code may only use the Java code using the plain (non-generic) types.