Very Simple Java Dynamic Casting

A simple question, but I spent more than an hour on it. My code is below. I need to make a speaker SomeClass sc. This way you pass the class name as a string in the function and use this class instead of the static someClass. How to do it?

SomeClass sc; if (someOtherClassObject instanceof SomeClass){ sc=(SomeClass) someOtherClassObject; 

I want to

 public void castDynamic (String strClassName){ //cast the classname referred by strClassName to SomeClass //if it is the instance of SomeClass } 

EDIT: There was a simplification above. Actual code is

 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 phrase: I get the object using request.getRequest (). I do not know what this object is. Therefore, I need to pass it to the specified class name. How to do it? All this. - SQC 13 minutes ago

+4
source share
4 answers

Do you want to instantiate a class by name?

First of all, you need to create a Class<?> Object:

 Class<?> cls = Class.forName(strClassName); 

Then create an instance (note that this can cause various exceptions - access violation, ClassNotFound , no public constructor without arguments, etc.)

 Object instance = cls.newInstance(); 

Then you can do this:

 return (SomeClass) instance; 

Please make sure you understand the differences between:

  • Class name (approximately file name)
  • Class object (essentially type information)
  • Instance class (actual object of this type)

You can also give the cls object a Class<? extends SomeClass> Class<? extends SomeClass> if you want. It does not give you much. And you can insert it to:

 return (SomeClass)(Class.forName(strClassName).newInstance()); 

Oh, but you can do type checking with the cls object before creating it. This way you only initiate it if it satisfies your API (implements the interface you want to get).

EDIT: Add another example code in the direction of reflection .

For instance:

 if (cls.isInstance(request)) { // ... } 

To call methods, you need to know an interface that you can use, or use reflection ( getMethod methods of the getMethod object):

 Method getRequest = cls.getMethod("getRequest"); getRequest.invoke(request); 
+6
source

The problem you are describing is not defined. Casting is an operation that takes an object and a class, and then checks whether this object is an instance of this class.

However, you are saying that you need something that cast(s) the classname referred by strClassName to SomeClass if it is the instance of SomeClass . In other words, you are looking for something that works on two classes (not a class and an object).

So, we need to repeat the problem. Here are three possible problems (+ solutions). I hope you need one of them.

 // 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 )

+7
source

Do you mean?

  Class clazz = Class.forName(strClassName); 
+1
source

You cannot perform dynamic casting in Java.

In your dynamic cast method, you can determine if the object is of the type identified by the string (in response to Adithya Surampudi), but you cannot do anything with it.

The best you can do is return a boolean indicating whether the object is of the type identified by the string.

0
source

Source: https://habr.com/ru/post/1385962/


All Articles