Class ClassFinder
- java.lang.Object
-
- umontreal.iro.lecuyer.util.ClassFinder
-
- All Implemented Interfaces:
- java.io.Serializable, java.lang.Cloneable
public class ClassFinder extends java.lang.Object implements java.lang.Cloneable, java.io.SerializableUtility class used to convert a simple class name to a fully qualified class object. TheClassclass 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 aforNamestatic method converting a string to aClass, 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 intoClassobjects. 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 aClassobject and to generate a simple name from aClassobject, 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 methodfindClasscan 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();
cf.getImports().add ("java.util.*");
Class<?> listClass = cf.findClass ("List");
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor and Description ClassFinder()Constructs a new class finder with an empty list of import declarations.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description ClassFinderclone()Clones this class finder, and copies its lists of import declarations.java.lang.Class<?>findClass(java.lang.String name)Tries to find the class corresponding to the simple name name.java.util.List<java.lang.String>getImports()Returns the current list of import declarations.java.lang.StringgetSimpleName(java.lang.Class<?> cls)Returns the simple name of the class cls that can be used when the imports contained in this class finder are used.voidrestoreImports()Restores the list of import declarations.voidsaveImports()Saves the current import list on the import stack.
-
-
-
Constructor Detail
-
ClassFinder
public ClassFinder()
Constructs a new class finder with an empty list of import declarations.
-
-
Method Detail
-
getImports
public java.util.List<java.lang.String> getImports()
Returns the current list of import declarations. This list may contain onlyString's of the form java.class.name or java.package.name.*.- Returns:
- the current list of import declarations.
-
saveImports
public void saveImports()
Saves the current import list on the import stack. This method makes a copy of the list returned bygetImportsand puts it on top of a stack to be restored later byrestoreImports.
-
restoreImports
public void restoreImports()
Restores the list of import declarations. This method removes the last list of import declarations from the stack. If the stack contains only one list, this list is cleared.
-
findClass
public java.lang.Class<?> findClass(java.lang.String name) throws java.lang.ClassNotFoundException, NameConflictExceptionTries to find the class corresponding to the simple name name. The method first considers the argument as a fully qualified class name and callsforName(name). If the class cannot be found, it considers the argument as a simple name. A simple name refers to a class without specifying the package declaring it. To convert simple names to qualified names, the method iterates through all the strings in the list returned bygetImports, applying the same rules as a Java compiler to resolve the class name. However, if an imported package or class does not exist, it will be ignored whereas the compiler would stop with an error.For the class with simple name name to be loaded, it must be imported explicitly (single-type import) or one of the imported packages must contain it (type import on-demand). If the class with name name is imported explicitly, this import declaration has precedence over any imported packages. If several import declaration match the given simple name, e.g., if several fully qualified names with the same simple name are imported, or if a class with simple name name exists in several packages, a
NameConflictExceptionis thrown.- Parameters:
name- the simple name of the class.- Returns:
- a reference to the class being loaded.
- Throws:
java.lang.ClassNotFoundException- if the class cannot be loaded.NameConflictException- if a name conflict occurred.
-
getSimpleName
public java.lang.String getSimpleName(java.lang.Class<?> cls)
Returns the simple name of the class cls that can be used when the imports contained in this class finder are used. For example, if java.lang.String.class is given to this method, String is returned if java.lang.* is among the import declarations.Note: this method does not try to find name conflicts. This operation is performed by
findClassonly. For example, if the list of imported declarations contains foo.bar.* and test.Foo, and the simple name for test.Foo is queried, the method returns Foo even if the package foo.bar contains a Foo class.- Parameters:
cls- the class for which the simple name is queried.- Returns:
- the simple class name.
-
clone
public ClassFinder clone()
Clones this class finder, and copies its lists of import declarations.- Overrides:
clonein classjava.lang.Object
-
-
DMelt 3.0 © DataMelt by jWork.ORG