How do I know if a private superclass method is overridden in Objective-C?

Since private methods are not private in Objective-C (the method dispatch mechanism does not distinguish between private v / s public methods), it is very easy for a subclass to override private superclass methods (more on this Cocoa Coding Guide ).

However, discovering this is not so simple. I am looking for a ready-made tool that can help me with this.

It is not just a static analysis tool because private methods are not displayed in the interface. This tool will either have to examine the binaries, or figure it out at runtime. For me, this tool will have to do something similar to the following:

  • Get a list of all public methods for each class used by the project by examining the headers, then either step 2 or step 3

  • Get overridden private methods by examining binary files

    a) Read the binary application + all the dynamic libraries that it refers to for a list of all the methods defined by each class used by the application.

    b) Of these, methods not found in step 1 are private methods.

    c) Go through the class hierarchy and define subclasses that override the private methods of superclasses

  • Get overridden private methods at runtime

    a) Launch the application

    b) Using the Objective-C runtime methods, we can get all the methods defined for all classes.

    c) Again, methods not found in step 1 are private methods.

    d) ,

- / , ( , NSManagedObject, Core Data). , , .

, , libclang, , otool nm, Objective-C runtime, , ?

. ; .

+4
1

, Objective-C .

. objc/runtime.h, :

/** 
 * Describes the instance methods implemented by a class.
 * 
 * @param cls The class you want to inspect.
 * @param outCount On return, contains the length of the returned array. 
 *  If outCount is NULL, the length is not returned.
 * 
 * @return An array of pointers of type Method describing the instance methods 
 *  implemented by the class—any instance methods implemented by superclasses are not included. 
 *  The array contains *outCount pointers followed by a NULL terminator. You must free the array with free().
 * 
 *  If cls implements no instance methods, or cls is Nil, returns NULL and *outCount is 0.
 * 
 * @note To get the class methods of a class, use \c class_copyMethodList(object_getClass(cls), &count).
 * @note To get the implementations of methods that may be implemented by superclasses, 
 *  use \c class_getInstanceMethod or \c class_getClassMethod.
 */
OBJC_EXPORT Method *class_copyMethodList(Class cls, unsigned int *outCount) 
     __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
+1

Source: https://habr.com/ru/post/1529132/


All Articles