Jump to content
  • Advertisement
Sign in to follow this  
CodeZero

[java] ClassLoader Problem

This topic is 4174 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have an abstract class JBot. During runtime I'm wanting to load in classes that extend JBot and create instances of them. Here is my code so far:
ClassLoader jBotLoader = this.getClass().getClassLoader();
			
Class jBotClass = jBotLoader.loadClass( className );
JBot jBot = (JBot)jBotClass.newInstance();

I get a ClassCastException on the last line. I know this has something to do with one ClassLoader not being able to see what another has loaded. But isn't there a way to have all the classes load from the same ClassLoader and avoid this error? Thanks, Jake

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by CaptainJester
What you are trying to do is right. I have done it. Try using Class.forName instead of ClassLoader.


Class.forName is the answer, but for the code to be robust it isn't enough. Because the dynamically loaded classes are intended to be subclasses of Jbot, there should be a check to verify that this is the case. So you would do something like this (Exception handling ignored here):


JBot instantiateSubclass(String className) throws Exception {
Class clazz = Class.forName(className);
for(Class current = clazz.getSuperClass(); current != null; current = current.getSuperClass()) {
if(current == JBot.class) {
return (JBot)clazz.newInstance();
}
}
return null;
}








Of course, you'd likely want to handle exceptions in such a function, rather than propagating them. Declaring a method to just throw Exception isn't really good practice.

You might want to take a look at this ClassUtils class. It contains several methods for instantiating objects. I've been using it for a while in my Java projects and it's quite handy. The version I'm using now has one extra public method implemented like so:


public static Object instantiate(String className) {
return instantiate(findClass(className));
}







Add that method and this class should cover nearly any situation in which you need simple dynamic instantiation (and when I say "simple", I mean using the default ClassLoader).

Share this post


Link to post
Share on other sites
I'd also like to chime in very briefly and mention that
Class#isAssignableFrom(Class<?> cls)
can be used as a simple shortcut to determine if a class object is a superclass/superinterface of another. So if
JBot.class.isAssignableFrom(loadedClass)
returns true you should be safe to do
someBot = (JBot)loadedClass.newInstance();
Reflection FTW, eh?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!