Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #8518 > unrolled thread

JNI generic type of jobject

Started byPhilipp Kraus <philipp.kraus@flashpixx.de>
First post2011-10-04 14:23 +0200
Last post2011-10-04 12:15 -0700
Articles 7 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  JNI generic type of jobject Philipp Kraus <philipp.kraus@flashpixx.de> - 2011-10-04 14:23 +0200
    Re: JNI generic type of jobject Silvio <silvio@moc.com> - 2011-10-04 14:28 +0200
      Re: JNI generic type of jobject Philipp Kraus <philipp.kraus@flashpixx.de> - 2011-10-04 14:49 +0200
      Re: JNI generic type of jobject Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-10-04 07:53 -0700
        Re: JNI generic type of jobject Philipp Kraus <philipp.kraus@flashpixx.de> - 2011-10-04 18:01 +0200
        Re: JNI generic type of jobject Lew <lewbloch@gmail.com> - 2011-10-04 12:01 -0700
          Re: JNI generic type of jobject Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-10-04 12:15 -0700

#8518 — JNI generic type of jobject

FromPhilipp Kraus <philipp.kraus@flashpixx.de>
Date2011-10-04 14:23 +0200
SubjectJNI generic type of jobject
Message-ID<j6etsu$lkf$1@online.de>
Hello,

I use JNI calls for some Java classes. Some Java classes are generic 
classes like:

class mytestclass<T> {

    public native void mymethod();

}

The stub shows:

JNIEXPORT void JNICALL Java_mytestclass_mymethod(JNIEnv* p_env, jobject 
p_object)

How can I get from the jobject which object type is the generic 
parameter T? Because I would
like to create different codes if I do something like:

mytestclass<int> x = new mytestclass<int>();
x.mymethod();

mytestclass<String> x = new mytestclass<String>();
x.mymethod();

Thanks

Phil

[toc] | [next] | [standalone]


#8519

FromSilvio <silvio@moc.com>
Date2011-10-04 14:28 +0200
Message-ID<4e8afbde$0$2529$e4fe514c@news2.news.xs4all.nl>
In reply to#8518
On 10/04/2011 02:23 PM, Philipp Kraus wrote:
> Hello,
>
> I use JNI calls for some Java classes. Some Java classes are generic
> classes like:
>
> class mytestclass<T> {
>
> public native void mymethod();
>
> }
>
> The stub shows:
>
> JNIEXPORT void JNICALL Java_mytestclass_mymethod(JNIEnv* p_env, jobject
> p_object)
>
> How can I get from the jobject which object type is the generic
> parameter T? Because I would
> like to create different codes if I do something like:
>
> mytestclass<int> x = new mytestclass<int>();
> x.mymethod();
>
> mytestclass<String> x = new mytestclass<String>();
> x.mymethod();
>
> Thanks
>
> Phil
>
>

No such thing is possible. Generics are a compile time thing. At runtime 
only the raw types are available.

Silvio

[toc] | [prev] | [next] | [standalone]


#8520

FromPhilipp Kraus <philipp.kraus@flashpixx.de>
Date2011-10-04 14:49 +0200
Message-ID<j6evbu$mmq$1@online.de>
In reply to#8519
On 2011-10-04 14:28:14 +0200, Silvio said:

> On 10/04/2011 02:23 PM, Philipp Kraus wrote:
>> Hello,
>> 
>> I use JNI calls for some Java classes. Some Java classes are generic
>> classes like:
>> 
>> class mytestclass<T> {
>> 
>> public native void mymethod();
>> 
>> }
>> 
>> The stub shows:
>> 
>> JNIEXPORT void JNICALL Java_mytestclass_mymethod(JNIEnv* p_env, jobject
>> p_object)
>> 
>> How can I get from the jobject which object type is the generic
>> parameter T? Because I would
>> like to create different codes if I do something like:
>> 
>> mytestclass<int> x = new mytestclass<int>();
>> x.mymethod();
>> 
>> mytestclass<String> x = new mytestclass<String>();
>> x.mymethod();
>> 
>> Thanks
>> 
>> Phil
>> 
>> 
> 
> No such thing is possible. Generics are a compile time thing. At 
> runtime only the raw types are available.

Why? jobject is a pointer to the Java object that calls the JNI 
function, JNIEnv is also a pointer to the running
environment, so I should get the type of the generic parameter, because 
the runtime / object knows them
so within the JNI call the parameter must exists.

I can also run in the Java class T.getClass().getName() and the Java 
code returns the name of the type
the JNI call should do the same like  JNIEnv->GetObjectClass( 
jobject.getTemplateVar(0) ) but I need
the name / call of the "getTemplateVar(int)" function which gets an 
object to the first template parameter
of the Java object

