• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doing without instanceof

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can I use generics or covariant return types to somehow avoid using iinstanceof here:

target.setField(source.getField(field));

In this case both the setter and getter demand field to be a subclass of type Field, rather than of Field as presently.

These are types and methods from a third party library, I cannot change the implementations.

TIA
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even with Java 1.0, you don't need to use instanceof in this case - just cast to the type that is required.

If you can't change the code of the methods, generics or covariant return types can't help you, or so it seems to me.
 
John Coleman
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:
Even with Java 1.0, you don't need to use instanceof in this case - just cast to the type that is required.

If you can't change the code of the methods, generics or covariant return types can't help you, or so it seems to me.



Unfortunately I think your comment above is right, and I have to code:

if (field instanceof StringField) {
target.setField((StringField) field);
} else
if (field instanceof DoubleField) {
target.setField((DoubleField) field);
} else
if (field instanceof IntField) {
target.setField((IntField) field);
} else
if (field instanceof CharField) {
target.setField((CharField) field);
} else
if (field instanceof BooleanField) {
target.setField((BooleanField) field);
} else
if (field instanceof UtcDateOnlyField) {
target.setField((UtcDateOnlyField) field);
} else
if (field instanceof UtcTimeOnlyField) {
target.setField((UtcTimeOnlyField) field);
} else
if (field instanceof UtcTimeStampField) {
target.setField((UtcTimeStampField) field);
}

But that's inefficient to execute and maintain.

Something like this looks good, but since there is no setField<T>, not possible.

setField(field.getClass(), field, target);

static <T> void setField(Class<T> clazz, Field field, FieldMap target) {
target.setField(clazz.cast(field));
}
 
When people don’t understand what you are doing they call you crazy. But this tiny ad just doesn't care:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic