Search code examples
androidtypescriptnativescript.class-file

Send Java Class as function argument


I want to pass a Java class as argument to a Java method. The method is getKeySpec() of class java.security.KeyFactory:

getKeySpec(Key key, Class<T> keySpec)

The NativeScript Typings for this method is

public getKeySpec(param0: java.security.Key, param1: java.lang.Class<any>): java.security.spec.KeySpec;

How do I pass a java.lang.Class<any> to this method? I want to pass java.security.spec.X509EncodedKeySpec.

This is my current TypeScript code, which fails at the getKeySpec() call.

function getPublicKey(keyPair: java.security.KeyPair): string {
    const kf = java.security.KeyFactory.getInstance("RSA");
    let pubKeySpec = kf.getKeySpec(keyPair.getPublic(), java.security.spec.X509EncodedKeySpec);
    return pubKeySpec.getEncoded();
}

Solution

  • Like

    let pubKeySpec = kf.getKeySpec(keyPair.getPublic(), X509EncodedKeySpec.class);