1

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();
}
3
  • "which fails" can you be more specific about that part? you can't send a "class", you can send either an instance of Class, or an instance of a class. Java has methods, not functions. Then again, it seems this is not java code, so why do you tag it as being Java? Commented Sep 21, 2018 at 10:56
  • The typescript compiler complains with the following: [ts] Argument of type 'typeof X509EncodedKeySpec' is not assignable to parameter of type 'Class<any>'. Commented Sep 21, 2018 at 10:58
  • Aha, seems to be valid according to typescript (which does not complain any more). I will test it later today. Commented Sep 21, 2018 at 11:00

1 Answer 1

2

Like

let pubKeySpec = kf.getKeySpec(keyPair.getPublic(), X509EncodedKeySpec.class);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.