Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #7725
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail |
|---|---|
| From | BGB <cr88192@hotmail.com> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Define Type at runtime |
| Date | Thu, 08 Sep 2011 14:54:31 -0700 |
| Organization | albasani.net |
| Lines | 95 |
| Message-ID | <j4bdim$rhe$1@news.albasani.net> (permalink) |
| References | <4e678737$1@news.x-privat.org> <5a51c17a-6488-492c-bad4-857a622f5c46@glegroupsg2000goo.googlegroups.com> <j4b09c$stb$1@news.albasani.net> <ddb2e08e-5510-4145-a182-0bb18b627454@glegroupsg2000goo.googlegroups.com> <4E6931A7.5010208@hotmail.com> <db1b4673-eeef-4c69-ab71-8458faa57614@glegroupsg2000goo.googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Trace | news.albasani.net xObeA/Rn44DQ2CgR7mMXb3Dqi81ngClxWeojf57DRMs/naxZir/cnrq9UHiYgzvSyx8ta+F7XdEdKICKtqerTA== |
| NNTP-Posting-Date | Thu, 8 Sep 2011 21:54:30 +0000 (UTC) |
| Injection-Info | news.albasani.net; logging-data="ND92Raz8VIygIl/JC5Wv+zYvr3y2OP2gO/Kwlo4YBrfPoCmrKpJcBeucvrgOPTSd14a39eYxTnS8935xpsyqinCYiudFefhmtsQ1AhrEY8ELnUMbb9fqOm87rs1h6SBM"; mail-complaints-to="abuse@albasani.net" |
| User-Agent | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0.2) Gecko/20110902 Thunderbird/6.0.2 |
| In-Reply-To | <db1b4673-eeef-4c69-ab71-8458faa57614@glegroupsg2000goo.googlegroups.com> |
| Cancel-Lock | sha1:Xwc4HXagRPEsx3WrKD/kifUQXOs= |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:7725 |
Show key headers only | View raw
On 9/8/2011 2:35 PM, Lew wrote:
> BGB wrote:
>> Lew wrote:
>>> BGB wrote:
>>>> Lew wrote:
>>>>> John wrote:
>>>>>> If I have a method:
>>>>>> public void someMethod(Object o){
>>>>>> ...
>>>>>> Entity<T> entity;
>>>>>> ....
>>>>>> }
>>>>>>
>>>>>> How could I define at runtime the type T if I have an Object?
>>>>>
>>>>> You can do an illegal cast with the help of '@SuppressWarnings("unchecked")' and a 'catch ( ClassCastException exc )'.
>>>>>
>>>>> You can under certain circumstances use a run-time type token of type 'Class<T>'.
>>>>>
>>>>> You can write your method to be type-safe in the first place and avoid the problem.
>>>>>
>>>>> Without some context we can't know what you really want. Your question is far too non-specific.
>>>>>
>>>>
>>>> and, probably, not to forget "instanceof".
>>>
>>> That's pretty useless in this context.
>>>
<snip>
>>
>> but, whatever works...
>
> By which I assume you at least mean compiles.
>
> 'instanceof Entity<T>' will not compile.
>
where did I ever write "instanceof Entity<T>"?...
the fact that it will not compile, on account of not being valid code,
is not the issue, because I never wrote that, or suggested that fragment
as a valid solution...
> 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 = obtainASubtypeInstance();
> foo.polymorphicMethod();
>
but, this only works assuming that some "Super" exists within the class
heirarchy... what if it does not? what if the original objects in
question were never designed with this use case in mind? ...
if it is just a big pile of unrelated objects (with neither a common
superclass nor a common interface) then instanceof will probably work...
like, say:
if(obj instanceof Integer)
...
else if(obj instanceof String)
...
else if(obj instanceof JLabel)
...
but, again, it is a big question of what is being done and why, which
was not addressed here.
pattern vs antipattern vs ... isn't really an issue IMO, more just
scare-tactics and prescriptivism (people don't like something or a way
of doing something, so they call it an antipattern...).
the bigger question is when and why someone does something, not that
they have done it (at least, as far as programming goes).
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar
Define Type at runtime John <john@yahoo.com> - 2011-09-07 17:01 +0200
Re: Define Type at runtime Mayeul <mayeul.marguet@free.fr> - 2011-09-07 17:20 +0200
Re: Define Type at runtime John <john@yahoo.com> - 2011-09-07 17:25 +0200
Re: Define Type at runtime Robert Klemme <shortcutter@googlemail.com> - 2011-09-08 08:12 -0700
Re: Define Type at runtime John <jonh@yahoo.com> - 2011-09-09 09:01 +0200
Re: Define Type at runtime Robert Klemme <shortcutter@googlemail.com> - 2011-09-09 04:47 -0700
Re: Define Type at runtime Lew <lewbloch@gmail.com> - 2011-09-08 09:32 -0700
Re: Define Type at runtime BGB <cr88192@hotmail.com> - 2011-09-08 11:07 -0700
Re: Define Type at runtime Lew <lewbloch@gmail.com> - 2011-09-08 11:25 -0700
Re: Define Type at runtime BGB <cr88192@hotmail.com> - 2011-09-08 14:20 -0700
Re: Define Type at runtime Lew <lewbloch@gmail.com> - 2011-09-08 14:35 -0700
Re: Define Type at runtime BGB <cr88192@hotmail.com> - 2011-09-08 14:54 -0700
Re: Define Type at runtime Lew <lewbloch@gmail.com> - 2011-09-08 17:10 -0700
Re: Define Type at runtime BGB <cr88192@hotmail.com> - 2011-09-09 00:32 -0700
Re: Define Type at runtime John <jonh@yahoo.com> - 2011-09-09 09:02 +0200
csiph-web