Topic: Java: Instantiate classes by name from a file (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=28631" title="Pages that link to Topic: Java: Instantiate classes by name from a file (Page 1 of 1)" rel="nofollow" >Topic: Java: Instantiate classes by name from a file <span class="small">(Page 1 of 1)</span>\

 
Skaarjj
Maniac (V) Mad Scientist

From: :morF
Insane since: May 2000

IP logged posted posted 11-11-2006 02:43 Edit Quote

Okay, I don't even know if this is possible, I'm just hoping it is.

I have a program I'm writing. It's got a few different configuration files that it loads and parses. I want one of these to be a modules file. See, the program provides a framework, then the modules file lists input commands, and the class that runs them. Allows me to interactively add/remove modules without ever having to restart the program. The modules all implement the interface Module, which has a single method: public static void process(String message). Since all modules share this interface, it allows me to create a TreeMap based on them.

public TreeMap<String,Module> modules = new TreeMap<String,Module>();

Upon loading, the modules file will be read line-by-line. The last entry on the line will be the class that handles the commands, and everything leading up to it will be a command that will be fed (as part of String message) to the class' process() method. This is all well and good so far, but I'm wondering this:

If I load up a set of strings from this file, is there any way I can tell Java "create an instance of the class named in StringA"?


Justice 4 Pat Richard

Skaarjj
Maniac (V) Mad Scientist

From: :morF
Insane since: May 2000

IP logged posted posted 11-11-2006 06:51 Edit Quote

Further Googlesearch has lead me to the abstract class ClassLoader. I know that I can make use of the JVM's ClassLoader to load my classes by their names (although I don't know how I can do that, yet) and it will return a Class object to me. What I don't know, yet, is if that Class object is much the same as the Object class. I doubt it is, somehow, so I don't even know if this is what I'm looking for.


Justice 4 Pat Richard

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

IP logged posted posted 11-11-2006 19:09 Edit Quote

Module module = (Module)Class.forName('YourClassName');

Dan @ Code Town

GRUMBLE
Paranoid (IV) Mad Scientist

From: Omicron Persei 8
Insane since: Oct 2000

IP logged posted posted 11-11-2006 20:14 Edit Quote

the java relfection api might be useful too.

http://java.sun.com/docs/books/tutorial/reflect/index.html

Skaarjj
Maniac (V) Mad Scientist

From: :morF
Insane since: May 2000

IP logged posted posted 11-12-2006 00:38 Edit Quote

The solution that actually appears to have worked (so far, because I haven't yet tested it completely) is a combination of the two.

code:
Class currClass;
Modules currModule;
try {
	currClass = Class.forName("ClassNameFromFile");
	currModule = (Modules)currClass.newInstance();
} catch (ClassNotFoundException e) {
	System.out.println("Could not find module");
	e.printStackTrace();
} catch (InstantiationException e) {
	System.out.println("Could not create a module instance");
	e.printStackTrace();
} catch (IllegalAccessException e) {
	System.out.println("Could not access named module");
	e.printStackTrace();
}



Of course, for this to work, you have to import java.lang.relfect.*; but, eh, that's just a line of code, not hard to do.

Thanks for all your help.


Justice 4 Pat Richard

(Edited by Skaarjj on 11-12-2006 00:39)

Skaarjj
Maniac (V) Mad Scientist

From: :morF
Insane since: May 2000

IP logged posted posted 11-12-2006 09:35 Edit Quote

Okay, before I say such things, I should pay attention to the warnings my compiler puts out, such as 'import java.lang.reflect.* is never used'. So you don't need to import it, just use the code.


Justice 4 Pat Richard



Post Reply
 
Your User Name:
Your Password:
Login Options: Remember Me On This Computer
 
Your Text:
Loading...
Options: Show Signature
Enable Slimies
Enable Linkwords

« BackwardsOnwards »

Show Forum Drop Down Menu