Hi Blair,
i have a general purpose JNI object constructor method called
"JNI_CreateInstance" that works like this (i added the method call at the
end)

/*
Parameters:
-"env":the current java thread.
-"className":a fully cualified name.
-"cons_sig": a constructor signature.if not suplied,the default class
constructor will be called.
-"...":additional parameters required for the creation of an object of type
"className".
Returns:
an object of type "className" or NULL if error.
Description:
Creates an instance of an object of type "className" calling the properly
java constructor.
*/
jobject JNI_CreateInstance(JNIEnv *env,const char * cons_sig,char
*className,...){ 
  va_list ap;
  jmethodID mId;
  jobject o;
  
  if((env!=NULL)&&(className!=NULL)){
    jclass c= (*env)->FindClass(env,className);
    if((*env)->ExceptionOccurred(env)){
        EWT_DEBUG_LOG_1(0,"Class not found %s\n",className);
        (*env)->ExceptionDescribe(env);
        return NULL;
    }
    if (cons_sig==NULL){
     mId=(*env)->GetMethodID(env,c,"<init>", "()V");
   }
   else{
     char *txt=Ewt_Malloc(3+strlen(cons_sig)+1);
     int i;
      txt[0]='(';
     for(i=0;cons_sig[i]!='\0';i++){
      txt[i+1]=cons_sig[i];
    }
    txt[++i]=')';
    txt[++i]='V';
    txt[++i]='\0';  
    mId=(*env)->GetMethodID(env,c,"<init>",txt);
     }
   if ( mId == NULL ) {/*an exception has already been thrown*/
      EWT_DEBUG_LOG(0,"method <init> not found\n");
      (*env)->ExceptionDescribe(env);
      (*env)->DeleteLocalRef(env,c); /* free local reference*/      
      return NULL;
   }
   va_start(ap,className);
   o=(*env)->NewObjectV(env,c,mId,ap);
  (*env)->DeleteLocalRef(env,c);//i do not need this
   va_end(ap);
   if (o== NULL ) {/*an exception has already been thrown*/      
       EWT_DEBUG_LOG(0,"object could not be created\n");
       (*env)->ExceptionDescribe(env);
       (*env)-> DeleteLocalRef(env,o);
      return NULL;
   }
   return o;
  }
  return NULL;
}

/*END*/

METHOD
 CALL:

mouseEvent=JNI_CreateInstance(env,"Ljava/ewt/Component;IJIIIIZ","java/ewt/ev
ent/MouseEvent",source->instance,event->status,NGL_GetTime(),0,(event->x ),(
event->y),0,JNI_FALSE);

Note: "source->instance"  is a reference to a subclass of Component. I�m
using JDK1.2.2.

After the mouseEvent is created i make a call to the EventQueue to put the
event on the Java side .Something like this:

 cuevent=(*env)->NewGlobalRef(env,mouseEvent);
 .....//some code
 (*env)->CallVoidMethod(env,defaultEventQueue,mId,cuevent);

On the Java side the EventQueue have acces to the "mouseEvent" only if it is
passed as a parameter from the native code like above.Setting the mouseEvent
as an instance variable of the EventQueue class (through  a
(*env)->SetObjectField(env,defaultEventQueue,filedID,cuevent) call)  works
fine but when i try to get acces to the event the VM jumps nowere. The event
is not null but the VM doesn�t find it!





----- Original Message -----
From: "Blair Wyman" <[EMAIL PROTECTED]>
To: "JDJList" <[EMAIL PROTECTED]>
Sent: Friday, January 31, 2003 6:41 PM
Subject: [jdjlist] Re: JNI



Presumably you're doing something like:

      clz = (*env)->FindClass(env, "java/awt/MouseEvent");
      ctor = (*env)->GetMethodID(env, clz, "<init>",
"(Ljava/lang/Component;IJIIIIZI)V");
      obj = (*env)->NewObject(env, clz, ctor, PARM1, PARM2...
...

What are you passing as PARM1 to the MouseEvent constructor?  If I read the
1.4.1 code correctly, this "source object" parameter should eventually get
stored in the protected transient "source" field of EventObject, and should
therefore be accessible to Java via getSource().

Is what you're putting there a reference to a Component (or some subclass
of Component)?

On Thursday, 01/30/2003 at 11:20 CET, "Tomas Bonilla Rau"
<[EMAIL PROTECTED]> wrote:
> i am having a java application going to the native  side (written in C)
to
> build an Event (lets say a  MouseEvent(source,id,....)). The thread goes
to the
> java side,puts the  event in a queue and then back to native to wait for
more
> Events. The  constructor for the MouseEvent is called from the native
side
> sucessfully.
> The problem: on the java side I have  no access to the source of the
> MouseEvent.The  source is not null but I can not get the class or even
call any
> of its  methods. The Thread runs stand alone, the source is saved as
global
> on a buffer at the native side and there is a reference to the source on
the
> java side as well.
> Could please  anyone tell if I am missing  something related to JNI so
that i
> can not access the source at the java  side.?

-blair

Blair Wyman -- iSeries JVM -- (507) 253-2891
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"It is a sobering thought that when Mozart was my age,
he had been dead for two years." -- Tom Lehrer




____________________________________________________
To change your JDJList options, please visit:
http://www.sys-con.com/java/list.cfm

Be respectful! Clean up your posts before replying
____________________________________________________


____________________________________________________
To change your JDJList options, please visit:
http://www.sys-con.com/java/list.cfm

Be respectful! Clean up your posts before replying
____________________________________________________

Reply via email to