 |
 |
Sharing Objects Java <=> Objective-C
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Edmond, OK USA
Status:
Offline
|
|
I know it's possible but I can't find any way to use a Java library from within Objective-C. For example, for all I can tell Acquisition is written in Objective-C, but utilizes many network components that are written in Java. How is this accomplished?
Say I have these classes in Java:
Code:
public class Manager
{
public Bean getBean()
{
return new BeanImpl();
}
public static Manager getInstance()
{
return new Manager();
}
}
public interface Bean
{
// . . .
void x();
}
public class BeanImpl implements Bean
{
// . . .
public void x()
{
// . . .
}
}
How would I go about doing this in Objective-C:
Code:
// This is ObjC pseudo-code
Manager man = Manager.getInstance();
Bean bean = man.getBean();
bean.x();
This must be possible, but Apple doesn't have any documentation that I can find. Also, what is the overhead of this? Is there a load of boxing or marshalling going on?
And is the reverse possible? Can I send an ObjC object into Java and call methods on it?
I would like to avoid JNI if at all possible.
This is on Panther and I have all the developer tools installed, in case the info is there.
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
|
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Edmond, OK USA
Status:
Offline
|
|
Originally posted by Chuckit:
Courtesy of Cocoa Dev Central: The Java Bridge
Thank you very much. I had never seen that site before, very useful.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Oct 2000
Location: Edinburgh, Scotland
Status:
Offline
|
|
Firstly, I apologise for dredging up such an old thread.
Secondly, I'm trying to do the reverse of what's in Cocoa Dev Central's web page: I need to access an Objective-C object from within my Java code. I've got a cocoa-java project and I need to call various CFM routines which can only be done via Obj-C if I've understood correctly (this bit works correctly in its own mini obj-c project).
What I'm trying as a test is the following:
Code:
testClass.m
@implementation testClass
-(void) testMethod
{
NSLog(@"test line printed\n");
}
@end
In my Java code, I want to create an instance of testClass and call testMethod. I was under the impression that the Java-ObjC bridge would allow me just to do
Code:
testClass myTestClass = new testClass();
but it moans at compile time saying "testClass" can't be found/resolved.
I'd be really grateful for any pointers as my project's presently sitting in front of a bloody big brick wall! Thanks.
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Firstly, in both Java and Objective-C, class names should start with a capital letter.
Second, I believe this thread on CocoaDev covers the issue pretty well (though I'm not sure you'll really love the answers).
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
|
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Oct 2000
Location: Edinburgh, Scotland
Status:
Offline
|
|
Originally Posted by Angus_D
Use JNI.
Care to elaborate? Please?
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
|
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jun 2005
Status:
Offline
|
|
Java can't see the obj-c methods but you can talk directly to your Obj-C objects by using NSKeyValueCoding
public void someJavaMethod(NSObject objcObject) {
objcObject.valueForKey("testMethod");
}
if you want to return a value you use valueForKey(), if you want to pass a value you use takeValueForKey() and do the following:
in Obj-C change the method to
- (void)setTestMethod: (NSString*)msg
{
NSLog(msg);
}
(note that the 'set' prefix is nessesary as takeValueForKey() looks for a setter method)
then in java call it:
public void someJavaMethod(NSObject objcObject) {
objcObject.takeValueForKey("this is a test message from java!", "testMethod");
}
Obviously this can be a bit cumbersome but if all you wanna do is call some of your existing code it can save some time.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|