简体   繁体   English

具有泛型参数的 Java2OP 接口

[英]Java2OP interface with generic parameters

I try to build a Delphi bridge file to access an Android library written in Kotlin.我尝试构建一个 Delphi 桥文件来访问用 Kotlin 编写的 Android 库。 The decompiled.jar has the following declarations:反编译后的.jar有如下声明:

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:我能用 Java2OP 得到的最好结果是这个 Delphi 声明:

  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.您会看到它生成了Function2 ,但没有其泛型参数。 The invoke method is therefore commented.因此, invoke方法被注释掉了。

Is it possible to modify the Delphi code to support the generic parameters?是否可以修改 Delphi 代码以支持泛型参数?

Thanks for your help Dominik感谢您的帮助多米尼克

The generic parameters are only in the Java source code.泛型参数仅在 Java 源代码中。

The Java compiler ensures type safety at compile time and then discards the element type information. Java 编译器在编译时确保类型安全,然后丢弃元素类型信息。 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.这是标准行为,您的 Delphi 代码只能使用普通(非通用)类型的 Java 代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM