aspose file tools*
The moose likes Java in General and the fly likes doing without instanceof Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "doing without instanceof" Watch "doing without instanceof" New topic
Author

doing without instanceof

John Coleman
Ranch Hand

Joined: Jul 24, 2001
Posts: 65
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


John Coleman, MSTA<br />Sun Certified Programmer for the Java� 2 Platform<br />john.coleman@eurobase-international.com<br />Eurobase banking solutions<br /><a href="http://www.eurobase-international.com/banking" target="_blank" rel="nofollow">http://www.eurobase-international.com/banking</a>
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
John Coleman
Ranch Hand

Joined: Jul 24, 2001
Posts: 65
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));
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doing without instanceof