Return-Path: X-Original-To: apmail-incubator-empire-db-dev-archive@minotaur.apache.org Delivered-To: apmail-incubator-empire-db-dev-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id D6B87644E for ; Mon, 25 Jul 2011 06:37:16 +0000 (UTC) Received: (qmail 172 invoked by uid 500); 25 Jul 2011 06:37:16 -0000 Delivered-To: apmail-incubator-empire-db-dev-archive@incubator.apache.org Received: (qmail 115 invoked by uid 500); 25 Jul 2011 06:37:07 -0000 Mailing-List: contact empire-db-dev-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: empire-db-dev@incubator.apache.org Delivered-To: mailing list empire-db-dev@incubator.apache.org Received: (qmail 99990 invoked by uid 99); 25 Jul 2011 06:36:59 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 25 Jul 2011 06:36:59 +0000 X-ASF-Spam-Status: No, hits=-0.7 required=5.0 tests=FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,SPF_PASS,T_TO_NO_BRKTS_FREEMAIL X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of francisdb@gmail.com designates 209.85.212.47 as permitted sender) Received: from [209.85.212.47] (HELO mail-vw0-f47.google.com) (209.85.212.47) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 25 Jul 2011 06:36:53 +0000 Received: by vws2 with SMTP id 2so3052183vws.6 for ; Sun, 24 Jul 2011 23:36:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=iTH7/dNKGRr53Ja1i3CiNROTYjN3jZWg7hVaAsY4P50=; b=U44KpIzguxlcQf5jmZRLoTRxKYWckjqY0b48Y3RenTzYALskHTx+TPHtGl/Cp5JoaN MzjaaQJa2etPQH2tibqJsx1jDNXYv2yMCvtwG+j+Q4z9BqfhkjBvZVo0JBTKnn9HLXA+ F2Tfqn+zRZ572ElEh3fUGEVFEG1UAQEHCeGUk= Received: by 10.52.30.65 with SMTP id q1mr2847112vdh.141.1311575792072; Sun, 24 Jul 2011 23:36:32 -0700 (PDT) MIME-Version: 1.0 Received: by 10.52.113.163 with HTTP; Sun, 24 Jul 2011 23:36:12 -0700 (PDT) In-Reply-To: References: <7838B39A-6472-4762-A535-3D129E65A000@andreasfink.com> From: Francis De Brabandere Date: Mon, 25 Jul 2011 08:36:12 +0200 Message-ID: Subject: Re: Stupid generics question (possibly) To: empire-db-dev@incubator.apache.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Checked: Checked by ClamAV on apache.org Because this generics info is not available at runtime? http://gafter.blogspot.com/2004/09/puzzling-through-erasure-answer.html http://gafter.blogspot.com/2006/11/reified-generics-for-java.html On Mon, Jul 25, 2011 at 8:26 AM, Rainer D=F6bele wrote: > Hi Andi, > > thanks for your answer. I should possibly have a look at Guava. > > As mentioned the problem is not really the DBReader, but the fact, that i= n order to fill the list, new instances of T have to created. > So basically the problem can be reduced to: > > public class BeanResult extends ArrayList > { > > =A0 =A0public BeanResult(Class c, DBCommand cmd) > =A0 =A0{ > =A0 =A0 =A0 =A0T obj =3D c.newInstance(); > =A0 =A0} > ... > > And the question remains, whether or not it is possible at all, to get ri= d of the "Class c" parameter. > I don't really understand, why it is not possible to extract the Class fr= om T. > > Regards, > Rainer > > > Andreas Fink wrote: >> from: Andreas Fink [mailto:andi@andreasfink.com] >> to: empire-db-dev@incubator.apache.org >> re: Re: Stupid generics question (possibly) >> >> Hi Rainer. >> >> As long as you do not refactor "new DBReader()" to "new DBReader()" >> there won't be a way around it. >> If you do not need to know the type in "reader.getBeanList" except for >> casting, this Could suffice: >> >> class DBReader { >> >> =A0 =A0 =A0 getBeanList(beanResult, maxItems) { >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 ... >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 T someData =3D (T) someMethodCall(); >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 ... >> =A0 =A0 =A0 } >> >> } >> >> If you need to do more, stick to what you posted (this is how Google >> Guava does it, btw.) or do something funny at runtime using reflection. >> >> The best source for help in cases like this is Angelika Langers generics >> FAQ: http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html >> >> Cheers, >> Andi. >> >> On Jul 24, 2011, at 2:00 PM, Rainer D=F6bele wrote: >> >> > Hi, >> > >> > >> > >> > This might be a stupid question possibly but I just don't feel like >> investigating myself. >> > >> > >> > >> > I have introduced a new simple helper class called BeanResult to >> simplify queries for a list of POJO's a bit. >> > >> > I looks roughly like this: >> > >> > >> > >> > public class BeanResult extends ArrayList >> > >> > { >> > >> > =A0 =A0private DBCommand cmd; >> > >> > =A0 =A0private Class type; >> > >> > >> > >> > =A0 =A0public BeanResult(Class type, DBCommand cmd) >> > >> > =A0 =A0{ >> > >> > =A0 =A0 =A0 =A0this.type =3D type; >> > >> > =A0 =A0 =A0 =A0this.cmd =3D cmd; >> > >> > =A0 =A0} >> > >> > >> > >> > =A0 =A0public int fetch(Connection conn, int maxItems) >> > >> > =A0 =A0{ >> > >> > =A0 =A0 =A0 =A0clear(); >> > >> > =A0 =A0 =A0 =A0DBReader reader =3D new DBReader(); >> > >> > =A0 =A0 =A0 =A0try { >> > >> > =A0 =A0 =A0 =A0 =A0 =A0// Open and Read >> > >> > =A0 =A0 =A0 =A0 =A0 =A0reader.open(cmd, conn); >> > >> > =A0 =A0 =A0 =A0 =A0 =A0reader.getBeanList(this, type, maxItems); >> > >> > =A0 =A0 =A0 =A0 =A0 =A0return size(); >> > >> > >> > >> > =A0 =A0 =A0 =A0} finally { >> > >> > =A0 =A0 =A0 =A0 =A0 =A0reader.close(); >> > >> > =A0 =A0 =A0 =A0} >> > >> > =A0 =A0} >> > >> > } >> > >> > >> > >> > In order to use it, I must write the following: >> > >> > >> > >> > BeanResult result =3D new >> BeanResult(SampleBean.class, cmd); >> > >> > >> > >> > What I don't like is, that I have to pass the SampleBean.class in the >> constructor. >> > >> > I would rather just write: >> > >> > >> > >> > BeanResult result =3D new BeanResult(cmd); >> > >> > >> > >> > This is beause the getBeanList uses type.newInstance() to create new >> Instances of the bean. >> > >> > >> > >> > Does anyone know whether and how to determine the Class from T or >> another option to get rid of this extra parameter. >> > >> > I have checked the code in under the EMPIREDB-99 branch. >> > >> > >> > >> > Regards >> > >> > Rainer >> > >> > >> > >> >> >> -- >> web: =A0 http://andreasfink.com/ >> mail: =A0af@andreasfink.com >> mobil: +34 65 1728443 >> tel: =A0 +34 93 2082949 > > --=20 http://www.somatik.be Microsoft gives you windows, Linux gives you the whole house.