Reputation: 611
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 that class in place of static someClass. How to go about it?
SomeClass sc;
if (someOtherClassObject instanceof SomeClass){
sc=(SomeClass) someOtherClassObject;
What I want is
public void castDynamic (String strClassName){
//cast the classname referred by strClassName to SomeClass
//if it is the instance of SomeClass
}
EDIT: The above was simplification. The actual code is this
public void X(String className, RequestInterface request)
{
//My current code is this, I need to change so that "XRequest"
//can be any class referred by "className",
//and "request.getRequest" the object belonging to "className" class
//I don't want static XRequest xvr, it should be fetched dynamically
XRequest xvr;
if (request.getRequest() instanceof XRequest){
xvr=(XRequest) request.getRequest();
client.setRequest(xvr);
}
}
Another simple rephrase: I get an object using request.getRequest(). I have no clue what that object is. So I need to cast it to the classstring name provided. How to do that? That's all. – SQC 13 mins ago
Upvotes: 5
Views: 33481
Reputation: 77454
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);
Upvotes: 8
Reputation: 30723
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)
Upvotes: 7