Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #8541
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: JNI generic type of jobject |
| Date | 2011-10-04 12:01 -0700 |
| Organization | http://groups.google.com |
| Message-ID | <10625927.131.1317754868953.JavaMail.geo-discussion-forums@prfc6> (permalink) |
| References | <j6etsu$lkf$1@online.de> <4e8afbde$0$2529$e4fe514c@news2.news.xs4all.nl> <C5Fiq.2704$x14.849@newsfe22.iad> |
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
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar
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
csiph-web