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


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

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 17:10 -0700
Organization http://groups.google.com
Message-ID <032ddf9b-9c3a-47c4-a9db-082e81b8a048@glegroupsg2000goo.googlegroups.com> (permalink)
References (2 earlier) <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> <j4bdim$rhe$1@news.albasani.net>

Show all headers | View raw


BGB wrote:
> Lew wrote:
>> BGB wrote:
>>> Lew wrote:
>>>> BGB 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?
>>>>>>
...[snip] ...

> >>>> 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>"?...

You suggested he use 'instanceof' to cast to type 'T' or, depending on what the OP meant, 'Entity<T>', since you suggested to use 'instanceof' in the OP's context.  Didn't you intend to answer the OP's question, or were you attempting to provide information that didn't pertain to the question at hand?

If you intended to pertain to the question, then you were advising 'instanceof' to check for a non-reifiable type.

> 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? ...

Then you use overloads instead of overrides.

Either way, it's probably a case of bad design to have that much 'instanceof' chaining - very much a non-O-O approach.
 
> if it is just a big pile of unrelated objects (with neither a common 
> 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 maintainable 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 
> was not addressed here.

Chance are, though, that what is being done and why is better served by doing things in a more typesafe fashion.

> pattern vs antipattern vs ... isn't really an issue IMO, more just 

You are correct, that is your opinion.

> scare-tactics and prescriptivism (people don't like something or a way 
> 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 
> 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 'instanceof' 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 the wild were examples of poor design.

-- 
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