Re: SOLVED Question for the Java Gurus
Re: SOLVED Question for the Java Gurus
- Subject: Re: SOLVED Question for the Java Gurus
- From: Jonathan Rochkind <email@hidden>
- Date: Thu, 22 Apr 2004 10:41:35 -0500
At 3:06 PM +1000 4/22/04, Owen McKerrow wrote:
Yeah sorry, re-read the email and yes I didn't explain myself very
well. The function was just an example really, just to put the
Object tempOb = (entity)array.objectAtIndex(0); line in some form of context.
The question was basically, given the name of a class as a string,
is there anyway to use that String for casting an object to the
class type of class who's name is the contents of the string.
You would have to use the java reflection APIs to do this. I'm not
sure it would be useful for what you're talking about, since you
wouldn't know any of the methods you could _call_ on this object,
you'd have to use the java reflection APIs for _that_ too. But the
Java reflection APIs do allow you to do the sort of thing you're
talking about---not by class-casting exactly, but by other ways,
check out the docs
I've never had a reason to use them the way you are talking about,
and am having trouble thinking of one. Unless you are doing
something really specialized, I think that's probably the wrong tree
to be barking up. (The right one might be using Interfaces
appropriately).
--Jonathan
In the end I did it another way :
/**This class is deisgned to take a NSArray of objects from a join
table between two entities and return a NSArray of the objects from
one of the entities.
That is if you have 2 tables 'Frog' and 'Food" with the join table
'Eats' between them, you can pass this class the Frogs NSArray of
Eats objects and ask for an NSArray of Food items that match the
given relationship.
It only every really needs to be used when there is something in the
join table that doesn't allow it to be flattened.*/
/**
Given an Array of join table objects and a
relationship name, it will seek to return an array of objects from
the other side of that relationship
@param objects The array of objects you wish to strip
@param relationship The relationship that you wish to
get an array of objects from
*/
public static NSMutableArray stripJoin(NSArray objects,
String relationship) throws Exception
{
int i;
NSMutableArray wantedObjects = new NSMutableArray();
for (i=0;i < objects.count();i++) {
Object temp = objects.objectAtIndex(i);
Method tempMethod =
temp.getClass().getMethod(relationship,null);
wantedObjects.addObject(tempMethod.invoke(temp,null));
}
return wantedObjects;
}
Owen
On 22/04/2004, at 2:17 PM, Art Isbell wrote:
On Apr 21, 2004, at 5:48 PM, Owen McKerrow wrote:
How can I make the following code work if entity is an instance of
String passed in as an argument to the function. i.e. Can I cast
to a specific Class if all I have is the name of the Class as a
String ? If not is there another way I can do this ?
public getOtherObjects(String entity, NSArray objects) {
for(int i=0;i<objects.count();i++) {
Object tempOb = (entity)array.objectAtIndex(0);
}
}
Not sure what you're trying to do since the above function
really does nothing. If objects is an array of enterprise objects,
then you can assign an array element to a variable of type
EOEnterpriseObject:
EOEnterpriseObject tempOb =
(EOEnterpriseObject)objects.objectAtIndex(0);
Aloha,
Art
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.
'The test of a first-rate intelligence is the ability to hold two
opposed ideas in the mind at the same time and still be able to
function.'
-F.Scott Fitzgerald,
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.