[Adium-commits] adium 2046:6bc140dd9efb: Convert our debug classes over to moder...

adium-commits at adiumx.com adium-commits at adiumx.com
Wed Apr 29 23:26:15 UTC 2009


details:	http://hg.adiumx.com/adium/rev/6bc140dd9efb
revision:	2046:6bc140dd9efb
author:		David Smith <catfish.man at gmail.com>
date:		Wed Apr 29 16:25:58 2009 -0700

Convert our debug classes over to modern objc api

diffstat:

 Source/AIAdium.m           |   1 +
 Source/AIDictionaryDebug.m |  61 ++----------------------------
 Source/AIStringDebug.m     |  53 +-------------------------
 3 files changed, 8 insertions(+), 107 deletions(-)

diffs (182 lines):

diff -r 363bd358be29 -r 6bc140dd9efb Source/AIAdium.m
--- a/Source/AIAdium.m	Wed Apr 29 10:34:00 2009 -0700
+++ b/Source/AIAdium.m	Wed Apr 29 16:25:58 2009 -0700
@@ -52,6 +52,7 @@
 #import <Sparkle/Sparkle.h>
 #import "ESAddressBookIntegrationAdvancedPreferences.h"
 #import <Adium/AdiumAuthorization.h>
+#import <sys/sysctl.h>
 
 #ifdef DEBUG_BUILD
 #import "ESDebugController.h"
diff -r 363bd358be29 -r 6bc140dd9efb Source/AIDictionaryDebug.m
--- a/Source/AIDictionaryDebug.m	Wed Apr 29 10:34:00 2009 -0700
+++ b/Source/AIDictionaryDebug.m	Wed Apr 29 16:25:58 2009 -0700
@@ -33,33 +33,17 @@
 
 #ifdef DEBUG_BUILD
 #import <objc/objc-class.h>
-
- at interface AIDictionaryDebug ()
-+ (IMP)replaceSelector:(SEL)sel ofClass:(Class)oldClass withClass:(Class)newClass;
- at end
-
 #endif
 
 @implementation AIDictionaryDebug
 
 #ifdef DEBUG_BUILD
 
-typedef void (*SetObjectForKeyIMP)(id, SEL, id, id);
-SetObjectForKeyIMP	originalSetObjectForKey = nil;
-
-typedef void (*RemoveObjectForKeyIMP)(id, SEL, id);
-RemoveObjectForKeyIMP	originalRemoveObjectForKey = nil;
-
-extern void _objc_flush_caches(Class);
-
 + (void)load
 {
-	originalSetObjectForKey = (SetObjectForKeyIMP)[AIDictionaryDebug replaceSelector:@selector(setObject:forKey:)
-																			 ofClass:NSClassFromString(@"NSCFDictionary")
-																		   withClass:[AIDictionaryDebug class]];
-	originalRemoveObjectForKey = (RemoveObjectForKeyIMP)[AIDictionaryDebug replaceSelector:@selector(removeObjectForKey:)
-																				ofClass:NSClassFromString(@"NSCFDictionary")
-																			  withClass:[AIDictionaryDebug class]];
+	Class NSCFDictionaryClass = NSClassFromString(@"NSCFDictionary");
+	method_exchangeImplementations(class_getInstanceMethod(self, @selector(setObject:forKey:)), class_getInstanceMethod(NSCFDictionaryClass, @selector(setObject:forKey:)));
+	method_exchangeImplementations(class_getInstanceMethod(self, @selector(removeObjectForKey:)), class_getInstanceMethod(NSCFDictionaryClass, @selector(removeObjectForKey:)));
 }
 
 + (void)breakpoint
@@ -73,7 +57,7 @@
 	NSAssert3(object != nil, @"%@: Attempted to set %@ for %@",self,object,key);
 	NSAssert3(key != nil, @"%@: Attempted to set %@ for %@",self,object,key);
 
-	originalSetObjectForKey(self, @selector(setObject:forKey:),object,key);
+	method_invoke(self, class_getInstanceMethod([AIDictionaryDebug class], @selector(setObject:forKey:)), object, key);
 }
 
 - (void)removeObjectForKey:(id)key
@@ -81,42 +65,7 @@
 	if (!key) [AIDictionaryDebug breakpoint];
 	NSAssert1(key != nil, @"%@: Attempted to remove a nil key",self);
 
