Skip to content

Commit 3502ed0

Browse files
committed
Introduced MethodKind
1 parent bd38145 commit 3502ed0

File tree

5 files changed

+46
-9
lines changed

5 files changed

+46
-9
lines changed

Sources/InterposeKit/Deprecated/NSObject+Deprecated.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ extension NSObject {
3535
_ build: @escaping HookBuilder<MethodSignature, HookSignature>
3636
) throws -> Hook {
3737
let hook = try Hook(
38-
target: .class(self),
38+
target: .class(self, .instance),
3939
selector: selector,
4040
build: build
4141
)

Sources/InterposeKit/Hooks/Hook.swift

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,14 @@ public final class Hook {
7171
}
7272

7373
switch target {
74-
case .class(let `class`):
74+
case let .class(`class`, methodKind):
7575
return ClassHookStrategy(
7676
class: `class`,
77+
methodKind: methodKind,
7778
selector: selector,
7879
makeHookIMP: makeHookIMP
7980
)
80-
case .object(let object):
81+
case let .object(object):
8182
return ObjectHookStrategy(
8283
object: object,
8384
selector: selector,
@@ -238,10 +239,10 @@ extension Hook: CustomDebugStringConvertible {
238239

239240
public enum HookScope {
240241

241-
/// The scope that targets all instances of the class.
242-
case `class`
242+
/// The scope that targets a method on a class type (instance or class method).
243+
case `class`(MethodKind)
243244

244-
/// The scope that targets a specific instance of the class.
245+
/// The scope that targets a specific object instance.
245246
case object(NSObject)
246247

247248
}
@@ -259,7 +260,13 @@ public enum HookState: Equatable {
259260

260261
}
261262

263+
/// Represents the target of a hook operation—either a class type or a specific object instance.
262264
internal enum HookTarget {
263-
case `class`(AnyClass)
265+
266+
/// A hook targeting a method defined on a class, either an instance method or a class method.
267+
case `class`(AnyClass, MethodKind)
268+
269+
/// A hook targeting a method on a specific object instance.
264270
case object(NSObject)
271+
265272
}

Sources/InterposeKit/Hooks/HookStrategy/ClassHookStrategy/ClassHookStrategy.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ internal final class ClassHookStrategy: HookStrategy {
88

99
internal init(
1010
`class`: AnyClass,
11+
methodKind: MethodKind,
1112
selector: Selector,
1213
makeHookIMP: @escaping () -> IMP
1314
) {
1415
self.class = `class`
1516
self.selector = selector
17+
self.methodKind = methodKind
1618
self.makeHookIMP = makeHookIMP
1719
}
1820

@@ -21,9 +23,10 @@ internal final class ClassHookStrategy: HookStrategy {
2123
// ============================================================================ //
2224

2325
internal let `class`: AnyClass
24-
internal var scope: HookScope { .class }
26+
internal var scope: HookScope { .class(self.methodKind) }
2527
internal let selector: Selector
2628

29+
private let methodKind: MethodKind
2730
private let makeHookIMP: () -> IMP
2831

2932
// ============================================================================ //

Sources/InterposeKit/Interpose.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ public enum Interpose {
1414
public static func prepareHook<MethodSignature, HookSignature>(
1515
on `class`: AnyClass,
1616
for selector: Selector,
17+
methodKind: MethodKind = .instance,
1718
methodSignature: MethodSignature.Type,
1819
hookSignature: HookSignature.Type,
1920
build: @escaping HookBuilder<MethodSignature, HookSignature>
2021
) throws -> Hook {
2122
try Hook(
22-
target: .class(`class`),
23+
target: .class(`class`, methodKind),
2324
selector: selector,
2425
build: build
2526
)
@@ -29,13 +30,15 @@ public enum Interpose {
2930
public static func applyHook<MethodSignature, HookSignature>(
3031
on `class`: AnyClass,
3132
for selector: Selector,
33+
methodKind: MethodKind = .instance,
3234
methodSignature: MethodSignature.Type,
3335
hookSignature: HookSignature.Type,
3436
build: @escaping HookBuilder<MethodSignature, HookSignature>
3537
) throws -> Hook {
3638
let hook = try prepareHook(
3739
on: `class`,
3840
for: selector,
41+
methodKind: methodKind,
3942
methodSignature: methodSignature,
4043
hookSignature: hookSignature,
4144
build: build
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public enum MethodKind: Equatable {
2+
3+
/// An instance method, e.g. `-[MyClass doSomething]`.
4+
case instance
5+
6+
/// A class method, e.g. `+[MyClass doSomething]`.
7+
case `class`
8+
9+
}
10+
11+
extension MethodKind {
12+
13+
/// Returns the Objective-C method prefix symbol for this kind, `-` for instance methods
14+
/// and `+` for class methods.
15+
internal var symbolPrefix: String {
16+
switch self {
17+
case .instance:
18+
return "-"
19+
case .class:
20+
return "+"
21+
}
22+
}
23+
24+
}

0 commit comments

Comments
 (0)