Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: JNI types and usage newbie



On Friday, April 30, 2004, at 07:52 AM, Dmitry Markman wrote:

here is what I mean:


on native side you have NSString * so you send it to java side as int:
NSString *nsstr;
int arg = (int)nsstr;
env->Call<Some>Method(mid,....,arg,...)

Java side

NSString jnsstr = sommemethod(arg)

so I'm asking about <somemethod> implementation.

I'm still not sure I'm entirely following - here maybe it's a JNI thing.
You are invoking a java method passing it as an arg a int NSString *
you want that to return a NSString, or int NSString* ???

Or are you asking how do you make your method a selector from Java
With JNIDirect
protected static int getSelector(String name) {
return sel_registerName((name + '\0').getBytes());
}

or how do you get an IMP implementation pointer for your java method as a Cocoa selector?
With JNIDirect

pollptr = jnidirect.Linker.newMethodClosure(pollMethod,this);

How do you tell Cocoa that you have a class with this java method as a selector with the given implementation?
Create the class definition
if (createClassDefinition("nsqtmp3","NSObject")) {
nsqtmp3_class = getClass(getName());
}
else System.out.println("nsqtmp3: failed to define class");

then add your methods

initptr = jnidirect.Linker.newMethodClosure(initMethod,this);
// Add the init method indicating the int args should be treated as selectors
ObjectiveCMethod initobjc = new ObjectiveCMethod(class_name,"init",initptr,initMethod,new String[] { "v0@4","i","i" });
pollptr = jnidirect.Linker.newMethodClosure(pollMethod,this);
ObjectiveCMethod pollobjc = new ObjectiveCMethod(class_name,"pollMovie",pollptr,pollMethod,new String[] { "v0@4","i","i","^{NSTimer}" });
deallocptr = jnidirect.Linker.newMethodClosure(deallocMethod,this);
ObjectiveCMethod deallocobjc = new ObjectiveCMethod(class_name,"dealloc",deallocptr,deallocMethod,null);
addInstanceMethods(new ObjectiveCMethod[] { initobjc, pollobjc, deallocobjc });
// Get the selectors
initsel = getSelector("init");
deallocsel = getSelector("dealloc");
pollsel = getSelector("pollMovie");

Your implementation methods must start with the two 'hidden' variables that ObjectiveC always passes, self and _cmd the identity of the caller if I remember right? Anyhow ObjectiveC needs them, hides them in normal gcc compilation but Java needs to provide them.

public void pollMovie(int hidden_self,int _cmd,int timerptr) {

I anticipated you would need those before I tried my first home grown selector from what the language guide indicated. Kind of proud of that although the necessity probably wasn't all that difficult to predict.
There is of course no native side. You do all this from Java, the JNI glue is generated by JNIDirect. I'm not sure what part of this is still supposed to be impossible from your viewpoint but none of it is using JNIDirect. You can make your Java class a brand new full-fledged Cocoa class as far as ObjectiveC is concerned. Or if you don't want to keep the abstraction that the Java class corresponds one-to-one to a Cocoa class you can make your Java class a ObjectiveC class factory that makes as many different kinds of Cocoa classes as you want. Maybe, I haven't tried that.

In the mechanics of all this I have not run into anything impossible? Maybe something in the JNIDirect glue is what you're looking for and would be worth looking at. Or you can just use JNIDirect. I bet with a little time you could probably figure out how to get my NSOpenGLView working and I could have some nice red rectangles or something visual for a sample of java and cocoa doing that.

Mike Hall <mikehall at spacestar dot net>
<http://www.spacestar.net/users/mikehall>
_______________________________________________
java-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/java-dev
Do not post admin requests to the list. They will be ignored.


References: 
 >Re: JNI types and usage newbie (From: Dmitry Markman <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.