Send Java Class as function argument - android

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();
}

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

Related

How to get an Interface in Kotlin from Android SDK with reflection and use it?

I need to use an interface from the Android Camera class that is not exported in the SDK. It is setup like this
//interface defined as
public interface Camera$CameraMetaDataCallback {
void onCameraMetaData(byte[] data, Camera camera);
};
//set callback defined
class Camera {
...
public final void setMetadataCb(CameraMetaDataCallback cb)
{
mCameraMetaDataCallback = cb;
native_setMetadataCb(cb!=null);
}
...
}
I can get the interface class I am assuming with
val CameraMetaDataCallback = Class.forName("Camera\$CameraMetaDataCallback").kotlin
and the method with
val CameraSetMetadataCb = Camera::class.java.getMethod("setMetadataCb", CameraMetaDataCallback.java)
but how do I actually use the interface object and set a callback?
You'd use cameraSetMetadataCb.invloke(objectToCallItOn, param1, param2, param3, ...) That calls the method referred to by the cameraSetMetadataCb object using objectToCallOn as the this pointer and passes the given parameters. But as I mentioned in comments above- you really don't want to do this, unless you have total control over the hardware it runs on. Any change, even a minor patch, to the OS can change this function's signature or remove it entirely and break your app.

How to mock static java method from kotlin , mockkStatic not working

I want to mock static method of java class from Kotlin test case.
I am using below code that does not work.
It always called actual method.
mockkStatic(Aes::class)
every { Aes.decrypt(PASSWORD, SECRET_KEY) } returns PASSWORD
Actual method in java class:
public static String decrypt(String text, String secretKey) {}
The good strategy for this is to use wrapper objects around static methods if there is no other way around (for example static method belongs to 3rd party library)
class AESWrapper {
fun decrypt(String text, String secretKey) {
return Aes.decrypt(text, secretKey)
}
}
There are other solutions like PowerMock, but then you need to use PowerMockRunner as I remember which can limit you in the future

How to replace method reference(::) in java to normal constructor call?

Earlier I have model class in java which uses autovalue. Now, it's converted to Kotlin data class.
Model Class -->
public static SampleClass create(
#NonNull final SamplePost post,
#NonNull final List<SampleComment> comments) {
return new AutoValue_SampleClass(post, comments);
}
Calling Class -->
return Observable.zip(...
SampleClass::create);
}
new data class -->
data class SampleClass(val post: DiscussionPost,
val comments: List<SampleComment>) : Parcelable
Now how to call it for data class?
U can use SampleClass::new to call the constructor.
If I understand correctly, you don't need a constructor call, but a constructor reference. The syntax for it is ::SampleClass. But this may not trigger SAM conversion, in which case you'll need
Observable.zip(...,
{ post, comments -> SampleClass(post, comments) })
EDIT: The above assumes that zip is called from Kotlin, if you want to call it from Java, see #ebasha's answer.

Haxe: How can I setup a class to store data in an array of type that I pass in the constructor

I am coding in Haxe, Lime, OpenFl. I am trying to set up a Class to store data in a Map, referenced by class instance. The class type is to be passed in the constructor, via inference. But I am quite new to all this and can't quite figure out the syntax, this is what I got so far:
class DynamicStore<A>
{
private var hashA:Map<Class<A>,String>;
public function new<A>(paramA:Class<A>) {
hashA = new Map();
}
}
But this gives me the following error:
Abstract Map has no #:to function that accepts IMap<Class<DynamicStore.A>, String>
Is there a way to do this?
A question first:
do you really want to use classes as key? or objects?
In classes should be the key
It would be much simpler to use the classe's full name as key, like "mypackage.blob.MyClass". It's safer, easier to handle and debug.
Map<String, String>
Would suffice in that case.
If objects should be keys
Then the code would look like:
import haxe.ds.ObjectMap;
class Test<A>
{
static function main() {}
private var hashA :ObjectMap<A,String>;
public function new(paramA:A) {
hashA = new ObjectMap<A,String>();
}
}
The reason "Map" cannot be directly used in this case is that "Map" is a syntactic sugar, being resolved to StringMap, IntMap or others depending on the key type. If it doesn't know what kind of map to be used, it cannot proceed (this is mainly due to cross-compiling issues).
Remark
As a final note, I would mention your construction seems a bit wacky/strange to me. It would be interesting to know what you are trying to achieve and why you structure it the way you do.
I don't think you can use Class as the key of a Map. A good work around it to use a String as a key and the fully qualified names of the types. You can also define an abstract to move from the Type to String easily ... something like the following (code not-tested);
private var hashA : Map<String, String>;
public function addClass(className : ClassId, ...)
And the abstract will look something like this:
abstract ClassId(String) {
inline public function new(name : String) this = name;
#:from public static inline function fromClass(cls : Class<Dynamic>)
return new ClassId(Type.getClassName(cls));
#:to public inline function toClass() : Class<Dynamic>
return Type.resolveClass(this);
#:to public inline function toString() : String
return this;
}

Get Class object from another Class JNI

Java code:
public class ParentClass
{
class ChildClass
{
public String strUrl;
/**
* Standard Constructor.
*/
public ChildClass( )
{
strUrl = "";
{
}
// Some code goes here ....
}
How you can see I have ParentClass and ChildClass in it. Now from my JNI code I want to get ChildClass and call it's constructor. In JNI I have ParentClass object.
What must I do to get ChildClass object from ParentClass and call ChildClass functions or set members?
In oracle java the syntax will be
env->FindClass("ParentClass$ChildClass");
This may works also for android.
Plus constructor of inner class have additional parameter, reference to outer class.

Categories

Resources