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!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: Define Type at runtime Date: Thu, 8 Sep 2011 17:10:17 -0700 (PDT) Organization: http://groups.google.com Lines: 127 Message-ID: <032ddf9b-9c3a-47c4-a9db-082e81b8a048@glegroupsg2000goo.googlegroups.com> References: <4e678737$1@news.x-privat.org> <5a51c17a-6488-492c-bad4-857a622f5c46@glegroupsg2000goo.googlegroups.com> <4E6931A7.5010208@hotmail.com> 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 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1315527018 23977 127.0.0.1 (9 Sep 2011 00:10:18 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 9 Sep 2011 00:10:18 +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:7739 BGB wrote: > Lew wrote: >> BGB wrote: >>> Lew wrote: >>>> BGB wrote: >>>>>> John wrote: >>>>>>> If I have a method: >>>>>>> public void someMethod(Object o){ >>>>>>> ... >>>>>>> Entity entity; >>>>>>> .... >>>>>>> } >>>>>>> >>>>>>> How could I define at runtime the type T if I have an Object? >>>>>> ...[snip] ... > >>>> and, probably, not to forget "instanceof". > >>> > >>> That's pretty useless in this context. > >>> >=20 > >=20 >>> >>> but, whatever works... >> >> By which I assume you at least mean compiles. >> >> 'instanceof Entity' will not compile. >=20 > where did I ever write "instanceof Entity"?... You suggested he use 'instanceof' to cast to type 'T' or, depending on what= the OP meant, 'Entity', since you suggested to use 'instanceof' in the = OP's context. Didn't you intend to answer the OP's question, or were you a= ttempting to provide information that didn't pertain to the question at han= d? If you intended to pertain to the question, then you were advising 'instanc= eof' to check for a non-reifiable type. > the fact that it will not compile, on account of not being valid code,=20 > is not the issue, because I never wrote that, or suggested that fragment= =20 > as a valid solution... >=20 >> if-then chains of type comparisons are a major antipattern, a point you = gloss over. It means that you aren't using polymorphism and type-safe code= . It's a Bad Thing. >> >> if (x instanceof Foo) >> { >> // do one thing >> } >> else if (x instanceof Bar) >> { >> // do another thing >> } >> else if (x instanceof Baz) >> { >> // OK, this code is far too stupid. Refactor intelligently. >> } >> ... >> >> You're supposed to have >> >> Super foo =3D obtainASubtypeInstance(); >> foo.polymorphicMethod(); >> >=20 > but, this only works assuming that some "Super" exists within the class= =20 > heirarchy... what if it does not? what if the original objects in=20 > question were never designed with this use case in mind? ... Then you use overloads instead of overrides. Either way, it's probably a case of bad design to have that much 'instanceo= f' chaining - very much a non-O-O approach. =20 > if it is just a big pile of unrelated objects (with neither a common=20 > superclass nor a common interface) then instanceof will probably work... More likely a refactoring would work better. Yes, it will "work", but it's= brittle and shows that some analysis was not completed. > like, say: > if(obj instanceof Integer) > ... > else if(obj instanceof String) > ... > else if(obj instanceof JLabel) > ... The need for such is rare indeed, vanishingly rare. The usual and more mai= ntainable approach is to use overloads. The problem with what you suggest = is it pushes compile-time checking into run-time checking. > but, again, it is a big question of what is being done and why, which=20 > was not addressed here. Chance are, though, that what is being done and why is better served by doi= ng things in a more typesafe fashion. > pattern vs antipattern vs ... isn't really an issue IMO, more just=20 You are correct, that is your opinion. > scare-tactics and prescriptivism (people don't like something or a way=20 > of doing something, so they call it an antipattern...). That's not what I'm doing. > the bigger question is when and why someone does something, not that=20 > they have done it (at least, as far as programming goes). Chance are that the OP's scenario is poorly served by 'instanceof' chains, = particularly since they're trying to check on a non-reifiable type and 'ins= tanceof' won't compile anyway. You are correct that it depends on the use case, but very few use cases are= best served by the 'instanceof' antipattern. All the ones I've seen in th= e wild were examples of poor design. --=20 Lew