How-to articles, tricks, and solutions about REFLECTION
Calling a function of a module by using its name (a string)
To call a function from a module by using its name as a string, you can use the importlib module.
Can you find all classes in a package using reflection?
Yes, it is possible to find all classes in a package using reflection in Java.
Converting Integer to Long
To convert an Integer object to a Long object in Java, you can use the longValue() method of the Integer class, which returns the value of the Integer object as a long.
Creating an instance using the class name and calling constructor
To create an instance of a class using its class name and calling its constructor, you can use the Class.forName method and the newInstance method.
Get generic type of class at runtime
If you want to get the generic type of a class at runtime, you can use the Type class in the System namespace.
Getting the name of the currently executing method
To get the name of the currently executing method in Java, you can use the Thread.currentThread().getStackTrace() method.
How do I invoke a Java method when given the method name as a string?
To invoke a Java method when given the method name as a string, you can use reflection. Reflection is a feature of the Java language that allows you to inspect and manipulate classes, fields, and methods at runtime.
How to create a generic array in Java?
In Java, it is not possible to create a generic array directly. However, you can create an array of a specific type and then cast it to a generic type.
How to list all functions in a module?
To list all functions in a module, you can use the dir() function to get a list of all the names defined in the module, and then use the inspect module to check if each name is a function.