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 { |
| 25 // use sel_registerName() and not @selector to avoid warning |
| 26 SEL selectorWithNoImplementation = 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 = [NSData dataWithBytes:rawCacheKey length:sizeof(rawCacheK
ey)]; |
| 37 NSNumber *cachedValue = [_OCMReturnTypeCache objectForKey:cacheKey]; |
| 38 |
| 39 if(cachedValue == nil) |
| 40 { |
| 41 NSMethodSignature *sig = [self instanceMethodSignatureForSelector:aSelec
tor]; |
| 42 needsStructureReturn = [sig usesSpecialStructureReturn]; |
| 43 [_OCMReturnTypeCache setObject:@(needsStructureReturn) forKey:cacheKey]; |
| 44 } |
| 45 else |
| 46 { |
| 47 needsStructureReturn = [cachedValue boolValue]; |
| 48 } |
| 49 |
| 50 if(needsStructureReturn) |
| 51 return class_getMethodImplementation_stret([NSObject class], selectorWit
hNoImplementation); |
| 52 #endif |
| 53 |
| 54 return class_getMethodImplementation([NSObject class], selectorWithNoImpleme
ntation); |
| 55 } |
| 56 |
| 57 |
| 58 + (void)enumerateMethodsInClass:(Class)aClass usingBlock:(void (^)(Class cls, SE
L sel))aBlock |
| 59 { |
| 60 for(Class cls = aClass; cls != nil; cls = class_getSuperclass(cls)) |
| 61 { |
| 62 Method *methodList = class_copyMethodList(cls, NULL); |
| 63 if(methodList == NULL) |
| 64 continue; |
| 65 |
| 66 for(Method *mPtr = methodList; *mPtr != NULL; mPtr++) |
| 67 { |
| 68 SEL sel = method_getName(*mPtr); |
| 69 aBlock(cls, sel); |
| 70 } |
| 71 free(methodList); |
| 72 } |
| 73 } |
| 74 |
| 75 |
| 76 @end |
OLD | NEW |