Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #7722

Re: Define Type at runtime

From Lew <lewbloch@gmail.com>
Newsgroups comp.lang.java.programmer
Subject Re: Define Type at runtime
Date 2011-09-08 14:35 -0700
Organization http://groups.google.com
Message-ID <db1b4673-eeef-4c69-ab71-8458faa57614@glegroupsg2000goo.googlegroups.com> (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>

Show all headers | View raw


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.
>>
>>> eg, [sic] rather than, say:
>>> Object obja;
>>> try {
>>>       SomeObject objb=(SomeObject)obja;
>>>       ...
>>> }catch(ClassCastException ex)
>>> {
>>> }
>>>
>>> one could be like:
>>> if(obja instanceof SomeObject)
>>> {
>>>       SomeObject objb=(SomeObject)obja;
>>>       ...
>>> }
>>>
>>> which could be more useful if one wants to do the types N-way...
>>>
>>> if(...)
>>> {
>>>       ...
>>> }else if(...)
>>> {
>>>       ...
>>> }else ...
>>>
>>>
>>> or such...
>>
>> That's bad advice on several fronts.  First of all, the OP was asking about restoring generics information, for which 'instanceof' is not useful, and second, multi-'if' constructs on type are a red flag of bad design.  Don't recommend bad design.
>>
> 
> AFAICT, the OP was asking about dynamic type checking, which is what 
> instanceof is for.

They were asking about the generic type, which most emphatically is *not* what 'instanceof' is for.  You even cited the post: "How could I define at runtime the type T if I have an Object?" where "T" was the generic type parameter in the OP's example.  'instanceof' won't help there.  In fact, it will not even compile.

JLS §15.20.2
"It is a compile-time error if the ReferenceType mentioned after the instanceof operator does not denote a reifiable type (§4.7)."
 
> the bigger issue though would be, why the OP was dealing with the case 
> in the first case (like, yes, does a common base-class exist besides 
> "Object", or is there a common interface, ...), but failing this, nested 
> if/else and instanceof does seem like a valid solution (and is probably 

Wrong.

> still better than, say, a series of try/catch blocks intermixed with 
> return statements).

Except for that failure-to-compile thing.

> try {
>      ...
>      return;
> }catch(...) { ... }
> 
> try {
>      ...
>      return;
> }catch(...) { ... }
> 
> ...
> 
> 
> but, whatever works...

By which I assume you at least mean compiles.

'instanceof Entity<T>' will not compile.

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();

-- 
Lew

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

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