Archived
Confirmed sucessful Firebase/Crashlytics installation
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
// Copyright 2018 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#import "Private/GULSwizzler.h"
|
||||
|
||||
#import <objc/runtime.h>
|
||||
|
||||
#ifdef DEBUG
|
||||
#import <GoogleUtilities/GULLogger.h>
|
||||
#import "../Common/GULLoggerCodes.h"
|
||||
|
||||
static GULLoggerService kGULLoggerSwizzler = @"[GoogleUtilities/MethodSwizzler]";
|
||||
#endif
|
||||
|
||||
dispatch_queue_t GetGULSwizzlingQueue(void) {
|
||||
static dispatch_queue_t queue;
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
queue = dispatch_queue_create("com.google.GULSwizzler", DISPATCH_QUEUE_SERIAL);
|
||||
});
|
||||
return queue;
|
||||
}
|
||||
|
||||
@implementation GULSwizzler
|
||||
|
||||
+ (void)swizzleClass:(Class)aClass
|
||||
selector:(SEL)selector
|
||||
isClassSelector:(BOOL)isClassSelector
|
||||
withBlock:(nullable id)block {
|
||||
dispatch_sync(GetGULSwizzlingQueue(), ^{
|
||||
NSAssert(selector, @"The selector cannot be NULL");
|
||||
NSAssert(aClass, @"The class cannot be Nil");
|
||||
Class resolvedClass = aClass;
|
||||
Method method = nil;
|
||||
if (isClassSelector) {
|
||||
method = class_getClassMethod(aClass, selector);
|
||||
resolvedClass = object_getClass(aClass);
|
||||
} else {
|
||||
method = class_getInstanceMethod(aClass, selector);
|
||||
}
|
||||
NSAssert(method, @"You're attempting to swizzle a method that doesn't exist. (%@, %@)",
|
||||
NSStringFromClass(resolvedClass), NSStringFromSelector(selector));
|
||||
IMP newImp = imp_implementationWithBlock(block);
|
||||
#ifdef DEBUG
|
||||
IMP currentImp = class_getMethodImplementation(resolvedClass, selector);
|
||||
Class class = NSClassFromString(@"GULSwizzlingCache");
|
||||
if (class) {
|
||||
SEL cacheSelector = NSSelectorFromString(@"cacheCurrentIMP:forNewIMP:forClass:withSelector:");
|
||||
NSMethodSignature *methodSignature = [class methodSignatureForSelector:cacheSelector];
|
||||
if (methodSignature != nil) {
|
||||
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:methodSignature];
|
||||
[inv setSelector:cacheSelector];
|
||||
[inv setTarget:class];
|
||||
[inv setArgument:&(currentImp) atIndex:2];
|
||||
[inv setArgument:&(newImp) atIndex:3];
|
||||
[inv setArgument:&(resolvedClass) atIndex:4];
|
||||
[inv setArgument:(void *_Nonnull) & (selector) atIndex:5];
|
||||
[inv invoke];
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
const char *typeEncoding = method_getTypeEncoding(method);
|
||||
__unused IMP originalImpOfClass =
|
||||
class_replaceMethod(resolvedClass, selector, newImp, typeEncoding);
|
||||
|
||||
#ifdef DEBUG
|
||||
// If !originalImpOfClass, then the IMP came from a superclass.
|
||||
if (originalImpOfClass) {
|
||||
SEL selector = NSSelectorFromString(@"originalIMPOfCurrentIMP:");
|
||||
NSMethodSignature *methodSignature = [class methodSignatureForSelector:selector];
|
||||
if (methodSignature != nil) {
|
||||
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:methodSignature];
|
||||
[inv setSelector:selector];
|
||||
[inv setTarget:class];
|
||||
[inv setArgument:&(currentImp) atIndex:2];
|
||||
[inv invoke];
|
||||
IMP testOriginal;
|
||||
[inv getReturnValue:&testOriginal];
|
||||
if (originalImpOfClass != testOriginal) {
|
||||
GULLogWarning(kGULLoggerSwizzler, NO,
|
||||
[NSString stringWithFormat:@"I-SWZ%06ld",
|
||||
(long)kGULSwizzlerMessageCodeMethodSwizzling000],
|
||||
@"Swizzling class: %@ SEL:%@ after it has been previously been swizzled.",
|
||||
NSStringFromClass(resolvedClass), NSStringFromSelector(selector));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
});
|
||||
}
|
||||
|
||||
+ (nullable IMP)currentImplementationForClass:(Class)aClass
|
||||
selector:(SEL)selector
|
||||
isClassSelector:(BOOL)isClassSelector {
|
||||
NSAssert(selector, @"The selector cannot be NULL");
|
||||
NSAssert(aClass, @"The class cannot be Nil");
|
||||
if (selector == NULL || aClass == nil) {
|
||||
return nil;
|
||||
}
|
||||
__block IMP currentIMP = nil;
|
||||
dispatch_sync(GetGULSwizzlingQueue(), ^{
|
||||
Method method = nil;
|
||||
if (isClassSelector) {
|
||||
method = class_getClassMethod(aClass, selector);
|
||||
} else {
|
||||
method = class_getInstanceMethod(aClass, selector);
|
||||
}
|
||||
NSAssert(method, @"The Method for this class/selector combo doesn't exist (%@, %@).",
|
||||
NSStringFromClass(aClass), NSStringFromSelector(selector));
|
||||
if (method == nil) {
|
||||
return;
|
||||
}
|
||||
currentIMP = method_getImplementation(method);
|
||||
NSAssert(currentIMP, @"The IMP for this class/selector combo doesn't exist (%@, %@).",
|
||||
NSStringFromClass(aClass), NSStringFromSelector(selector));
|
||||
});
|
||||
return currentIMP;
|
||||
}
|
||||
|
||||
+ (BOOL)selector:(SEL)selector existsInClass:(Class)aClass isClassSelector:(BOOL)isClassSelector {
|
||||
Method method = isClassSelector ? class_getClassMethod(aClass, selector)
|
||||
: class_getInstanceMethod(aClass, selector);
|
||||
return method != nil;
|
||||
}
|
||||
|
||||
+ (NSArray<id> *)ivarObjectsForObject:(id)object {
|
||||
NSMutableArray *array = [NSMutableArray array];
|
||||
unsigned int count;
|
||||
Ivar *vars = class_copyIvarList([object class], &count);
|
||||
for (NSUInteger i = 0; i < count; i++) {
|
||||
const char *typeEncoding = ivar_getTypeEncoding(vars[i]);
|
||||
// Check to see if the ivar is an object.
|
||||
if (strncmp(typeEncoding, "@", 1) == 0) {
|
||||
id ivarObject = object_getIvar(object, vars[i]);
|
||||
[array addObject:ivarObject];
|
||||
}
|
||||
}
|
||||
free(vars);
|
||||
return array;
|
||||
}
|
||||
@end
|
||||
Generated
+207
@@ -0,0 +1,207 @@
|
||||
/*
|
||||
* Copyright 2018 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* GULOriginalIMPConvenienceMacros.h
|
||||
*
|
||||
* This header contains convenience macros for invoking the original IMP of a swizzled method.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Invokes original IMP when the original selector takes no arguments.
|
||||
*
|
||||
* @param __receivingObject The object on which the IMP is invoked.
|
||||
* @param __swizzledSEL The selector used for swizzling.
|
||||
* @param __returnType The return type of the original implementation.
|
||||
* @param __originalIMP The original IMP.
|
||||
*/
|
||||
#define GUL_INVOKE_ORIGINAL_IMP0(__receivingObject, __swizzledSEL, __returnType, __originalIMP) \
|
||||
((__returnType(*)(id, SEL))__originalIMP)(__receivingObject, __swizzledSEL)
|
||||
|
||||
/**
|
||||
* Invokes original IMP when the original selector takes 1 argument.
|
||||
*
|
||||
* @param __receivingObject The object on which the IMP is invoked.
|
||||
* @param __swizzledSEL The selector used for swizzling.
|
||||
* @param __returnType The return type of the original implementation.
|
||||
* @param __originalIMP The original IMP.
|
||||
* @param __arg1 The first argument.
|
||||
*/
|
||||
#define GUL_INVOKE_ORIGINAL_IMP1(__receivingObject, __swizzledSEL, __returnType, __originalIMP, \
|
||||
__arg1) \
|
||||
((__returnType(*)(id, SEL, __typeof__(__arg1)))__originalIMP)(__receivingObject, __swizzledSEL, \
|
||||
__arg1)
|
||||
|
||||
/**
|
||||
* Invokes original IMP when the original selector takes 2 arguments.
|
||||
*
|
||||
* @param __receivingObject The object on which the IMP is invoked.
|
||||
* @param __swizzledSEL The selector used for swizzling.
|
||||
* @param __returnType The return type of the original implementation.
|
||||
* @param __originalIMP The original IMP.
|
||||
* @param __arg1 The first argument.
|
||||
* @param __arg2 The second argument.
|
||||
*/
|
||||
#define GUL_INVOKE_ORIGINAL_IMP2(__receivingObject, __swizzledSEL, __returnType, __originalIMP, \
|
||||
__arg1, __arg2) \
|
||||
((__returnType(*)(id, SEL, __typeof__(__arg1), __typeof__(__arg2)))__originalIMP)( \
|
||||
__receivingObject, __swizzledSEL, __arg1, __arg2)
|
||||
|
||||
/**
|
||||
* Invokes original IMP when the original selector takes 3 arguments.
|
||||
*
|
||||
* @param __receivingObject The object on which the IMP is invoked.
|
||||
* @param __swizzledSEL The selector used for swizzling.
|
||||
* @param __returnType The return type of the original implementation.
|
||||
* @param __originalIMP The original IMP.
|
||||
* @param __arg1 The first argument.
|
||||
* @param __arg2 The second argument.
|
||||
* @param __arg3 The third argument.
|
||||
*/
|
||||
#define GUL_INVOKE_ORIGINAL_IMP3(__receivingObject, __swizzledSEL, __returnType, __originalIMP, \
|
||||
__arg1, __arg2, __arg3) \
|
||||
((__returnType(*)(id, SEL, __typeof__(__arg1), __typeof__(__arg2), \
|
||||
__typeof__(__arg3)))__originalIMP)(__receivingObject, __swizzledSEL, __arg1, \
|
||||
__arg2, __arg3)
|
||||
|
||||
/**
|
||||
* Invokes original IMP when the original selector takes 4 arguments.
|
||||
*
|
||||
* @param __receivingObject The object on which the IMP is invoked.
|
||||
* @param __swizzledSEL The selector used for swizzling.
|
||||
* @param __returnType The return type of the original implementation.
|
||||
* @param __originalIMP The original IMP.
|
||||
* @param __arg1 The first argument.
|
||||
* @param __arg2 The second argument.
|
||||
* @param __arg3 The third argument.
|
||||
* @param __arg4 The fourth argument.
|
||||
*/
|
||||
#define GUL_INVOKE_ORIGINAL_IMP4(__receivingObject, __swizzledSEL, __returnType, __originalIMP, \
|
||||
__arg1, __arg2, __arg3, __arg4) \
|
||||
((__returnType(*)(id, SEL, __typeof__(__arg1), __typeof__(__arg2), __typeof__(__arg3), \
|
||||
__typeof__(__arg4)))__originalIMP)(__receivingObject, __swizzledSEL, __arg1, \
|
||||
__arg2, __arg3, __arg4)
|
||||
|
||||
/**
|
||||
* Invokes original IMP when the original selector takes 5 arguments.
|
||||
*
|
||||
* @param __receivingObject The object on which the IMP is invoked.
|
||||
* @param __swizzledSEL The selector used for swizzling.
|
||||
* @param __returnType The return type of the original implementation.
|
||||
* @param __originalIMP The original IMP.
|
||||
* @param __arg1 The first argument.
|
||||
* @param __arg2 The second argument.
|
||||
* @param __arg3 The third argument.
|
||||
* @param __arg4 The fourth argument.
|
||||
* @param __arg5 The fifth argument.
|
||||
*/
|
||||
#define GUL_INVOKE_ORIGINAL_IMP5(__receivingObject, __swizzledSEL, __returnType, __originalIMP, \
|
||||
__arg1, __arg2, __arg3, __arg4, __arg5) \
|
||||
((__returnType(*)(id, SEL, __typeof__(__arg1), __typeof__(__arg2), __typeof__(__arg3), \
|
||||
__typeof__(__arg4), __typeof__(__arg5)))__originalIMP)( \
|
||||
__receivingObject, __swizzledSEL, __arg1, __arg2, __arg3, __arg4, __arg5)
|
||||
|
||||
/**
|
||||
* Invokes original IMP when the original selector takes 6 arguments.
|
||||
*
|
||||
* @param __receivingObject The object on which the IMP is invoked.
|
||||
* @param __swizzledSEL The selector used for swizzling.
|
||||
* @param __returnType The return type of the original implementation.
|
||||
* @param __originalIMP The original IMP.
|
||||
* @param __arg1 The first argument.
|
||||
* @param __arg2 The second argument.
|
||||
* @param __arg3 The third argument.
|
||||
* @param __arg4 The fourth argument.
|
||||
* @param __arg5 The fifth argument.
|
||||
* @param __arg6 The sixth argument.
|
||||
*/
|
||||
#define GUL_INVOKE_ORIGINAL_IMP6(__receivingObject, __swizzledSEL, __returnType, __originalIMP, \
|
||||
__arg1, __arg2, __arg3, __arg4, __arg5, __arg6) \
|
||||
((__returnType(*)(id, SEL, __typeof__(__arg1), __typeof__(__arg2), __typeof__(__arg3), \
|
||||
__typeof__(__arg4), __typeof__(__arg5), __typeof__(__arg6)))__originalIMP)( \
|
||||
__receivingObject, __swizzledSEL, __arg1, __arg2, __arg3, __arg4, __arg5, __arg6)
|
||||
|
||||
/**
|
||||
* Invokes original IMP when the original selector takes 7 arguments.
|
||||
*
|
||||
* @param __receivingObject The object on which the IMP is invoked.
|
||||
* @param __swizzledSEL The selector used for swizzling.
|
||||
* @param __returnType The return type of the original implementation.
|
||||
* @param __originalIMP The original IMP.
|
||||
* @param __arg1 The first argument.
|
||||
* @param __arg2 The second argument.
|
||||
* @param __arg3 The third argument.
|
||||
* @param __arg4 The fourth argument.
|
||||
* @param __arg5 The fifth argument.
|
||||
* @param __arg6 The sixth argument.
|
||||
* @param __arg7 The seventh argument.
|
||||
*/
|
||||
#define GUL_INVOKE_ORIGINAL_IMP7(__receivingObject, __swizzledSEL, __returnType, __originalIMP, \
|
||||
__arg1, __arg2, __arg3, __arg4, __arg5, __arg6, __arg7) \
|
||||
((__returnType(*)(id, SEL, __typeof__(__arg1), __typeof__(__arg2), __typeof__(__arg3), \
|
||||
__typeof__(__arg4), __typeof__(__arg5), __typeof__(__arg6), \
|
||||
__typeof__(__arg7)))__originalIMP)( \
|
||||
__receivingObject, __swizzledSEL, __arg1, __arg2, __arg3, __arg4, __arg5, __arg6, __arg7)
|
||||
|
||||
/**
|
||||
* Invokes original IMP when the original selector takes 8 arguments.
|
||||
*
|
||||
* @param __receivingObject The object on which the IMP is invoked.
|
||||
* @param __swizzledSEL The selector used for swizzling.
|
||||
* @param __returnType The return type of the original implementation.
|
||||
* @param __originalIMP The original IMP.
|
||||
* @param __arg1 The first argument.
|
||||
* @param __arg2 The second argument.
|
||||
* @param __arg3 The third argument.
|
||||
* @param __arg4 The fourth argument.
|
||||
* @param __arg5 The fifth argument.
|
||||
* @param __arg6 The sixth argument.
|
||||
* @param __arg7 The seventh argument.
|
||||
* @param __arg8 The eighth argument.
|
||||
*/
|
||||
#define GUL_INVOKE_ORIGINAL_IMP8(__receivingObject, __swizzledSEL, __returnType, __originalIMP, \
|
||||
__arg1, __arg2, __arg3, __arg4, __arg5, __arg6, __arg7, __arg8) \
|
||||
((__returnType(*)(id, SEL, __typeof__(__arg1), __typeof__(__arg2), __typeof__(__arg3), \
|
||||
__typeof__(__arg4), __typeof__(__arg5), __typeof__(__arg6), \
|
||||
__typeof__(__arg7), __typeof__(__arg8)))__originalIMP)( \
|
||||
__receivingObject, __swizzledSEL, __arg1, __arg2, __arg3, __arg4, __arg5, __arg6, __arg7, \
|
||||
__arg8)
|
||||
|
||||
/**
|
||||
* Invokes original IMP when the original selector takes 9 arguments.
|
||||
*
|
||||
* @param __receivingObject The object on which the IMP is invoked.
|
||||
* @param __swizzledSEL The selector used for swizzling.
|
||||
* @param __returnType The return type of the original implementation.
|
||||
* @param __originalIMP The original IMP.
|
||||
* @param __arg1 The first argument.
|
||||
* @param __arg2 The second argument.
|
||||
* @param __arg3 The third argument.
|
||||
* @param __arg4 The fourth argument.
|
||||
* @param __arg5 The fifth argument.
|
||||
* @param __arg6 The sixth argument.
|
||||
* @param __arg7 The seventh argument.
|
||||
* @param __arg8 The eighth argument.
|
||||
* @param __arg9 The ninth argument.
|
||||
*/
|
||||
#define GUL_INVOKE_ORIGINAL_IMP9(__receivingObject, __swizzledSEL, __returnType, __originalIMP, \
|
||||
__arg1, __arg2, __arg3, __arg4, __arg5, __arg6, __arg7, __arg8, \
|
||||
__arg9) \
|
||||
((__returnType(*)(id, SEL, __typeof__(__arg1), __typeof__(__arg2), __typeof__(__arg3), \
|
||||
__typeof__(__arg4), __typeof__(__arg5), __typeof__(__arg6), \
|
||||
__typeof__(__arg7), __typeof__(__arg8), __typeof__(__arg9)))__originalIMP)( \
|
||||
__receivingObject, __swizzledSEL, __arg1, __arg2, __arg3, __arg4, __arg5, __arg6, __arg7, \
|
||||
__arg8, __arg9)
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright 2018 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
/** This class handles the runtime manipulation necessary to instrument selectors. It stores the
|
||||
* classes and selectors that have been swizzled, and runs all operations on its own queue.
|
||||
*/
|
||||
@interface GULSwizzler : NSObject
|
||||
|
||||
/** Manipulates the Objective-C runtime to replace the original IMP with the supplied block.
|
||||
*
|
||||
* @param aClass The class to swizzle.
|
||||
* @param selector The selector of the class to swizzle.
|
||||
* @param isClassSelector A BOOL specifying whether the selector is a class or instance selector.
|
||||
* @param block The block that replaces the original IMP.
|
||||
*/
|
||||
+ (void)swizzleClass:(Class)aClass
|
||||
selector:(SEL)selector
|
||||
isClassSelector:(BOOL)isClassSelector
|
||||
withBlock:(nullable id)block;
|
||||
|
||||
/** Returns the current IMP for the given class and selector.
|
||||
*
|
||||
* @param aClass The class to use.
|
||||
* @param selector The selector to find the implementation of.
|
||||
* @param isClassSelector A BOOL specifying whether the selector is a class or instance selector.
|
||||
* @return The implementation of the selector in the runtime.
|
||||
*/
|
||||
+ (nullable IMP)currentImplementationForClass:(Class)aClass
|
||||
selector:(SEL)selector
|
||||
isClassSelector:(BOOL)isClassSelector;
|
||||
|
||||
/** Checks the runtime to see if a selector exists on a class. If a property is declared as
|
||||
* @dynamic, we have a reverse swizzling situation, where the implementation of a method exists
|
||||
* only in concrete subclasses, and NOT in the superclass. We can detect that situation using
|
||||
* this helper method. Similarly, we can detect situations where a class doesn't implement a
|
||||
* protocol method.
|
||||
*
|
||||
* @param selector The selector to check for.
|
||||
* @param aClass The class to check.
|
||||
* @param isClassSelector A BOOL specifying whether the selector is a class or instance selector.
|
||||
* @return YES if the method was found in this selector/class combination, NO otherwise.
|
||||
*/
|
||||
+ (BOOL)selector:(SEL)selector existsInClass:(Class)aClass isClassSelector:(BOOL)isClassSelector;
|
||||
|
||||
/** Returns a list of all Objective-C (and not primitive) ivars contained by the given object.
|
||||
*
|
||||
* @param object The object whose ivars will be iterated.
|
||||
* @return The list of ivar objects.
|
||||
*/
|
||||
+ (NSArray<id> *)ivarObjectsForObject:(id)object;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
Reference in New Issue
Block a user