Simple question but I have spent over an hour with this. My code is below. I need to make SomeClass sc dynamic. So you pass the class name as a string in a function and use
The problem you're describing is not well defined. Casting is an operation that takes an object and a Class and then checks if that object is an instance of the given class.
However you're saying you need something that cast(s) the classname referred by strClassName to SomeClass if it is the instance of SomeClass
. In other words, you're looking something that works on two classes (rather than a class and an object).
So, we need to restate the problem. Here are three possible problems (+ solutions) I'm hoping one of them is what you need
// Problem 1: check whether "object" can be casted to the class
// whose name is "className"
public void castDynamic(Object object, String className) {
Class cls = Class.forName(className);
cls.cast(object);
}
// Problem 2: check whether the class whose name is "className"
// is a subclass of SomeClass (A is a subclass of B if A (transitively)
// extends or implements B).
public void castDynamic(String className) {
Class cls = Class.forName(className);
SomeClass.class.asSubclass(cls);
}
// Problem 3: check whether SomeClass is a sublcass the class
// whose name is "className"
public void castDynamic(String className) {
Class cls = Class.forName(className);
cls.asSubclass(SomeClass.class);
}
Full details: http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#asSubclass(java.lang.Class)
Do you mean?
Class clazz = Class.forName(strClassName);
You want to instantiate a class by it's name?
First of all, you need to make a Class<?>
object:
Class<?> cls = Class.forName(strClassName);
Then instantiate this (note, this can throw various exceptions - access violation, ClassNotFound
, no public constructor without arguments etc.)
Object instance = cls.newInstance();
Then you can cast it:
return (SomeClass) instance;
Please make sure you understand the differences between:
You can also cast the cls
object to have type Class<? extends SomeClass>
, if you want. It doesn't give you much, though. And you can inline it, to:
return (SomeClass)(Class.forName(strClassName).newInstance());
Oh, but you can do type checking with the cls
object, before instantiating it. So you only instanciate it, if it satisfies your API (implements the interface you want to get).
EDIT: add further example code, towards reflection.
For example:
if (cls.isInstance(request)) {
// ...
}
For invoking methods, you either need to know an interface that you can cast to, or use reflection (the getMethod
methods of the cls
object):
Method getRequest = cls.getMethod("getRequest");
getRequest.invoke(request);