Thanks

Phil

[toc] | [prev] | [next] | [standalone]


#8525

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2011-10-04 07:53 -0700
Message-ID<C5Fiq.2704$x14.849@newsfe22.iad>
In reply to#8519
On 10/4/11 5:28 AM, Silvio wrote:
> On 10/04/2011 02:23 PM, Philipp Kraus wrote:
>> Hello,
>>
>> I use JNI calls for some Java classes. Some Java classes are generic
>> classes like:
>>
>> class mytestclass<T> {
>>
>> public native void mymethod();
>>
>> }
>>
>> The stub shows:
>>
>> JNIEXPORT void JNICALL Java_mytestclass_mymethod(JNIEnv* p_env, jobject
>> p_object)
>>
>> How can I get from the jobject which object type is the generic
>> parameter T? Because I would
>> like to create different codes if I do something like:
>>
>> mytestclass<int> x = new mytestclass<int>();
>> x.mymethod();
>>
>> mytestclass<String> x = new mytestclass<String>();
>> x.mymethod();
>>
>> Thanks
>>
>> Phil
>>
>>
>
> No such thing is possible. Generics are a compile time thing. At runtime
> only the raw types are available.
>
> Silvio

int is not a valid generic type parameter, as int is a primitive and 
generic types must by Object types.

Also, generics are not the same as C++ templates.  There isn't different 
code created for each concrete usage. Its all exactly the same code.

If you are doing different behavior based on the compile time type, then 
you need  to do a little bit more work to implement the strategy pattern.

class MyTestClass<T> {
   private MyMethodStrategy<T> strategy;

   public mymethod() {
      strategy.mymethod(this);
   }
}

interface MyMethodStrategy<T> {
     void mymethod(MyTestClass<T> testClass);
}


class MyStringMethodStrategy implement MyMethodStrategy<String> {
    public native void mymethod(MyTestClass<String> testClass);
}


class MyIntegerMethodStrategy implement MyMethodStrategy<Integer> {
    public native void mymethod(MyTestClass<Integer> testClass);
}


Then you will have two different native methods to implement each strategy.

[toc] | [prev] | [next] | [standalone]


#8529

FromPhilipp Kraus <philipp.kraus@flashpixx.de>
Date2011-10-04 18:01 +0200
Message-ID<j6fakj$v5v$1@online.de>
In reply to#8525
On 2011-10-04 16:53:22 +0200, Daniel Pitts said:

> On 10/4/11 5:28 AM, Silvio wrote:
>> On 10/04/2011 02:23 PM, Philipp Kraus wrote:
>>> Hello,
>>> 
>>> I use JNI calls for some Java classes. Some Java classes are generic
>>> classes like:
>>> 
>>> class mytestclass<T> {
>>> 
>>> public native void mymethod();
>>> 
>>> }
>>> 
>>> The stub shows:
>>> 
>>> JNIEXPORT void JNICALL Java_mytestclass_mymethod(JNIEnv* p_env, jobject
>>> p_object)
>>> 
>>> How can I get from the jobject which object type is the generic
>>> parameter T? Because I would
>>> like to create different codes if I do something like:
>>> 
>>> mytestclass<int> x = new mytestclass<int>();
>>> x.mymethod();
>>> 
>>> mytestclass<String> x = new mytestclass<String>();
>>> x.mymethod();
>>> 
>>> Thanks
>>> 
>>> Phil
>>> 
>>> 
>> 
>> No such thing is possible. Generics are a compile time thing. At runtime
>> only the raw types are available.
>> 
>> Silvio
> 
> int is not a valid generic type parameter, as int is a primitive and 
> generic types must by Object types.
> 
> Also, generics are not the same as C++ templates.  There isn't 
> different code created for each concrete usage. Its all exactly the 
> same code.
> 
> If you are doing different behavior based on the compile time type, 
> then you need  to do a little bit more work to implement the strategy 
> pattern.
> 
> class MyTestClass<T> {
>    private MyMethodStrategy<T> strategy;
> 
>    public mymethod() {
>       strategy.mymethod(this);
>    }
> }
> 
> interface MyMethodStrategy<T> {
>      void mymethod(MyTestClass<T> testClass);
> }
> 
> 
> class MyStringMethodStrategy implement MyMethodStrategy<String> {
>     public native void mymethod(MyTestClass<String> testClass);
> }
> 
> 
> class MyIntegerMethodStrategy implement MyMethodStrategy<Integer> {
>     public native void mymethod(MyTestClass<Integer> testClass);
> }
> 
> 
> Then you will have two different native methods to implement each strategy.

This a very good solution, I implement my own pattern
Thanks

[toc] | [prev] | [next] | [standalone]


#8541

