Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Re: generic constructor call with concret type Date: Wed, 05 Oct 2011 22:14:09 +0200 Lines: 75 Message-ID: <9f3s4iFrcmU1@mid.individual.net> References: <0dba7690-f51d-4b1f-bd59-92463784de71@20g2000yqq.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net s0r9SjK9HtTdinNRrehWeAmLntSFFG7TUyzbQtHLrptItLLWg= Cancel-Lock: sha1:5XiP5w5LUkiYlwxBjNQIrkN6xBY= User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.23) Gecko/20110922 Lightning/1.0b2 Thunderbird/3.1.15 In-Reply-To: Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:8580 On 10/05/2011 11:58 AM, Philipp Kraus wrote: > On 2011-10-05 11:22:50 +0200, Robert Klemme said: > >> @SuppressWarnings("unchecked") >> public Run(Class cl) { >> if (cl == Float.class) { >> helper = (Inner) new DoFloat(); >> } else if (cl == Double.class) { >> helper = (Inner) new DoDouble(); >> } else { >> throw new IllegalArgumentException("Invalid type: " + cl); >> } >> } >> >> @Override >> public T doSomething() { >> return helper.doAnother(); >> } >> >> // Test >> public static void main(String[] args) { >> final Base rd = new Run(Double.class); >> final Base rf = new Run(Float.class); >> System.out.println("Double: " + rd.doSomething()); >> System.out.println("Float: " + rf.doSomething()); >> >> final Base ri = new Run(Integer.class); >> System.out.println("Integer: " + ri.doSomething()); >> >> // final Base wontCompile = new Run(Double.class); >> } >> } > > so I need a parameter for the type > >> Btw, what do you need that for? > > See my posting some days ago. I use a JNI call with C++ templates so I > must set the template > parameter on compile-time. Within the JNI call I can't determine the > generic argument of the > instantiated java object (because it is set on java-compile-time). So I > create dual JNI calls one > for float and one for double, but I must decide on the java ctor call if > I instantiate the float or double > object, you do this in the with if(cl == Float.Class), in my source code > the DoFloat is the JNI call > for the C++ template float (otherwise the double). A simpler solution would be public abstract class Base { public static Base createDoubleHandler() { return new RunDouble(); } public static Base createFloatHandler() { ... } public T anylogic(T x) { // return x == null ? null : specific(x); } protected abstract T specific(T abc); } final class RunDouble extends Base { protected Double specific(Double x) { return 123d + x; } } ... Kind regards robert