使用 Objective-C 元数据生成类依赖图
这个人想出了一个非常简洁的工具来生成类依赖图 - 但是,它依赖于解析源代码并查找 #import
指令。
这很简洁,但我对此有很多问题。尤其重要的是,它没有考虑导入的导入,也没有考虑前缀标头,也没有考虑导入引用的文件中的类是否实际被使用。
我想做一些类似于 class-dump
的事情,并检查存储在 Mach-O 文件中的 Objective-C 元数据,以生成类依赖项的内存中表示。
我不想从头开始做这件事,所以我想知道:
- 它已经完成了吗?
- 是否有一个开源库可以为我提供提取这些信息所需的基础工具(一个检查 Mach-O 文件并创建其中包含的 Objective-C 信息的外观的库 - 这样我就可以迭代所有的类、它们的方法、属性、ivars 等,并扫描对其他类的引用)我认为 class-dump 的源代码将是一个很好的起点。
- 如果你有这方面的经验,我想要完成的事情是否可行?
- 我需要克服哪些障碍?
This guy came up with a pretty neat tool to generate a class dependency graph - however, it relies on parsing your source code and looking for #import
directives.
This is neat, but I have a number of problems with this. Not least of which is it doesn't take into account imports of imports nor prefix headers nor whether-or-not the class(es) in the file referenced by the import are actually being used.
I'd like to do something more akin to class-dump
and examine the Objective-C metadata stored in the Mach-O file to generate an in-memory representation of the class dependencies.
I'd rather not do this from scratch, so I'm wondering:
- Has it already been done?
- Is there an open-source library which would provide me with the foundational tools I need to extract this information (a library which examines the Mach-O file and creates a façade of the Objective-C information contained within - such that I could iterate over all of the classes, their methods, properties, ivars, etc and scan for references to other classes) I figure class-dump's source would be a good place to start.
- If you have experience in this sort of thing, is what I'm trying to accomplish feasible?
- What roadblocks will I need to overcome?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知没有。
class-dump 的核心是
libMachObjC
,它完全可以完成您的任务想要,即解析所有类/方法/ivars 等等。 API非常干净,应该很容易使用。不幸的是,不能,因为有些类没有声明真正的类,而是使用
id
代替。例如,以下是可以从 UIKit 的类转储中提取的信息:_rowData
ivar 类型信息是id
但如果您在运行时检查,您会看到_rowData
是UITableViewRowData
类的实例。此信息不存在于 Mach-O 二进制文件中,因此您无法找到 UITableView 和 UITableViewRowData 之间的关系。这同样适用于方法参数。Not that I know of.
At the core of class-dump is
libMachObjC
which does exatly what you want, i.e. parse all classes/methods/ivars and more. The API is very clean, it should be very easy to use.Unfortunately, no because some classes don't declare the real class but use
id
instead. For example, here is the information that can be extracted from a class-dump of UIKit:The
_rowData
ivar type information isid
but if you check at runtime you will see that_rowData
is an instance of theUITableViewRowData
class. This information is not present in the Mach-O binary so you have no way to find the relation betweenUITableView
andUITableViewRowData
. The same applies for method parameters.这是一个依赖于 mach.o 文件中的信息并根据该信息生成图形依赖关系的解决方案: https://github.com/PaulTaykalo/objc-dependency-visualizer
Here's a solution that relies on information in mach.o files, and generates graph dependency based on that information: https://github.com/PaulTaykalo/objc-dependency-visualizer
是的 - 但我不能推荐一个好的公共实施
大多数用例将受益于使用 objc 运行时设施
objc/...
而不是检查二进制文件。是的。我使用 objc 运行时做了类似的事情。
这很大程度上取决于您想要的详细程度...如果您没有找到这样的实现,那么实现时间,但我认为如果您在 objc 运行时中谷歌搜索更深奥的函数,您会找到一些选项;也许您会在(开放)语言绑定或桥梁中找到一个?
如果您最终自己编写了一个,您可以使用 objc_getClassList 来获取注册的 objc 类,然后从那里访问您想要的属性/信息。
yes - but i can't recommend a good public implementation
most use cases would benefit by using the objc runtime facilities
objc/...
rather than examining the binary.yes. i've done something similar using the objc runtime.
that depends largely on the level of detail you want... implementation time if you find no such implementation, but i figure you will find a few options if you google the more esoteric functions in the objc runtime; perhaps you would find one in an (open) language binding or bridge?
if you do end up writing one yourself, you can get registered objc classes using
objc_getClassList
, then access the properties/information you want from there.