Skip to content

Commit

Permalink
Support the NSSet,NSMutableSet
Browse files Browse the repository at this point in the history
  • Loading branch information
李杰 committed Nov 11, 2018
1 parent d89d2e4 commit eaff9d3
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 127 deletions.
2 changes: 1 addition & 1 deletion JJException.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |s|
#

s.name = "JJException"
s.version = "0.1.2"
s.version = "0.1.3"
s.summary = "Handle the objective-c crash exception."

# This description is used to generate tags and improve search results.
Expand Down
12 changes: 12 additions & 0 deletions JJException.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
61025B8921563A6400191522 /* NSObject+UnrecognizedSelectorHook.h in Sources */ = {isa = PBXBuildFile; fileRef = 61A7F53E21427E4100C87FA1 /* NSObject+UnrecognizedSelectorHook.h */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
61025B8A21563A6400191522 /* NSObject+UnrecognizedSelectorHook.m in Sources */ = {isa = PBXBuildFile; fileRef = 61A7F53F21427E4100C87FA1 /* NSObject+UnrecognizedSelectorHook.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
61025B8B21563ABB00191522 /* JJException.h in Headers */ = {isa = PBXBuildFile; fileRef = 613E0A8C21496ED100B8D0DF /* JJException.h */; settings = {ATTRIBUTES = (Public, ); }; };
6109EE7A2198244A00CB15E6 /* NSSet+SetHook.m in Sources */ = {isa = PBXBuildFile; fileRef = 6109EE792198244A00CB15E6 /* NSSet+SetHook.m */; };
6109EE7D2198264F00CB15E6 /* NSMutableSet+MutableSetHook.m in Sources */ = {isa = PBXBuildFile; fileRef = 6109EE7C2198264F00CB15E6 /* NSMutableSet+MutableSetHook.m */; };
61196D4720F261C800022DDC /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 61196D4620F261C800022DDC /* AppDelegate.m */; };
61196D4A20F261C800022DDC /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 61196D4920F261C800022DDC /* ViewController.m */; };
61196D4D20F261C800022DDC /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 61196D4B20F261C800022DDC /* Main.storyboard */; };
Expand Down Expand Up @@ -87,6 +89,10 @@
61025B4F2153E58F00191522 /* NSMutableAttributedString+MutableAttributedStringHook.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSMutableAttributedString+MutableAttributedStringHook.m"; sourceTree = "<group>"; };
61025B5C215639B700191522 /* JJException.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = JJException.framework; sourceTree = BUILT_PRODUCTS_DIR; };
61025B5F215639B700191522 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
6109EE782198244A00CB15E6 /* NSSet+SetHook.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSSet+SetHook.h"; sourceTree = "<group>"; };
6109EE792198244A00CB15E6 /* NSSet+SetHook.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSSet+SetHook.m"; sourceTree = "<group>"; };
6109EE7B2198264F00CB15E6 /* NSMutableSet+MutableSetHook.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSMutableSet+MutableSetHook.h"; sourceTree = "<group>"; };
6109EE7C2198264F00CB15E6 /* NSMutableSet+MutableSetHook.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSMutableSet+MutableSetHook.m"; sourceTree = "<group>"; };
61196D4220F261C800022DDC /* JJException.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = JJException.app; sourceTree = BUILT_PRODUCTS_DIR; };
61196D4520F261C800022DDC /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
61196D4620F261C800022DDC /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -287,6 +293,10 @@
61025B4C2153E36100191522 /* NSAttributedString+AttributedStringHook.m */,
61025B4E2153E58F00191522 /* NSMutableAttributedString+MutableAttributedStringHook.h */,
61025B4F2153E58F00191522 /* NSMutableAttributedString+MutableAttributedStringHook.m */,
6109EE782198244A00CB15E6 /* NSSet+SetHook.h */,
6109EE792198244A00CB15E6 /* NSSet+SetHook.m */,
6109EE7B2198264F00CB15E6 /* NSMutableSet+MutableSetHook.h */,
6109EE7C2198264F00CB15E6 /* NSMutableSet+MutableSetHook.m */,
);
path = ARC;
sourceTree = "<group>";
Expand Down Expand Up @@ -498,7 +508,9 @@
613E0AAD214FF41B00B8D0DF /* NSObject+DeallocBlock.m in Sources */,
61025B502153E58F00191522 /* NSMutableAttributedString+MutableAttributedStringHook.m in Sources */,
613E0A8E21496ED100B8D0DF /* JJException.m in Sources */,
6109EE7D2198264F00CB15E6 /* NSMutableSet+MutableSetHook.m in Sources */,
61196D4720F261C800022DDC /* AppDelegate.m in Sources */,
6109EE7A2198244A00CB15E6 /* NSSet+SetHook.m in Sources */,
61A7F5562144D0FA00C87FA1 /* NSObject+SwizzleHook.m in Sources */,
61A7F54E21427EB700C87FA1 /* NSMutableArray+MutableArrayHook.m in Sources */,
61A7F54D21427EB700C87FA1 /* NSDictionary+DictionaryHook.m in Sources */,
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,128 +2,4 @@
<Bucket
type = "1"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "JJException/Source/MRC/NSObject+UnrecognizedSelectorHook.m"
timestampString = "561397405.780197"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "10"
endingLineNumber = "10"
landmarkName = "unknown"
landmarkType = "0">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "JJException/Source/ARC/NSNotificationCenter+ClearNotification.m"
timestampString = "561635354.75411"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "29"
endingLineNumber = "29"
landmarkName = "-processAddObserver:selector:name:object:swizzleInfo:"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.SymbolicBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "[__NSArrayM removeObjectsInRange:]"
moduleName = "">
<Locations>
<Location
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "-[__NSArrayM removeObjectsInRange:]"
moduleName = "CoreFoundation"
usesParentBreakpointCondition = "Yes"
offsetFromSymbolStart = "0">
</Location>
</Locations>
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "JJExceptionTests/JJExceptionTests.m"
timestampString = "561806569.301136"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "127"
endingLineNumber = "127"
landmarkName = "-testMutableString"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.SymbolicBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "[NSMutableArray removeObject:]"
moduleName = "">
<Locations>
<Location
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "-[NSMutableArray removeObject:]"
moduleName = "CoreFoundation"
usesParentBreakpointCondition = "Yes"
offsetFromSymbolStart = "0">
</Location>
</Locations>
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "JJException/Source/MRC/NSMutableArray+MutableArrayHook.m"
timestampString = "561799150.152006"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "84"
endingLineNumber = "84"
landmarkName = "-hookRemoveObjectsInRange:"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "JJExceptionTests/JJExceptionTests.m"
timestampString = "561806569.301685"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "99"
endingLineNumber = "99"
landmarkName = "-testZombieException"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
19 changes: 19 additions & 0 deletions JJException/Source/ARC/NSMutableSet+MutableSetHook.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// NSMutableSet+MutableSetHook.h
// JJException
//
// Created by Jezz on 2018/11/11.
// Copyright © 2018年 Jezz. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NSMutableSet (MutableSetHook)