FromLew <lewbloch@gmail.com>
Date2011-10-04 12:01 -0700
Message-ID<10625927.131.1317754868953.JavaMail.geo-discussion-forums@prfc6>
In reply to#8525
Daniel Pitts wrote:
>>  Philipp Kraus wrote:
>>> I use JNI calls for some Java classes. Some Java classes are generic
>>> classes like:
>>>
>>> class mytestclass<T> {
>>>
>>>   public native void mymethod();
>>>
>>> }
>>>
>>> The stub shows:
>>>
>>> JNIEXPORT void JNICALL Java_mytestclass_mymethod(JNIEnv* p_env, jobject
>>> p_object)
>>>
>>> How can I get from the jobject which object type is the generic
>>> parameter T? Because I would
>>> like to create different codes if I do something like:
>>>
>>>   mytestclass<int> x = new mytestclass<int>();
>>>   x.mymethod();
>>>
>>>   mytestclass<String> x = new mytestclass<String>();
>>>   x.mymethod();
>>
> int is not a valid generic type parameter, as int is a primitive and 
> generic types must be Object types.
> 
> Also, generics are not the same as C++ templates.  There isn't different 
> code created for each concrete usage. Its all exactly the same code.
> 
> If you are doing different behavior based on the compile time type, then 
> you need  to do a little bit more work to implement the strategy pattern.
> 
> class MyTestClass<T> {
>    private MyMethodStrategy<T> strategy;
> 
>    public mymethod() {
>       strategy.mymethod(this);
>    }
> }
> 
> interface MyMethodStrategy<T> {
>      void mymethod(MyTestClass<T> testClass);
> }
> 
> 
> class MyStringMethodStrategy implement MyMethodStrategy<String> {
>     public native void mymethod(MyTestClass<String> testClass);
> }
> 
> 
> class MyIntegerMethodStrategy implement MyMethodStrategy<Integer> {
>     public native void mymethod(MyTestClass<Integer> testClass);
> }
> 
> 
> Then you will have two different native methods to implement each strategy.

Kudos for a great answer!
+1

This pattern or ones like it are frequent and very helpful in generics programming.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#8544

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2011-10-04 12:15 -0700
Message-ID<DXIiq.1731$Gu.1318@newsfe14.iad>
In reply to#8541
On 10/4/11 12:01 PM, Lew wrote:
> Daniel Pitts wrote:
>>>   Philipp Kraus wrote:
>>>> I use JNI calls for some Java classes. Some Java classes are generic
>>>> classes like:
>>>>
>>>> class mytestclass<T>  {
>>>>
>>>>    public native void mymethod();
>>>>
>>>> }
>>>>
>>>> The stub shows:
>>>>
>>>> JNIEXPORT void JNICALL Java_mytestclass_mymethod(JNIEnv* p_env, jobject
>>>> p_object)
>>>>
>>>> How can I get from the jobject which object type is the generic
>>>> parameter T? Because I would
>>>> like to create different codes if I do something like:
>>>>
>>>>    mytestclass<int>  x = new mytestclass<int>();
>>>>    x.mymethod();
>>>>
>>>>    mytestclass<String>  x = new mytestclass<String>();
>>>>    x.mymethod();
>>>
>> int is not a valid generic type parameter, as int is a primitive and
>> generic types must be Object types.
>>
>> Also, generics are not the same as C++ templates.  There isn't different
>> code created for each concrete usage. Its all exactly the same code.
>>
>> If you are doing different behavior based on the compile time type, then
>> you need  to do a little bit more work to implement the strategy pattern.
>>
>> class MyTestClass<T>  {
>>     private MyMethodStrategy<T>  strategy;
>>
>>     public mymethod() {
>>        strategy.mymethod(this);
>>     }
>> }
>>
>> interface MyMethodStrategy<T>  {
>>       void mymethod(MyTestClass<T>  testClass);
>> }
>>
>>
>> class MyStringMethodStrategy implement MyMethodStrategy<String>  {
>>      public native void mymethod(MyTestClass<String>  testClass);
>> }
>>
>>
>> class MyIntegerMethodStrategy implement MyMethodStrategy<Integer>  {
>>      public native void mymethod(MyTestClass<Integer>  testClass);
>> }
>>
>>
>> Then you will have two different native methods to implement each strategy.
>
> Kudos for a great answer!
> +1
>
> This pattern or ones like it are frequent and very helpful in generics programming.
>
They also provide greater flexibility for pluggable (user-defined) 
behavior.  When applicable, I often replace protected methods with 
strategy interfaces. Especially if any two protected methods are 
independent of one another in the same class.  Truth be told, I usually 
don't create protected methods at all any more, unless they are in some 
sort of adapter class for a "un-handled use-case" or some such.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web