How to Convert a String to a Class Method

Convert string to Python class object?


Warning: eval() can be used to execute arbitrary Python code. You should never use eval() with untrusted strings. (See Security of Python's eval() on untrusted strings?)

This seems simplest.

>>> class Foo(object):
... pass
...
>>> eval("Foo")
<class '__main__.Foo'>

Convert a string to an external class/method API in python

You'll want to use getattr. Like so:

import externalLib

class InternalLib():
def function(param1='ext1'):
# Calls externalLib.ext1()
# Omit the final () for externalLib.ext1
return getattr(externalLib, param1)()

getattr(object, "attribute") is the same as object.attribute.

Java, how to convert a string to class and call its static method?

To create new instance you need to do the following

Class c = Class.forName("Item");
Item i = (Item)c.newInstance();

If you want to invoke static method you just call it on class instead of instance

Item.test();

Or you can use reflection without directly reference to class

Class c = Class.forName("Item");
Method method = c.getMethod("test");
method.invoke(null);

Convert String into a Class Object


I am storing a class object into a string using toString() method. Now, I want to convert the string into that class object.

Your question is ambiguous. It could mean at least two different things, one of which is ... well ... a serious misconception on your part.


If you did this:

SomeClass object = ...
String s = object.toString();

then the answer is that there is no simple way to turn s back into an instance of SomeClass. You couldn't do it even if the toString() method gave you one of those funky "SomeClass@xxxxxxxx" strings. (That string does not encode the state of the object, or even a reference to the object. The xxxxxxxx part is the object's identity hashcode. It is not unique, and cannot be magically turned back into a reference to the object.)

The only way you could turn the output of toString back into an object would be to:

  • code the SomeClass.toString() method so that included all relevant state for the object in the String it produced, and
  • code a constructor or factory method that explicitly parsed a String in the format produced by the toString() method.

This is probably a bad approach. Certainly, it is a lot of work to do this for non-trivial classes.


If you did something like this:

SomeClass object = ...
Class c = object.getClass();
String cn = c.toString();

then you could get the same Class object back (i.e. the one that is in c) as follows:

Class c2 = Class.forName(cn);

This gives you the Class but there is no magic way to reconstruct the original instance using it. (Obviously, the name of the class does not contain the state of the object.)


If you are looking for a way to serialize / deserialize an arbitrary object without going to the effort of coding the unparse / parse methods yourself, then you shouldn't be using toString() method at all. Here are some alternatives that you can use:

  • The Java Object Serialization APIs as described in the links in @Nishant's answer.
  • JSON serialization as described in @fatnjazzy's answer.
  • An XML serialization library like XStream.
  • An ORM mapping.

Each of these approaches has advantages and disadvantages ... which I won't go into here.

How to convert string to java class and invoke it

You should use the Class.forName() method:

    String className = "java.lang.String";
Class clazz = Class.forName(className);

Then you can call the newInstance() method (if the class has an empty constructor):

    Object obj = clazz.newInstance();

and then - since you know what kind of classes you have in your excel - you can check the class and cast appropriately:

    if (obj instanceof String) {
String value = (String) obj;
}

Convert string into a function call withing the same class

Don't use globals() (the functions are not in the global symbol table), just use getattr:

ran_test_func = getattr(self, ran_test_opt)

How do I convert a string to a class method?


Post.send(anything)

Convert Java Object/String to actual Class

You should be able to do this with Class.forName()

Example:

String className = "com.some.test.example.FollTest";

Class c = Class.forName(className);

Annotation[] annotation = c.getAnnotations();


Related Topics



Leave a reply



Submit