-	originalRemoveObjectForKey(self, @selector(removeObjectForKey:),key);
-}
-
-+ (IMP)replaceSelector:(SEL)sel ofClass:(Class)oldClass withClass:(Class)newClass
-{
-	IMP original = [oldClass instanceMethodForSelector:sel];
-
-	if (!original) {
-		NSLog(@"Cannot find implementation for '%@' in %@",
-		      NSStringFromSelector(sel),
-		      NSStringFromClass(oldClass));
-		return NULL;
-	}
-
-	Method method = class_getInstanceMethod(oldClass, sel); 
-
-	// original to change
-	if (!method) {
-		NSLog(@"Cannot find method for '%@' in %@",
-		      NSStringFromSelector(sel),
-		      NSStringFromClass(oldClass));
-		return NULL;
-	}
-
-	IMP new = class_getMethodImplementation(newClass, sel); // new method to use
-	if (!new) {
-		NSLog(@"Cannot find implementation for '%@' in %@",
-		      NSStringFromSelector(sel),
-		      NSStringFromClass(newClass));
-		return NULL;
-	}
-
-	method_setImplementation(method, new);
-	_objc_flush_caches(oldClass);
-
-	return original;
+	method_invoke(self, class_getInstanceMethod([AIDictionaryDebug class], @selector(removeObjectForKey:)), key);
 }
 
 #endif
diff -r 363bd358be29 -r 6bc140dd9efb Source/AIStringDebug.m
--- a/Source/AIStringDebug.m	Wed Apr 29 10:34:00 2009 -0700
+++ b/Source/AIStringDebug.m	Wed Apr 29 16:25:58 2009 -0700
@@ -33,27 +33,15 @@
 
 #ifdef DEBUG_BUILD
 #import <objc/objc-class.h>
-
- at interface AIStringDebug ()
-+ (IMP)replaceSelector:(SEL)sel ofClass:(Class)oldClass withClass:(Class)newClass;
- at end
-
 #endif
 
 @implementation AIStringDebug
 
 #ifdef DEBUG_BUILD
 
-typedef id (*StringByAppendingStringIMP)(id, SEL, id);
-StringByAppendingStringIMP	originalStringByAppendingString = nil;
-
-extern void _objc_flush_caches(Class);
-
 + (void)load
 {
-	originalStringByAppendingString = (StringByAppendingStringIMP)[AIStringDebug replaceSelector:@selector(stringByAppendingString:)
-																						 ofClass:NSClassFromString(@"NSCFString")
-																					   withClass:[AIStringDebug class]];
+	method_exchangeImplementations(class_getInstanceMethod(self, @selector(stringByAppendingString:)), class_getInstanceMethod(NSClassFromString(@"NSCFDictionary"), @selector(stringByAppendingString:)));
 }
 
 + (void)breakpoint
@@ -64,44 +52,7 @@
 - (NSString *)stringByAppendingString:(NSString *)string
 {
 	if (!string) [AIStringDebug breakpoint];
-	return originalStringByAppendingString(self, @selector(stringByAppendingString:),string);
-}
-
-+ (IMP)replaceSelector:(SEL)sel ofClass:(Class)oldClass withClass:(Class)newClass
-{
-    IMP original = [oldClass instanceMethodForSelector:sel];
-	
-	if (!original) {
-		NSLog(@"Cannot find implementation for '%@' in %@",
-			  NSStringFromSelector(sel),
-			  NSStringFromClass(oldClass));
-		return NULL;
-	}
-	
-	Method method = class_getInstanceMethod(oldClass, sel); 
-	
-	// original to change
-    if (!method) {
-        NSLog(@"Cannot find method for '%@' in %@",
-			  NSStringFromSelector(sel),
-			  NSStringFromClass(oldClass));
-        return NULL;
-    }
-	
-	// new method to use
-    IMP new = class_getMethodImplementation(newClass, sel);
-	if (!new) {
-		NSLog(@"Cannot find implementation for '%@' in %@",
-			  NSStringFromSelector(sel),
-			  NSStringFromClass(newClass));
-		return NULL;
-	}
-
-	method_setImplementation(method, new);
-	
-    _objc_flush_caches(oldClass);
-	
-    return original;
+	return method_invoke(self, class_getInstanceMethod([AIStringDebug class], @selector(stringByAppendingString:)), string);
 }
 
 #endif




More information about the commits mailing list