public class ClassFinderextends Objectimplements Cloneable, SerializableUtility class used to convert a simple class name to a fully qualified class object. The
Classclass can be used to obtain information about a class (its name, its fields, methods, constructors, etc.), and to construct objects, even if the exact class is known at runtime only. It provides a
forNamestatic method converting a string to a
Class, but the given string must be a fully qualified name.
Sometimes, configuration files may need to contain Java class names. After they are extracted from the file, these class names are given to
forNameto be converted into
Classobjects. Unfortunately, only fully qualified class names will be accepted as input, which clutters configuration files, especially if long package names are used. This class permits the definition of a set of import declarations in a way similar to the Java Language Specification. It provides methods to convert a simple class name to a
Classobject and to generate a simple name from a
Classobject, based on the import rules.
The first step for using a class finder is to construct an instance of this class. Then, one needs to retrieve the initially empty list of import declarations by using
getImports, and update it with the actual import declarations. Then, the method
findClasscan find a class using the import declarations. For example, the following code retrieves the class object for the List class in package java.utilClassFinder cf = new ClassFinder();
Class<?> listClass = cf.findClass ("List");
- See Also:
- Serialized Form