View Full Version : Dru! HELP!!! (other java folks are welcome too) :)
inkedmn
11-14-2002, 04:45 AM
ok, here's what i would like to do...
i need to create a method that takes a String as an argument, creates an instance of an object with the same name, and returns the instance...
in other words...
say i have a Movie.class file. i want this method to get an instance of the Movie object and return it (so that i can access all it's methods/attributes).
(i'm trying to figure out dynamic creation/deletion of objects for Joanabot)
any idears?
[edit]
this is a method that does what i want (mostly), except it returns type Object. i want to do something like this that returns type <instance of object 'name'>.
import java.lang.reflect.*;
public class LoaderTest {
public Object makeNew(String name) {
try {
Class t = Class.forName(name);
Object q = t.newInstance();
return q;
} catch (Exception e) {
System.out.println(e);
}
}
}
thanks :)
Strike
11-14-2002, 09:45 AM
Wouldn't you just cast it to whatever you want?
Dru Lee Parsec
11-14-2002, 12:19 PM
Strike is right. If you're going to pass the method different class names then it's going to return different classes back. I'd suggest that since you know what kind of class you're trying to create when you call this method that you cast it at that point.
Something like this
SpecificClass myNewClass = (SpecificClass)makeNew("SpecificClass);
inkedmn
11-14-2002, 02:27 PM
that's the thing, i DON'T know the type of class i'll be passing...
the idea behind this is that i'd be able to create new instances of JoanaBot modules by passing the name of the module to this method (and having this work for ALL types of modules).
either way, i think i've figured out a way around this. i'm just going to have a ModuleManager class (or something similar) that will have methods that return the different types of objects i need, and unloading will be done another way that i haven't quite hacked out in my head yet...
thanks anyway amigo :)
My best answer, assuming you can't make all the classes implement a common interface, is to have a switch statement for each class you may be returning.. ie.
if (name == "myClass") return (myClass)q;
if (...
clearly that sucks, because you'll have to change it for every new module you add.. there must be a better way..
Doesn't java support some kind of "eval" or something?
Strike
11-14-2002, 04:56 PM
kmj raises a good point in that you probably should have a sort of base class that all the possible classes you will be getting back would be implementing. In fact, I imagine Java even has mechanisms of some sort to "loosely" compare the class you get with that base class to see if it does indeed implement (bolded because I mean it as a keyword and not just a concept) the base class. If not, I'd raise an exception.
If you do this, then you have a whole slew of potential bugs solved because you'd only have to debug one interface instead of a myriad of different ones. Not to mention it just feels cleaner overall that way.
bwkaz
11-14-2002, 04:56 PM
Originally posted by kmj
if (name == "myClass") return (myClass)q;
if (...
No, no, no. Do the cast outside the function that returns the object.
Make the factory function return whatever object comes back from newInstance(), but cast it to what you need after the caller calls your makeNew() function.
import java.lang.reflect.*;
public class LoaderTest {
public static void main(String[] args) {
JoanaModule1 mod1 = (JoanaModule1)makeNew("JoanaModule1");
JoanaModule2 mod2 = (JoanaModule2)makeNew("JoanaModule2");
// for that matter, you can even do, with another import, this:
Graphics2D g = (Graphics2D)makeNew("Graphics2D");
// The important part is, you make the caller do the cast, outside makeNew()
}
public static Object makeNew(String name) {
try {
Class t = Class.forName(name);
Object q = t.newInstance();
return q;
} catch (Exception e) {
System.out.println(e);
}
}
}
Dru Lee Parsec
11-14-2002, 05:44 PM
that's the thing, i DON'T know the type of class i'll be passing...
Then they should all implement an interface and you cast all of the classes to that interface. Now that I re-read the comments I see that Strike already said essentially the same thing. Ya beat me to it Strike!
inkedmn
11-15-2002, 02:32 AM
cool, i'll do that...
thanks guys!!!
Bradmont
11-15-2002, 10:55 AM
Originally posted by Strike
kmj raises a good point in that you probably should have a sort of base class that all the possible classes you will be getting back would be implementing. In fact, I imagine Java even has mechanisms of some sort to "loosely" compare the class you get with that base class to see if it does indeed implement (bolded because I mean it as a keyword and not just a concept) the base class. If not, I'd raise an exception.
I'm not sure for interfaces, but for base classes you can do:
if (foo instanceof JoanaBotModule){
and whatnot;
}
It wouldn't surprise me if you could do it for interfaces, too.
Bradmont
11-15-2002, 10:59 AM
http://java.about.com/library/tutorials/bltut-013l.htm?terms=instanceof
Here's a little more info about instanceof. It doesn't specifically say whether you can use it for interfaces, but it seems to imply that you can.
No, no, no. Do the cast outside the function that returns the object.
Oh, yeah. Duh. :)
Actually Stuka gets original gangsta credit for the "implement the same fucking interface, ya douchebag" idea, on the eye are sea.
Captain Goatse
11-16-2002, 09:47 AM
I'm working with a DynamicCore basic OOP api, which lets you basically load anything to your wrapper class, which then decides what has just been loaded. My best advice would be that you have abstract manager class, then inherit all the loadable classes from that, then set up the basic properies and then just decide what to do with the laoded things. It will work fine, its just quite complicated to do. I'm not releasing DynamicCore yet since its not fully developed and finished, but when its out, you can get the source.
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.