Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can we test respondsToSelector: ? #5

Open
uchuugaka opened this issue Apr 2, 2018 · 7 comments
Open

How can we test respondsToSelector: ? #5

uchuugaka opened this issue Apr 2, 2018 · 7 comments

Comments

@uchuugaka
Copy link

First, I am happy to know this is still alive, as it is awesome!
Low level, but awesome.

My question is, what is the approach to test respondsToSelector: ?
I ask because the most common, and tragic mistake I have hit again and again is the stringly selector calling and missing a trailing colon on a selector that takes an argument.
In pure Objective-C this generally isn't a problem because Xcode corrects it most of the time (it is rare to have valid selectors with and without the colon because even Apple/NeXT learned that was troublesome) also because the exeption thrown will have the selector in the stack trace.
Arguably, even the Objective-C runtime could be more forthright about that, but it is also possible the object or its type are not what one expected.

So, I'd like to add this in for debugging (and potentially raise a Pull Request for making it a debug action to check for respondsToSelector: before each call.)

Any tips?

@uchuugaka
Copy link
Author

I think I got it, for anyone else's future reference, or correction if wrong.

I was trying to figure out which name of finishedLaunching BOOL property of an NSRunningApplication instance to use.
(of course it responds to both... ugh.)
But this syntax for calling respondsToSelector: seems to work.
In particular it seems one needs to import Pointer from jna

import com.sun.jna.Pointer;

// code that creates an NSRunningApplication instance named runningApp goes here...

String[] selectors = { "isFinishedLaunching", "finishedLaunching" };

for (String aSelector : selectors) {
  Pointer selPointer = ca.weblite.objc.Runtime.INSTANCE.sel_getUid(aSelector);
  System.out.println(String.format("runningApp respondsToSelector \"%s\" ? %b",
                                   aSelector,
                                   runningApp.send("respondsToSelector:", selPointer)));
}

@ind1go
Copy link

ind1go commented Apr 11, 2018

Have you had continued success with this method? I've been trying to work out the right selector I need (not an ObjC coder...) and I just get true from everything.

@uchuugaka
Copy link
Author

No.
I cannot get this to line up with my results in Xcode for the same class and methods.
I still do not know how to properly check respondsToSelector: with this bridge.

@LOG-TAG
Copy link

LOG-TAG commented Mar 19, 2019

@uchuugaka @shannah any blog post or tutorials available for a beginner developer to run this test code? Add ObjCBridge.jar and jna.jar to your classpath means macOS java classpath via terminal or via xcode?

@shannah
Copy link
Owner

shannah commented Mar 19, 2019

There is no need for XCode. ObjCBridge.jar and jna.jar go in your java classpath (e.g. in terminal, or in your IDE, however you build your java project). libjcocoa.dylib needs to be in your java library path.

@LOG-TAG
Copy link

LOG-TAG commented Mar 21, 2019

Thanks, I will stop bugging you here, are you available in any of the slackgroup for asking more beginner level questions? (email also works if you don't mind)

@mojo2012
Copy link
Collaborator

@uchuugaka
have you tried something like this:

var appClass = Runtime.INSTANCE.objc_getClass("NSRunningApplication");
var isFinishedSelPointer = Runtime.INSTANCE.sel_getUid("isFinishedLaunching");
var isFinishedRetPointer = Runtime.INSTANCE.class_respondsToSelector(appClass, isFinishedSelPointer);

It returns a Pointer native@0x1 (I guess this means true?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants