JNI RegisterNatives from java side

Discussion in 'Java' started by Ali, Feb 23, 2005.

  1. Ali

    Ali Guest

    I am looking for an equivalent of RegisterNatives method in JNI which
    can be used in java and not C.

    Going a bit deeper, there is this JNI dll which has a standard java
    format for its methods (Java_packageName_className) and for several
    reasons I have to map the methods to some java classes in another
    package and with different names.

    A quick solution would be using RegisterNatives in the JNI dll and
    manually specifying the class, however, one rule of the game is to not
    touch the jni dll source code.

    Now I wonder if there is any equivalent of RegisterNatives which can
    be used in the java files. Any decent examples are well appreciated.
    Ali, Feb 23, 2005
    #1
    1. Advertising

  2. On 22 Feb 2005 21:55:27 -0800, Ali wrote:
    > I am looking for an equivalent of RegisterNatives method in JNI which
    > can be used in java and not C.
    >
    > Going a bit deeper, there is this JNI dll which has a standard java
    > format for its methods (Java_packageName_className) and for several
    > reasons I have to map the methods to some java classes in another
    > package and with different names.
    >
    > A quick solution would be using RegisterNatives in the JNI dll and
    > manually specifying the class, however, one rule of the game is to
    > not touch the jni dll source code.
    >
    > Now I wonder if there is any equivalent of RegisterNatives which can
    > be used in the java files. Any decent examples are well appreciated.


    I know of no way to do the equivalent of RegisterNatives from Java,
    however if the new class is a subclass of the original, it will
    inherit the existing methods.

    Or you could use compostion, and invoke the original native methods
    from similarly named methods in your new class. That's not much more
    work per method than declaring it as a native method would have been.

    Finally , you could write a new DLL that properly "belongs" to the new
    class but is dependent on the original DLL. From its JNI_Onload,
    invoke RegisterNatives.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Feb 23, 2005
    #2
    1. Advertising

  3. On 23 Feb 2005 08:02:02 +0100, Gordon Beaton wrote:
    > Or you could use compostion,


    I seem to have spelled "delegation" wrong here...

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Feb 23, 2005
    #3
  4. Ali

    Guest

    Gordon,

    I didn't quite get what you mean by delegation here.

    Assume we have this method in the dll:

    Java_somePackage_SomeClass_someMethod

    and this method in the java side:

    anotherPackage.AnotherClass.anotherMethod

    Now, what's the event? who's the listener? Could you provide a minimum
    example.
    , Feb 23, 2005
    #4
  5. On 23 Feb 2005 02:30:57 -0800, wrote:
    > Assume we have this method in the dll:
    >
    > Java_somePackage_SomeClass_someMethod
    >
    > and this method in the java side:
    >
    > anotherPackage.AnotherClass.anotherMethod
    >
    > Now, what's the event? who's the listener? Could you provide a
    > minimum example.


    If your DLL contains that method, the corresponding class looks
    something like this (for the sake of the example I've also added a
    static method and some arguments):

    public class SomeClass {
    public native void someMethod(int foo);
    public static native void someStaticMethod(String gurka);
    }

    Keep an instance of SomeClass in your AnotherClass objects. Call its
    methods when yours are called:

    public class AnotherClass {
    SomeClass sc = new SomeClass();

    public native void anotherMethod(int bar) {
    sc.someMethod(bar);
    }

    public static void anotherStaticMethod(String baz) {
    Someclass.someStaticMethod(baz);
    }
    }

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Feb 23, 2005
    #5
  6. On 23 Feb 2005 13:12:38 +0100, Gordon Beaton wrote:
    > public native void anotherMethod(int bar) {
    > sc.someMethod(bar);
    > }


    That should have been:

    public void anotherMethod(int bar) {
    sc.someMethod(bar);
    }

    (cut'n'paste error...)

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Feb 23, 2005
    #6
  7. Ali

    Guest

    Thanks for the intuitive example Gordon. I understand you suggest to
    use a proxy class to handle this problem.

    Now, this question could be kind of irrelevant. Is there any way to
    'hide' the proxy class inside the actual class? I am asking this as it
    is important for this case to have both classes in *one* java
    compilation unit, ie in the same file.
    , Feb 23, 2005
    #7
  8. On 23 Feb 2005 04:37:00 -0800, wrote:
    > Thanks for the intuitive example Gordon. I understand you suggest to
    > use a proxy class to handle this problem.
    >
    > Now, this question could be kind of irrelevant. Is there any way to
    > 'hide' the proxy class inside the actual class? I am asking this as it
    > is important for this case to have both classes in *one* java
    > compilation unit, ie in the same file.


    You can include both classes in one source file, as long as only one
    of them is public.

    Note that you can't make the original class an inner class of the new
    one, or its DLL will need to change.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Feb 23, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Hunsley

    IBM's JNI fails where Sun's JNI works

    Alex Hunsley, Nov 3, 2003, in forum: Java
    Replies:
    4
    Views:
    847
    Alex Hunsley
    Nov 4, 2003
  2. Pasturel Jean-Louis

    Porting JNI Windows under JNI LINUX + Wine ?

    Pasturel Jean-Louis, Feb 29, 2004, in forum: Java
    Replies:
    5
    Views:
    901
    Pasturel Jean-Louis
    Mar 3, 2004
  3. vasanth
    Replies:
    0
    Views:
    2,682
    vasanth
    Jan 25, 2005
  4. vasanth
    Replies:
    0
    Views:
    619
    vasanth
    Jan 25, 2005
  5. bgabrhelik
    Replies:
    0
    Views:
    799
    bgabrhelik
    Sep 29, 2009
Loading...

Share This Page