+ (void)jj_swizzleNSMutableSet;

@end

NS_ASSUME_NONNULL_END
40 changes: 40 additions & 0 deletions JJException/Source/ARC/NSMutableSet+MutableSetHook.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// NSMutableSet+MutableSetHook.m
// JJException
//
// Created by Jezz on 2018/11/11.
// Copyright © 2018年 Jezz. All rights reserved.
//

#import "NSMutableSet+MutableSetHook.h"
#import "NSObject+SwizzleHook.h"
#import <objc/runtime.h>
#import "JJExceptionProxy.h"

@implementation NSMutableSet (MutableSetHook)

+ (void)jj_swizzleNSMutableSet{
NSMutableSet* instanceObject = [NSMutableSet new];
Class cls = object_getClass(instanceObject);

swizzleInstanceMethod(cls,@selector(addObject:), @selector(hookAddObject:));
swizzleInstanceMethod(cls,@selector(removeObject:), @selector(hookRemoveObject:));
}

- (void) hookAddObject:(id)object {
if (object) {
[self hookAddObject:object];
} else {
handleCrashException(JJExceptionGuardArrayContainer,@"NSSet addObject nil object");
}
}

- (void) hookRemoveObject:(id)object {
if (object) {
[self hookRemoveObject:object];
} else {
handleCrashException(JJExceptionGuardArrayContainer,@"NSSet removeObject nil object");
}
}

