How to do unboxing in Kotlin? - kotlin

I have a java method that takes a primitive long type.
I need to invoke the method using reflection, but when I pass my Long object to it I get java.lang.IllegalArgumentException.
I suspect this is due to the fact that the function wants a long primitive and not a Long object.
I'm new with Kotlin but I get that there is no unboxing in it.
I can't change the method.
How do I solve this?

Related

Find NSMutableArray<ObjectType> ObjectType at runtime

I have an NSMutableArray in which i want to add those objects which conforms the ObjectType. Is there any way i can get the ObjectType declared using light weight generics so when adding an object i can check whether the object is about to insert is ObjectType; if yes insert it else just forget it.
Thanks.
In Objective-C all type analysis is done at runtime and only at runtime. (The compiler gives warnings at compile time, but the produced code is bit-identical to code with any other object type.) The lightweight generics are for Swift. We didn't need that in the past 30 years. (Wow, this is really long in computer sciences.)
So, any code related to the mutable array with or without type specifier is identical. For Objective-C a static type information is the wrong thing. Even there would be a way to do that, it would be anticonceptual.
So: No.
Why do you want to do that? Don't do it.

Is there a way to define custom basic type in QML?

I need it for returning my custom point type with specialized methods by value, not pointer, from C++ Q_INVOKABLE method to QML. I cannot return pointer because new points are created many times during calculations and its must be destroyed just after using.

Searching Functions in Objective C

