| OLD | NEW |
| (Empty) | |
| 1 /* |
| 2 * Copyright (c) 2009-2015 Erik Doernenburg and contributors |
| 3 * |
| 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 * not use these files except in compliance with the License. You may obtain |
| 6 * a copy of the License at |
| 7 * |
| 8 * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 * |
| 10 * Unless required by applicable law or agreed to in writing, software |
| 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 * License for the specific language governing permissions and limitations |
| 14 * under the License. |
| 15 */ |
| 16 |
| 17 #import "NSObject+OCMAdditions.h" |
| 18 #import "NSMethodSignature+OCMAdditions.h" |
| 19 #import <objc/runtime.h> |
| 20 |
| 21 @implementation NSObject (OCMAdditions) |
| 22 |
| 23 + (IMP)instanceMethodForwarderForSelector:(SEL)aSelector { |
| 24 // use sel_registerName() and not @selector to avoid warning |
| 25 SEL selectorWithNoImplementation = |
| 26 sel_registerName("methodWhichMustNotExist::::"); |
| 27 |
| 28 #ifndef __arm64__ |
| 29 static NSMutableDictionary* _OCMReturnTypeCache; |
| 30 |
| 31 if (_OCMReturnTypeCache == nil) |
| 32 _OCMReturnTypeCache = [[NSMutableDictionary alloc] init]; |
| 33 |
| 34 BOOL needsStructureReturn; |
| 35 void* rawCacheKey[2] = {(void*)self, aSelector}; |
| 36 NSData* cacheKey = |
| 37 [NSData dataWithBytes:rawCacheKey length:sizeof(rawCacheKey)]; |
| 38 NSNumber* cachedValue = [_OCMReturnTypeCache objectForKey:cacheKey]; |
| 39 |
| 40 if (cachedValue == nil) { |
| 41 NSMethodSignature* sig = |
| 42 [self instanceMethodSignatureForSelector:aSelector]; |
| 43 needsStructureReturn = [sig usesSpecialStructureReturn]; |
| 44 [_OCMReturnTypeCache setObject:@(needsStructureReturn) forKey:cacheKey]; |
| 45 } else { |
| 46 needsStructureReturn = [cachedValue boolValue]; |
| 47 } |
| 48 |
| 49 if (needsStructureReturn) |
| 50 return class_getMethodImplementation_stret([NSObject class], |
| 51 selectorWithNoImplementation); |
| 52 #endif |
| 53 |
| 54 return class_getMethodImplementation([NSObject class], |
| 55 selectorWithNoImplementation); |
| 56 } |
| 57 |
| 58 + (void)enumerateMethodsInClass:(Class)aClass |
| 59 usingBlock:(void (^)(Class cls, SEL sel))aBlock { |
| 60 for (Class cls = aClass; cls != nil; cls = class_getSuperclass(cls)) { |
| 61 Method* methodList = class_copyMethodList(cls, NULL); |
| 62 if (methodList == NULL) |
| 63 continue; |
| 64 |
| 65 for (Method* mPtr = methodList; *mPtr != NULL; mPtr++) { |
| 66 SEL sel = method_getName(*mPtr); |
| 67 aBlock(cls, sel); |
| 68 } |
| 69 free(methodList); |
| 70 } |
| 71 } |
| 72 |
| 73 @end |
| OLD | NEW |