Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!nx02.iad01.newshosting.com!newshosting.com!69.16.185.11.MISMATCH!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: JNI generic type of jobject Date: Tue, 4 Oct 2011 12:01:08 -0700 (PDT) Organization: http://groups.google.com Lines: 67 Message-ID: <10625927.131.1317754868953.JavaMail.geo-discussion-forums@prfc6> References: <4e8afbde$0$2529$e4fe514c@news2.news.xs4all.nl> Reply-To: comp.lang.java.programmer@googlegroups.com NNTP-Posting-Host: 2620:0:1000:437c:224:d7ff:fe69:5838 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: posting.google.com 1317754869 25060 127.0.0.1 (4 Oct 2011 19:01:09 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 4 Oct 2011 19:01:09 +0000 (UTC) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=2620:0:1000:437c:224:d7ff:fe69:5838; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T User-Agent: G2/1.0 X-Google-Web-Client: true Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:8541 Daniel Pitts wrote: >> Philipp Kraus wrote: >>> I use JNI calls for some Java classes. Some Java classes are generic >>> classes like: >>> >>> class mytestclass { >>> >>> 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 x = new mytestclass(); >>> x.mymethod(); >>> >>> mytestclass x = new mytestclass(); >>> 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 { > private MyMethodStrategy strategy; > > public mymethod() { > strategy.mymethod(this); > } > } > > interface MyMethodStrategy { > void mymethod(MyTestClass testClass); > } > > > class MyStringMethodStrategy implement MyMethodStrategy { > public native void mymethod(MyTestClass testClass); > } > > > class MyIntegerMethodStrategy implement MyMethodStrategy { > public native void mymethod(MyTestClass 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