I have a strange task. I need to get an array that contains all the functions in an objective c object. I then need to be able to tell if each function is a class method or not. Then I need to get the names (preferably an NSString) of each parameter and the type each parameter takes. Is there a way to do this? If not, does anyone know how to access the keys and values coded in the NSCoding Protocol function -(void)encodeWithCoder:(NSCoder*)aCoder; without using NSKeyedArchiver? What I am trying to do here is display a list of properties required to initialize an object. All my objects use class methods to initialize themselves. I am making a level editor that allows me to edit properties that differ between objects and I don't feel like writing getPropertyList and initWithProperties functions for every single object since I have already done this by implementing the NSCoding protocol.
I need to get an array that contains all the functions in an objective c object. I then need to be able to tell if each function is a class method or not.
Easy enough: you want class_copyMethodList(), which gets you just the instance methods for that class. To get the class methods, pass the class object, e.g. class_copyMethodList(object_getClass([NSString class]), &count);
Then I need to get the names (preferably an NSString) of each parameter and the type each parameter takes.
The parameter name part is probably not possible. They're not included in the method's metadata, and I'm pretty sure that they don't survive compilation at all; digging them out of the executable if they're there would certainly not be easy.
The types, however, are easily accessible via one of two runtime functions: either method_getTypeEncoding(), which gets you the signature string for the method's return and arguments, or method_getArgumentType(), which will let you loop over the argument types (the returned strings use the same code as the full type string).
If not, does anyone know how to access the keys and values coded in the NSCoding Protocol function -(void)encodeWithCoder:(NSCoder*)aCoder without using NSKeyedArchiver?
Are you talking about the particular implementation that you've made for encodeWithCoder:? You want the list of ivars implied by [coder encodeObject:firstIvar forKey:#"firstIvar"]; [coder encodeObject:secondIvar forKey:#"secondIvar"];? I'm not sure what that has to do with method signatures, but if so, you could make an NSCoder subclass that creates a dictionary from when you pass it as the coder and send encodeWithCoder: to your objects (see this answer I posted the other day).
What I am trying to do here is display a list of properties required to initialize an object.
What about a class method that returns an array with the names of the properties?
+ (NSArray *)essentialPropertyNames {
return [NSArray arrayWithObjects:#"firstIvar", #"secondIvar", nil];
}
That would probably be less effort than picking through the runtime/class metadata and wouldn't be any less odd.
All my objects use class methods to initialize themselves.
That sounds unusual at best. In Cocoa, instances should use some form of -init to do their initialization.

Using Pointers safely in Objective-C

Lets say I have an object with an integer instance variables and 1 member function. The function runs on a separate thread, and consistently updates the value of the instance variable. I have a second function (part of a different class) that also runs on a separate thread, and needs real time access to the integer instance variable in the first object. Therefore, I pass in a pointer to the instance variable to the second function and the second function just dereferences the pointer. This way the second function always has access to the updated value of the instance variable.
However, I do not want the second function to be able to change the value of the instance variable. I want it to have read-only access, but since I am passing in a pointer to the instance variable, will it be able to change the value of the instance variable? If so, how do I restrict pointer dereferencing to read only? If this isn't possible, what would be the safest solution to this problem?
Mac OS X Snowleopard, Xcode 3.2.6. Objective-C with Cocoa.
EDIT: Sorry I forgot to mention that I can't make the instance variable constant, because I need the class it belongs to to be able to modify it. If I make it constant, it would completely restricting writing to the variable.
You can use the type system here. Instead of having something like int* you can have const int *, which means it's a pointer to a constant int. It's possible to get around this by casting back to (int *) inside the function, but this is a violation of the type system (hence the explicit cast) and well-behaved functions won't do that.
Note, you may also need to throw in volatile if your function needs to make sure it has the up-to-date value. Otherwise the compiler may decide it's ok to cache the results of the dereference somewhere.
The right way to do this is make the instance variable a property, and to give the second function a pointer to the object instead of a pointer to the instance variable. The code can then call the object's accessor for the property. It's not clear how your code is structured, but it's also possible to make the property read-only for method outside the class definition, and read-write for methods that are part of the class.
Finally, since you're accessing the property from more than one thread, you'll need to provide some sort of synchronization to avoid having both threads try to access the property at the same time. The simplest way to do that is to omit 'nonatomic' (which is how most people reflexively declare their properties) from the property declaration, which will cause the property accessors to be atomic. The second simplest way is to use the #synchronized directive.

Objective-C Find all init (constructor methods)

using the "Method * class_copyMethodList(Class cls, unsigned int *outCount)" function one can get a list of all methods that exist on an objective-C class.
I would like to know how to find which of these methods are constructors as I am writing an IOC container. I would like to determine the constructors and their parameter types.
I would like to know how to find which of these methods are
constructors as I am writing an IOC container. I would like to
determine the constructors and their parameter types.
In short, you can't. Or, at the least, you'll find that down this path lies madness.
First, Objective-C does not have constructors. It has initializers, sometimes many, and -- for a properly written class -- only one of which is the designated initializer. There is no way to identify the designated initializer at compile time or run time.
How do I use this with a Method * and no instantiated member of the
class?
You don't. First you allocate an instance of the class, then you initialize the instance.
Overall, this level of abstraction just isn't done in Objective-C outside of academic investigations. It can be done, but it is generally avoided because of the fragility of the resulting solution and the hairball of code-hell that is trying to dynamically support the underlying C ABI (go look at the source to libffi).
If you want to go down this path, then you are far better off either defining a custom abstract class that all of your containers will subclass that can provide the binding logic to the class behind it.
Or use protocols; i.e. a class could implement an IOCBean protocol and one method would be initIOCGoop that is the designated initializer goo.
Doing this generically for all classes is going to be rife with fragility, special cases, and will require a gigantic mess of code that will be difficult to maintain over time.
You can get the method signature by using the following method:
methodSignatureForSelector:
From the documentation:
An NSMethodSignature object records type information for the arguments and return value of a method. It is used to forward messages that the receiving object does not respond to—most notably in the case of distributed objects. You typically create an NSMethodSignature object using NSObject’s methodSignatureForSelector: instance method (on Mac OS X v10.5 and later you can also use signatureWithObjCTypes:). It is then used to create an NSInvocation object, which is passed as the argument to a forwardInvocation: message to send the invocation on to whatever other object can handle the message. In the default case, NSObject invokes doesNotRecognizeSelector:, which raises an exception. For distributed objects, the NSInvocation object is encoded using the information in the NSMethodSignature object and sent to the real object represented by the receiver of the message.