2 * Copyright 2007, 2008 Duncan McGregor
4 * This file is part of Rococoa, a library to allow Java to talk to Cocoa.
6 * Rococoa is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Rococoa is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with Rococoa. If not, see <http://www.gnu.org/licenses/>.
20 #import "ProxyForJava.h"
22 id proxyForJavaObject(void* methodInvokedCallback, void* methodSignatureCallback) {
23 return [[ProxyForJava alloc] initWithMethodInvokedCallback: methodInvokedCallback methodSignatureCallback: methodSignatureCallback];
26 @interface ProxyForJava (Private)
27 + (char *)cstringPtrForSelector:(CFStringRef) cfstring;
30 @implementation ProxyForJava
32 - (id) initWithMethodInvokedCallback: (void*) theMethodInvokedCallback methodSignatureCallback: (void*) theMethodSignatureCallback {
35 methodInvokedCallback = theMethodInvokedCallback;
36 methodSignatureCallback = theMethodSignatureCallback;
41 // Passes a given invocation to the real object the proxy represents.
42 - (void)forwardInvocation:(NSInvocation *) anInvocation {
43 // calls back to Java on methodInvokedCallback,
44 SEL selector = [anInvocation selector];
45 NSString* selectorName = NSStringFromSelector(selector);
46 // NSLog(@"forwardInvocation for %@", selectorName);
47 methodInvokedCallback([ProxyForJava cstringPtrForSelector:(CFStringRef) selectorName], anInvocation);
50 - (BOOL)respondsToSelector:(SEL)aSelector {
51 // NSString* selectorName = NSStringFromSelector(aSelector);
52 // NSLog(@"respondsToSelector %@", selectorName);
53 NSMethodSignature* signature = [self methodSignatureForSelector:aSelector];
54 return signature != nil;
57 // Override this method in your concrete subclass to return a proper NSMethodSignature object for the
58 // given selector and the class your proxy objects stand in for.
59 - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
60 NSString* selectorName = NSStringFromSelector(aSelector);
61 // NSLog(@"methodSignatureForSelector %@", selectorName);
62 if (aSelector == @selector(hash) || aSelector == @selector(isEqual:)) {
63 return [super methodSignatureForSelector: aSelector];
65 const char* methodSignature = methodSignatureCallback([ProxyForJava cstringPtrForSelector:(CFStringRef) selectorName]);
66 if (NULL == methodSignature) {
67 // No method with signature of selector implemented
68 // NSLog(@"No method with signature of selector implemented: %@", selectorName);
71 NSMethodSignature* result = [NSMethodSignature signatureWithObjCTypes: methodSignature];
75 + (const char *)cstringPtrForSelector:(CFStringRef) selectorName {
76 const char* selectorNameChar = CFStringGetCStringPtr(selectorName, CFStringGetFastestEncoding(selectorName));
77 if (NULL == selectorNameChar) {
78 // NSLog(@"CFStringGetCStringPtr failed for selector %@", selectorName);
81 return selectorNameChar;