Class NSObject

java.lang.Object
ca.weblite.objc.Proxy
ca.weblite.objc.NSObject
All Implemented Interfaces:
Peerable, PeerableRecipient, Recipient

public class NSObject extends Proxy implements PeerableRecipient
The base class for objects that can interact with the Objective-C runtime. NSObjects are connected to both an Objective-C peer object, and an Objective-C parent object. The peer is a reflection of the object in Objective-C. It is a WLProxy object that will simply forward messages from Objective-C to Java.

The parent object is used as a sort of superclass so that messages that aren't explicitly handled by the Java class can be handled by the superclass.

Simple Example

The following example shows a subclass of NSObject that is used as a delegate for an NSOpenPanel. Notice, that, by using the @Msg annotation, the start() method is effectively called via Objective-C. Similarly, the panelSelectionDidChange() method is called by the NSOpenPanel class to respond to events when the user clicks on a different item in the open dialog.

If you run this application, it will open an NSOpenPanel modal dialog and allow you to select a file. If you run this program and select a single file, the output will look something like:

see NSOpenPanelSample
  • Field Details

    • parent

      public com.sun.jna.Pointer parent
      Pointer to the parent objective-c object of this object.
  • Constructor Details

    • NSObject

      public NSObject(String className)

      Constructor for NSObject.

      Parameters:
      className - a String object.
    • NSObject

      public NSObject()
      Creates null proxy (i.e. a proxy around a null pointer). In order to make this class functional and register it with the objective-c runtime, you still need to call one of the init() method variants.
    • NSObject

      public NSObject(com.sun.jna.Pointer peer)
      Creates an NSObject to wrap (i.e. send messages to) the specified Objective-C object. This doesn't actually register an object yet with the Objective-C runtime. You must still call init() to do this.
      Parameters:
      peer - a Pointer object.
    • NSObject

      public NSObject(Client c)
      Creates a null proxy using the specified client as the default client with which to send messages to the objective-c runtime.
      Parameters:
      c - The client that should be used to send messages in this object.
    • NSObject

      public NSObject(Client c, com.sun.jna.Pointer peer)
      Creates a proxy for the specified objective-c object.
      Parameters:
      c - The client that should be used for sending messages via this proxy.
      peer - The peer object.
  • Method Details

    • getMethodMap

      protected static Map<String,Method> getMethodMap(Class<?> cls)
      Returns the method map for a particular class. The Map that is returned maps string selectors to Method objects.
      Parameters:
      cls - The class whose map we wish to obtain
      Returns:
      The map that maps string selectors
    • init

      public NSObject init(com.sun.jna.Pointer parent)
      Initializes this object and registers it with the Objective-C runtime.
      Parameters:
      parent - a Pointer object.
      Returns:
      Self for chaining.
    • init

      public NSObject init(String cls)
      Initializes this object and registers it with the Objective-C runtime.
      Parameters:
      cls - The name of the class to use as the super class for this object.
      Returns:
      Self for chaining.
    • methodForSelector

      public Method methodForSelector(String selector)
      Returns the java method that responds to a specific selector for the current object.
      Parameters:
      selector - The
      Returns:
      The method object that handles the specified selector (or null if none is specified).
      See Also:
    • methodSignatureForSelector

      public com.sun.jna.Pointer methodSignatureForSelector(com.sun.jna.Pointer selector)
      Returns the NSMethodSignature (Objective-C) object pointer for the specified selector. If there is a Java method registered with this selector, then it will return its signature. Otherwise it will return the method signature of the parent object.
      Parameters:
      selector - a Pointer object.
      Returns:
      Pointer to an NSMethodSignature object.
      See Also:
    • methodSignatureForSelector

      public long methodSignatureForSelector(long lselector)
      Returns the method signature for a specified selector. Returns the NSMethodSignature (Objective-C) object pointer for the specified selector. If there is a Java method registered with this selector, then it will return its signature. Otherwise it will return the method signature of the parent object.
      Specified by:
      methodSignatureForSelector in interface Recipient
      Parameters:
      lselector - The pointer to the selector to check.
      Returns:
      Pointer to the NSMethodSignature object for the specified selector.
      See Also:
    • forwardInvocationToParent

      public void forwardInvocationToParent(com.sun.jna.Pointer invocation)
      Forwards an NSInvocation to the parent object to be handled. The parent will handle the invocation (if it contains an appropriate selector), but the peer will still be treated as the "Self" of the message. I.e. this acts exactly like calling super() in an OO language.
      Parameters:
      invocation - Pointer to the objective-c NSInvocation object.
      See Also:
    • forwardInvocationToParent

      public void forwardInvocationToParent(long linvocation)
      Forwards an NSInvocation to the parent object to be handled. The parent will handle the invocation (if it contains an appropriate selector), but the peer will still be treated as the "Self" of the message. I.e. this acts exactly like calling super() in an OO language.
      Parameters:
      linvocation - a long.
      See Also:
    • forwardInvocation

      public void forwardInvocation(com.sun.jna.Pointer invocation)
      Handles a method invocation. This will first check to see if there is a matching Java method in this class (method requires the @Msg annotation), and call that method if it is available. Otherwise it will obtain the method implementation from the parent class and execute it. The return value is added to the NSInvocation object. This method is used by the Native WLProxy to pipe all messages to this object's peer through Java so that it has a chance to process it.
      Parameters:
      invocation - NSInvocation Objective-C object that is to be invoked.
      See Also:
    • forwardInvocation

      public void forwardInvocation(long linvocation)
      Handles the invocation of a method on the recipient. Typically this should either be handled by a java method, or routed to some parent object that is being proxied. Handles a method invocation. This will first check to see if there is a matching Java method in this class (method requires the @Msg annotation), and call that method if it is available. Otherwise it will obtain the method implementation from the parent class and execute it. The return value is added to the NSInvocation object. This method is used by the Native WLProxy to pipe all messages to this object's peer through Java so that it has a chance to process it.
      Specified by:
      forwardInvocation in interface Recipient
      Parameters:
      linvocation - The NSInvocation object.
      Throws:
      NSMessageInvocationException - If an exception occurs while attempting to invoke the method.
      See Also:
    • respondsToSelector

      public boolean respondsToSelector(com.sun.jna.Pointer selector)
      Checks whether this object responds to the given selector. This is used by the WLProxy (Objective-C peer object) to route requests for its NSProxy respondsToSelector: message. This will check to see if there is a registered java method in the class that responds to the selector (based on the @Msg annotation). Then it will check the parent object to see if it responds to the selector.
      Parameters:
      selector - Pointer to the selector to check.
      Returns:
      a boolean.
      See Also:
    • respondsToSelector

      public boolean respondsToSelector(long lselector)
      Checks to see if this object responds to the specified selector. Checks whether this object responds to the given selector. This is used by the WLProxy (Objective-C peer object) to route requests for its NSProxy respondsToSelector: message. This will check to see if there is a registered java method in the class that responds to the selector (based on the @Msg annotation). Then it will check the parent object to see if it responds to the selector.
      Specified by:
      respondsToSelector in interface Recipient
      Parameters:
      lselector - a long.
      Returns:
      True if the object responds to the specified selector.
      See Also:
    • chain

      public NSObject chain(com.sun.jna.Pointer selector, Object... args)

      chain.

      Overrides:
      chain in class Proxy
      Parameters:
      selector - a Pointer object.
      args - a Object object.
      Returns:
      a Proxy object.
    • chain

      public NSObject chain(String selector, Object... args)

      chain.

      Overrides:
      chain in class Proxy
      Parameters:
      selector - a String object.
      args - a Object object.
      Returns:
      a Proxy object.
    • chain

      public NSObject chain(Message... msgs)

      chain.

      Overrides:
      chain in class Proxy
      Parameters:
      msgs - a Message object.
      Returns:
      a Proxy object.
    • dealloc

      public NSObject dealloc()

      dealloc.

      Returns:
      a NSObject object.