@end
19 changes: 19 additions & 0 deletions JJException/Source/ARC/NSSet+SetHook.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// NSSet+SetHook.h
// JJException
//
// Created by Jezz on 2018/11/11.
// Copyright © 2018年 Jezz. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NSSet (SetHook)

+ (void)jj_swizzleNSSet;

@end

NS_ASSUME_NONNULL_END
27 changes: 27 additions & 0 deletions JJException/Source/ARC/NSSet+SetHook.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// NSSet+SetHook.m
// JJException
//
// Created by Jezz on 2018/11/11.
// Copyright © 2018年 Jezz. All rights reserved.
//

#import "NSSet+SetHook.h"
#import "NSObject+SwizzleHook.h"
#import "JJExceptionProxy.h"

@implementation NSSet (SetHook)

+ (void)jj_swizzleNSSet{
[NSSet jj_swizzleClassMethod:@selector(setWithObject:) withSwizzleMethod:@selector(hookSetWithObject:)];
}

+ (instancetype)hookSetWithObject:(id)object{
if (object){
return [self hookSetWithObject:object];
}
handleCrashException(JJExceptionGuardArrayContainer,@"NSSet setWithObject nil object");
return nil;
}

@end
2 changes: 1 addition & 1 deletion JJException/Source/MRC/NSObject+KVOCrash.m
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ - (void)dealloc{
- (void)clearKVOData{
for (KVOObjectItem* item in self.kvoObjectSet) {
//Invoke the origin removeObserver,do not check array
handleCrashException(JJExceptionGuardKVOCrash,[NSString stringWithFormat:@"KVO forgot remove keyPath:%@",item.keyPath]);
handleCrashException(JJExceptionGuardKVOCrash,[NSString stringWithFormat:@"KVO forgot remove keyPath:%@ from which object:%@",item.keyPath,NSStringFromClass(object_getClass(self.whichObject))]);
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
@try {
Expand Down
2 changes: 2 additions & 0 deletions JJException/Source/Main/JJExceptionProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ - (void)setIsProtectException:(BOOL)isProtectException{
if(self.exceptionGuardCategory & JJExceptionGuardArrayContainer){
[NSArray performSelector:@selector(jj_swizzleNSArray)];
[NSMutableArray performSelector:@selector(jj_swizzleNSMutableArray)];
[NSSet performSelector:@selector(jj_swizzleNSSet)];
[NSMutableSet performSelector:@selector(jj_swizzleNSMutableSet)];
}
if(self.exceptionGuardCategory & JJExceptionGuardDictionaryContainer){
[NSDictionary performSelector:@selector(jj_swizzleNSDictionary)];
Expand Down
14 changes: 13 additions & 1 deletion JJExceptionTests/JJExceptionTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,23 @@ - (void)testMutableAttributedString{
[attribute2 replaceCharactersInRange:NSMakeRange(0, 100) withString:@"1"];
}

- (void)testNSSet{
NSObject* nilObject = nil;
__unused NSSet* set = [NSSet setWithObject:nilObject];
}

- (void)testNSMutableSet{
NSObject* nilObject = nil;
NSMutableSet* m = [NSMutableSet set];
[m addObject:nilObject];
[m removeObject:nilObject];
}

- (void)testJJExceptionPerformance{
[self measureBlock:^{
NSArray* array = @[@"112",@"12",@"12",@"12",@"12",@"12",@"12",@"12",@"12",@"12"];
for (int i = 0;i < 1000000;i++) {
id object = [array objectAtIndex:1];
__unused id object = [array objectAtIndex:1];
}
}];
}
Expand Down

0 comments on commit eaff9d3

Please sign in to comment.