Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post01.iad.highwinds-media.com!newsfe14.iad.POSTED!83aa503d!not-for-mail From: Daniel Pitts User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:6.0.2) Gecko/20110902 Thunderbird/6.0.2 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: JNI generic type of jobject References: <4e8afbde$0$2529$e4fe514c@news2.news.xs4all.nl> <10625927.131.1317754868953.JavaMail.geo-discussion-forums@prfc6> In-Reply-To: <10625927.131.1317754868953.JavaMail.geo-discussion-forums@prfc6> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Lines: 73 Message-ID: X-Complaints-To: abuse@newsrazor.net NNTP-Posting-Date: Tue, 04 Oct 2011 19:15:47 UTC Date: Tue, 04 Oct 2011 12:15:46 -0700 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:8544 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 { >>>> >>>> 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. > 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.